Skip to content

Commit 5ea614c

Browse files
committed
qt: qrtextedit: rm some duplication
There was too much code duplication - there still is a bit... - in some places buttons had text "Read QR code with camera", in others it was "Read QR code from camera" - 63c224c added a "on_qr_from_file_input_btn" input method, which was not added everywhere. - was missing in add_qr_input_combined_button and in editor_contextMenuEvent
1 parent 6f50414 commit 5ea614c

File tree

4 files changed

+34
-64
lines changed

4 files changed

+34
-64
lines changed

electrum/gui/qt/paytoedit.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@
2424
# SOFTWARE.
2525

2626
from functools import partial
27-
from typing import Optional, TYPE_CHECKING
27+
from typing import Optional, TYPE_CHECKING, Union
2828

2929
from PyQt6.QtCore import Qt, QTimer, QSize, QStringListModel
3030
from PyQt6.QtCore import pyqtSignal
31-
from PyQt6.QtGui import QFontMetrics, QFont
31+
from PyQt6.QtGui import QFontMetrics, QFont, QContextMenuEvent
3232
from PyQt6.QtWidgets import QTextEdit, QWidget, QLineEdit, QStackedLayout, QCompleter
3333

3434
from electrum.payment_identifier import PaymentIdentifier
3535
from electrum.logging import Logger
3636
from electrum.util import EventListener, event_listener
3737

3838
from . import util
39-
from .util import MONOSPACE_FONT, GenericInputHandler, editor_contextMenuEvent, ColorScheme
39+
from .util import MONOSPACE_FONT, GenericInputHandler, ColorScheme, add_input_actions_to_context_menu
4040

4141
if TYPE_CHECKING:
4242
from .send_tab import SendTab
@@ -193,8 +193,8 @@ def on_completed(item: str):
193193
setText=self.try_payment_identifier,
194194
)
195195

196-
self.text_edit.contextMenuEvent = partial(editor_contextMenuEvent, self.text_edit, self)
197-
self.line_edit.contextMenuEvent = partial(editor_contextMenuEvent, self.line_edit, self)
196+
self.text_edit.contextMenuEvent = partial(self.custom_context_menu_event, tl_edit=self.text_edit)
197+
self.line_edit.contextMenuEvent = partial(self.custom_context_menu_event, tl_edit=self.line_edit)
198198

199199
self.edit_timer = QTimer(self)
200200
self.edit_timer.setSingleShot(True)
@@ -206,6 +206,12 @@ def on_completed(item: str):
206206
self.register_callbacks()
207207
self.destroyed.connect(lambda: self.unregister_callbacks())
208208

209+
def custom_context_menu_event(self, e: 'QContextMenuEvent', *, tl_edit: Union[QTextEdit, QLineEdit]) -> None:
210+
m = tl_edit.createStandardContextMenu()
211+
m.addSeparator()
212+
add_input_actions_to_context_menu(self, m)
213+
m.exec(e.globalPos())
214+
209215
@event_listener
210216
def on_event_contacts_updated(self):
211217
self.update_completer()

electrum/gui/qt/qrtextedit.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from electrum.simple_config import SimpleConfig
77

88
from .util import ButtonsTextEdit, MessageBoxMixin, ColorScheme, read_QIcon
9-
from .util import get_icon_camera, get_icon_qrcode
9+
from .util import get_icon_camera, get_icon_qrcode, add_input_actions_to_context_menu
1010

1111

1212
class ShowQRTextEdit(ButtonsTextEdit):
@@ -78,27 +78,20 @@ def add_input_buttons(self, config, allow_multi, setText):
7878
def contextMenuEvent(self, e):
7979
m = self.createStandardContextMenu()
8080
m.addSeparator()
81-
m.addAction(get_icon_camera(), _("Read QR code with camera"), self.on_qr_from_camera_input_btn)
82-
m.addAction(read_QIcon("picture_in_picture.png"), _("Read QR code from screen"), self.on_qr_from_screenshot_input_btn)
83-
m.addAction(read_QIcon("qr_file.png"), _("Read QR code from file"), self.on_qr_from_file_input_btn)
84-
m.addAction(read_QIcon("file.png"), _("Read text from file"), self.on_input_file)
81+
add_input_actions_to_context_menu(self, m)
8582
m.exec(e.globalPos())
8683

8784

88-
class ScanShowQRTextEdit(ButtonsTextEdit, MessageBoxMixin):
85+
class ScanShowQRTextEdit(ScanQRTextEdit):
8986

90-
def __init__(self, text="", allow_multi: bool = False, *, config: SimpleConfig):
91-
ButtonsTextEdit.__init__(self, text)
92-
self.setReadOnly(False)
93-
self.add_qr_input_combined_button(config=config, show_error=self.show_error, allow_multi=allow_multi)
87+
def __init__(self, *args, config: SimpleConfig, **kwargs):
88+
ScanQRTextEdit.__init__(self, *args, **kwargs, config=config)
9489
self.add_qr_show_button(config=config)
95-
run_hook('scan_text_edit', self)
9690
run_hook('show_text_edit', self)
9791

9892
def contextMenuEvent(self, e):
9993
m = self.createStandardContextMenu()
10094
m.addSeparator()
101-
m.addAction(get_icon_camera(), _("Read QR code from camera"), self.on_qr_from_camera_input_btn)
102-
m.addAction(read_QIcon("picture_in_picture.png"), _("Read QR code from screen"), self.on_qr_from_screenshot_input_btn)
95+
add_input_actions_to_context_menu(self, m)
10396
m.addAction(get_icon_qrcode(), _("Show as QR code"), self.on_qr_show_btn)
10497
m.exec(e.globalPos())

electrum/gui/qt/send_tab.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from .amountedit import AmountEdit, BTCAmountEdit, SizedFreezableLineEdit
2626
from .paytoedit import InvalidPaymentIdentifier
2727
from .util import (WaitingDialog, HelpLabel, MessageBoxMixin, EnterButton, char_width_in_lineedit,
28-
get_icon_camera, read_QIcon, ColorScheme, IconLabel, Spinner)
28+
get_icon_camera, read_QIcon, ColorScheme, IconLabel, Spinner, add_input_actions_to_context_menu)
2929
from .invoice_list import InvoiceList
3030

3131
if TYPE_CHECKING:
@@ -178,10 +178,7 @@ def reset_max(text):
178178
self.invoice_list = InvoiceList(self)
179179
self.toolbar, menu = self.invoice_list.create_toolbar_with_menu('')
180180

181-
menu.addAction(get_icon_camera(), _("Read QR code with camera"), self.payto_e.on_qr_from_camera_input_btn)
182-
menu.addAction(read_QIcon("picture_in_picture.png"), _("Read QR code from screen"), self.payto_e.on_qr_from_screenshot_input_btn)
183-
menu.addAction(read_QIcon("qr_file.png"), _("Read QR code from file"), self.payto_e.on_qr_from_file_input_btn)
184-
menu.addAction(read_QIcon("file.png"), _("Read invoice from file"), self.payto_e.on_input_file)
181+
add_input_actions_to_context_menu(self.payto_e, menu)
185182
self.paytomany_menu = menu.addToggle(_("&Pay to many"), self.toggle_paytomany)
186183
menu.addSeparator()
187184
menu.addAction(_("Import invoices"), self.window.import_invoices)

electrum/gui/qt/util.py

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -726,13 +726,15 @@ def get_icon_camera() -> QIcon:
726726
return read_QIcon(name)
727727

728728

729-
def editor_contextMenuEvent(self, p: 'PayToEdit', e: 'QContextMenuEvent') -> None:
730-
m = self.createStandardContextMenu()
731-
m.addSeparator()
732-
m.addAction(get_icon_camera(), _("Read QR code with camera"), p.on_qr_from_camera_input_btn)
733-
m.addAction(read_QIcon("picture_in_picture.png"), _("Read QR code from screen"), p.on_qr_from_screenshot_input_btn)
734-
m.addAction(read_QIcon("file.png"), _("Read file"), p.on_input_file)
735-
m.exec(e.globalPos())
729+
def add_input_actions_to_context_menu(gih: 'GenericInputHandler', m: QMenu) -> None:
730+
if gih.on_qr_from_camera_input_btn:
731+
m.addAction(get_icon_camera(), _("Read QR code with camera"), gih.on_qr_from_camera_input_btn)
732+
if gih.on_qr_from_screenshot_input_btn:
733+
m.addAction(read_QIcon("picture_in_picture.png"), _("Read QR code from screen"), gih.on_qr_from_screenshot_input_btn)
734+
if gih.on_qr_from_file_input_btn:
735+
m.addAction(read_QIcon("qr_file.png"), _("Read QR code from file"), gih.on_qr_from_file_input_btn)
736+
if gih.on_input_file:
737+
m.addAction(read_QIcon("file.png"), _("Read text from file"), gih.on_input_file)
736738

737739

738740
def scan_qr_from_screenshot() -> QrCodeResult:
@@ -758,6 +760,11 @@ def scan_qr_from_screenshot() -> QrCodeResult:
758760

759761

760762
class GenericInputHandler:
763+
on_qr_from_camera_input_btn: Callable[[], None] = None
764+
on_qr_from_screenshot_input_btn: Callable[[], None] = None
765+
on_qr_from_file_input_btn: Callable[[], None] = None
766+
on_input_file: Callable[[], None] = None
767+
761768
def input_qr_from_camera(
762769
self,
763770
*,
@@ -1019,39 +1026,6 @@ def qr_show():
10191026
# side-effect: we export this method:
10201027
self.on_qr_show_btn = qr_show
10211028

1022-
def add_qr_input_combined_button(
1023-
self,
1024-
*,
1025-
config: 'SimpleConfig',
1026-
allow_multi: bool = False,
1027-
show_error: Callable[[str], None],
1028-
setText: Callable[[str], None] = None,
1029-
):
1030-
input_qr_from_camera = partial(
1031-
self.input_qr_from_camera,
1032-
config=config,
1033-
allow_multi=allow_multi,
1034-
show_error=show_error,
1035-
setText=setText,
1036-
)
1037-
input_qr_from_screenshot = partial(
1038-
self.input_qr_from_screenshot,
1039-
allow_multi=allow_multi,
1040-
show_error=show_error,
1041-
setText=setText,
1042-
)
1043-
self.add_menu_button(
1044-
icon=get_icon_camera(),
1045-
tooltip=_("Read QR code"),
1046-
options=[
1047-
(get_icon_camera(), _("Read QR code from camera"), input_qr_from_camera),
1048-
("picture_in_picture.png", _("Read QR code from screen"), input_qr_from_screenshot),
1049-
],
1050-
)
1051-
# side-effect: we export these methods:
1052-
self.on_qr_from_camera_input_btn = input_qr_from_camera
1053-
self.on_qr_from_screenshot_input_btn = input_qr_from_screenshot
1054-
10551029
def add_qr_input_from_camera_button(
10561030
self,
10571031
*,
@@ -1067,7 +1041,7 @@ def add_qr_input_from_camera_button(
10671041
show_error=show_error,
10681042
setText=setText,
10691043
)
1070-
self.addButton(get_icon_camera(), input_qr_from_camera, _("Read QR code from camera"))
1044+
self.addButton(get_icon_camera(), input_qr_from_camera, _("Read QR code with camera"))
10711045
# side-effect: we export these methods:
10721046
self.on_qr_from_camera_input_btn = input_qr_from_camera
10731047

0 commit comments

Comments
 (0)