275cfa55ff28d1e22914786c9d62c03c975e1a7d
[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
27
28
29 class HelpButton(QPushButton):
30     def __init__(self, text):
31         QPushButton.__init__(self, '?')
32         self.setFocusPolicy(Qt.NoFocus)
33         self.setFixedWidth(20)
34         self.clicked.connect(lambda: QMessageBox.information(self, 'Help', text, 'OK') )
35
36
37
38
39 def close_button(dialog, label=_("Close") ):
40     hbox = QHBoxLayout()
41     hbox.addStretch(1)
42     b = QPushButton(label)
43     hbox.addWidget(b)
44     b.clicked.connect(dialog.close)
45     b.setDefault(True)
46     return hbox
47
48 def ok_cancel_buttons2(dialog, ok_label=_("OK") ):
49     hbox = QHBoxLayout()
50     hbox.addStretch(1)
51     b = QPushButton(_("Cancel"))
52     hbox.addWidget(b)
53     b.clicked.connect(dialog.reject)
54     b = QPushButton(ok_label)
55     hbox.addWidget(b)
56     b.clicked.connect(dialog.accept)
57     b.setDefault(True)
58     return hbox, b
59
60 def ok_cancel_buttons(dialog, ok_label=_("OK") ):
61     hbox, b = ok_cancel_buttons2(dialog, ok_label)
62     return hbox
63
64 def text_dialog(parent, title, label, ok_label, default=None):
65     dialog = QDialog(parent)
66     dialog.setMinimumWidth(500)
67     dialog.setWindowTitle(title)
68     dialog.setModal(1)
69     l = QVBoxLayout()
70     dialog.setLayout(l)
71     l.addWidget(QLabel(label))
72     txt = QTextEdit()
73     if default:
74         txt.setText(default)
75     l.addWidget(txt)
76     l.addLayout(ok_cancel_buttons(dialog, ok_label))
77     if dialog.exec_():
78         return unicode(txt.toPlainText())
79
80
81
82
83 def address_field(addresses):
84     hbox = QHBoxLayout()
85     address_e = QLineEdit()
86     if addresses:
87         address_e.setText(addresses[0])
88     def func():
89         i = addresses.index(str(address_e.text())) + 1
90         i = i % len(addresses)
91         address_e.setText(addresses[i])
92     button = QPushButton(_('Address'))
93     button.clicked.connect(func)
94     hbox.addWidget(button)
95     hbox.addWidget(address_e)
96     return hbox, address_e
97
98     
99
100
101
102 class MyTreeWidget(QTreeWidget):
103     def __init__(self, parent):
104         QTreeWidget.__init__(self, parent)
105         self.setContextMenuPolicy(Qt.CustomContextMenu)
106         self.connect(self, SIGNAL('itemActivated(QTreeWidgetItem*, int)'), self.itemactivated)
107
108     def itemactivated(self, item):
109         if not item: return
110         for i in range(0,self.viewport().height()/5):
111             if self.itemAt(QPoint(0,i*5)) == item:
112                 break
113         else:
114             return
115         for j in range(0,30):
116             if self.itemAt(QPoint(0,i*5 + j)) != item:
117                 break
118         self.emit(SIGNAL('customContextMenuRequested(const QPoint&)'), QPoint(50, i*5 + j - 1))
119