2 changed files with 102 additions and 61 deletions
@ -1,97 +1,140 @@ |
|||||||
from PyQt5.QtWidgets import QFileDialog |
from functools import partial |
||||||
|
from typing import Callable |
||||||
|
|
||||||
from electrum.i18n import _ |
from electrum.i18n import _ |
||||||
from electrum.plugin import run_hook |
from electrum.plugin import run_hook |
||||||
from electrum.simple_config import SimpleConfig |
from electrum.simple_config import SimpleConfig |
||||||
from electrum.util import UserFacingException |
|
||||||
from electrum.logging import Logger |
|
||||||
|
|
||||||
from .util import ButtonsTextEdit, MessageBoxMixin, ColorScheme, getOpenFileName |
from .util import ButtonsTextEdit, MessageBoxMixin, ColorScheme, getOpenFileName |
||||||
from .qrreader import scan_qrcode |
from .qrreader import scan_qrcode |
||||||
|
|
||||||
|
|
||||||
|
def qr_show(text_edit, *, config: SimpleConfig) -> None: |
||||||
|
from .qrcodewidget import QRDialog |
||||||
|
try: |
||||||
|
s = str(text_edit.text()) |
||||||
|
except: |
||||||
|
s = text_edit.text() |
||||||
|
if not s: |
||||||
|
return |
||||||
|
QRDialog( |
||||||
|
data=s, |
||||||
|
parent=text_edit, |
||||||
|
config=config, |
||||||
|
).exec_() |
||||||
|
|
||||||
|
|
||||||
|
def qr_input( |
||||||
|
text_edit, |
||||||
|
*, |
||||||
|
config: SimpleConfig, |
||||||
|
allow_multi: bool = False, |
||||||
|
show_error: Callable[[str], None], |
||||||
|
) -> None: |
||||||
|
def cb(success: bool, error: str, data): |
||||||
|
if not success: |
||||||
|
if error: |
||||||
|
show_error(error) |
||||||
|
return |
||||||
|
if not data: |
||||||
|
data = '' |
||||||
|
if allow_multi: |
||||||
|
new_text = text_edit.text() + data + '\n' |
||||||
|
else: |
||||||
|
new_text = data |
||||||
|
text_edit.setText(new_text) |
||||||
|
|
||||||
|
scan_qrcode(parent=text_edit, config=config, callback=cb) |
||||||
|
|
||||||
|
|
||||||
|
def file_input( |
||||||
|
text_edit, |
||||||
|
*, |
||||||
|
config: SimpleConfig, |
||||||
|
show_error: Callable[[str], None], |
||||||
|
) -> None: |
||||||
|
fileName = getOpenFileName( |
||||||
|
parent=text_edit, |
||||||
|
title='select file', |
||||||
|
config=config, |
||||||
|
) |
||||||
|
if not fileName: |
||||||
|
return |
||||||
|
try: |
||||||
|
try: |
||||||
|
with open(fileName, "r") as f: |
||||||
|
data = f.read() |
||||||
|
except UnicodeError as e: |
||||||
|
with open(fileName, "rb") as f: |
||||||
|
data = f.read() |
||||||
|
data = data.hex() |
||||||
|
except BaseException as e: |
||||||
|
show_error(_('Error opening file') + ':\n' + repr(e)) |
||||||
|
else: |
||||||
|
text_edit.setText(data) |
||||||
|
|
||||||
|
|
||||||
class ShowQRTextEdit(ButtonsTextEdit): |
class ShowQRTextEdit(ButtonsTextEdit): |
||||||
|
|
||||||
def __init__(self, text=None, *, config: SimpleConfig): |
def __init__(self, text=None, *, config: SimpleConfig): |
||||||
ButtonsTextEdit.__init__(self, text) |
ButtonsTextEdit.__init__(self, text) |
||||||
self.config = config |
self.config = config |
||||||
self.setReadOnly(True) |
self.setReadOnly(True) |
||||||
|
# qr_show |
||||||
|
self.qr_show = partial(qr_show, self, config=config) |
||||||
icon = "qrcode_white.png" if ColorScheme.dark_scheme else "qrcode.png" |
icon = "qrcode_white.png" if ColorScheme.dark_scheme else "qrcode.png" |
||||||
self.addButton(icon, self.qr_show, _("Show as QR code")) |
self.addButton(icon, self.qr_show, _("Show as QR code")) |
||||||
|
|
||||||
run_hook('show_text_edit', self) |
run_hook('show_text_edit', self) |
||||||
|
|
||||||
def qr_show(self): |
|
||||||
from .qrcodewidget import QRDialog |
|
||||||
try: |
|
||||||
s = str(self.toPlainText()) |
|
||||||
except: |
|
||||||
s = self.toPlainText() |
|
||||||
QRDialog( |
|
||||||
data=s, |
|
||||||
parent=self, |
|
||||||
config=self.config, |
|
||||||
).exec_() |
|
||||||
|
|
||||||
def contextMenuEvent(self, e): |
def contextMenuEvent(self, e): |
||||||
m = self.createStandardContextMenu() |
m = self.createStandardContextMenu() |
||||||
m.addAction(_("Show as QR code"), self.qr_show) |
m.addAction(_("Show as QR code"), self.qr_show) |
||||||
m.exec_(e.globalPos()) |
m.exec_(e.globalPos()) |
||||||
|
|
||||||
|
|
||||||
class ScanQRTextEdit(ButtonsTextEdit, MessageBoxMixin, Logger): |
class ScanQRTextEdit(ButtonsTextEdit, MessageBoxMixin): |
||||||
|
|
||||||
def __init__(self, text="", allow_multi=False, *, config: SimpleConfig): |
def __init__(self, text="", allow_multi: bool = False, *, config: SimpleConfig): |
||||||
ButtonsTextEdit.__init__(self, text) |
ButtonsTextEdit.__init__(self, text) |
||||||
Logger.__init__(self) |
|
||||||
self.allow_multi = allow_multi |
|
||||||
self.config = config |
self.config = config |
||||||
self.setReadOnly(False) |
self.setReadOnly(False) |
||||||
|
# file_input |
||||||
|
self.file_input = partial(file_input, self, config=config, show_error=self.show_error) |
||||||
self.addButton("file.png", self.file_input, _("Read file")) |
self.addButton("file.png", self.file_input, _("Read file")) |
||||||
|
# qr_input |
||||||
|
self.qr_input = partial(qr_input, self, config=config, show_error=self.show_error, allow_multi=allow_multi) |
||||||
icon = "camera_white.png" if ColorScheme.dark_scheme else "camera_dark.png" |
icon = "camera_white.png" if ColorScheme.dark_scheme else "camera_dark.png" |
||||||
self.addButton(icon, self.qr_input, _("Read QR code")) |
self.addButton(icon, self.qr_input, _("Read QR code")) |
||||||
|
|
||||||
run_hook('scan_text_edit', self) |
run_hook('scan_text_edit', self) |
||||||
|
|
||||||
def file_input(self): |
def contextMenuEvent(self, e): |
||||||
fileName = getOpenFileName( |
m = self.createStandardContextMenu() |
||||||
parent=self, |
m.addAction(_("Read QR code"), self.qr_input) |
||||||
title='select file', |
m.exec_(e.globalPos()) |
||||||
config=self.config, |
|
||||||
) |
|
||||||
if not fileName: |
class ScanShowQRTextEdit(ButtonsTextEdit, MessageBoxMixin): |
||||||
return |
|
||||||
try: |
def __init__(self, text="", allow_multi: bool = False, *, config: SimpleConfig): |
||||||
try: |
ButtonsTextEdit.__init__(self, text) |
||||||
with open(fileName, "r") as f: |
self.config = config |
||||||
data = f.read() |
self.setReadOnly(False) |
||||||
except UnicodeError as e: |
# qr_input |
||||||
with open(fileName, "rb") as f: |
self.qr_input = partial(qr_input, self, config=config, show_error=self.show_error, allow_multi=allow_multi) |
||||||
data = f.read() |
icon = "camera_white.png" if ColorScheme.dark_scheme else "camera_dark.png" |
||||||
data = data.hex() |
self.addButton(icon, self.qr_input, _("Read QR code")) |
||||||
except BaseException as e: |
# qr_show |
||||||
self.show_error(_('Error opening file') + ':\n' + repr(e)) |
self.qr_show = partial(qr_show, self, config=config) |
||||||
else: |
icon = "qrcode_white.png" if ColorScheme.dark_scheme else "qrcode.png" |
||||||
self.setText(data) |
self.addButton(icon, self.qr_show, _("Show as QR code")) |
||||||
|
|
||||||
def qr_input(self, *, callback=None) -> None: |
run_hook('scan_text_edit', self) |
||||||
def cb(success: bool, error: str, data): |
run_hook('show_text_edit', self) |
||||||
if not success: |
|
||||||
if error: |
|
||||||
self.show_error(error) |
|
||||||
return |
|
||||||
if not data: |
|
||||||
data = '' |
|
||||||
if self.allow_multi: |
|
||||||
new_text = self.text() + data + '\n' |
|
||||||
else: |
|
||||||
new_text = data |
|
||||||
self.setText(new_text) |
|
||||||
if callback and success: |
|
||||||
callback(data) |
|
||||||
|
|
||||||
scan_qrcode(parent=self.top_level_window(), config=self.config, callback=cb) |
|
||||||
|
|
||||||
def contextMenuEvent(self, e): |
def contextMenuEvent(self, e): |
||||||
m = self.createStandardContextMenu() |
m = self.createStandardContextMenu() |
||||||
m.addAction(_("Read QR code"), self.qr_input) |
m.addAction(_("Read QR code"), self.qr_input) |
||||||
|
m.addAction(_("Show as QR code"), self.qr_show) |
||||||
m.exec_(e.globalPos()) |
m.exec_(e.globalPos()) |
||||||
|
|||||||
Loading…
Reference in new issue