From 9390a97e9ee272c21c1f04bdda26625db1beb7ba Mon Sep 17 00:00:00 2001 From: Neil Booth Date: Wed, 13 Jan 2016 20:34:42 +0900 Subject: [PATCH] Bring the network dialog into the new framework --- gui/qt/installwizard.py | 63 ++++++++++++++++++---------------------- gui/qt/network_dialog.py | 34 +++++++++++++--------- lib/wizard.py | 10 +++++-- 3 files changed, 57 insertions(+), 50 deletions(-) diff --git a/gui/qt/installwizard.py b/gui/qt/installwizard.py index e99a4228..db5dc34b 100644 --- a/gui/qt/installwizard.py +++ b/gui/qt/installwizard.py @@ -8,7 +8,7 @@ import electrum from electrum.i18n import _ from seed_dialog import SeedDisplayLayout, SeedWarningLayout, SeedInputLayout -from network_dialog import NetworkDialog +from network_dialog import NetworkChoiceLayout from util import * from password_dialog import PasswordLayout, PW_NEW, PW_PASSPHRASE @@ -110,12 +110,17 @@ class InstallWizard(WindowModalDialog, WizardBase): self.show() self.raise_() + def finished(self): + '''Ensure the dialog is closed.''' + self.accept() + self.refresh_gui() + def set_icon(self, filename): prior_filename, self.icon_filename = self.icon_filename, filename self.logo.setPixmap(QPixmap(filename).scaledToWidth(70)) return prior_filename - def set_main_layout(self, layout, title=None): + def set_main_layout(self, layout, title=None, raise_on_cancel=True): self.title.setText(title or "") self.title.setVisible(bool(title)) # Get rid of any prior layout @@ -127,7 +132,8 @@ class InstallWizard(WindowModalDialog, WizardBase): self.next_button.setEnabled(True) self.main_widget.setVisible(True) self.please_wait.setVisible(False) - if not self.loop.exec_(): + result = self.loop.exec_() + if not result and raise_on_cancel: raise UserCancelled self.title.setVisible(False) self.cancel_button.setEnabled(False) @@ -135,6 +141,7 @@ class InstallWizard(WindowModalDialog, WizardBase): self.main_widget.setVisible(False) self.please_wait.setVisible(True) self.refresh_gui() + return result def refresh_gui(self): # For some reason, to refresh the GUI this needs to be called twice @@ -204,9 +211,6 @@ class InstallWizard(WindowModalDialog, WizardBase): the password or None for no password.""" return self.pw_layout(msg or MSG_ENTER_PASSWORD, PW_NEW) - def choose_server(self, network): - self.network_dialog(network) - def show_restore(self, wallet, network): if network: def task(): @@ -295,35 +299,26 @@ class InstallWizard(WindowModalDialog, WizardBase): self.set_main_layout(vbox) return get_texts() - def network_dialog(self, network): - grid = QGridLayout() - grid.setSpacing(5) - label = QLabel(_("Electrum communicates with remote servers to get information about your transactions and addresses. The servers all fulfil the same purpose only differing in hardware. In most cases you simply want to let Electrum pick one at random if you have a preference though feel free to select a server manually.") + "\n\n" \ - + _("How do you want to connect to a server:")+" ") - label.setWordWrap(True) - grid.addWidget(label, 0, 0) - gb = QGroupBox() - b1 = QRadioButton(gb) - b1.setText(_("Auto connect")) - b1.setChecked(True) - b2 = QRadioButton(gb) - b2.setText(_("Select server manually")) - grid.addWidget(b1,1,0) - grid.addWidget(b2,2,0) - vbox = QVBoxLayout() - vbox.addLayout(grid) - vbox.addStretch(1) - vbox.addLayout(Buttons(CancelButton(self), OkButton(self, _('Next')))) + def choose_server(self, network): + title = _("Electrum communicates with remote servers to get " + "information about your transactions and addresses. The " + "servers all fulfil the same purpose only differing in " + "hardware. In most cases you simply want to let Electrum " + "pick one at random. However if you prefer feel free to " + "select a server manually.") + choices = [_("Auto connect"), _("Select server manually")] + choices_title = _("How do you want to connect to a server? ") + clayout = ChoicesLayout(choices_title, choices) + self.set_main_layout(clayout.layout(), title) - self.set_layout(vbox) - if not self.exec_(): - return - - if b2.isChecked(): - NetworkDialog(network, self.config, None).do_exec() - else: - self.config.set_key('auto_connect', True, True) - network.auto_connect = True + auto_connect = True + if clayout.selected_index() == 1: + nlayout = NetworkChoiceLayout(network, self.config, wizard=True) + if self.set_main_layout(nlayout.layout(), raise_on_cancel=False): + nlayout.accept() + auto_connect = False + self.config.set_key('auto_connect', auto_connect, True) + network.auto_connect = auto_connect def query_choice(self, msg, choices): clayout = ChoicesLayout(msg, choices) diff --git a/gui/qt/network_dialog.py b/gui/qt/network_dialog.py index 7b13f315..6a3aa584 100644 --- a/gui/qt/network_dialog.py +++ b/gui/qt/network_dialog.py @@ -32,7 +32,20 @@ class NetworkDialog(WindowModalDialog): def __init__(self, network, config, parent): WindowModalDialog.__init__(self, parent, _('Network')) self.setMinimumSize(375, 20) + self.nlayout = NetworkChoiceLayout(network, config) + vbox = QVBoxLayout(self) + vbox.addLayout(nlayout.layout()) + vbox.addLayout(Buttons(CancelButton(self), OkButton(self))) + def do_exec(self): + result = self.exec_() + if result: + self.nlayout.accept() + return result + + +class NetworkChoiceLayout(object): + def __init__(self, network, config, wizard=False): self.network = network self.config = config self.protocol = None @@ -42,7 +55,7 @@ class NetworkDialog(WindowModalDialog): if not proxy_config: proxy_config = { "mode":"none", "host":"localhost", "port":"9050"} - if parent: + if not wizard: n = len(network.get_interfaces()) if n: status = _("Blockchain") + ": " + "%d "%(network.get_local_height()) + _("blocks") + ".\n" + _("Getting block headers from %d nodes.")%n @@ -56,7 +69,6 @@ class NetworkDialog(WindowModalDialog): status = _("Please choose a server.") + "\n" + _("Select 'Cancel' if you are offline.") vbox = QVBoxLayout() - vbox.setSpacing(30) hbox = QHBoxLayout() l = QLabel() l.setPixmap(QPixmap(":icons/network.png")) @@ -69,6 +81,7 @@ class NetworkDialog(WindowModalDialog): + _("This blockchain is used to verify the transactions sent by the address server.") hbox.addWidget(HelpButton(msg)) vbox.addLayout(hbox) + vbox.addSpacing(15) # grid layout grid = QGridLayout() @@ -101,7 +114,7 @@ class NetworkDialog(WindowModalDialog): self.autoconnect_cb.setToolTip(msg) label = _('Active Servers') if network.is_connected() else _('Default Servers') - self.servers_list_widget = QTreeWidget(parent) + self.servers_list_widget = QTreeWidget() self.servers_list_widget.setHeaderLabels( [ label, _('Limit') ] ) self.servers_list_widget.setMaximumHeight(150) self.servers_list_widget.setColumnWidth(0, 240) @@ -155,11 +168,10 @@ class NetworkDialog(WindowModalDialog): grid.addWidget(self.proxy_mode, 4, 1) grid.addWidget(self.proxy_host, 4, 2) grid.addWidget(self.proxy_port, 4, 3) + self.layout_ = vbox - # buttons - vbox.addLayout(Buttons(CancelButton(self), OkButton(self))) - self.setLayout(vbox) - + def layout(self): + return self.layout_ def init_servers_list(self): self.servers_list_widget.clear() @@ -211,12 +223,7 @@ class NetworkDialog(WindowModalDialog): self.server_port.setText( port ) self.ssl_cb.setChecked(protocol=='s') - - def do_exec(self): - - if not self.exec_(): - return - + def accept(self): host = str(self.server_host.text()) port = str(self.server_port.text()) protocol = 's' if self.ssl_cb.isChecked() else 't' @@ -236,4 +243,3 @@ class NetworkDialog(WindowModalDialog): auto_connect = self.autoconnect_cb.isChecked() self.network.set_parameters(host, port, protocol, proxy, auto_connect) - return True diff --git a/lib/wizard.py b/lib/wizard.py index e21492ed..ce710647 100644 --- a/lib/wizard.py +++ b/lib/wizard.py @@ -119,6 +119,10 @@ class WizardBase(PrintError): """Show restore result""" pass + def finished(self): + """Called when the wizard is done.""" + pass + @classmethod def open_wallet(self, network, filename, config, create_wizard): '''The main entry point of the wizard. Open a wallet from the given @@ -156,7 +160,7 @@ class WizardBase(PrintError): if network: # Show network dialog if config does not exist - if config.get('server') is None: + if config.get('auto_connect') is None: wizard().choose_server(network) else: wizard().show_warning(_('You are offline')) @@ -171,7 +175,9 @@ class WizardBase(PrintError): if is_restore: wizard().show_restore(wallet, network) - self.my_wizard = None + if self.my_wizard: + self.my_wizard.finished() + self.my_wizard = None return wallet