-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnc_amount_edit.py
238 lines (155 loc) · 6.13 KB
/
gnc_amount_edit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# reimplementation of gnc_amount_edit in python
# unused for the moment
import sys
#import gobject
from gi.repository import GObject
#import gtk
from gi.repository import Gtk
from gi.repository import Gdk
import re
import pdb
from sw_app_utils import GncPrintAmountInfo
from sw_app_utils import PrintAmount
import gnucash
from gnucash import GncNumeric
from gnucash.gnucash_core_c import GNC_HOW_RND_ROUND_HALF_UP
# how to do internationalization in python
#import gettext
#gettext.bindtextdomain('myapplication', '/path/to/my/language/directory')
#gettext.textdomain('myapplication')
#_ = gettext.gettext
# dummy function for internationalization
def N_(msg):
return msg
class GNCAmountEditPython(Gtk.Entry):
# we must make this a new GObject type to allow for original gnucash definition
__gtype_name__ = 'GNCAmountEditPython'
__gsignals__ = {
'amount_changed' : (GObject.SignalFlags.RUN_FIRST, None, (int,))
}
def __init__ (self, min_places=0, max_places=0, fraction=1):
super(GNCAmountEditPython,self).__init__()
self.need_to_parse = False
self.print_info = GncPrintAmountInfo()
self.print_info.min_decimal_places = min_places
self.print_info.max_decimal_places = max_places
self.fraction = fraction
self.evaluate_on_enter = True
# gnc_amount_edit_set_print_info (edit, print_info);
# gnc_amount_edit_set_fraction (edit, fraction);
# gnc_amount_edit_set_evaluate_on_enter (edit, TRUE);
# gtk_entry_set_alignment (GTK_ENTRY(edit), 1.0);
self.set_alignment(1.0)
self.amount = GncNumeric(0,1)
self.connect("changed", self.changed_cb)
self.connect("key-press-event", self.key_press_event)
self.connect("activate", self.activate)
def changed_cb (self, actionobj, userdata=None):
print("edit changed_cb",actionobj,userdata, file=sys.stderr)
self.need_to_parse = True
def key_press_event (self, widget, event):
print("key_press_event", widget, event)
# this code changes keypad decimal key for currencies
if event.keyval == Gdk.KEY_KP_Decimal:
#if self.print_info.monetary:
# event.keyval = gnc_localeconv.mon_deciman_point[0]
# event.string[0] = gnc_localeconv.mon_deciman_point[0]
pass
# this calls parent function??
# DONT DO THIS!! (it doesnt exist in any case)
#result = super(GNCAmountEditPython,self).key_press_event(widget,event)
# just ensure return False to perform more key event processing
# returning True from this function means stop processing key event now
result = False
if event.keyval == Gdk.KEY_Return:
if not self.evaluate_on_enter:
if not (event.state & (Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.MOD1_MASK | Gdk.ModifierType.SHIFT_MASK)):
return result
elif event.keyval == Gdk.KEY_KP_Enter:
pass
else:
return result
self.evaluate()
return True
def activate (self, widget, event=None):
# this function is called on Return/Enter
print("activate", widget, event)
def set_amount (self, amount):
amount_string = PrintAmount(amount,self.print_info)
#amount_string = str(amount)
self.set_text(amount_string)
self.amount = amount
self.need_to_parse = False
def get_amount (self):
self.evaluate()
return self.amount
def set_damount (self, damount):
#amount_string = PrintAmount(amount,self.print_info)
#amount_string = str(amount)
if self.fraction > 0:
fraction = self.fraction
else:
fraction = 100000
amount = gnucash.gnucash_core_c.double_to_gnc_numeric(damount, fraction, GNC_HOW_RND_ROUND_HALF_UP)
amount = GncNumeric(instance=amount)
self.set_amount(amount)
def get_damount (self):
self.evaluate()
return self.amount.to_double()
def expr_is_valid (self, empty_ok):
txt_string = self.get_text()
if txt_string == None or txt_string == "":
amount = GncNumeric(0,1)
if empty_ok:
return (-1, amount)
else:
return (0, amount)
error_loc = None
# where to get this function??
#ok = GncExpParserParse(txt_string, amount, error_loc)
# junkily check for the moment
#pdb.set_trace()
mtch = re.search(r'^[0-9.,]+$',txt_string)
if mtch == None:
error_loc = len(txt_string)
ok = False
else:
ok = True
if txt_string.find(',') >= 0:
txt_string = txt_string.replace(',','')
if txt_string.find('.') >= 0:
tmpval = float(txt_string)*self.fraction
amount = GncNumeric(int(tmpval),self.fraction)
else:
tmpval = int(txt_string)*self.fraction
amount = GncNumeric(tmpval,self.fraction)
if ok:
return (0, amount)
if error_loc != None:
return (error_loc, None)
return (1, None)
def set_evaluate_on_enter (self, evaluate_on_enter):
self.evaluate_on_enter = evaluate_on_enter
def evaluate (self):
if not self.need_to_parse:
return True
(result, amount) = self.expr_is_valid(False)
if result == -1:
return True
if result == 0:
old_amount = self.amount
if self.fraction > 0:
amount = amount.convert(self.fraction,GNC_HOW_RND_ROUND_HALF_UP)
self.set_amount(amount)
if not amount == old_amount:
self.emit("amount_changed",0)
return True
self.set_position(result)
return False
def set_print_info (self, print_info):
self.print_info = self.print_info
self.print_info.use_symbol = 0
def set_fraction (self, fraction):
# ensure positive
fraction = max(0,fraction)
self.fraction = fraction