setWindowIcon() to electrum.png
[electrum-nvc.git] / lib / gui_lite.py
index bdd025a..58895a9 100644 (file)
@@ -1,9 +1,11 @@
 from PyQt4.QtCore import *
 from PyQt4.QtGui import *
 from decimal import Decimal as D
+from util import appdata_dir, get_resource_path as rsrc
 from i18n import _
 import decimal
 import exchange_rate
+import os.path
 import random
 import re
 import sys
@@ -28,12 +30,22 @@ class Timer(QThread):
             self.emit(SIGNAL('timersignal'))
             time.sleep(0.5)
 
+def resize_line_edit_width(line_edit, text_input):
+    metrics = QFontMetrics(qApp.font())
+    # Create an extra character to add some space on the end
+    text_input += "A"
+    line_edit.setMinimumWidth(metrics.width(text_input))
+
 class ElectrumGui:
 
     def __init__(self, wallet):
         self.wallet = wallet
         self.app = QApplication(sys.argv)
-        with open("data/style.css") as style_file:
+        if os.path.exists("data"):
+            QDir.setCurrent("data")
+        else:
+            QDir.setCurrent(appdata_dir())
+        with open(rsrc("style.css")) as style_file:
             self.app.setStyleSheet(style_file.read())
 
     def main(self, url):
@@ -51,7 +63,7 @@ class ElectrumGui:
         self.expert.connect_slots(timer)
         self.expert.update_wallet()
 
-        sys.exit(self.app.exec_())
+        self.app.exec_()
 
     def expand(self):
         self.mini.hide()
@@ -71,6 +83,10 @@ class ElectrumGui:
                                       QMessageBox.No)
         return choice == QMessageBox.Yes
 
+    def restore_or_create(self):
+        qt_gui_object = gui_qt.ElectrumGui(self.wallet, self.app)
+        return qt_gui_object.restore_or_create()
+
 class MiniWindow(QDialog):
 
     def __init__(self, actuator, expand_callback):
@@ -78,13 +94,13 @@ class MiniWindow(QDialog):
 
         self.actuator = actuator
 
-        accounts_button = IconButton("data/icons/accounts.png")
+        accounts_button = IconButton(rsrc("icons", "accounts.png"))
         accounts_button.setObjectName("accounts_button")
 
         self.accounts_selector = QMenu()
         accounts_button.setMenu(self.accounts_selector)
 
-        interact_button = IconButton("data/icons/interact.png")
+        interact_button = IconButton(rsrc("icons", "interact.png"))
         interact_button.setObjectName("interact_button")
 
         app_menu = QMenu(interact_button)
@@ -99,12 +115,13 @@ class MiniWindow(QDialog):
         self.connect(about_action, SIGNAL("triggered()"), self.show_about)
         self.connect(quit_action, SIGNAL("triggered()"), self.close)
 
-        expand_button = IconButton("data/icons/expand.png")
+        expand_button = IconButton(rsrc("icons", "expand.png"))
         expand_button.setObjectName("expand_button")
         self.connect(expand_button, SIGNAL("clicked()"), expand_callback)
 
         self.btc_balance = None
-        self.quote_currencies = ("EUR", "USD", "GBP")
+        self.quote_currencies = ["EUR", "USD", "GBP"]
+        self.actuator.set_configured_currency(self.set_quote_currency)
         self.exchanger = exchange_rate.Exchanger(self)
         # Needed because price discovery is done in a different thread
         # which needs to be sent back to this main one to update the GUI
@@ -113,26 +130,24 @@ class MiniWindow(QDialog):
         self.balance_label = BalanceLabel(self.change_quote_currency)
         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)
+        self.receive_button = QPushButton(_("&Receive"))
+        self.receive_button.setObjectName("receive_button")
+        self.receive_button.setDefault(True)
+        self.connect(self.receive_button, SIGNAL("clicked()"),
+                     self.copy_address)
 
         self.address_input = TextedLineEdit(_("Enter a Bitcoin address..."))
         self.address_input.setObjectName("address_input")
         self.connect(self.address_input, SIGNAL("textEdited(QString)"),
                      self.address_field_changed)
-        metrics = QFontMetrics(qApp.font())
-        self.address_input.setMinimumWidth(
-            metrics.width("1E4vM9q25xsyDwWwdqHUWnwshdWC9PykmL"))
+        resize_line_edit_width(self.address_input,
+                               "1BtaFUr3qVvAmwrsuDuu5zk6e4s2rxd2Gy")
 
         self.address_completions = QStringListModel()
         address_completer = QCompleter(self.address_input)
         address_completer.setCaseSensitivity(False)
         address_completer.setModel(self.address_completions)
         self.address_input.setCompleter(address_completer)
-        self.address_completions.setStringList(["1brmlab", "hello"])
 
         self.valid_address = QCheckBox()
         self.valid_address.setObjectName("valid_address")
@@ -168,13 +183,19 @@ class MiniWindow(QDialog):
         main_layout.addWidget(expand_button, 2, 0)
 
         main_layout.addWidget(self.balance_label, 0, 1)
-        main_layout.addWidget(copy_button, 0, 2)
+        main_layout.addWidget(self.receive_button, 0, 2)
 
         main_layout.addLayout(address_layout, 1, 1, 1, -1)
 
         main_layout.addLayout(amount_layout, 2, 1)
         main_layout.addWidget(send_button, 2, 2)
 
+        quit_shortcut = QShortcut(QKeySequence("Ctrl+Q"), self)
+        self.connect(quit_shortcut, SIGNAL("activated()"), self.close)
+        close_shortcut = QShortcut(QKeySequence("Ctrl+W"), self)
+        self.connect(close_shortcut, SIGNAL("activated()"), self.close)
+
+        self.setWindowIcon(QIcon(":electrum.png"))
         self.setWindowTitle("Electrum")
         self.setWindowFlags(Qt.Window|Qt.MSWindowsFixedSizeDialogHint)
         self.layout().setSizeConstraint(QLayout.SetFixedSize)
@@ -198,9 +219,16 @@ class MiniWindow(QDialog):
     def deactivate(self):
         pass
 
+    def set_quote_currency(self, currency):
+        assert currency in self.quote_currencies
+        self.quote_currencies.remove(currency)
+        self.quote_currencies = [currency] + self.quote_currencies
+        self.refresh_balance()
+
     def change_quote_currency(self):
         self.quote_currencies = \
             self.quote_currencies[1:] + self.quote_currencies[0:1]
+        self.actuator.set_config_currency(self.quote_currencies[0])
         self.refresh_balance()
 
     def refresh_balance(self):
@@ -219,10 +247,11 @@ class MiniWindow(QDialog):
         btc_balance = "%.2f" % (btc_balance / bitcoin(1))
         self.balance_label.set_balance_text(btc_balance, quote_text)
         main_account_info = \
-            "Checking - %s BTC %s" % (btc_balance, quote_text)
+            "Checking - %s BTC" % btc_balance
         self.setWindowTitle("Electrum - %s" % main_account_info)
         self.accounts_selector.clear()
-        self.accounts_selector.addAction("%s" % main_account_info)
+        self.accounts_selector.addAction("%s %s" % (main_account_info,
+                                                    quote_text))
 
     def amount_input_changed(self, amount_text):
         try:
@@ -259,6 +288,10 @@ class MiniWindow(QDialog):
         else:
             self.valid_address.setChecked(False)
 
+    def copy_address(self):
+        receive_popup = ReceivePopup(self.receive_button)
+        self.actuator.copy_address(receive_popup)
+
     def update_completions(self, completions):
         self.address_completions.setStringList(completions)
 
@@ -350,10 +383,10 @@ class TextedLineEdit(QLineEdit):
 def ok_cancel_buttons(dialog):
     row_layout = QHBoxLayout()
     row_layout.addStretch(1)
-    ok_button = QPushButton("OK")
+    ok_button = QPushButton(_("OK"))
     row_layout.addWidget(ok_button)
     ok_button.clicked.connect(dialog.accept)
-    cancel_button = QPushButton("Cancel")
+    cancel_button = QPushButton(_("Cancel"))
     row_layout.addWidget(cancel_button)
     cancel_button.clicked.connect(dialog.reject)
     return row_layout
@@ -386,15 +419,60 @@ class PasswordDialog(QDialog):
             return
         return unicode(self.password_input.text())
 
+class ReceivePopup(QDialog):
+
+    def leaveEvent(self, event):
+        self.close()
+
+    def setup(self, address):
+        label = QLabel(_("Copied your Bitcoin address to the clipboard!"))
+        address_display = QLineEdit(address)
+        address_display.setReadOnly(True)
+        resize_line_edit_width(address_display, address)
+
+        main_layout = QVBoxLayout(self)
+        main_layout.addWidget(label)
+        main_layout.addWidget(address_display)
+
+        self.setMouseTracking(True)
+        self.setWindowTitle("Electrum - " + _("Receive Bitcoin payment"))
+        self.setWindowFlags(Qt.Window|Qt.FramelessWindowHint|Qt.MSWindowsFixedSizeDialogHint)
+        self.layout().setSizeConstraint(QLayout.SetFixedSize)
+        #self.setFrameStyle(QFrame.WinPanel|QFrame.Raised)
+        #self.setAlignment(Qt.AlignCenter)
+
+    def popup(self):
+        parent = self.parent()
+        top_left_pos = parent.mapToGlobal(parent.rect().bottomLeft())
+        self.move(top_left_pos)
+        center_mouse_pos = self.mapToGlobal(self.rect().center())
+        QCursor.setPos(center_mouse_pos)
+        self.show()
+
 class MiniActuator:
 
     def __init__(self, wallet):
         self.wallet = wallet
 
-    def copy_address(self):
+    def set_configured_currency(self, set_quote_currency):
+        currency = self.wallet.conversion_currency
+        # currency can be none when Electrum is used for the first
+        # time and no setting has been created yet.
+        if currency is not None:
+            set_quote_currency(currency)
+
+    def set_config_currency(self, conversion_currency):
+        self.wallet.conversion_currency = conversion_currency
+
+    def copy_address(self, receive_popup):
         addrs = [addr for addr in self.wallet.all_addresses()
                  if not self.wallet.is_change(addr)]
-        qApp.clipboard().setText(random.choice(addrs))
+        # Select most recent addresses from gap limit
+        addrs = addrs[-self.wallet.gap_limit:]
+        copied_address = random.choice(addrs)
+        qApp.clipboard().setText(copied_address)
+        receive_popup.setup(copied_address)
+        receive_popup.popup()
 
     def send(self, address, amount, parent_window):
         dest_address = self.fetch_destination(address)
@@ -480,6 +558,7 @@ class MiniDriver(QObject):
 
         self.initializing()
         self.connect(self, SIGNAL("updatesignal()"), self.update)
+        self.update_callback()
 
     # This is a hack to workaround that Qt does not like changing the
     # window properties from this other thread before the runloop has
@@ -542,7 +621,7 @@ class MiniDriver(QObject):
 
 if __name__ == "__main__":
     app = QApplication(sys.argv)
-    with open("data/style.css") as style_file:
+    with open(rsrc("style.css")) as style_file:
         app.setStyleSheet(style_file.read())
     mini = MiniWindow()
     sys.exit(app.exec_())