Corrige coding em cabeƧalhos.
[cascardo/irpf-gui.git] / src / baseui.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 BaseUI():
66     def get_string(self, prompt=""):
67         return raw_input(prompt.encode("utf-8"))
68     def menu(self, list):
69         i = 1
70         for l in list:
71             self.show_item(str(i) + ". " + l)
72             i += 1
73         ans = self.get_string("Option: ")
74         try:
75             opt = int(ans)
76             if opt >= i:
77                 return -1
78             return opt - 1
79         except Exception, e:
80             return -1
81     def show_item(self, s):
82         print s
83     def item(self):
84         menu = [ "Exit", "Edit", "Delete" ]
85         actions = [ None, "edit", "delete" ]
86         r = self.menu(menu)
87         if r >= 0:
88             return actions[r]
89         return None
90     def list(self, ls):
91         self.show_item("0. Exit")
92         self.show_item("1. Add")
93         i = 2
94         for l in ls:
95             self.show_item(str(i) + ". " + l)
96             i += 1
97         ans = self.get_string("Option: ")
98         try:
99             opt = int(ans)
100             if opt >= i or opt <= 0:
101                 return (-1, None)
102             elif opt == 1:
103                 return (-1, "add")
104             return (opt - 1, self.item())
105         except Exception, e:
106             return (-1, None)
107     def form(self, fs):
108         exit = False
109         while not exit:
110             self.show_item("0. Exit")
111             i = 1
112             for f in fs:
113                 if f.get_type() == "string":
114                     self.show_item(str(i) + ". " + f.get_name() + ": " + f.get_value())
115                 elif f.get_type() == "options":
116                     self.show_item(str(i) + ". " + f.get_name() + ": " + f.get_cur_display())
117                 i += 1
118             ans = self.get_string("Option: ")
119             try:
120                 opt = int(ans)
121                 if opt >= i:
122                     # raise/show error/message about wrong option
123                     opt = -1
124             except Exception, e:
125                 raise e
126             if opt == 0:
127                 exit = True
128             else:
129                 self.show_form(fs[opt - 1])
130     def show_form(self, f):
131         if f.get_type() == "string":
132             self.show_item(f.get_name() + ": " + f.get_value())
133             s = self.get_string(f.get_name() + ": ")
134             f.set_value(s)
135         elif f.get_type() == "options":
136             self.show_options(f)
137     def show_options(self, f):
138         self.show_item(f.get_name() + ": " + f.get_cur_display())
139         for i in range(f.get_length()):
140             self.show_item(str(i) + ". " + f.get_display(i))
141         ans = self.get_string("Option: ")
142         try:
143             opt = int(ans)
144             if opt >= i:
145                 # raise/show error/message about wrong option
146                 opt = -1
147         except Exception, e:
148             raise e
149         f.set_value(f.get_value(opt))
150
151
152 # vim:tabstop=4:expandtab:smartindent