Browse Source

qt: factor out util.MessageBoxMixin.msg_box into function and use it

so these dialogs also get our custom default settings applied,
e.g. their text is selectable by mouse
regtest_lnd
SomberNight 6 years ago
parent
commit
f6dfcccf8c
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 20
      electrum/gui/qt/__init__.py
  2. 4
      electrum/gui/qt/history_list.py
  3. 13
      electrum/gui/qt/installwizard.py
  4. 13
      electrum/gui/qt/main_window.py
  5. 35
      electrum/gui/qt/util.py

20
electrum/gui/qt/__init__.py

@ -53,7 +53,7 @@ from electrum.logging import Logger
from .installwizard import InstallWizard, WalletAlreadyOpenInMemory from .installwizard import InstallWizard, WalletAlreadyOpenInMemory
from .util import get_default_language, read_QIcon, ColorScheme from .util import get_default_language, read_QIcon, ColorScheme, custom_message_box
from .main_window import ElectrumWindow from .main_window import ElectrumWindow
from .network_dialog import NetworkDialog from .network_dialog import NetworkDialog
from .stylesheet_patcher import patch_qt_stylesheet from .stylesheet_patcher import patch_qt_stylesheet
@ -227,8 +227,10 @@ class ElectrumGui(Logger):
wallet = self.daemon.load_wallet(path, None) wallet = self.daemon.load_wallet(path, None)
except BaseException as e: except BaseException as e:
self.logger.exception('') self.logger.exception('')
QMessageBox.warning(None, _('Error'), custom_message_box(icon=QMessageBox.Warning,
_('Cannot load wallet') + ' (1):\n' + str(e)) parent=None,
title=_('Error'),
text=_('Cannot load wallet') + ' (1):\n' + str(e))
# if app is starting, still let wizard to appear # if app is starting, still let wizard to appear
if not app_is_starting: if not app_is_starting:
return return
@ -237,8 +239,10 @@ class ElectrumGui(Logger):
wallet = self._start_wizard_to_select_or_create_wallet(path) wallet = self._start_wizard_to_select_or_create_wallet(path)
except (WalletFileException, BitcoinException) as e: except (WalletFileException, BitcoinException) as e:
self.logger.exception('') self.logger.exception('')
QMessageBox.warning(None, _('Error'), custom_message_box(icon=QMessageBox.Warning,
_('Cannot load wallet') + ' (2):\n' + str(e)) parent=None,
title=_('Error'),
text=_('Cannot load wallet') + ' (2):\n' + str(e))
if not wallet: if not wallet:
return return
# create or raise window # create or raise window
@ -250,8 +254,10 @@ class ElectrumGui(Logger):
window = self._create_window_for_wallet(wallet) window = self._create_window_for_wallet(wallet)
except BaseException as e: except BaseException as e:
self.logger.exception('') self.logger.exception('')
QMessageBox.warning(None, _('Error'), custom_message_box(icon=QMessageBox.Warning,
_('Cannot create window for wallet') + ':\n' + str(e)) parent=None,
title=_('Error'),
text=_('Cannot create window for wallet') + ':\n' + str(e))
if app_is_starting: if app_is_starting:
wallet_dir = os.path.dirname(path) wallet_dir = os.path.dirname(path)
path = os.path.join(wallet_dir, get_new_wallet_name(wallet_dir)) path = os.path.join(wallet_dir, get_new_wallet_name(wallet_dir))

4
electrum/gui/qt/history_list.py

@ -618,8 +618,8 @@ class HistoryList(MyTreeView, AcceptFileDragDrop):
if len(to_delete) > 1: if len(to_delete) > 1:
question = (_("Are you sure you want to remove this transaction and {} child transactions?") question = (_("Are you sure you want to remove this transaction and {} child transactions?")
.format(len(to_delete) - 1)) .format(len(to_delete) - 1))
answer = QMessageBox.question(self.parent, _("Please confirm"), question, QMessageBox.Yes, QMessageBox.No) if not self.parent.question(msg=question,
if answer == QMessageBox.No: title=_("Please confirm")):
return return
for tx in to_delete: for tx in to_delete:
self.wallet.remove_transaction(tx) self.wallet.remove_transaction(tx)

13
electrum/gui/qt/installwizard.py

@ -263,25 +263,24 @@ class InstallWizard(QDialog, MessageBoxMixin, BaseWizard):
self.temp_storage.decrypt(password) self.temp_storage.decrypt(password)
break break
except InvalidPassword as e: except InvalidPassword as e:
QMessageBox.information(None, _('Error'), str(e)) self.show_message(title=_('Error'), msg=str(e))
continue continue
except BaseException as e: except BaseException as e:
self.logger.exception('') self.logger.exception('')
QMessageBox.information(None, _('Error'), str(e)) self.show_message(title=_('Error'), msg=str(e))
raise UserCancelled() raise UserCancelled()
elif self.temp_storage.is_encrypted_with_hw_device(): elif self.temp_storage.is_encrypted_with_hw_device():
try: try:
self.run('choose_hw_device', HWD_SETUP_DECRYPT_WALLET, storage=self.temp_storage) self.run('choose_hw_device', HWD_SETUP_DECRYPT_WALLET, storage=self.temp_storage)
except InvalidPassword as e: except InvalidPassword as e:
QMessageBox.information( self.show_message(title=_('Error'),
None, _('Error'), msg=_('Failed to decrypt using this hardware device.') + '\n' +
_('Failed to decrypt using this hardware device.') + '\n' +
_('If you use a passphrase, make sure it is correct.')) _('If you use a passphrase, make sure it is correct.'))
self.reset_stack() self.reset_stack()
return self.select_storage(path, get_wallet_from_daemon) return self.select_storage(path, get_wallet_from_daemon)
except BaseException as e: except BaseException as e:
self.logger.exception('') self.logger.exception('')
QMessageBox.information(None, _('Error'), str(e)) self.show_message(title=_('Error'), msg=str(e))
raise UserCancelled() raise UserCancelled()
if self.temp_storage.is_past_initial_decryption(): if self.temp_storage.is_past_initial_decryption():
break break
@ -290,7 +289,7 @@ class InstallWizard(QDialog, MessageBoxMixin, BaseWizard):
else: else:
raise Exception('Unexpected encryption version') raise Exception('Unexpected encryption version')
return self.temp_storage.path, (self.temp_storage if self.temp_storage.file_exists() else None) return self.temp_storage.path, (self.temp_storage if self.temp_storage.file_exists() else None) #
def run_upgrades(self, storage): def run_upgrades(self, storage):
path = storage.path path = storage.path

13
electrum/gui/qt/main_window.py

@ -239,13 +239,10 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
# If the option hasn't been set yet # If the option hasn't been set yet
if config.get('check_updates') is None: if config.get('check_updates') is None:
choice = QMessageBox.question(self, choice = self.question(title="Electrum - " + _("Enable update check"),
"Electrum - " + _("Enable update check"), msg=_("For security reasons we advise that you always use the latest version of Electrum.") + " " +
_("For security reasons we advise that you always use the latest version of Electrum.") + " " + _("Would you like to be notified when there is a newer version of Electrum available?"))
_("Would you like to be notified when there is a newer version of Electrum available?"), config.set_key('check_updates', bool(choice), save=True)
QMessageBox.Yes,
QMessageBox.No)
config.set_key('check_updates', choice == QMessageBox.Yes, save=True)
if config.get('check_updates', False): if config.get('check_updates', False):
# The references to both the thread and the window need to be stored somewhere # The references to both the thread and the window need to be stored somewhere
@ -1282,7 +1279,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
_("You can disable this setting in '{}'.").format(_('Preferences')) + '\n' + _("You can disable this setting in '{}'.").format(_('Preferences')) + '\n' +
_('Also, dust is not kept as change, but added to the fee.') + '\n' + _('Also, dust is not kept as change, but added to the fee.') + '\n' +
_('Also, when batching RBF transactions, BIP 125 imposes a lower bound on the fee.')) _('Also, when batching RBF transactions, BIP 125 imposes a lower bound on the fee.'))
QMessageBox.information(self, 'Fee rounding', text) self.show_message(title=_('Fee rounding'), msg=text)
self.feerounding_icon = QPushButton(read_QIcon('info.png'), '') self.feerounding_icon = QPushButton(read_QIcon('info.png'), '')
self.feerounding_icon.setFixedWidth(20) self.feerounding_icon.setFixedWidth(20)

35
electrum/gui/qt/util.py

@ -103,7 +103,10 @@ class HelpLabel(QLabel):
self.font = QFont() self.font = QFont()
def mouseReleaseEvent(self, x): def mouseReleaseEvent(self, x):
QMessageBox.information(self, 'Help', self.help_text) custom_message_box(icon=QMessageBox.Information,
parent=self,
title=_('Help'),
text=self.help_text)
def enterEvent(self, event): def enterEvent(self, event):
self.font.setUnderline(True) self.font.setUnderline(True)
@ -127,7 +130,10 @@ class HelpButton(QPushButton):
self.clicked.connect(self.onclick) self.clicked.connect(self.onclick)
def onclick(self): def onclick(self):
QMessageBox.information(self, 'Help', self.help_text) custom_message_box(icon=QMessageBox.Information,
parent=self,
title=_('Help'),
text=self.help_text)
class InfoButton(QPushButton): class InfoButton(QPushButton):
@ -139,7 +145,10 @@ class InfoButton(QPushButton):
self.clicked.connect(self.onclick) self.clicked.connect(self.onclick)
def onclick(self): def onclick(self):
QMessageBox.information(self, 'Info', self.help_text) custom_message_box(icon=QMessageBox.Information,
parent=self,
title=_('Info'),
text=self.help_text)
class Buttons(QHBoxLayout): class Buttons(QHBoxLayout):
@ -217,10 +226,23 @@ class MessageBoxMixin(object):
return self.msg_box(QMessageBox.Information, parent, return self.msg_box(QMessageBox.Information, parent,
title or _('Information'), msg, **kwargs) title or _('Information'), msg, **kwargs)
def msg_box(self, icon, parent, title, text, buttons=QMessageBox.Ok, def msg_box(self, icon, parent, title, text, *, buttons=QMessageBox.Ok,
defaultButton=QMessageBox.NoButton, *, rich_text=False, defaultButton=QMessageBox.NoButton, rich_text=False,
checkbox=None): checkbox=None):
parent = parent or self.top_level_window() parent = parent or self.top_level_window()
return custom_message_box(icon=icon,
parent=parent,
title=title,
text=text,
buttons=buttons,
defaultButton=defaultButton,
rich_text=rich_text,
checkbox=checkbox)
def custom_message_box(*, icon, parent, title, text, buttons=QMessageBox.Ok,
defaultButton=QMessageBox.NoButton, rich_text=False,
checkbox=None):
if type(icon) is QPixmap: if type(icon) is QPixmap:
d = QMessageBox(QMessageBox.Information, title, str(text), buttons, parent) d = QMessageBox(QMessageBox.Information, title, str(text), buttons, parent)
d.setIconPixmap(icon) d.setIconPixmap(icon)
@ -229,7 +251,7 @@ class MessageBoxMixin(object):
d.setWindowModality(Qt.WindowModal) d.setWindowModality(Qt.WindowModal)
d.setDefaultButton(defaultButton) d.setDefaultButton(defaultButton)
if rich_text: if rich_text:
d.setTextInteractionFlags(Qt.TextSelectableByMouse| Qt.LinksAccessibleByMouse) d.setTextInteractionFlags(Qt.TextSelectableByMouse | Qt.LinksAccessibleByMouse)
d.setTextFormat(Qt.RichText) d.setTextFormat(Qt.RichText)
else: else:
d.setTextInteractionFlags(Qt.TextSelectableByMouse) d.setTextInteractionFlags(Qt.TextSelectableByMouse)
@ -238,6 +260,7 @@ class MessageBoxMixin(object):
d.setCheckBox(checkbox) d.setCheckBox(checkbox)
return d.exec_() return d.exec_()
class WindowModalDialog(QDialog, MessageBoxMixin): class WindowModalDialog(QDialog, MessageBoxMixin):
'''Handy wrapper; window modal dialogs are better for our multi-window '''Handy wrapper; window modal dialogs are better for our multi-window
daemon model as other wallet windows can still be accessed.''' daemon model as other wallet windows can still be accessed.'''

Loading…
Cancel
Save