Install wizard: simplify create_or_restore

- Use ChoiceLayout to organize the layouts
- Do wallet filtering in wizard.py as it's GUI independent.
- Only pass the descriptions.
This commit is contained in:
Neil Booth 2016-01-10 22:57:31 +09:00
parent 85b48191da
commit 384b8cd5f2
2 changed files with 29 additions and 59 deletions

View File

@ -171,57 +171,24 @@ class InstallWizard(WindowModalDialog, WizardBase):
self.show()
def query_create_or_restore(self, wallet_kinds):
"""Ask the user what they want to do, and to what wallet kind.
wallet_kinds is an array of tuples (kind, description).
Return a tuple (action, kind). Action is 'create' or 'restore',
and kind is one of the wallet kinds passed."""
vbox = QVBoxLayout()
"""Ask the user what they want to do, and which wallet kind.
wallet_kinds is an array of translated wallet descriptions.
Return a a tuple (action, kind_index). Action is 'create' or
'restore', and kind the index of the wallet kind chosen."""
actions = [_("Create a new wallet"),
_("Restore a wallet or import keys")]
main_label = QLabel(_("Electrum could not find an existing wallet."))
actions_clayout = ChoicesLayout(_("What do you want to do?"), actions)
wallet_clayout = ChoicesLayout(_("Wallet kind:"), wallet_kinds)
vbox = QVBoxLayout()
vbox.addWidget(main_label)
grid = QGridLayout()
grid.setSpacing(5)
gb1 = QGroupBox(_("What do you want to do?"))
vbox.addWidget(gb1)
vbox1 = QVBoxLayout()
gb1.setLayout(vbox1)
b1 = QRadioButton(gb1)
b1.setText(_("Create new wallet"))
b1.setChecked(True)
b2 = QRadioButton(gb1)
b2.setText(_("Restore a wallet or import keys"))
group1 = QButtonGroup()
group1.addButton(b1)
group1.addButton(b2)
vbox1.addWidget(b1)
vbox1.addWidget(b2)
gb2 = QGroupBox(_("Wallet type:"))
vbox.addWidget(gb2)
vbox2 = QVBoxLayout()
gb2.setLayout(vbox2)
group2 = QButtonGroup()
for i, (wtype,name) in enumerate(wallet_kinds):
if not filter(lambda x:x[0]==wtype, electrum.wallet.wallet_types):
continue
button = QRadioButton(gb2)
button.setText(name)
vbox2.addWidget(button)
group2.addButton(button)
group2.setId(button, i)
if i==0:
button.setChecked(True)
vbox.addLayout(actions_clayout.layout())
vbox.addLayout(wallet_clayout.layout())
vbox.addStretch(1)
OK = OkButton(self, _('Next'))
vbox.addLayout(Buttons(CancelButton(self), OK))
self.set_layout(vbox)
@ -231,9 +198,8 @@ class InstallWizard(WindowModalDialog, WizardBase):
if not self.exec_():
raise UserCancelled
action = 'create' if b1.isChecked() else 'restore'
wallet_type = wallet_kinds[group2.checkedId()][0]
return action, wallet_type
action = ['create', 'restore'][actions_clayout.selected_index()]
return action, wallet_clayout.selected_index()
def verify_seed(self, seed, is_valid=None):
while True:

View File

@ -19,7 +19,7 @@
from electrum import WalletStorage
from electrum.plugins import run_hook
from util import PrintError
from wallet import Wallet
from wallet import Wallet, wallet_types
from i18n import _
MSG_GENERATING_WAIT = _("Electrum is generating your addresses, please wait...")
@ -64,10 +64,10 @@ class WizardBase(PrintError):
raise NotImplementedError
def query_create_or_restore(self, wallet_kinds):
"""Ask the user what they want to do, and to what wallet kind.
wallet_kinds is an array of tuples (kind, description).
Return a tuple (action, kind). Action is 'create' or 'restore',
and kind is one of the wallet kinds passed."""
"""Ask the user what they want to do, and which wallet kind.
wallet_kinds is an array of translated wallet descriptions.
Return a a tuple (action, kind_index). Action is 'create' or
'restore', and kind the index of the wallet kind chosen."""
raise NotImplementedError
def query_multisig(self, action):
@ -186,21 +186,25 @@ class WizardBase(PrintError):
a wallet and return it.'''
self.remove_from_recently_open(storage.path)
action, kind = self.query_create_or_restore(WizardBase.wallet_kinds)
# Filter out any unregistered wallet kinds
registered_kinds = zip(*wallet_types)[0]
kinds, descriptions = zip(*[pair for pair in WizardBase.wallet_kinds
if pair[0] in registered_kinds])
action, kind_index = self.query_create_or_restore(descriptions)
assert action in WizardBase.user_actions
assert kind in [k for k, desc in WizardBase.wallet_kinds]
kind = kinds[kind_index]
if kind == 'multisig':
wallet_type = self.query_multisig(action)
elif kind == 'hardware':
wallet_types, choices = self.plugins.hardware_wallets(action)
hw_wallet_types, choices = self.plugins.hardware_wallets(action)
if action == 'create':
msg = _('Select the hardware wallet to create')
else:
msg = _('Select the hardware wallet to restore')
choice = self.query_choice(msg, choices)
wallet_type = wallet_types[choice]
wallet_type = hw_wallet_types[choice]
elif kind == 'twofactor':
wallet_type = '2fa'
else: