List UI: uma interface para listas, ao estilo CRUD.
[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 item(self):
82         menu = [ "Exit", "Edit", "Delete" ]
83         actions = [ None, "edit", "delete" ]
84         r = self.menu(menu)
85         if r >= 0:
86             return actions[r]
87         return None
88     def list(self, ls):
89         self.show_item("0. Exit")
90         self.show_item("1. Add")
91         i = 2
92         for l in ls:
93             self.show_item(str(i) + ". " + l)
94             i += 1
95         ans = self.get_string("Option: ")
96         try:
97             opt = int(ans)
98             if opt >= i or opt <= 0:
99                 return (-1, None)
100             elif opt == 1:
101                 return (-1, "add")
102             return (opt - 1, self.item())
103         except Exception, e:
104             return (-1, None)
105     def form(self, fs):
106         exit = False
107         while not exit:
108             self.show_item("0. Exit")
109             i = 1
110             for f in fs:
111                 if f.get_type() == "string":
112                     self.show_item(str(i) + ". " + f.get_name() + ": " + f.get_value())
113                 elif f.get_type() == "options":
114                     self.show_item(str(i) + ". " + f.get_name() + ": " + f.get_cur_display())
115                 i += 1
116             ans = self.get_string("Option: ")
117             try:
118                 opt = int(ans)
119                 if opt >= i:
120                     # raise/show error/message about wrong option
121                     opt = -1
122             except Exception, e:
123                 raise e
124             if opt == 0:
125                 exit = True
126             else:
127                 self.show_form(fs[opt - 1])
128     def show_form(self, f):
129         if f.get_type() == "string":
130             self.show_item(f.get_name() + ": " + f.get_value())
131             s = self.get_string(f.get_name() + ": ")
132             f.set_value(s)
133         elif f.get_type() == "options":
134             self.show_options(f)
135     def show_options(self, f):
136         self.show_item(f.get_name() + ": " + f.get_cur_display())
137         for i in range(f.get_length()):
138             self.show_item(str(i) + ". " + f.get_display(i))
139         ans = self.get_string("Option: ")
140         try:
141             opt = int(ans)
142             if opt >= i:
143                 # raise/show error/message about wrong option
144                 opt = -1
145         except Exception, e:
146             raise e
147         f.set_value(f.get_value(opt))
148
149
150 # vim:tabstop=4:expandtab:smartindent