option to display mBTC
[electrum-nvc.git] / gui / amountedit.py
1 # -*- coding: utf-8 -*-
2
3 from PyQt4.QtCore import *
4 from PyQt4.QtGui import *
5
6
7 class AmountEdit(QLineEdit):
8
9     def __init__(self, text_getter, is_int = False, parent=None):
10         QLineEdit.__init__(self, parent)
11         self.text_getter = text_getter
12         self.textChanged.connect(self.numbify)
13         self.is_int = is_int
14
15
16     def paintEvent(self, event):
17         QLineEdit.paintEvent(self, event)
18         if self.text_getter:
19              panel = QStyleOptionFrameV2()
20              self.initStyleOption(panel)
21              textRect = self.style().subElementRect(QStyle.SE_LineEditContents, panel, self)
22              textRect.adjust(2, 0, -10, 0)
23              painter = QPainter(self)
24              painter.setPen(self.palette().brush(QPalette.Disabled, QPalette.Text).color())
25              painter.drawText(textRect, Qt.AlignRight | Qt.AlignVCenter, self.text_getter())
26
27
28     def numbify(self):
29         text = unicode(self.text()).strip()
30         pos = self.cursorPosition()
31         chars = '0123456789'
32         if not self.is_int: chars +='.'
33         s = ''.join([i for i in text if i in chars])
34         if not self.is_int:
35             if '.' in s:
36                 p = s.find('.')
37                 s = s.replace('.','')
38                 s = s[:p] + '.' + s[p:p+8]
39         self.setText(s)
40         self.setCursorPosition(pos)