3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2012 thomasv@gitorious
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.
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.
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/>.
19 from PyQt4.QtCore import *
20 from PyQt4.QtGui import *
23 class PayToEdit(QTextEdit):
25 def __init__(self, *args, **kwargs):
26 QTextEdit.__init__(self, *args, **kwargs)
27 self.document().contentsChanged.connect(self.update_size)
30 self.setMinimumHeight(27)
31 self.setMaximumHeight(27)
32 #self.setStyleSheet("QTextEdit { border-style:solid; border-width: 1px;}")
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)
43 def setCompleter(self, completer):
45 self.c.setWidget(self)
46 self.c.setCompletionMode(QCompleter.PopupCompletion)
47 self.c.activated.connect(self.insertCompletion)
50 def insertCompletion(self, completion):
51 if self.c.widget() != self:
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)
61 def textUnderCursor(self):
62 tc = self.textCursor()
63 tc.select(QTextCursor.WordUnderCursor)
64 return tc.selectedText()
67 def keyPressEvent(self, e):
68 if self.c.popup().isVisible():
69 if e.key() in [Qt.Key_Enter, Qt.Key_Return]:
73 isShortcut = (e.modifiers() and Qt.ControlModifier) and e.key() == Qt.Key_E
75 if not self.c or not isShortcut:
76 QTextEdit.keyPressEvent(self, e)
78 ctrlOrShift = e.modifiers() and (Qt.ControlModifier or Qt.ShiftModifier)
79 if self.c is None or (ctrlOrShift and e.text().isEmpty()):
82 eow = QString("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-=")
83 hasModifier = (e.modifiers() != Qt.NoModifier) and not ctrlOrShift;
84 completionPrefix = self.textUnderCursor()
86 if not isShortcut and (hasModifier or e.text().isEmpty() or completionPrefix.length() < 1 or eow.contains(e.text().right(1)) ):
90 if completionPrefix != self.c.completionPrefix():
91 self.c.setCompletionPrefix(completionPrefix);
92 self.c.popup().setCurrentIndex(self.c.completionModel().index(0, 0))
94 cr = self.cursorRect()
95 cr.setWidth(self.c.popup().sizeHintForColumn(0) + self.c.popup().verticalScrollBar().sizeHint().width())