URI: 
       tqrtextedit.py - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
       tqrtextedit.py (3034B)
       ---
            1 from PyQt5.QtWidgets import QFileDialog
            2 
            3 from electrum.i18n import _
            4 from electrum.plugin import run_hook
            5 from electrum.simple_config import SimpleConfig
            6 from electrum.util import UserFacingException
            7 
            8 from .util import ButtonsTextEdit, MessageBoxMixin, ColorScheme, getOpenFileName
            9 
           10 
           11 class ShowQRTextEdit(ButtonsTextEdit):
           12 
           13     def __init__(self, text=None, *, config: SimpleConfig):
           14         ButtonsTextEdit.__init__(self, text)
           15         self.config = config
           16         self.setReadOnly(True)
           17         icon = "qrcode_white.png" if ColorScheme.dark_scheme else "qrcode.png"
           18         self.addButton(icon, self.qr_show, _("Show as QR code"))
           19 
           20         run_hook('show_text_edit', self)
           21 
           22     def qr_show(self):
           23         from .qrcodewidget import QRDialog
           24         try:
           25             s = str(self.toPlainText())
           26         except:
           27             s = self.toPlainText()
           28         QRDialog(
           29             data=s,
           30             parent=self,
           31             config=self.config,
           32         ).exec_()
           33 
           34     def contextMenuEvent(self, e):
           35         m = self.createStandardContextMenu()
           36         m.addAction(_("Show as QR code"), self.qr_show)
           37         m.exec_(e.globalPos())
           38 
           39 
           40 class ScanQRTextEdit(ButtonsTextEdit, MessageBoxMixin):
           41 
           42     def __init__(self, text="", allow_multi=False, *, config: SimpleConfig):
           43         ButtonsTextEdit.__init__(self, text)
           44         self.allow_multi = allow_multi
           45         self.config = config
           46         self.setReadOnly(False)
           47         self.addButton("file.png", self.file_input, _("Read file"))
           48         icon = "camera_white.png" if ColorScheme.dark_scheme else "camera_dark.png"
           49         self.addButton(icon, self.qr_input, _("Read QR code"))
           50         run_hook('scan_text_edit', self)
           51 
           52     def file_input(self):
           53         fileName = getOpenFileName(
           54             parent=self,
           55             title='select file',
           56             config=self.config,
           57         )
           58         if not fileName:
           59             return
           60         try:
           61             try:
           62                 with open(fileName, "r") as f:
           63                     data = f.read()
           64             except UnicodeError as e:
           65                 with open(fileName, "rb") as f:
           66                     data = f.read()
           67                 data = data.hex()
           68         except BaseException as e:
           69             self.show_error(_('Error opening file') + ':\n' + repr(e))
           70         else:
           71             self.setText(data)
           72 
           73     def qr_input(self):
           74         from electrum import qrscanner
           75         data = ''
           76         try:
           77             data = qrscanner.scan_barcode(self.config.get_video_device())
           78         except UserFacingException as e:
           79             self.show_error(e)
           80         except BaseException as e:
           81             self.logger.exception('camera error')
           82             self.show_error(repr(e))
           83         if not data:
           84             data = ''
           85         if self.allow_multi:
           86             new_text = self.text() + data + '\n'
           87         else:
           88             new_text = data
           89         self.setText(new_text)
           90         return data
           91 
           92     def contextMenuEvent(self, e):
           93         m = self.createStandardContextMenu()
           94         m.addAction(_("Read QR code"), self.qr_input)
           95         m.exec_(e.globalPos())