118a1e587c40709854b1e4ef50e4892bdf73d10e
[cascardo/irpf-gui.git] / src / form.py
1 # coding=utf-8
2 #
3 #   Copyright 2014 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 # -*- mode: python; encoding: utf-8; -*-
18
19 class BaseForm():
20     def __init__(self):
21         self.type = "none"
22         self.name = "Entry"
23     def get_type(self):
24         return self.type
25     def get_name(self):
26         return self.name
27
28 class StringForm(BaseForm):
29     def __init__(self, name, value):
30         self.type = "string"
31         self.name = name
32         if value == None:
33             value = ""
34         self.value = value
35     def get_value(self):
36         return self.value
37     def set_value(self, value):
38         self.value = value
39
40 class OptionsForm(BaseForm):
41     def __init__(self, name, options, value):
42         self.type = "options"
43         self.name = name
44         self.options = options
45         self.value = value
46         self.index = self.find_index()
47     def find_index(self):
48         return map(lambda x: x[0], self.options).index(self.value)
49     def get_length(self):
50         return len(self.options)
51     def get_display(self, index):
52         return self.options[index][1]
53     def get_value(self, index):
54         return self.options[index][0]
55     def get_cur_display(self):
56         return self.options[self.index][1]
57     def get_cur_value(self):
58         return self.value
59     def get_index(self):
60         return self.index
61     def set_value(self, value):
62         self.value = value
63         self.index = self.find_index()
64
65 class AttrForm(StringForm):
66     def __init__(self, name, attr, element):
67         self.element = element
68         self.attr = attr
69         StringForm.__init__(self, name, self.element.get_attr(self.attr))
70     def set_value(self, value):
71         StringForm.set_value(self, value)
72         self.element.set_attr(self.attr, value)
73
74 class TipoForm(OptionsForm):
75     def __init__(self, name, attr, element, oclass, vnp):
76         l = oclass.list()
77         o = map(lambda x: (x[vnp[0]], x[vnp[1]]), l)
78         self.element = element
79         self.attr = attr
80         OptionsForm.__init__(self, name, o, element.get_attr(attr))
81     def set_value(self, value):
82         OptionsForm.set_value(self, value)
83         self.element.set_attr(self.attr, value)