Permite valores vazios para OptionForm.
[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         if self.value == None:
49             return -1
50         return map(lambda x: x[0], self.options).index(self.value)
51     def get_length(self):
52         return len(self.options)
53     def get_display(self, index):
54         return self.options[index][1]
55     def get_value(self, index):
56         return self.options[index][0]
57     def get_cur_display(self):
58         if self.index < 0:
59             return ""
60         return self.options[self.index][1]
61     def get_cur_value(self):
62         return self.value
63     def get_index(self):
64         return self.index
65     def set_value(self, value):
66         self.value = value
67         self.index = self.find_index()
68
69 class AttrForm(StringForm):
70     def __init__(self, name, attr, element):
71         self.element = element
72         self.attr = attr
73         StringForm.__init__(self, name, self.element.get_attr(self.attr))
74     def set_value(self, value):
75         StringForm.set_value(self, value)
76         self.element.set_attr(self.attr, value)
77
78 class TipoForm(OptionsForm):
79     def __init__(self, name, attr, element, oclass, vnp):
80         l = oclass.list()
81         o = map(lambda x: (x[vnp[0]], x[vnp[1]]), l)
82         self.element = element
83         self.attr = attr
84         OptionsForm.__init__(self, name, o, element.get_attr(attr))
85     def set_value(self, value):
86         OptionsForm.set_value(self, value)
87         self.element.set_attr(self.attr, value)