install wizard: use a single window
[electrum-nvc.git] / gui / gui_classic / qt_util.py
1 from electrum.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, w=None):
27
28     s = Timer()
29     s.start()
30     if not w:
31         w = QDialog()
32         w.resize(200, 70)
33         w.setWindowTitle('Electrum')
34     else:
35         if w.layout(): QWidget().setLayout(w.layout())
36
37     l = QLabel('')
38     vbox = QVBoxLayout(w)
39     vbox.addWidget(l)
40     w.show()
41     def ff():
42         s = f()
43         if s: l.setText(s)
44         else: w.accept()
45     w.connect(s, SIGNAL('timersignal'), ff)
46     w.exec_()
47     #w.destroy()
48
49
50 class HelpButton(QPushButton):
51     def __init__(self, text):
52         QPushButton.__init__(self, '?')
53         self.setFocusPolicy(Qt.NoFocus)
54         self.setFixedWidth(20)
55         self.clicked.connect(lambda: QMessageBox.information(self, 'Help', text, 'OK') )
56
57
58
59 def backup_wallet(path):
60     import shutil
61     directory, fileName = os.path.split(path)
62     try:
63         otherpath = unicode( QFileDialog.getOpenFileName(QWidget(), _('Enter a filename for the copy of your wallet'), directory) )
64         if otherpath and path!=otherpath:
65             shutil.copy2(path, otherpath)
66             QMessageBox.information(None,"Wallet backup created", _("A copy of your wallet file was created in")+" '%s'" % str(otherpath))
67     except (IOError, os.error), reason:
68         QMessageBox.critical(None,"Unable to create backup", _("Electrum was unable to copy your wallet file to the specified location.")+"\n" + str(reason))
69
70 def ok_cancel_buttons(dialog, ok_label=_("OK") ):
71     hbox = QHBoxLayout()
72     hbox.addStretch(1)
73     b = QPushButton(_("Cancel"))
74     hbox.addWidget(b)
75     b.clicked.connect(dialog.reject)
76     b = QPushButton(ok_label)
77     hbox.addWidget(b)
78     b.clicked.connect(dialog.accept)
79     b.setDefault(True)
80     return hbox
81
82 def text_dialog(parent, title, label, ok_label):
83     dialog = QDialog(parent)
84     dialog.setMinimumWidth(500)
85     dialog.setWindowTitle(title)
86     dialog.setModal(1)
87     l = QVBoxLayout()
88     dialog.setLayout(l)
89     l.addWidget(QLabel(label))
90     txt = QTextEdit()
91     l.addWidget(txt)
92     l.addLayout(ok_cancel_buttons(dialog, ok_label))
93     if dialog.exec_():
94         return unicode(txt.toPlainText())
95