Refactor share mode info widget into its own file and custom class, and run .show_more() and .show_less() instead of .show() and .hide()

This commit is contained in:
Micah Lee 2018-09-28 15:47:49 -07:00
parent cddc8c06d4
commit f056ce576e
No known key found for this signature in database
GPG key ID: 403C2657CD994F73
2 changed files with 137 additions and 83 deletions

View file

@ -28,6 +28,7 @@ from onionshare.web import Web
from .file_selection import FileSelection from .file_selection import FileSelection
from .downloads import Downloads from .downloads import Downloads
from .threads import CompressThread from .threads import CompressThread
from .info import Info
from ..mode import Mode from ..mode import Mode
from ..widgets import Alert from ..widgets import Alert
@ -77,35 +78,7 @@ class ShareMode(Mode):
self.downloads_completed = 0 self.downloads_completed = 0
# Information about share, and show downloads button # Information about share, and show downloads button
self.info_label = QtWidgets.QLabel() self.info = Info(self.common, self)
self.info_label.setStyleSheet(self.common.css['mode_info_label'])
self.info_in_progress_downloads_count = QtWidgets.QLabel()
self.info_in_progress_downloads_count.setStyleSheet(self.common.css['mode_info_label'])
self.info_completed_downloads_count = QtWidgets.QLabel()
self.info_completed_downloads_count.setStyleSheet(self.common.css['mode_info_label'])
self.update_downloads_completed()
self.update_downloads_in_progress()
self.info_toggle_button = QtWidgets.QPushButton()
self.info_toggle_button.setDefault(False)
self.info_toggle_button.setFixedWidth(30)
self.info_toggle_button.setFixedHeight(30)
self.info_toggle_button.setFlat(True)
self.info_toggle_button.setIcon( QtGui.QIcon(self.common.get_resource_path('images/downloads_toggle.png')) )
self.info_toggle_button.clicked.connect(self.toggle_downloads)
self.info_layout = QtWidgets.QHBoxLayout()
self.info_layout.addWidget(self.info_label)
self.info_layout.addStretch()
self.info_layout.addWidget(self.info_in_progress_downloads_count)
self.info_layout.addWidget(self.info_completed_downloads_count)
self.info_layout.addWidget(self.info_toggle_button)
self.info_widget = QtWidgets.QWidget()
self.info_widget.setLayout(self.info_layout)
# Primary action layout # Primary action layout
self.primary_action_layout.addWidget(self.filesize_warning) self.primary_action_layout.addWidget(self.filesize_warning)
@ -117,7 +90,7 @@ class ShareMode(Mode):
# Main layout # Main layout
self.main_layout = QtWidgets.QVBoxLayout() self.main_layout = QtWidgets.QVBoxLayout()
self.main_layout.addWidget(self.info_widget) self.main_layout.addWidget(self.info)
self.main_layout.addLayout(self.file_selection) self.main_layout.addLayout(self.file_selection)
self.main_layout.addWidget(self.primary_action) self.main_layout.addWidget(self.primary_action)
self.main_layout.addWidget(self.min_width_widget) self.main_layout.addWidget(self.min_width_widget)
@ -218,7 +191,7 @@ class ShareMode(Mode):
self.filesize_warning.hide() self.filesize_warning.hide()
self.downloads_in_progress = 0 self.downloads_in_progress = 0
self.downloads_completed = 0 self.downloads_completed = 0
self.update_downloads_in_progress() self.info.update_downloads_in_progress()
self.file_selection.file_list.adjustSize() self.file_selection.file_list.adjustSize()
def cancel_server_custom(self): def cancel_server_custom(self):
@ -234,7 +207,7 @@ class ShareMode(Mode):
Connection to Tor broke. Connection to Tor broke.
""" """
self.primary_action.hide() self.primary_action.hide()
self.info_widget.hide() self.info.show_less()
def handle_request_load(self, event): def handle_request_load(self, event):
""" """
@ -252,7 +225,7 @@ class ShareMode(Mode):
filesize = self.web.share_mode.download_filesize filesize = self.web.share_mode.download_filesize
self.downloads.add(event["data"]["id"], filesize) self.downloads.add(event["data"]["id"], filesize)
self.downloads_in_progress += 1 self.downloads_in_progress += 1
self.update_downloads_in_progress() self.info.update_downloads_in_progress()
self.system_tray.showMessage(strings._('systray_download_started_title', True), strings._('systray_download_started_message', True)) self.system_tray.showMessage(strings._('systray_download_started_title', True), strings._('systray_download_started_message', True))
@ -268,10 +241,10 @@ class ShareMode(Mode):
# Update the total 'completed downloads' info # Update the total 'completed downloads' info
self.downloads_completed += 1 self.downloads_completed += 1
self.update_downloads_completed() self.info.update_downloads_completed()
# Update the 'in progress downloads' info # Update the 'in progress downloads' info
self.downloads_in_progress -= 1 self.downloads_in_progress -= 1
self.update_downloads_in_progress() self.info.update_downloads_in_progress()
# Close on finish? # Close on finish?
if self.common.settings.get('close_after_first_download'): if self.common.settings.get('close_after_first_download'):
@ -282,7 +255,7 @@ class ShareMode(Mode):
if self.server_status.status == self.server_status.STATUS_STOPPED: if self.server_status.status == self.server_status.STATUS_STOPPED:
self.downloads.cancel(event["data"]["id"]) self.downloads.cancel(event["data"]["id"])
self.downloads_in_progress = 0 self.downloads_in_progress = 0
self.update_downloads_in_progress() self.info.update_downloads_in_progress()
def handle_request_canceled(self, event): def handle_request_canceled(self, event):
""" """
@ -292,7 +265,7 @@ class ShareMode(Mode):
# Update the 'in progress downloads' info # Update the 'in progress downloads' info
self.downloads_in_progress -= 1 self.downloads_in_progress -= 1
self.update_downloads_in_progress() self.info.update_downloads_in_progress()
self.system_tray.showMessage(strings._('systray_download_canceled_title', True), strings._('systray_download_canceled_message', True)) self.system_tray.showMessage(strings._('systray_download_canceled_title', True), strings._('systray_download_canceled_message', True))
def on_reload_settings(self): def on_reload_settings(self):
@ -302,7 +275,7 @@ class ShareMode(Mode):
""" """
if self.server_status.file_selection.get_num_files() > 0: if self.server_status.file_selection.get_num_files() > 0:
self.primary_action.show() self.primary_action.show()
self.info_widget.show() self.info.show_more()
def update_primary_action(self): def update_primary_action(self):
self.common.log('ShareMode', 'update_primary_action') self.common.log('ShareMode', 'update_primary_action')
@ -311,7 +284,7 @@ class ShareMode(Mode):
file_count = self.file_selection.file_list.count() file_count = self.file_selection.file_list.count()
if file_count > 0: if file_count > 0:
self.primary_action.show() self.primary_action.show()
self.info_widget.show() self.info.show_more()
# Update the file count in the info label # Update the file count in the info label
total_size_bytes = 0 total_size_bytes = 0
@ -321,13 +294,13 @@ class ShareMode(Mode):
total_size_readable = self.common.human_readable_filesize(total_size_bytes) total_size_readable = self.common.human_readable_filesize(total_size_bytes)
if file_count > 1: if file_count > 1:
self.info_label.setText(strings._('gui_file_info', True).format(file_count, total_size_readable)) self.info.update_label(strings._('gui_file_info', True).format(file_count, total_size_readable))
else: else:
self.info_label.setText(strings._('gui_file_info_single', True).format(file_count, total_size_readable)) self.info.update_label(strings._('gui_file_info_single', True).format(file_count, total_size_readable))
else: else:
self.primary_action.hide() self.primary_action.hide()
self.info_widget.hide() self.info.show_less()
# Resize window # Resize window
self.resize_window() self.resize_window()
@ -338,49 +311,10 @@ class ShareMode(Mode):
""" """
self.downloads_completed = 0 self.downloads_completed = 0
self.downloads_in_progress = 0 self.downloads_in_progress = 0
self.update_downloads_completed() self.info.update_downloads_completed()
self.update_downloads_in_progress() self.info.update_downloads_in_progress()
self.downloads.reset() self.downloads.reset()
def update_downloads_completed(self):
"""
Update the 'Downloads completed' info widget.
"""
if self.downloads_completed == 0:
image = self.common.get_resource_path('images/share_completed_none.png')
else:
image = self.common.get_resource_path('images/share_completed.png')
self.info_completed_downloads_count.setText('<img src="{0:s}" /> {1:d}'.format(image, self.downloads_completed))
self.info_completed_downloads_count.setToolTip(strings._('info_completed_downloads_tooltip', True).format(self.downloads_completed))
def update_downloads_in_progress(self):
"""
Update the 'Downloads in progress' info widget.
"""
if self.downloads_in_progress == 0:
image = self.common.get_resource_path('images/share_in_progress_none.png')
else:
image = self.common.get_resource_path('images/share_in_progress.png')
self.info_in_progress_downloads_count.setText('<img src="{0:s}" /> {1:d}'.format(image, self.downloads_in_progress))
self.info_in_progress_downloads_count.setToolTip(strings._('info_in_progress_downloads_tooltip', True).format(self.downloads_in_progress))
def toggle_downloads(self):
"""
Toggle showing and hiding the Downloads widget
"""
self.common.log('ShareMode', 'toggle_downloads')
if self.downloads.isVisible():
self.downloads.hide()
self.info_toggle_button.setIcon( QtGui.QIcon(self.common.get_resource_path('images/downloads_toggle.png')) )
self.info_toggle_button.setFlat(True)
else:
self.downloads.show()
self.info_toggle_button.setIcon( QtGui.QIcon(self.common.get_resource_path('images/downloads_toggle_selected.png')) )
self.info_toggle_button.setFlat(False)
self.resize_window()
def resize_window(self): def resize_window(self):
min_width = 450 min_width = 450
if self.downloads.isVisible(): if self.downloads.isVisible():

View file

@ -0,0 +1,120 @@
# -*- coding: utf-8 -*-
"""
OnionShare | https://onionshare.org/
Copyright (C) 2014-2018 Micah Lee <micah@micahflee.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from PyQt5 import QtCore, QtWidgets, QtGui
from onionshare import strings
class Info(QtWidgets.QWidget):
"""
Share mode information widget
"""
def __init__(self, common, share_mode):
super(Info, self).__init__()
self.common = common
self.share_mode = share_mode
# Label
self.label = QtWidgets.QLabel()
self.label.setStyleSheet(self.common.css['mode_info_label'])
# In prorgess and completed labels
self.in_progress_downloads_count = QtWidgets.QLabel()
self.in_progress_downloads_count.setStyleSheet(self.common.css['mode_info_label'])
self.completed_downloads_count = QtWidgets.QLabel()
self.completed_downloads_count.setStyleSheet(self.common.css['mode_info_label'])
# Toggle button
self.toggle_button = QtWidgets.QPushButton()
self.toggle_button.setDefault(False)
self.toggle_button.setFixedWidth(30)
self.toggle_button.setFixedHeight(30)
self.toggle_button.setFlat(True)
self.toggle_button.setIcon( QtGui.QIcon(self.common.get_resource_path('images/downloads_toggle.png')) )
self.toggle_button.clicked.connect(self.toggle_downloads)
# Info layout
layout = QtWidgets.QHBoxLayout()
layout.addWidget(self.label)
layout.addStretch()
layout.addWidget(self.in_progress_downloads_count)
layout.addWidget(self.completed_downloads_count)
layout.addWidget(self.toggle_button)
self.setLayout(layout)
self.update_downloads_completed()
self.update_downloads_in_progress()
def update_label(self, s):
"""
Updates the text of the label.
"""
self.label.setText(s)
def update_downloads_completed(self):
"""
Update the 'Downloads completed' info widget.
"""
if self.share_mode.downloads_completed == 0:
image = self.common.get_resource_path('images/share_completed_none.png')
else:
image = self.common.get_resource_path('images/share_completed.png')
self.completed_downloads_count.setText('<img src="{0:s}" /> {1:d}'.format(image, self.share_mode.downloads_completed))
self.completed_downloads_count.setToolTip(strings._('info_completed_downloads_tooltip', True).format(self.share_mode.downloads_completed))
def update_downloads_in_progress(self):
"""
Update the 'Downloads in progress' info widget.
"""
if self.share_mode.downloads_in_progress == 0:
image = self.common.get_resource_path('images/share_in_progress_none.png')
else:
image = self.common.get_resource_path('images/share_in_progress.png')
self.in_progress_downloads_count.setText('<img src="{0:s}" /> {1:d}'.format(image, self.share_mode.downloads_in_progress))
self.in_progress_downloads_count.setToolTip(strings._('info_in_progress_downloads_tooltip', True).format(self.share_mode.downloads_in_progress))
def toggle_downloads(self):
"""
Toggle showing and hiding the Downloads widget
"""
self.common.log('ShareMode', 'toggle_downloads')
if self.share_mode.downloads.isVisible():
self.share_mode.downloads.hide()
self.toggle_button.setIcon( QtGui.QIcon(self.common.get_resource_path('images/downloads_toggle.png')) )
self.toggle_button.setFlat(True)
else:
self.share_mode.downloads.show()
self.toggle_button.setIcon( QtGui.QIcon(self.common.get_resource_path('images/downloads_toggle_selected.png')) )
self.toggle_button.setFlat(False)
self.share_mode.resize_window()
def show_less(self):
"""
Remove clutter widgets that aren't necessary.
"""
self.label.hide()
def show_more(self):
"""
Show all widgets.
"""
self.label.show()