Files
pallectrum/electrum/gui/qt/qrtextedit.py

76 lines
2.3 KiB
Python
Raw Normal View History

2017-09-23 05:54:38 +02:00
from PyQt5.QtWidgets import QFileDialog
2014-06-14 12:17:44 +02:00
2018-09-25 18:15:28 +02:00
from electrum.i18n import _
from electrum.plugin import run_hook
from .util import ButtonsTextEdit, MessageBoxMixin, ColorScheme
class ShowQRTextEdit(ButtonsTextEdit):
def __init__(self, text=None):
2015-05-08 20:00:13 +02:00
ButtonsTextEdit.__init__(self, text)
self.setReadOnly(1)
icon = "qrcode_white.png" if ColorScheme.dark_scheme else "qrcode.png"
self.addButton(icon, self.qr_show, _("Show as QR code"))
run_hook('show_text_edit', self)
2014-06-14 12:17:44 +02:00
def qr_show(self):
2017-01-30 12:36:56 +03:00
from .qrcodewidget import QRDialog
try:
s = str(self.toPlainText())
2014-09-04 23:33:31 +02:00
except:
2017-01-30 12:36:56 +03:00
s = self.toPlainText()
QRDialog(s).exec_()
2014-06-14 12:17:44 +02:00
2015-10-18 16:00:28 +08:00
def contextMenuEvent(self, e):
m = self.createStandardContextMenu()
m.addAction(_("Show as QR code"), self.qr_show)
m.exec_(e.globalPos())
2015-12-23 18:31:36 +09:00
class ScanQRTextEdit(ButtonsTextEdit, MessageBoxMixin):
def __init__(self, text="", allow_multi=False):
ButtonsTextEdit.__init__(self, text)
self.allow_multi = allow_multi
self.setReadOnly(0)
self.addButton("file.png", self.file_input, _("Read file"))
icon = "camera_white.png" if ColorScheme.dark_scheme else "camera_dark.png"
self.addButton(icon, self.qr_input, _("Read QR code"))
run_hook('scan_text_edit', self)
def file_input(self):
fileName, __ = QFileDialog.getOpenFileName(self, 'select file')
if not fileName:
return
2018-03-06 11:22:49 +01:00
try:
with open(fileName, "r") as f:
data = f.read()
except BaseException as e:
self.show_error(_('Error opening file') + ':\n' + repr(e))
2018-03-06 11:22:49 +01:00
else:
self.setText(data)
2014-06-14 12:17:44 +02:00
def qr_input(self):
from electrum import qrscanner, get_config
2014-08-23 17:45:47 +02:00
try:
2017-02-17 20:56:38 +01:00
data = qrscanner.scan_barcode(get_config().get_video_device())
2015-12-23 18:31:36 +09:00
except BaseException as e:
self.show_error(repr(e))
2017-02-17 20:56:38 +01:00
data = ''
if not data:
data = ''
if self.allow_multi:
new_text = self.text() + data + '\n'
else:
new_text = data
self.setText(new_text)
return data
def contextMenuEvent(self, e):
m = self.createStandardContextMenu()
m.addAction(_("Read QR code"), self.qr_input)
m.exec_(e.globalPos())