UI: interface de menus
[cascardo/irpf-gui.git] / src / baseui.py
1 # -*- mode: python; encoding: 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         self.value = value
33     def get_value(self):
34         return self.value
35     def set_value(self, value):
36         self.value = value
37
38 class OptionsForm(BaseForm):
39     def __init__(self, name, options, value):
40         self.type = "options"
41         self.name = name
42         self.options = options
43         self.value = value
44         self.index = self.find_index()
45     def find_index(self):
46         return map(lambda x: x[0], self.options).index(self.value)
47     def get_length(self):
48         return len(self.options)
49     def get_display(self, index):
50         return self.options[index][1]
51     def get_value(self, index):
52         return self.options[index][0]
53     def get_cur_display(self):
54         return self.options[self.index][1]
55     def get_cur_value(self):
56         return self.value
57     def get_index(self):
58         return self.index
59     def set_value(self, value):
60         self.value = value
61         self.index = self.find_index()
62
63 class BaseUI():
64     def get_string(self, prompt=""):
65         return raw_input(prompt.encode("utf-8"))
66     def menu(self, list):
67         i = 1
68         for l in list:
69             self.show_item(str(i) + ". " + l)
70             i += 1
71         ans = self.get_string("Option: ")
72         try:
73             opt = int(ans)
74             if opt >= i:
75                 return -1
76             return opt - 1
77         except Exception, e:
78             return -1
79     def show_item(self, s):
80         print s
81     def form(self, fs):
82         exit = False
83         while not exit:
84             self.show_item("0. Exit")
85             i = 1
86             for f in fs:
87                 if f.get_type() == "string":
88                     self.show_item(str(i) + ". " + f.get_name() + ": " + f.get_value())
89                 elif f.get_type() == "options":
90                     self.show_item(str(i) + ". " + f.get_name() + ": " + f.get_cur_display())
91                 i += 1
92             ans = self.get_string("Option: ")
93             try:
94                 opt = int(ans)
95                 if opt >= i:
96                     # raise/show error/message about wrong option
97                     opt = -1
98             except Exception, e:
99                 raise e
100             if opt == 0:
101                 exit = True
102             else:
103                 self.show_form(fs[opt - 1])
104     def show_form(self, f):
105         if f.get_type() == "string":
106             self.show_item(f.get_name() + ": " + f.get_value())
107             s = self.get_string(f.get_name() + ": ")
108             f.set_value(s)
109         elif f.get_type() == "options":
110             self.show_options(f)
111     def show_options(self, f):
112         self.show_item(f.get_name() + ": " + f.get_cur_display())
113         for i in range(f.get_length()):
114             self.show_item(str(i) + ". " + f.get_display(i))
115         ans = self.get_string("Option: ")
116         try:
117             opt = int(ans)
118             if opt >= i:
119                 # raise/show error/message about wrong option
120                 opt = -1
121         except Exception, e:
122             raise e
123         f.set_value(f.get_value(opt))
124
125
126 # vim:tabstop=4:expandtab:smartindent