-WIP-electrum-btcp/plugins/virtualkeyboard/qt.py

62 lines
1.7 KiB
Python
Raw Normal View History

2017-09-22 20:54:38 -07:00
from PyQt5.QtGui import *
from PyQt5.QtWidgets import (QVBoxLayout, QGridLayout, QPushButton)
2014-08-31 02:42:40 -07:00
from electrum.plugins import BasePlugin, hook
2013-09-11 02:45:58 -07:00
from electrum.i18n import _
2015-02-28 03:08:23 -08:00
import random
2013-03-15 10:35:05 -07:00
2017-02-05 02:38:44 -08:00
class Plugin(BasePlugin):
vkb = None
vkb_index = 0
2013-03-15 10:35:05 -07:00
2014-08-31 02:42:40 -07:00
@hook
2013-03-15 10:35:05 -07:00
def password_dialog(self, pw, grid, pos):
vkb_button = QPushButton(_("+"))
vkb_button.setFixedWidth(20)
vkb_button.clicked.connect(lambda: self.toggle_vkb(grid, pw))
grid.addWidget(vkb_button, pos, 2)
self.kb_pos = 2
2015-02-28 03:08:23 -08:00
self.vkb = None
2013-03-15 10:35:05 -07:00
def toggle_vkb(self, grid, pw):
2015-02-28 03:08:23 -08:00
if self.vkb:
grid.removeItem(self.vkb)
2013-03-15 10:35:05 -07:00
self.vkb = self.virtual_keyboard(self.vkb_index, pw)
grid.addLayout(self.vkb, self.kb_pos, 0, 1, 3)
self.vkb_index += 1
def virtual_keyboard(self, i, pw):
2017-02-05 02:38:44 -08:00
i = i % 3
2013-03-15 10:35:05 -07:00
if i == 0:
chars = 'abcdefghijklmnopqrstuvwxyz '
elif i == 1:
chars = 'ABCDEFGHIJKLMNOPQRTSUVWXYZ '
elif i == 2:
chars = '1234567890!?.,;:/%&()[]{}+-'
2013-03-15 10:35:05 -07:00
n = len(chars)
s = []
2017-02-05 02:38:44 -08:00
for i in range(n):
2013-03-15 10:35:05 -07:00
while True:
2017-02-05 02:38:44 -08:00
k = random.randint(0, n - 1)
2013-03-15 10:35:05 -07:00
if k not in s:
s.append(k)
break
def add_target(t):
2015-02-28 03:08:23 -08:00
return lambda: pw.setText(str(pw.text()) + t)
2013-03-15 10:35:05 -07:00
vbox = QVBoxLayout()
grid = QGridLayout()
grid.setSpacing(2)
for i in range(n):
l_button = QPushButton(chars[s[i]])
l_button.setFixedWidth(25)
l_button.setFixedHeight(25)
2015-02-28 03:08:23 -08:00
l_button.clicked.connect(add_target(chars[s[i]]))
2017-02-05 02:38:44 -08:00
grid.addWidget(l_button, i // 6, i % 6)
2013-03-15 10:35:05 -07:00
vbox.addLayout(grid)
return vbox