paytoedit
[electrum-nvc.git] / gui / qt / paytoedit.py
1 #!/usr/bin/env python
2 #
3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2012 thomasv@gitorious
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 from PyQt4.QtCore import *
20 from PyQt4.QtGui import *
21
22
23 class PayToEdit(QTextEdit):
24
25     def __init__(self, *args, **kwargs):
26         QTextEdit.__init__(self, *args, **kwargs)
27         self.document().contentsChanged.connect(self.update_size)
28         self.heightMin = 0
29         self.heightMax = 150
30         self.setMinimumHeight(27)
31         self.setMaximumHeight(27)
32         #self.setStyleSheet("QTextEdit { border-style:solid; border-width: 1px;}")
33         self.c = None
34
35
36     def update_size(self):
37         docHeight = self.document().size().height()
38         if self.heightMin <= docHeight <= self.heightMax:
39             self.setMinimumHeight(docHeight + 2)
40             self.setMaximumHeight(docHeight + 2)
41
42
43     def setCompleter(self, completer):
44         self.c = completer
45         self.c.setWidget(self)
46         self.c.setCompletionMode(QCompleter.PopupCompletion)
47         self.c.activated.connect(self.insertCompletion)
48
49
50     def insertCompletion(self, completion):
51         if self.c.widget() != self:
52             return
53         tc = self.textCursor()
54         extra = completion.length() - self.c.completionPrefix().length()
55         tc.movePosition(QTextCursor.Left)
56         tc.movePosition(QTextCursor.EndOfWord)
57         tc.insertText(completion.right(extra))
58         self.setTextCursor(tc)
59  
60
61     def textUnderCursor(self):
62         tc = self.textCursor()
63         tc.select(QTextCursor.WordUnderCursor)
64         return tc.selectedText()
65
66
67     def keyPressEvent(self, e):
68         if self.c.popup().isVisible():
69             if e.key() in [Qt.Key_Enter, Qt.Key_Return]:
70                 e.ignore()
71                 return
72
73         isShortcut = (e.modifiers() and Qt.ControlModifier) and e.key() == Qt.Key_E
74
75         if not self.c or not isShortcut:
76             QTextEdit.keyPressEvent(self, e)
77
78         ctrlOrShift = e.modifiers() and (Qt.ControlModifier or Qt.ShiftModifier)
79         if self.c is None or (ctrlOrShift and e.text().isEmpty()):
80             return
81
82         eow = QString("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-=")
83         hasModifier = (e.modifiers() != Qt.NoModifier) and not ctrlOrShift;
84         completionPrefix = self.textUnderCursor()
85
86         if not isShortcut and (hasModifier or e.text().isEmpty() or completionPrefix.length() < 1 or eow.contains(e.text().right(1)) ):
87             self.c.popup().hide()
88             return
89
90         if completionPrefix != self.c.completionPrefix():
91             self.c.setCompletionPrefix(completionPrefix);
92             self.c.popup().setCurrentIndex(self.c.completionModel().index(0, 0))
93
94         cr = self.cursorRect()
95         cr.setWidth(self.c.popup().sizeHintForColumn(0) + self.c.popup().verticalScrollBar().sizeHint().width())
96         self.c.complete(cr)
97
98