From d2cad7bbbbfa1b6b175ec39de36a76453192137d Mon Sep 17 00:00:00 2001 From: ThomasV Date: Sat, 14 Jun 2014 12:17:44 +0200 Subject: [PATCH] new widget: QRTextEdit --- gui/qt/main_window.py | 45 ++++------------------------------------ gui/qt/paytoedit.py | 14 +++++++++--- gui/qt/qrcodewidget.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ gui/qt/qrtextedit.py | 43 ++++++++++++++++++++++++++++++++++++++ gui/qt/seed_dialog.py | 7 +++-- gui/qt/util.py | 3 +- icons.qrc | 1 + icons/qrcode.png | Bin 0 -> 314 bytes plugins/qrscanner.py | 13 ++++++----- 9 files changed, 125 insertions(+), 54 deletions(-) create mode 100644 gui/qt/qrtextedit.py create mode 100644 icons/qrcode.png diff --git a/gui/qt/main_window.py b/gui/qt/main_window.py index f8bd372..9f68e28 100644 --- a/gui/qt/main_window.py +++ b/gui/qt/main_window.py @@ -45,7 +45,7 @@ from electrum import bmp, pyqrnative from amountedit import AmountEdit, BTCAmountEdit, MyLineEdit from network_dialog import NetworkDialog -from qrcodewidget import QRCodeWidget +from qrcodewidget import QRCodeWidget, QRDialog from decimal import Decimal @@ -664,7 +664,7 @@ class ElectrumWindow(QMainWindow): from paytoedit import PayToEdit self.amount_e = BTCAmountEdit(self.get_decimal_point) - self.payto_e = PayToEdit(self.amount_e) + self.payto_e = PayToEdit(self) self.payto_help = HelpButton(_('Recipient of the funds.') + '\n\n' + _('You may enter a Bitcoin address, a label from your list of contacts (a list of completions will be proposed), or an alias (email-like address that forwards to a Bitcoin address)')) grid.addWidget(QLabel(_('Pay to')), 1, 0) grid.addWidget(self.payto_e, 1, 1, 1, 3) @@ -1669,44 +1669,9 @@ class ElectrumWindow(QMainWindow): def show_qrcode(self, data, title = _("QR code")): - if not data: return - d = QDialog(self) - d.setModal(1) - d.setWindowTitle(title) - d.setMinimumSize(270, 300) - vbox = QVBoxLayout() - qrw = QRCodeWidget(data) - vbox.addWidget(qrw, 1) - vbox.addWidget(QLabel(data), 0, Qt.AlignHCenter) - hbox = QHBoxLayout() - hbox.addStretch(1) - - filename = os.path.join(self.config.path, "qrcode.bmp") - - def print_qr(): - bmp.save_qrcode(qrw.qr, filename) - QMessageBox.information(None, _('Message'), _("QR code saved to file") + " " + filename, _('OK')) - - def copy_to_clipboard(): - bmp.save_qrcode(qrw.qr, filename) - self.app.clipboard().setImage(QImage(filename)) - QMessageBox.information(None, _('Message'), _("QR code saved to clipboard"), _('OK')) - - b = QPushButton(_("Copy")) - hbox.addWidget(b) - b.clicked.connect(copy_to_clipboard) - - b = QPushButton(_("Save")) - hbox.addWidget(b) - b.clicked.connect(print_qr) - - b = QPushButton(_("Close")) - hbox.addWidget(b) - b.clicked.connect(d.accept) - b.setDefault(True) - - vbox.addLayout(hbox) - d.setLayout(vbox) + if not data: + return + d = QRDialog(data, self, title) d.exec_() diff --git a/gui/qt/paytoedit.py b/gui/qt/paytoedit.py index db7c720..44db585 100644 --- a/gui/qt/paytoedit.py +++ b/gui/qt/paytoedit.py @@ -18,6 +18,7 @@ from PyQt4.QtCore import * from PyQt4.QtGui import * +from qrtextedit import QRTextEdit import re from decimal import Decimal @@ -29,11 +30,12 @@ RE_ALIAS = '(.*?)\s*\<([1-9A-HJ-NP-Za-km-z]{26,})\>' frozen_style = "QWidget { background-color:none; border:none;}" normal_style = "QTextEdit { }" -class PayToEdit(QTextEdit): +class PayToEdit(QRTextEdit): - def __init__(self, amount_edit): - QTextEdit.__init__(self) - self.amount_edit = amount_edit + def __init__(self, win): + QRTextEdit.__init__(self) + self.win = win + self.amount_edit = win.amount_e self.document().contentsChanged.connect(self.update_size) self.heightMin = 0 self.heightMax = 150 @@ -44,6 +46,10 @@ class PayToEdit(QTextEdit): self.outputs = [] self.is_pr = False + def qr_input(self): + from electrum.plugins import run_hook + run_hook('scan_qr_hook', lambda x: self.win.pay_from_URI(x)) + def lock_amount(self): self.amount_edit.setFrozen(True) diff --git a/gui/qt/qrcodewidget.py b/gui/qt/qrcodewidget.py index b1033af..6866686 100644 --- a/gui/qt/qrcodewidget.py +++ b/gui/qt/qrcodewidget.py @@ -82,3 +82,56 @@ class QRCodeWidget(QWidget): qp.drawRect(left+c*boxsize, top+r*boxsize, boxsize, boxsize) qp.end() + +import os +from electrum.i18n import _ + +class QRDialog(QDialog): + + def __init__(self, data, parent=None, title = "", show_text=False): + QDialog.__init__(self, parent) + + d = self + d.setModal(1) + d.setWindowTitle(title) + d.setMinimumSize(270, 300) + vbox = QVBoxLayout() + qrw = QRCodeWidget(data) + vbox.addWidget(qrw, 1) + if show_text: + text = QTextEdit() + text.setText(data) + text.setReadOnly(True) + vbox.addWidget(text) + hbox = QHBoxLayout() + hbox.addStretch(1) + + if parent: + self.config = parent.config + filename = os.path.join(self.config.path, "qrcode.bmp") + + def print_qr(): + bmp.save_qrcode(qrw.qr, filename) + QMessageBox.information(None, _('Message'), _("QR code saved to file") + " " + filename, _('OK')) + + def copy_to_clipboard(): + bmp.save_qrcode(qrw.qr, filename) + self.parent().app.clipboard().setImage(QImage(filename)) + QMessageBox.information(None, _('Message'), _("QR code saved to clipboard"), _('OK')) + + b = QPushButton(_("Copy")) + hbox.addWidget(b) + b.clicked.connect(copy_to_clipboard) + + b = QPushButton(_("Save")) + hbox.addWidget(b) + b.clicked.connect(print_qr) + + b = QPushButton(_("Close")) + hbox.addWidget(b) + b.clicked.connect(d.accept) + b.setDefault(True) + + vbox.addLayout(hbox) + d.setLayout(vbox) + diff --git a/gui/qt/qrtextedit.py b/gui/qt/qrtextedit.py new file mode 100644 index 0000000..742b97e --- /dev/null +++ b/gui/qt/qrtextedit.py @@ -0,0 +1,43 @@ +from electrum.i18n import _ +from PyQt4.QtGui import * +from PyQt4.QtCore import * + +class QRTextEdit(QTextEdit): + + def __init__(self): + QTextEdit.__init__(self) + self.button = QToolButton(self) + self.button.setIcon(QIcon(":icons/qrcode.png")) + self.button.setStyleSheet("QToolButton { border: none; padding: 0px; }") + self.button.setVisible(True) + self.button.clicked.connect(lambda: self.qr_show() if self.isReadOnly() else self.qr_input()) + #frameWidth = self.style().pixelMetric(QStyle.PM_DefaultFrameWidth) + #self.setStyleSheet(QString("QLineEdit { padding-right: %1px; } ").arg(self.button.sizeHint().width() + frameWidth + 1)) + #msz = self.minimumSizeHint() + #self.setMinimumSize(max(msz.width(), self.button.sizeHint().height() + frameWidth * 2 + 2), + # max(msz.height(), self.button.sizeHint().height() + frameWidth * 2 + 2)) + + def resizeEvent(self, e): + o = QTextEdit.resizeEvent(self, e) + sz = self.button.sizeHint() + frameWidth = self.style().pixelMetric(QStyle.PM_DefaultFrameWidth) + self.button.move(self.rect().right() - frameWidth - sz.width(), + (self.rect().bottom() - frameWidth - sz.height())) + return o + + def contextMenuEvent(self, e): + m = self.createStandardContextMenu() + if self.isReadOnly(): + m.addAction(_("Show as QR code"), self.qr_show) + else: + m.addAction(_("Read QR code"), self.qr_input) + m.exec_(e.globalPos()) + + def qr_show(self): + from qrcodewidget import QRDialog + QRDialog(str(self.toPlainText())).exec_() + + def qr_input(self): + from electrum.plugins import run_hook + run_hook('scan_qr_hook', self.setText) + diff --git a/gui/qt/seed_dialog.py b/gui/qt/seed_dialog.py index 17457a7..ca021e1 100644 --- a/gui/qt/seed_dialog.py +++ b/gui/qt/seed_dialog.py @@ -21,8 +21,9 @@ from PyQt4.QtCore import * import PyQt4.QtCore as QtCore from electrum.i18n import _ from electrum import mnemonic -from qrcodewidget import QRCodeWidget +from qrcodewidget import QRCodeWidget, QRDialog from util import close_button +from qrtextedit import QRTextEdit class SeedDialog(QDialog): def __init__(self, parent, seed, imported_keys): @@ -70,7 +71,7 @@ def show_seed_box(seed, sid=None): + _("If you ever need to recover your wallet from seed, you will need both this seed and your cold seed.") + " " \ label1 = QLabel(msg+ ":") - seed_text = QTextEdit(seed) + seed_text = QRTextEdit(seed) seed_text.setReadOnly(True) seed_text.setMaximumHeight(130) @@ -106,7 +107,7 @@ def enter_seed_box(msg, sid=None): label = QLabel(msg) label.setWordWrap(True) - seed_e = QTextEdit() + seed_e = QRTextEdit() seed_e.setMaximumHeight(100) seed_e.setTabChangesFocus(True) diff --git a/gui/qt/util.py b/gui/qt/util.py index 9842f05..e6c13a8 100644 --- a/gui/qt/util.py +++ b/gui/qt/util.py @@ -98,6 +98,7 @@ def ok_cancel_buttons(dialog, ok_label=_("OK") ): return hbox def text_dialog(parent, title, label, ok_label, default=None): + from qrtextedit import QRTextEdit dialog = QDialog(parent) dialog.setMinimumWidth(500) dialog.setWindowTitle(title) @@ -105,7 +106,7 @@ def text_dialog(parent, title, label, ok_label, default=None): l = QVBoxLayout() dialog.setLayout(l) l.addWidget(QLabel(label)) - txt = QTextEdit() + txt = QRTextEdit() if default: txt.setText(default) l.addWidget(txt) diff --git a/icons.qrc b/icons.qrc index 00e3613..5dce17b 100644 --- a/icons.qrc +++ b/icons.qrc @@ -24,5 +24,6 @@ icons/unconfirmed.png icons/network.png icons/dark_background.png + icons/qrcode.png diff --git a/icons/qrcode.png b/icons/qrcode.png new file mode 100644 index 0000000000000000000000000000000000000000..41a84aa1d424088bc50edb767a4037d3c30f8c29 GIT binary patch literal 314 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`k|nMYCBgY=CFO}lsSJ)O`AMk? zp1FzXsX?iUDV2pMQ*D5X?s&R5hE&{2`t$$4J+o?qAcsSVvOxCYldTLNrF3#6c5of} zsvzu8Av?wG`TE03ZHLmEX0a_cU+{6}hK_|FF7k35ICOk<8|$e=wnu9t4sobF>-Fc= z5h!ew4Y