generalize plugins to all guis
[electrum-nvc.git] / gui / 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
60 def close_button(dialog, label=_("Close") ):
61     hbox = QHBoxLayout()
62     hbox.addStretch(1)
63     b = QPushButton(label)
64     hbox.addWidget(b)
65     b.clicked.connect(dialog.close)
66     b.setDefault(True)
67     return hbox
68
69 def ok_cancel_buttons(dialog, ok_label=_("OK") ):
70     hbox = QHBoxLayout()
71     hbox.addStretch(1)
72     b = QPushButton(_("Cancel"))
73     hbox.addWidget(b)
74     b.clicked.connect(dialog.reject)
75     b = QPushButton(ok_label)
76     hbox.addWidget(b)
77     b.clicked.connect(dialog.accept)
78     b.setDefault(True)
79     return hbox
80
81 def text_dialog(parent, title, label, ok_label):
82     dialog = QDialog(parent)
83     dialog.setMinimumWidth(500)
84     dialog.setWindowTitle(title)
85     dialog.setModal(1)
86     l = QVBoxLayout()
87     dialog.setLayout(l)
88     l.addWidget(QLabel(label))
89     txt = QTextEdit()
90     l.addWidget(txt)
91     l.addLayout(ok_cancel_buttons(dialog, ok_label))
92     if dialog.exec_():
93         return unicode(txt.toPlainText())
94
95
96 class MyTreeWidget(QTreeWidget):
97     def __init__(self, parent):
98         QTreeWidget.__init__(self, parent)
99         self.setContextMenuPolicy(Qt.CustomContextMenu)
100         self.connect(self, SIGNAL('itemActivated(QTreeWidgetItem*, int)'), self.itemactivated)
101
102     def itemactivated(self, item):
103         if not item: return
104         for i in range(0,self.viewport().height()/5):
105             if self.itemAt(QPoint(0,i*5)) == item:
106                 break
107         else:
108             return
109         for j in range(0,30):
110             if self.itemAt(QPoint(0,i*5 + j)) != item:
111                 break
112         self.emit(SIGNAL('customContextMenuRequested(const QPoint&)'), QPoint(50, i*5 + j - 1))
113