Move classes Form para módulo próprio.
[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 BaseUI():
20     def get_string(self, prompt=""):
21         return raw_input(prompt.encode("utf-8"))
22     def menu(self, list):
23         i = 1
24         for l in list:
25             self.show_item(str(i) + ". " + l)
26             i += 1
27         ans = self.get_string("Option: ")
28         try:
29             opt = int(ans)
30             if opt >= i:
31                 return -1
32             return opt - 1
33         except Exception, e:
34             return -1
35     def show_item(self, s):
36         print s
37     def item(self):
38         menu = [ "Exit", "Edit", "Delete" ]
39         actions = [ None, "edit", "delete" ]
40         r = self.menu(menu)
41         if r >= 0:
42             return actions[r]
43         return None
44     def list(self, ls):
45         self.show_item("0. Exit")
46         self.show_item("1. Add")
47         i = 2
48         for l in ls:
49             self.show_item(str(i) + ". " + l)
50             i += 1
51         ans = self.get_string("Option: ")
52         try:
53             opt = int(ans)
54             if opt >= i or opt <= 0:
55                 return (-1, None)
56             elif opt == 1:
57                 return (-1, "add")
58             return (opt - 1, self.item())
59         except Exception, e:
60             return (-1, None)
61     def form(self, fs):
62         exit = False
63         while not exit:
64             self.show_item("0. Exit")
65             i = 1
66             for f in fs:
67                 if f.get_type() == "string":
68                     self.show_item(str(i) + ". " + f.get_name() + ": " + f.get_value())
69                 elif f.get_type() == "options":
70                     self.show_item(str(i) + ". " + f.get_name() + ": " + f.get_cur_display())
71                 i += 1
72             ans = self.get_string("Option: ")
73             try:
74                 opt = int(ans)
75                 if opt >= i:
76                     # raise/show error/message about wrong option
77                     opt = -1
78             except Exception, e:
79                 raise e
80             if opt == 0:
81                 exit = True
82             else:
83                 self.show_form(fs[opt - 1])
84     def show_form(self, f):
85         if f.get_type() == "string":
86             self.show_item(f.get_name() + ": " + f.get_value())
87             s = self.get_string(f.get_name() + ": ")
88             f.set_value(s)
89         elif f.get_type() == "options":
90             self.show_options(f)
91     def show_options(self, f):
92         self.show_item(f.get_name() + ": " + f.get_cur_display())
93         for i in range(f.get_length()):
94             self.show_item(str(i) + ". " + f.get_display(i))
95         ans = self.get_string("Option: ")
96         try:
97             opt = int(ans)
98             if opt >= i:
99                 # raise/show error/message about wrong option
100                 opt = -1
101         except Exception, e:
102             raise e
103         f.set_value(f.get_value(opt))
104
105
106 # vim:tabstop=4:expandtab:smartindent