separate directories for GUIs
[electrum-nvc.git] / gui / gui_classic / qt_util.py
1 from i18n import _
2 from PyQt4.QtGui import *
3 from PyQt4.QtCore import *
4 import os.path
5 import time
6
7
8 class Timer(QThread):
9     def run(self):
10         while True:
11             self.emit(SIGNAL('timersignal'))
12             time.sleep(0.5)
13
14
15 class EnterButton(QPushButton):
16     def __init__(self, text, func):
17         QPushButton.__init__(self, text)
18         self.func = func
19         self.clicked.connect(func)
20
21     def keyPressEvent(self, e):
22         if e.key() == Qt.Key_Return:
23             apply(self.func,())
24
25
26 def waiting_dialog(f):
27
28     s = Timer()
29     s.start()
30     w = QDialog()
31     w.resize(200, 70)
32     w.setWindowTitle('Electrum')
33     l = QLabel('')
34     vbox = QVBoxLayout()
35     vbox.addWidget(l)
36     w.setLayout(vbox)
37     w.show()
38     def ff():
39         s = f()
40         if s: l.setText(s)
41         else: w.close()
42     w.connect(s, SIGNAL('timersignal'), ff)
43     w.exec_()
44     w.destroy()
45
46
47 class HelpButton(QPushButton):
48     def __init__(self, text):
49         QPushButton.__init__(self, '?')
50         self.setFocusPolicy(Qt.NoFocus)
51         self.setFixedWidth(20)
52         self.clicked.connect(lambda: QMessageBox.information(self, 'Help', text, 'OK') )
53
54
55
56 def backup_wallet(path):
57     import shutil
58     directory, fileName = os.path.split(path)
59     try:
60         otherpath = unicode( QFileDialog.getOpenFileName(QWidget(), _('Enter a filename for the copy of your wallet'), directory) )
61         if otherpath and path!=otherpath:
62             shutil.copy2(path, otherpath)
63             QMessageBox.information(None,"Wallet backup created", _("A copy of your wallet file was created in")+" '%s'" % str(otherpath))
64     except (IOError, os.error), reason:
65         QMessageBox.critical(None,"Unable to create backup", _("Electrum was unable to copy your wallet file to the specified location.")+"\n" + str(reason))
66
67 def ok_cancel_buttons(dialog, ok_label=_("OK") ):
68     hbox = QHBoxLayout()
69     hbox.addStretch(1)
70     b = QPushButton(_("Cancel"))
71     hbox.addWidget(b)
72     b.clicked.connect(dialog.reject)
73     b = QPushButton(ok_label)
74     hbox.addWidget(b)
75     b.clicked.connect(dialog.accept)
76     b.setDefault(True)
77     return hbox
78
79 def text_dialog(parent, title, label, ok_label):
80     dialog = QDialog(parent)
81     dialog.setMinimumWidth(500)
82     dialog.setWindowTitle(title)
83     dialog.setModal(1)
84     l = QVBoxLayout()
85     dialog.setLayout(l)
86     l.addWidget(QLabel(label))
87     txt = QTextEdit()
88     l.addWidget(txt)
89     l.addLayout(ok_cancel_buttons(dialog, ok_label))
90     if dialog.exec_():
91         return unicode(txt.toPlainText())
92