amount_dialog.py (5256B)
1 from kivy.app import App 2 from kivy.factory import Factory 3 from kivy.properties import ObjectProperty 4 from kivy.lang import Builder 5 from decimal import Decimal 6 7 Builder.load_string(''' 8 9 <AmountDialog@Popup> 10 id: popup 11 title: _('Amount') 12 AnchorLayout: 13 anchor_x: 'center' 14 BoxLayout: 15 orientation: 'vertical' 16 size_hint: 0.9, 1 17 Widget: 18 size_hint: 1, 0.2 19 BoxLayout: 20 size_hint: 1, None 21 height: '80dp' 22 Button: 23 background_color: 0, 0, 0, 0 24 id: btc 25 text: kb.amount + ' ' + app.base_unit 26 color: (0.7, 0.7, 1, 1) if kb.is_fiat else (1, 1, 1, 1) 27 halign: 'right' 28 size_hint: 1, None 29 font_size: '20dp' 30 height: '48dp' 31 on_release: 32 kb.is_fiat = False 33 Button: 34 background_color: 0, 0, 0, 0 35 id: fiat 36 text: kb.fiat_amount + ' ' + app.fiat_unit 37 color: (1, 1, 1, 1) if kb.is_fiat else (0.7, 0.7, 1, 1) 38 halign: 'right' 39 size_hint: 1, None 40 font_size: '20dp' 41 height: '48dp' 42 disabled: not app.fx.is_enabled() 43 on_release: 44 kb.is_fiat = True 45 Widget: 46 size_hint: 1, 0.2 47 GridLayout: 48 id: kb 49 amount: '' 50 fiat_amount: '' 51 is_fiat: False 52 is_max: False 53 on_fiat_amount: if self.is_fiat: self.amount = app.fiat_to_btc(self.fiat_amount) 54 on_amount: if not self.is_fiat: self.fiat_amount = app.btc_to_fiat(self.amount) 55 size_hint: 1, None 56 update_amount: popup.update_amount 57 height: '300dp' 58 cols: 3 59 KButton: 60 text: '1' 61 KButton: 62 text: '2' 63 KButton: 64 text: '3' 65 KButton: 66 text: '4' 67 KButton: 68 text: '5' 69 KButton: 70 text: '6' 71 KButton: 72 text: '7' 73 KButton: 74 text: '8' 75 KButton: 76 text: '9' 77 KButton: 78 text: '.' 79 KButton: 80 text: '0' 81 KButton: 82 text: '<' 83 Widget: 84 size_hint: 1, None 85 height: '48dp' 86 Button: 87 id: but_max 88 opacity: 1 if root.show_max else 0 89 disabled: not root.show_max 90 size_hint: 1, None 91 height: '48dp' 92 text: 'Max' 93 on_release: 94 kb.is_fiat = False 95 kb.amount = app.get_max_amount() 96 kb.is_max = True 97 Button: 98 size_hint: 1, None 99 height: '48dp' 100 text: 'Clear' 101 on_release: 102 kb.amount = '' 103 kb.fiat_amount = '' 104 kb.is_max = False 105 Widget: 106 size_hint: 1, 0.2 107 BoxLayout: 108 size_hint: 1, None 109 height: '48dp' 110 Widget: 111 size_hint: 1, None 112 height: '48dp' 113 Button: 114 size_hint: 1, None 115 height: '48dp' 116 text: _('OK') 117 on_release: 118 root.callback('!' if kb.is_max else btc.text if kb.amount else '') 119 popup.dismiss() 120 ''') 121 122 from kivy.properties import BooleanProperty 123 124 class AmountDialog(Factory.Popup): 125 show_max = BooleanProperty(False) 126 app = App.get_running_app() 127 128 def __init__(self, show_max, amount, cb): 129 Factory.Popup.__init__(self) 130 self.show_max = show_max 131 self.callback = cb 132 if amount: 133 self.ids.kb.amount = amount 134 135 def update_amount(self, c): 136 kb = self.ids.kb 137 amount = kb.fiat_amount if kb.is_fiat else kb.amount # type: str 138 if c == '<': # delete 139 amount = amount[:-1] 140 elif c == '.' and amount in ['0', '']: 141 amount = '0.' 142 elif amount == '0': 143 amount = c 144 else: 145 try: 146 Decimal(amount+c) 147 amount += c 148 except: 149 pass 150 # truncate btc amounts to max precision: 151 if not kb.is_fiat and '.' in amount: 152 p = amount.find('.') 153 amount = amount.replace('.', '') 154 amount = amount[:p] + '.' + amount[p:p + self.app.decimal_point()] 155 if kb.is_fiat: 156 kb.fiat_amount = amount 157 else: 158 kb.amount = amount