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)
46 def lock_amount(self):
47 self.amount_edit.setFrozen(True)
49 def unlock_amount(self):
50 self.amount_edit.setFrozen(False)
52 def setFrozen(self, b):
54 self.setStyleSheet(frozen_style if b else normal_style)
57 self.setStyleSheet("QWidget { background-color:#00ff00;}")
59 def parse_address_and_amount(self, line):
60 x, y = line.split(',')
61 address = self.parse_address(x)
62 amount = self.parse_amount(y)
63 return address, amount
66 def parse_amount(self, x):
67 p = pow(10, self.amount_edit.decimal_point())
68 return int( p * Decimal(x.strip()))
71 def parse_address(self, line):
73 m = re.match('^'+RE_ALIAS+'$', r)
74 address = m.group(2) if m else r
75 assert bitcoin.is_address(address)
80 # filter out empty lines
81 lines = filter( lambda x: x, self.lines())
85 self.payto_address = None
89 self.payto_address = self.parse_address(lines[0])
93 if self.payto_address:
99 to_address, amount = self.parse_address_and_amount(line)
103 outputs.append((to_address, amount))
106 self.outputs = outputs
107 self.payto_address = None
110 self.amount_edit.setAmount(total)
112 self.amount_edit.setText("")
114 if total or len(lines)>1:
120 def get_outputs(self):
121 if self.payto_address:
123 amount = self.amount_edit.get_amount()
127 self.outputs = [(self.payto_address, amount)]
133 return str(self.toPlainText()).split('\n')
136 def is_multiline(self):
137 return len(self.lines()) > 1
140 def update_size(self):
141 docHeight = self.document().size().height()
142 if self.heightMin <= docHeight <= self.heightMax:
143 self.setMinimumHeight(docHeight + 2)
144 self.setMaximumHeight(docHeight + 2)
147 def setCompleter(self, completer):
149 self.c.setWidget(self)
150 self.c.setCompletionMode(QCompleter.PopupCompletion)
151 self.c.activated.connect(self.insertCompletion)
154 def insertCompletion(self, completion):
155 if self.c.widget() != self:
157 tc = self.textCursor()
158 extra = completion.length() - self.c.completionPrefix().length()
159 tc.movePosition(QTextCursor.Left)
160 tc.movePosition(QTextCursor.EndOfWord)
161 tc.insertText(completion.right(extra))
162 self.setTextCursor(tc)
165 def textUnderCursor(self):
166 tc = self.textCursor()
167 tc.select(QTextCursor.WordUnderCursor)
168 return tc.selectedText()
171 def keyPressEvent(self, e):
172 if self.isReadOnly():
175 if self.c.popup().isVisible():
176 if e.key() in [Qt.Key_Enter, Qt.Key_Return]:
180 if e.key() in [Qt.Key_Tab]:
184 if e.key() in [Qt.Key_Down, Qt.Key_Up] and not self.is_multiline():
188 isShortcut = (e.modifiers() and Qt.ControlModifier) and e.key() == Qt.Key_E
190 if not self.c or not isShortcut:
191 QTextEdit.keyPressEvent(self, e)
194 ctrlOrShift = e.modifiers() and (Qt.ControlModifier or Qt.ShiftModifier)
195 if self.c is None or (ctrlOrShift and e.text().isEmpty()):
198 eow = QString("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-=")
199 hasModifier = (e.modifiers() != Qt.NoModifier) and not ctrlOrShift;
200 completionPrefix = self.textUnderCursor()
202 if not isShortcut and (hasModifier or e.text().isEmpty() or completionPrefix.length() < 1 or eow.contains(e.text().right(1)) ):
203 self.c.popup().hide()
206 if completionPrefix != self.c.completionPrefix():
207 self.c.setCompletionPrefix(completionPrefix);
208 self.c.popup().setCurrentIndex(self.c.completionModel().index(0, 0))
210 cr = self.cursorRect()
211 cr.setWidth(self.c.popup().sizeHintForColumn(0) + self.c.popup().verticalScrollBar().sizeHint().width())