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 from decimal import Decimal
24 from electrum import bitcoin
26 RE_ADDRESS = '[1-9A-HJ-NP-Za-km-z]{26,}'
27 RE_ALIAS = '(.*?)\s*\<([1-9A-HJ-NP-Za-km-z]{26,})\>'
29 class PayToEdit(QTextEdit):
31 def __init__(self, amount_edit):
32 QTextEdit.__init__(self)
33 self.amount_edit = amount_edit
34 self.document().contentsChanged.connect(self.update_size)
37 self.setMinimumHeight(27)
38 self.setMaximumHeight(27)
39 #self.setStyleSheet("QTextEdit { border-style:solid; border-width: 1px;}")
43 def lock_amount(self):
48 def unlock_amount(self):
54 def parse_line(self, line):
55 recipient, amount = line.split(',')
56 amount = Decimal(amount.strip())
57 recipient = recipient.strip()
58 m = re.match(RE_ALIAS, recipient)
59 to_address = m.group(2) if m else recipient
60 assert bitcoin.is_address(to_address)
61 return to_address, amount
65 # filter out empty lines
66 lines = filter( lambda x: x, self.lines())
72 to_address, amount = self.parse_line(line)
75 outputs.append((to_address, amount))
78 self.outputs = outputs
80 self.amount_edit.setText(str(total) if total else "")
81 if total or len(lines)>1:
87 return str(self.toPlainText()).split('\n')
90 def is_multiline(self):
91 return len(self.lines()) > 1
94 def update_size(self):
95 docHeight = self.document().size().height()
96 if self.heightMin <= docHeight <= self.heightMax:
97 self.setMinimumHeight(docHeight + 2)
98 self.setMaximumHeight(docHeight + 2)
101 def setCompleter(self, completer):
103 self.c.setWidget(self)
104 self.c.setCompletionMode(QCompleter.PopupCompletion)
105 self.c.activated.connect(self.insertCompletion)
108 def insertCompletion(self, completion):
109 if self.c.widget() != self:
111 tc = self.textCursor()
112 extra = completion.length() - self.c.completionPrefix().length()
113 tc.movePosition(QTextCursor.Left)
114 tc.movePosition(QTextCursor.EndOfWord)
115 tc.insertText(completion.right(extra))
116 self.setTextCursor(tc)
120 def textUnderCursor(self):
121 tc = self.textCursor()
122 tc.select(QTextCursor.WordUnderCursor)
123 return tc.selectedText()
126 def keyPressEvent(self, e):
127 if self.c.popup().isVisible():
128 if e.key() in [Qt.Key_Enter, Qt.Key_Return]:
132 if e.key() in [Qt.Key_Tab]:
136 if e.key() in [Qt.Key_Down, Qt.Key_Up] and not self.is_multiline():
140 isShortcut = (e.modifiers() and Qt.ControlModifier) and e.key() == Qt.Key_E
142 if not self.c or not isShortcut:
143 QTextEdit.keyPressEvent(self, e)
147 ctrlOrShift = e.modifiers() and (Qt.ControlModifier or Qt.ShiftModifier)
148 if self.c is None or (ctrlOrShift and e.text().isEmpty()):
151 eow = QString("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-=")
152 hasModifier = (e.modifiers() != Qt.NoModifier) and not ctrlOrShift;
153 completionPrefix = self.textUnderCursor()
155 if not isShortcut and (hasModifier or e.text().isEmpty() or completionPrefix.length() < 1 or eow.contains(e.text().right(1)) ):
156 self.c.popup().hide()
159 if completionPrefix != self.c.completionPrefix():
160 self.c.setCompletionPrefix(completionPrefix);
161 self.c.popup().setCurrentIndex(self.c.completionModel().index(0, 0))
163 cr = self.cursorRect()
164 cr.setWidth(self.c.popup().sizeHintForColumn(0) + self.c.popup().verticalScrollBar().sizeHint().width())