-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount_edit_control.py
141 lines (117 loc) · 4.66 KB
/
account_edit_control.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
#!/bin/env python
# -*- coding: utf-8 -*-
from combo_control import combo_control
from common_methods import make_builder, is_blank, is_null_or_empty, show_error
import gtk
import sys
class account_edit_control:
"""
\~russian
\brief Контрол формы редактирования счета
"""
def set_comment(self, text):
"""\brief set comment field
\param text
"""
self.comment.set_text('' if text == None else text)
def set_name(self, name):
"""\brief set name field
\param name
"""
self.name.set_text(name)
def set_currency(self, currency):
"""\brief set currency field
\param currency
"""
self.currency_combo.set_value(currency)
def set_first_money(self, money_amount):
"""\brief set money amount field
\param money_amount
"""
self.first_money.set_value(money_amount)
def __init__(self, parent):
self._parent = parent
self.builder = make_builder('glade/account_edit.glade')
def shobject(name):
return self.builder.get_object(name)
self.window = shobject("account_edit")
self.window.set_transient_for(self._parent.window.builder.get_object('main_window'))
self.window.add_buttons(gtk.STOCK_SAVE, gtk.RESPONSE_ACCEPT, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
self.currency_combo = combo_control(shobject("account_edit_currency"))
self.first_money = shobject("account_edit_money")
self.first_money.get_adjustment().set_all(lower = 0, upper = sys.float_info.max, step_increment = 1, page_increment = 100)
self.first_money.set_digits(4)
self.name = shobject("account_edit_name")
self.comment = shobject("account_edit_comment").get_buffer()
shobject("account_edit_change").connect("clicked", self.change_clicked)
def change_clicked(self, button):
"""\brief change currency button clicked handler
\param button
"""
if self._parent.currency.run() == gtk.RESPONSE_ACCEPT:
self.update_currency()
def update_widget(self, currencies):
"""
\param currencies list of names of currencies
"""
self.currency_combo.update_widget(currencies)
def run(self):
"""\brief run the dialog
\retval gtk.RESPONSE_ACCEPT save button pressed
\retval gtk.RESPONSE_CANCEL cancel pressed
"""
ret = self.window.run()
while ret == gtk.RESPONSE_ACCEPT:
if not self.check_correctness():
ret = self.window.run()
else:
self.window.hide()
return ret
self.window.hide()
return ret
def check_correctness(self):
errs = []
if is_blank(self.name.get_text()):
errs.append(u'Необходимо указать имя счета')
vv = self.currency_combo.get_value()
if is_null_or_empty(vv):
errs.append(u'Нужно указать название валюты')
if len(errs) > 0:
show_error(reduce(lambda a, b:u'{0}\n{1}'.format(a, b), errs), self.window)
return False
return True
def get_data(self):
"""\brief return the data in dialog
\return hash table with keys \c name, \c money_name, \c money_count and \c comment
"""
ret = {'name' : self.name.get_text(),
'money_count' : self.first_money.get_value(),
'money_name' : self.currency_combo.get_value()}
if self.comment.get_char_count() > 0:
ret["comment"] = self.comment.get_text(self.comment.get_start_iter(), self.comment.get_end_iter())
return ret
def reset_widget(self, ):
"""\brief clear all fields and update currency combobox
"""
self.clear_all()
self.update_currency()
def clear_all(self, ):
"""\brief clear all fields of widget
"""
self.name.set_text("")
self.comment.set_text("")
self.currency_combo.set_value(None)
self.currency_combo.update_widget([])
self.first_money.set_value(0)
def update_currency(self, ):
"""\brief update currency_combo with posible values
"""
mm = self._parent.model.list_moneys()
self.update_widget(map(lambda a: a["name"], mm))
def load_to_widget(self, data):
if data.has_key("name"):
self.name.set_text(data['name'])
if data.has_key("first_money"):
self.first_money.set_value(data['first_money'])
if data.has_key('currency'):
self.currency_combo.set_value(data['currency'])