1
0

refactor qt.util.ChoiceWidget: introduce ChoiceItem

This commit is contained in:
SomberNight
2025-05-06 17:47:11 +00:00
parent ef49bb2109
commit ba3783f998
18 changed files with 153 additions and 116 deletions

View File

@@ -1,5 +1,5 @@
from functools import partial
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Sequence
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QPushButton, QLabel, QVBoxLayout, QWidget, QGridLayout
@@ -7,6 +7,8 @@ from PyQt6.QtWidgets import QPushButton, QLabel, QVBoxLayout, QWidget, QGridLayo
from electrum.i18n import _
from electrum.plugin import hook
from electrum.wallet import Multisig_Wallet
from electrum.keystore import Hardware_KeyStore
from electrum.util import ChoiceItem
from electrum.hw_wallet.qt import QtHandlerBase, QtPluginBase
from electrum.hw_wallet.plugin import only_hook_if_libraries_available
@@ -76,13 +78,13 @@ class Plugin(ColdcardPlugin, QtPluginBase):
buttons.append(btn_import_usb)
return buttons
def import_multisig_wallet_to_cc(self, main_window, coldcard_keystores):
def import_multisig_wallet_to_cc(self, main_window: 'ElectrumWindow', coldcard_keystores: Sequence[Hardware_KeyStore]):
from io import StringIO
from ckcc.protocol import CCProtocolPacker
index = main_window.query_choice(
_("Please select which {} device to use:").format(self.device),
[(i, ks.label) for i, ks in enumerate(coldcard_keystores)]
[ChoiceItem(key=i, label=ks.label) for i, ks in enumerate(coldcard_keystores)]
)
if index is not None:
selected_keystore = coldcard_keystores[index]

View File

@@ -29,7 +29,7 @@ from electrum import constants
from electrum.transaction import Transaction, PartialTransaction, PartialTxInput, Sighash
from electrum.i18n import _
from electrum.keystore import Hardware_KeyStore
from electrum.util import to_string, UserCancelled, UserFacingException, bfh
from electrum.util import to_string, UserCancelled, UserFacingException, bfh, ChoiceItem
from electrum.network import Network
from electrum.logging import get_logger
from electrum.plugin import runs_in_hwd_thread, run_in_hwd_thread
@@ -239,13 +239,13 @@ class DigitalBitbox_Client(HardwareClientBase):
def recover_or_erase_dialog(self):
msg = _("The Digital Bitbox is already seeded. Choose an option:") + "\n"
choices = [
(_("Create a wallet using the current seed")),
(_("Erase the Digital Bitbox"))
ChoiceItem(key="create", label=_("Create a wallet using the current seed")),
ChoiceItem(key="erase", label=_("Erase the Digital Bitbox")),
]
reply = self.handler.query_choice(msg, choices)
if reply is None:
raise UserCancelled()
if reply == 1:
if reply == "erase":
self.dbb_erase()
else:
if self.hid_send_encrypt(b'{"device":"info"}')['device']['lock']:
@@ -256,13 +256,13 @@ class DigitalBitbox_Client(HardwareClientBase):
def seed_device_dialog(self):
msg = _("Choose how to initialize your Digital Bitbox:") + "\n"
choices = [
(_("Generate a new random wallet")),
(_("Load a wallet from the micro SD card"))
ChoiceItem(key="generate", label=_("Generate a new random wallet")),
ChoiceItem(key="load", label=_("Load a wallet from the micro SD card")),
]
reply = self.handler.query_choice(msg, choices)
if reply is None:
raise UserCancelled()
if reply == 0:
if reply == "generate":
self.dbb_generate_wallet()
else:
if not self.dbb_load_backup(show_msg=False):
@@ -291,8 +291,8 @@ class DigitalBitbox_Client(HardwareClientBase):
return
choices = [
_('Do not pair'),
_('Import pairing from the Digital Bitbox desktop app'),
ChoiceItem(key=0, label=_('Do not pair')),
ChoiceItem(key=1, label=_('Import pairing from the Digital Bitbox desktop app')),
]
reply = self.handler.query_choice(_('Mobile pairing options'), choices)
if reply is None:
@@ -334,7 +334,8 @@ class DigitalBitbox_Client(HardwareClientBase):
backups = self.hid_send_encrypt(b'{"backup":"list"}')
if 'error' in backups:
raise UserFacingException(backups['error']['message'])
f = self.handler.query_choice(_("Choose a backup file:"), backups['backup'])
backup_choices = [ChoiceItem(key=idx, label=v) for (idx, v) in enumerate(backups['backup'])]
f = self.handler.query_choice(_("Choose a backup file:"), backup_choices)
if f is None:
raise UserCancelled()
key = self.backup_password_dialog()

View File

@@ -69,10 +69,6 @@ class DigitalBitbox_Handler(QtHandlerBase):
def __init__(self, win):
super(DigitalBitbox_Handler, self).__init__(win, 'Digital Bitbox')
def query_choice(self, msg, labels):
choices = [(i, v) for i, v in enumerate(labels)]
return QtHandlerBase.query_choice(self, msg, choices)
class WCDigitalBitboxScriptAndDerivation(WCScriptAndDerivation):
requestRecheck = pyqtSignal()

View File

@@ -14,6 +14,7 @@ from electrum.gui.qt.util import (WindowModalDialog, WWLabel, Buttons, CancelBut
from electrum.i18n import _
from electrum.plugin import hook
from electrum.logging import Logger
from electrum.util import ChoiceItem
from electrum.hw_wallet.qt import QtHandlerBase, QtPluginBase
from electrum.hw_wallet.trezor_qt_pinmatrix import PinMatrixWidget
@@ -619,10 +620,10 @@ class WCKeepkeyInitMethod(WalletWizardComponent):
).format(_info.model_name, _info.model_name)
choices = [
# Must be short as QT doesn't word-wrap radio button text
(TIM_NEW, _("Let the device generate a completely new seed randomly")),
(TIM_RECOVER, _("Recover from a seed you have previously written down")),
(TIM_MNEMONIC, _("Upload a BIP39 mnemonic to generate the seed")),
(TIM_PRIVKEY, _("Upload a master private key"))
ChoiceItem(key=TIM_NEW, label=_("Let the device generate a completely new seed randomly")),
ChoiceItem(key=TIM_RECOVER, label=_("Recover from a seed you have previously written down")),
ChoiceItem(key=TIM_MNEMONIC, label=_("Upload a BIP39 mnemonic to generate the seed")),
ChoiceItem(key=TIM_PRIVKEY, label=_("Upload a master private key")),
]
self.choice_w = ChoiceWidget(message=msg, choices=choices)
self.layout().addWidget(self.choice_w)

View File

@@ -14,6 +14,7 @@ from electrum.gui.qt.util import (WindowModalDialog, WWLabel, Buttons, CancelBut
from electrum.i18n import _
from electrum.plugin import hook
from electrum.logging import Logger
from electrum.util import ChoiceItem
from electrum.hw_wallet.qt import QtHandlerBase, QtPluginBase
from electrum.hw_wallet.trezor_qt_pinmatrix import PinMatrixWidget
@@ -551,10 +552,10 @@ class WCSafeTInitMethod(WalletWizardComponent):
).format(_info.model_name, _info.model_name)
choices = [
# Must be short as QT doesn't word-wrap radio button text
(TIM_NEW, _("Let the device generate a completely new seed randomly")),
(TIM_RECOVER, _("Recover from a seed you have previously written down")),
(TIM_MNEMONIC, _("Upload a BIP39 mnemonic to generate the seed")),
(TIM_PRIVKEY, _("Upload a master private key"))
ChoiceItem(key=TIM_NEW, label=_("Let the device generate a completely new seed randomly")),
ChoiceItem(key=TIM_RECOVER, label=_("Recover from a seed you have previously written down")),
ChoiceItem(key=TIM_MNEMONIC, label=_("Upload a BIP39 mnemonic to generate the seed")),
ChoiceItem(key=TIM_PRIVKEY, label=_("Upload a master private key"))
]
self.choice_w = ChoiceWidget(message=msg, choices=choices)
self.layout().addWidget(self.choice_w)

View File

@@ -12,6 +12,7 @@ from electrum.i18n import _
from electrum.logging import Logger
from electrum.plugin import hook
from electrum.keystore import ScriptTypeNotSupported
from electrum.util import ChoiceItem
from electrum.hw_wallet.qt import QtHandlerBase, QtPluginBase
from electrum.hw_wallet.trezor_qt_pinmatrix import PinMatrixWidget
@@ -847,8 +848,8 @@ class WCTrezorInitMethod(WalletWizardComponent, Logger):
message = _('Choose how you want to initialize your {}.').format(_info.model_name)
choices = [
# Must be short as QT doesn't word-wrap radio button text
(TIM_NEW, _("Let the device generate a completely new seed randomly")),
(TIM_RECOVER, _("Recover from a seed you have previously written down")),
ChoiceItem(key=TIM_NEW, label=_("Let the device generate a completely new seed randomly")),
ChoiceItem(key=TIM_RECOVER, label=_("Recover from a seed you have previously written down")),
]
self.choice_w = ChoiceWidget(message=message, choices=choices)
self.layout().addWidget(self.choice_w)

View File

@@ -34,7 +34,7 @@ from PyQt6.QtWidgets import (QTextEdit, QVBoxLayout, QLabel, QGridLayout, QHBoxL
from electrum.i18n import _
from electrum.plugin import hook
from electrum.util import InvalidPassword
from electrum.util import InvalidPassword, ChoiceItem
from electrum.logging import Logger, get_logger
from electrum import keystore
@@ -357,8 +357,8 @@ class WCChooseSeed(WalletWizardComponent):
WalletWizardComponent.__init__(self, parent, wizard, title=_('Create or restore'))
message = _('Do you want to create a new seed, or restore a wallet using an existing seed?')
choices = [
('createseed', _('Create a new seed')),
('haveseed', _('I already have a seed')),
ChoiceItem(key='createseed', label=_('Create a new seed')),
ChoiceItem(key='haveseed', label=_('I already have a seed')),
]
self.choice_w = ChoiceWidget(message=message, choices=choices)