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 *
21 from qrtextedit import QRTextEdit
24 from decimal import Decimal
25 from electrum import bitcoin
27 RE_ADDRESS = '[1-9A-HJ-NP-Za-km-z]{26,}'
28 RE_ALIAS = '(.*?)\s*\<([1-9A-HJ-NP-Za-km-z]{26,})\>'
30 frozen_style = "QWidget { background-color:none; border:none;}"
31 normal_style = "QTextEdit { }"
33 class PayToEdit(QRTextEdit):
35 def __init__(self, win):
36 QRTextEdit.__init__(self)
38 self.amount_edit = win.amount_e
39 self.document().contentsChanged.connect(self.update_size)
42 self.setMinimumHeight(27)
43 self.setMaximumHeight(27)
45 self.textChanged.connect(self.check_text)
48 self.scan_f = self.win.pay_from_URI
50 def lock_amount(self):
51 self.amount_edit.setFrozen(True)
53 def unlock_amount(self):
54 self.amount_edit.setFrozen(False)
56 def setFrozen(self, b):
58 self.setStyleSheet(frozen_style if b else normal_style)
62 self.setStyleSheet("QWidget { background-color:#80ff80;}")
66 self.setStyleSheet("QWidget { background-color:#ffcccc;}")
68 def parse_address_and_amount(self, line):
69 x, y = line.split(',')
70 address = self.parse_address(x)
71 amount = self.parse_amount(y)
72 return address, amount
75 def parse_amount(self, x):
76 p = pow(10, self.amount_edit.decimal_point())
77 return int( p * Decimal(x.strip()))
80 def parse_address(self, line):
82 m = re.match('^'+RE_ALIAS+'$', r)
83 address = m.group(2) if m else r
84 assert bitcoin.is_address(address)
92 # filter out empty lines
93 lines = filter( lambda x: x, self.lines())
97 self.payto_address = None
101 self.payto_address = self.parse_address(lines[0])
105 if self.payto_address:
111 to_address, amount = self.parse_address_and_amount(line)
115 outputs.append((to_address, amount))
118 self.outputs = outputs
119 self.payto_address = None
122 self.amount_edit.setAmount(total)
124 self.amount_edit.setText("")
126 if total or len(lines)>1:
132 def get_outputs(self):
133 if self.payto_address:
135 amount = self.amount_edit.get_amount()
139 self.outputs = [(self.payto_address, amount)]
141 return self.outputs[:]
145 return str(self.toPlainText()).split('\n')
148 def is_multiline(self):
149 return len(self.lines()) > 1
152 def update_size(self):
153 docHeight = self.document().size().height()
154 if self.heightMin <= docHeight <= self.heightMax:
155 self.setMinimumHeight(docHeight + 2)
156 self.setMaximumHeight(docHeight + 2)
159 def setCompleter(self, completer):
161 self.c.setWidget(self)
162 self.c.setCompletionMode(QCompleter.PopupCompletion)
163 self.c.activated.connect(self.insertCompletion)
166 def insertCompletion(self, completion):
167 if self.c.widget() != self:
169 tc = self.textCursor()
170 extra = completion.length() - self.c.completionPrefix().length()
171 tc.movePosition(QTextCursor.Left)
172 tc.movePosition(QTextCursor.EndOfWord)
173 tc.insertText(completion.right(extra))
174 self.setTextCursor(tc)
177 def textUnderCursor(self):
178 tc = self.textCursor()
179 tc.select(QTextCursor.WordUnderCursor)
180 return tc.selectedText()
183 def keyPressEvent(self, e):
184 if self.isReadOnly():
187 if self.c.popup().isVisible():
188 if e.key() in [Qt.Key_Enter, Qt.Key_Return]:
192 if e.key() in [Qt.Key_Tab]:
196 if e.key() in [Qt.Key_Down, Qt.Key_Up] and not self.is_multiline():
200 isShortcut = (e.modifiers() and Qt.ControlModifier) and e.key() == Qt.Key_E
202 if not self.c or not isShortcut:
203 QTextEdit.keyPressEvent(self, e)
206 ctrlOrShift = e.modifiers() and (Qt.ControlModifier or Qt.ShiftModifier)
207 if self.c is None or (ctrlOrShift and e.text().isEmpty()):
210 eow = QString("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-=")
211 hasModifier = (e.modifiers() != Qt.NoModifier) and not ctrlOrShift;
212 completionPrefix = self.textUnderCursor()
214 if not isShortcut and (hasModifier or e.text().isEmpty() or completionPrefix.length() < 1 or eow.contains(e.text().right(1)) ):
215 self.c.popup().hide()
218 if completionPrefix != self.c.completionPrefix():
219 self.c.setCompletionPrefix(completionPrefix);
220 self.c.popup().setCurrentIndex(self.c.completionModel().index(0, 0))
222 cr = self.cursorRect()
223 cr.setWidth(self.c.popup().sizeHintForColumn(0) + self.c.popup().verticalScrollBar().sizeHint().width())