2013-10-07 20:00:17 +00:00
|
|
|
import sys
|
|
|
|
from PyQt4.Qt import QApplication, QWidget, QGridLayout, QVBoxLayout
|
2013-10-07 21:27:02 +00:00
|
|
|
from PyQt4.QtGui import QPushButton, QLineEdit, QSizePolicy, QRegExpValidator
|
2013-10-07 22:18:44 +00:00
|
|
|
from PyQt4.QtCore import QObject, SIGNAL, QRegExp, Qt
|
2013-10-07 20:00:17 +00:00
|
|
|
|
|
|
|
class PinButton(QPushButton):
|
|
|
|
def __init__(self, password, encoded_value):
|
|
|
|
super(PinButton, self).__init__()
|
|
|
|
self.password = password
|
|
|
|
self.encoded_value = encoded_value
|
|
|
|
|
|
|
|
QObject.connect(self, SIGNAL('clicked()'), self._pressed)
|
|
|
|
|
|
|
|
def _pressed(self):
|
|
|
|
self.password.setText(self.password.text() + str(self.encoded_value))
|
|
|
|
self.password.setFocus()
|
|
|
|
|
|
|
|
class PinMatrixWidget(QWidget):
|
|
|
|
'''
|
|
|
|
Displays widget with nine blank buttons and password box.
|
|
|
|
Encodes button clicks into sequence of numbers for passing
|
|
|
|
into PinAck messages of Trezor.
|
|
|
|
'''
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
super(PinMatrixWidget, self).__init__(parent)
|
|
|
|
|
|
|
|
self.password = QLineEdit()
|
2013-10-07 21:27:02 +00:00
|
|
|
self.password.setValidator(QRegExpValidator(QRegExp('[1-9]+'), None))
|
2013-10-07 20:00:17 +00:00
|
|
|
self.password.setEchoMode(QLineEdit.Password)
|
|
|
|
|
|
|
|
grid = QGridLayout()
|
|
|
|
grid.setSpacing(0)
|
|
|
|
for x in range(9):
|
|
|
|
button = PinButton(self.password, x + 1)
|
|
|
|
button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
|
2013-10-07 22:18:44 +00:00
|
|
|
button.setFocusPolicy(Qt.NoFocus)
|
2013-10-07 20:00:17 +00:00
|
|
|
grid.addWidget(button, x / 3, x % 3)
|
|
|
|
|
|
|
|
vbox = QVBoxLayout()
|
|
|
|
vbox.addLayout(grid)
|
|
|
|
vbox.addWidget(self.password)
|
|
|
|
self.setLayout(vbox)
|
|
|
|
|
|
|
|
def get_value(self):
|
|
|
|
return self.password.text()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
'''
|
|
|
|
Demo application showing PinMatrix widget in action
|
|
|
|
'''
|
|
|
|
a = QApplication(sys.argv)
|
|
|
|
|
|
|
|
matrix = PinMatrixWidget()
|
|
|
|
|
|
|
|
def clicked():
|
|
|
|
print "PinMatrix value is", matrix.get_value()
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
ok = QPushButton('OK')
|
|
|
|
QObject.connect(ok, SIGNAL('clicked()'), clicked)
|
|
|
|
|
|
|
|
vbox = QVBoxLayout()
|
|
|
|
vbox.addWidget(matrix)
|
|
|
|
vbox.addWidget(ok)
|
|
|
|
|
|
|
|
w = QWidget()
|
|
|
|
w.setLayout(vbox)
|
2013-10-07 23:43:16 +00:00
|
|
|
w.move(100, 100)
|
2013-10-07 20:00:17 +00:00
|
|
|
w.show()
|
|
|
|
|
|
|
|
a.exec_()
|