f150e29f62826505a2964bbc17bb1f47edef70e9
[electrum-nvc.git] / plugins / virtualkeyboard.py
1 from PyQt4.QtGui import *
2 from electrum_gui import BasePlugin
3 from electrum_gui.i18n import _
4
5 class Plugin(BasePlugin):
6
7
8     def __init__(self, gui):
9         BasePlugin.__init__(self, gui, 'virtualkeyboard', 'Virtual Keyboard',
10                             _("Add an optional, mouse keyboard to the password dialog.\nWarning: do not use this if it makes you pick a weaker password."))
11         self.vkb = None
12         self.vkb_index = 0
13
14
15     def password_dialog(self, pw, grid, pos):
16         vkb_button = QPushButton(_("+"))
17         vkb_button.setFixedWidth(20)
18         vkb_button.clicked.connect(lambda: self.toggle_vkb(grid, pw))
19         grid.addWidget(vkb_button, pos, 2)
20         self.kb_pos = 2
21
22
23     def toggle_vkb(self, grid, pw):
24         if self.vkb: grid.removeItem(self.vkb)
25         self.vkb = self.virtual_keyboard(self.vkb_index, pw)
26         grid.addLayout(self.vkb, self.kb_pos, 0, 1, 3)
27         self.vkb_index += 1
28
29
30     def virtual_keyboard(self, i, pw):
31         import random
32         i = i%3
33         if i == 0:
34             chars = 'abcdefghijklmnopqrstuvwxyz '
35         elif i == 1:
36             chars = 'ABCDEFGHIJKLMNOPQRTSUVWXYZ '
37         elif i == 2:
38             chars = '1234567890!?.,;:/%&()[]{}+-'
39             
40         n = len(chars)
41         s = []
42         for i in xrange(n):
43             while True:
44                 k = random.randint(0,n-1)
45                 if k not in s:
46                     s.append(k)
47                     break
48
49         def add_target(t):
50             return lambda: pw.setText(str( pw.text() ) + t)
51             
52         vbox = QVBoxLayout()
53         grid = QGridLayout()
54         grid.setSpacing(2)
55         for i in range(n):
56             l_button = QPushButton(chars[s[i]])
57             l_button.setFixedWidth(25)
58             l_button.setFixedHeight(25)
59             l_button.clicked.connect(add_target(chars[s[i]]) )
60             grid.addWidget(l_button, i/6, i%6)
61
62         vbox.addLayout(grid)
63
64         return vbox
65