electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit c24bd3064370c11a5726775fe2366096dad73626
parent b6beb2e97c10a39d4f0380fed0c85186e5e6ff21
Author: Amir Taaki <genjix@riseup.net>
Date:   Fri, 29 Jun 2012 01:56:27 +0200

basic functionality:
- can type in address field which is validated
- amount field uses a validator
- copy bitcoin address for receiving funds
- send copied over (untested - needs work)

Diffstat:
Mlib/gui_lite.py | 146++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
1 file changed, 119 insertions(+), 27 deletions(-)

diff --git a/lib/gui_lite.py b/lib/gui_lite.py @@ -1,9 +1,11 @@ from PyQt4.QtCore import * from PyQt4.QtGui import * +from i18n import _ +import decimal +import random +import re import sys -_ = lambda trtext: trtext - def IconButton(filename, parent=None): pixmap = QPixmap(filename) icon = QIcon(pixmap) @@ -18,21 +20,23 @@ class ElectrumGui: self.app.setStyleSheet(style_file.read()) def main(self, url): - mini = MiniWindow() + actuator = MiniActuator(self.wallet) + mini = MiniWindow(actuator) driver = MiniDriver(self.wallet, mini) sys.exit(self.app.exec_()) class MiniWindow(QDialog): - def __init__(self): + def __init__(self, actuator): super(MiniWindow, self).__init__() + self.actuator = actuator + accounts_button = IconButton("data/icons/accounts.png") accounts_button.setObjectName("accounts_button") accounts_selector = QMenu() - accounts_selector.addAction("Normal (80.00 BTC)") - accounts_selector.addAction("Drugs (7.50 BTC)") + accounts_selector.addAction("Checking (80.00 BTC)") accounts_selector.addAction("Reddit Girls (3.50 BTC)") accounts_button.setMenu(accounts_selector) @@ -40,46 +44,54 @@ class MiniWindow(QDialog): interact_button.setObjectName("interact_button") app_menu = QMenu() - app_menu.addAction("Blaa") - file_menu = QMenu("File", app_menu) - file_menu.addAction("Foo") - file_menu.addAction("Bar") - app_menu.addMenu(file_menu) - app_menu.addAction("Other") interact_button.setMenu(app_menu) expand_button = IconButton("data/icons/expand.png") expand_button.setObjectName("expand_button") self.balance_label = BalanceLabel() - self.balance_label.set_balances("80.00", "60.00", "EUR") + # WTF?! + # TODO: Fix this! + import time + time.sleep(0.1) self.balance_label.setObjectName("balance_label") copy_button = QPushButton(_("&Copy Address")) copy_button.setObjectName("copy_button") copy_button.setDefault(True) + self.connect(copy_button, SIGNAL("clicked()"), + self.actuator.copy_address) # Use QCompleter - address_input = TextedLineEdit(_("Enter a Bitcoin address...")) - address_input.setObjectName("address_input") - valid_address = QCheckBox() - valid_address.setObjectName("valid_address") - valid_address.setEnabled(False) - #valid_address.setChecked(True) + self.address_input = TextedLineEdit(_("Enter a Bitcoin address...")) + self.address_input.setObjectName("address_input") + self.connect(self.address_input, SIGNAL("textChanged(QString)"), + self.address_field_changed) + self.valid_address = QCheckBox() + self.valid_address.setObjectName("valid_address") + self.valid_address.setEnabled(False) + self.valid_address.setChecked(False) address_layout = QHBoxLayout() - address_layout.addWidget(address_input) - address_layout.addWidget(valid_address) - - amount_input = TextedLineEdit(_("... and amount")) - amount_input.setObjectName("amount_input") + address_layout.addWidget(self.address_input) + address_layout.addWidget(self.valid_address) + + self.amount_input = TextedLineEdit(_("... and amount")) + self.amount_input.setObjectName("amount_input") + # This is changed according to the user's displayed balance + self.amount_validator = QDoubleValidator(self.amount_input) + self.amount_validator.setNotation(QDoubleValidator.StandardNotation) + self.amount_validator.setRange(0, 0) + self.amount_validator.setDecimals(2) + self.amount_input.setValidator(self.amount_validator) amount_layout = QHBoxLayout() - amount_layout.addWidget(amount_input) + amount_layout.addWidget(self.amount_input) amount_layout.addStretch() send_button = QPushButton(_("&Send")) send_button.setObjectName("send_button") + self.connect(send_button, SIGNAL("clicked()"), self.send) main_layout = QGridLayout(self) main_layout.addWidget(accounts_button, 0, 0) @@ -94,7 +106,7 @@ class MiniWindow(QDialog): main_layout.addLayout(amount_layout, 2, 1) main_layout.addWidget(send_button, 2, 2) - self.setWindowTitle("Electrum - Normal (80.00 BTC)") + self.setWindowTitle("Electrum") self.setWindowFlags(Qt.Window|Qt.MSWindowsFixedSizeDialogHint) self.layout().setSizeConstraint(QLayout.SetFixedSize) self.setObjectName("main_window") @@ -113,6 +125,18 @@ class MiniWindow(QDialog): def set_balances(self, btc_balance, quote_balance, quote_currency): self.balance_label.set_balances( \ btc_balance, quote_balance, quote_currency) + self.amount_validator.setRange(0, btc_balance) + self.setWindowTitle("Electrum - %s BTC"%btc_balance) + + def send(self): + self.actuator.send(self.address_input.text(), + self.amount_input.text(), self) + + def address_field_changed(self, address): + if self.actuator.is_valid(address): + self.valid_address.setChecked(True) + else: + self.valid_address.setChecked(False) class BalanceLabel(QLabel): @@ -161,6 +185,69 @@ class TextedLineEdit(QLineEdit): # also possible but more expensive: #qApp.setStyleSheet(qApp.styleSheet()) +class MiniActuator: + + def __init__(self, wallet): + self.wallet = wallet + + def copy_address(self): + addrs = [addr for addr in self.wallet.all_addresses() + if not self.wallet.is_change(addr)] + qApp.clipboard().setText(random.choice(addrs)) + + def send(self, address, amount, parent_window): + recipient = unicode(address).strip() + + # alias + match1 = re.match(ALIAS_REGEXP, r) + # label or alias, with address in brackets + match2 = re.match('(.*?)\s*\<([1-9A-HJ-NP-Za-km-z]{26,})\>', r) + + if match1: + dest_address = \ + self.wallet.get_alias(recipient, True, + self.show_message, self.question) + if not dest_address: + return + elif match2: + dest_address = match2.group(2) + else: + dest_address = recipient + + if not self.wallet.is_valid(dest_address): + QMessageBox.warning(parent_window, _('Error'), + _('Invalid Bitcoin Address') + ':\n' + dest_address, _('OK')) + return + + convert_amount = lambda amount: \ + int(decimal.Decimal(unicode(amount)) * 100000000) + + amount = convert_amount(amount) + + if self.wallet.use_encryption: + password = self.password_dialog() + if not password: + return + else: + password = None + + try: + tx = self.wallet.mktx(dest_address, amount, "", password, fee) + except BaseException, e: + self.show_message(str(e)) + return + + status, msg = self.wallet.sendtx( tx ) + if status: + QMessageBox.information(self, '', _('Payment sent.')+'\n'+msg, _('OK')) + self.do_clear() + self.update_contacts_tab() + else: + QMessageBox.warning(self, _('Error'), msg, _('OK')) + + def is_valid(self, address): + return self.wallet.is_valid(address) + class MiniDriver: INITIALIZING = 0 @@ -189,6 +276,9 @@ class MiniDriver: else: self.ready() + if self.wallet.up_to_date: + self.update_balance() + def initializing(self): if self.state == self.INITIALIZING: return @@ -212,9 +302,11 @@ class MiniDriver: return self.state = self.READY self.window.activate() + + def update_balance(self): conf_balance, unconf_balance = self.wallet.get_balance() balance = conf_balance if unconf_balance is None else unconf_balance - self.window.set_balances(balance, 0, 'EUR') + self.window.set_balances(balance, balance * 6, 'EUR') if __name__ == "__main__": app = QApplication(sys.argv)