UI: interface de menus
[cascardo/irpf-gui.git] / src / menu.py
1 # -*- mode: python; encoding: utf-8; -*-
2 #
3 #   Copyright 2013 Thadeu Lima de Souza Cascardo <cascardo@cascardo.info>
4 #
5 #   This program is free software: you can redistribute it and/or modify
6 #   it under the terms of the GNU General Public License as published by
7 #   the Free Software Foundation, either version 3 of the License, or
8 #   (at your option) any later version.
9 #
10 #   This program is distributed in the hope that it will be useful,
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #   GNU General Public License for more details.
14 #
15 #   You should have received a copy of the GNU General Public License
16 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 import baseui
19 import contribuinte
20 import ocupacoes
21 import sys
22
23 class OcupacaoForm(baseui.OptionsForm):
24     def __init__(self, ocupacoes, contribuinte):
25         g = ocupacoes.groups()
26         l = []
27         for i in sorted(g):
28             l.extend(g[i])
29         o = map(lambda x: (x[0], x[3]), l)
30         baseui.OptionsForm.__init__(self, u"Ocupações", o, contribuinte.get_campo_contribuinte("ocupacaoPrincipal"))
31         self.ocupacoes = ocupacoes
32         self.contribuinte = contribuinte
33     def set_value(self, value):
34         baseui.OptionsForm.set_value(self, value)
35         self.contribuinte.set_campo_contribuinte("ocupacaoPrincipal", value)
36
37 class ContribuinteForm(baseui.StringForm):
38     def __init__(self, name, attr, contribuinte):
39         self.contribuinte = contribuinte
40         self.attr = attr
41         baseui.StringForm.__init__(self, name, self.contribuinte.get_campo_contribuinte(self.attr))
42     def set_value(self, value):
43         baseui.StringForm.set_value(self, value)
44         self.contribuinte.set_campo_contribuinte(self.attr, value)
45
46 def DadosPessoais(UI, contrib):
47     form = []
48     ocup = ocupacoes.Ocupacoes()
49     form.append(ContribuinteForm("Nome", "nome", contrib))
50     form.append(OcupacaoForm(ocup, contrib))
51     for i in contribuinte.contribuinte_attributes:
52         form.append(ContribuinteForm(i, i, contrib))
53     UI.form(form)
54     return True
55
56 def menu(UI, contrib):
57     m = [ "Sair", "Dados Pessoais" ]
58     f = [ None, DadosPessoais ]
59     exit = False
60     while not exit:
61         r = UI.menu(m)
62         if r <= 0:
63             exit = True
64         else:
65             f[r](UI, contrib)
66
67 def main():
68     ret = False
69     UI = baseui.BaseUI()
70     while ret == False:
71         cpf = UI.get_string("Digite seu CPF: ")
72         try:
73             contrib = contribuinte.Contribuinte(cpf)
74             ret = menu(UI, contrib)
75         except RuntimeError, e:
76             print "CPF invalido"
77
78 if __name__ == '__main__':
79     main()
80
81 # vim:tabstop=4:expandtab:smartindent