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 frozen_style = "QWidget { background-color:none; border:none;}"
30 normal_style = "QTextEdit { }"
32 class PayToEdit(QTextEdit):
34 def __init__(self, amount_edit):
35 QTextEdit.__init__(self)
36 self.amount_edit = amount_edit
37 self.document().contentsChanged.connect(self.update_size)
40 self.setMinimumHeight(27)
41 self.setMaximumHeight(27)
43 self.textChanged.connect(self.check_text)
47 def lock_amount(self):
48 self.amount_edit.setFrozen(True)
50 def unlock_amount(self):
51 self.amount_edit.setFrozen(False)
53 def setFrozen(self, b):
55 self.setStyleSheet(frozen_style if b else normal_style)
59 self.setStyleSheet("QWidget { background-color:#00ff00;}")
62 def parse_address_and_amount(self, line):
63 x, y = line.split(',')
64 address = self.parse_address(x)
65 amount = self.parse_amount(y)
66 return address, amount
69 def parse_amount(self, x):
70 p = pow(10, self.amount_edit.decimal_point())
71 return int( p * Decimal(x.strip()))
74 def parse_address(self, line):
76 m = re.match('^'+RE_ALIAS+'$', r)
77 address = m.group(2) if m else r
78 assert bitcoin.is_address(address)
86 # filter out empty lines
87 lines = filter( lambda x: x, self.lines())
91 self.payto_address = None
95 self.payto_address = self.parse_address(lines[0])
99 if self.payto_address:
105 to_address, amount = self.parse_address_and_amount(line)
109 outputs.append((to_address, amount))
112 self.outputs = outputs
113 self.payto_address = None
116 self.amount_edit.setAmount(total)
118 self.amount_edit.setText("")
120 if total or len(lines)>1:
126 def get_outputs(self):
127 if self.payto_address:
129 amount = self.amount_edit.get_amount()
133 self.outputs = [(self.payto_address, amount)]
135 return self.outputs[:]
139 return str(self.toPlainText()).split('\n')
142 def is_multiline(self):
143 return len(self.lines()) > 1
146 def update_size(self):
147 docHeight = self.document().size().height()
148 if self.heightMin <= docHeight <= self.heightMax:
149 self.setMinimumHeight(docHeight + 2)
150 self.setMaximumHeight(docHeight + 2)
153 def setCompleter(self, completer):
155 self.c.setWidget(self)
156 self.c.setCompletionMode(QCompleter.PopupCompletion)
157 self.c.activated.connect(self.insertCompletion)
160 def insertCompletion(self, completion):
161 if self.c.widget() != self:
163 tc = self.textCursor()
164 extra = completion.length() - self.c.completionPrefix().length()
165 tc.movePosition(QTextCursor.Left)
166 tc.movePosition(QTextCursor.EndOfWord)
167 tc.insertText(completion.right(extra))
168 self.setTextCursor(tc)
171 def textUnderCursor(self):
172 tc = self.textCursor()
173 tc.select(QTextCursor.WordUnderCursor)
174 return tc.selectedText()
177 def keyPressEvent(self, e):
178 if self.isReadOnly():
181 if self.c.popup().isVisible():
182 if e.key() in [Qt.Key_Enter, Qt.Key_Return]:
186 if e.key() in [Qt.Key_Tab]:
190 if e.key() in [Qt.Key_Down, Qt.Key_Up] and not self.is_multiline():
194 isShortcut = (e.modifiers() and Qt.ControlModifier) and e.key() == Qt.Key_E
196 if not self.c or not isShortcut:
197 QTextEdit.keyPressEvent(self, e)
200 ctrlOrShift = e.modifiers() and (Qt.ControlModifier or Qt.ShiftModifier)
201 if self.c is None or (ctrlOrShift and e.text().isEmpty()):
204 eow = QString("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-=")
205 hasModifier = (e.modifiers() != Qt.NoModifier) and not ctrlOrShift;
206 completionPrefix = self.textUnderCursor()
208 if not isShortcut and (hasModifier or e.text().isEmpty() or completionPrefix.length() < 1 or eow.contains(e.text().right(1)) ):
209 self.c.popup().hide()
212 if completionPrefix != self.c.completionPrefix():
213 self.c.setCompletionPrefix(completionPrefix);
214 self.c.popup().setCurrentIndex(self.c.completionModel().index(0, 0))
216 cr = self.cursorRect()
217 cr.setWidth(self.c.popup().sizeHintForColumn(0) + self.c.popup().verticalScrollBar().sizeHint().width())