UI: interface de menus
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Thu, 13 Mar 2014 10:51:39 +0000 (07:51 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Thu, 13 Mar 2014 10:53:37 +0000 (07:53 -0300)
Um menu com opções para alterações dos dados pessoais.

A classe base de UI permite implementar novos tipos de interface.

src/baseui.py [new file with mode: 0644]
src/menu.py [new file with mode: 0644]

diff --git a/src/baseui.py b/src/baseui.py
new file mode 100644 (file)
index 0000000..4d340ed
--- /dev/null
@@ -0,0 +1,126 @@
+# -*- mode: python; encoding: utf-8; -*-
+#
+#   Copyright 2014 Thadeu Lima de Souza Cascardo <cascardo@cascardo.info>
+#
+#   This program is free software: you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#   the Free Software Foundation, either version 3 of the License, or
+#   (at your option) any later version.
+#
+#   This program is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License
+#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+# -*- mode: python; encoding: utf-8; -*-
+
+class BaseForm():
+    def __init__(self):
+        self.type = "none"
+        self.name = "Entry"
+    def get_type(self):
+        return self.type
+    def get_name(self):
+        return self.name
+
+class StringForm(BaseForm):
+    def __init__(self, name, value):
+        self.type = "string"
+        self.name = name
+        self.value = value
+    def get_value(self):
+        return self.value
+    def set_value(self, value):
+        self.value = value
+
+class OptionsForm(BaseForm):
+    def __init__(self, name, options, value):
+        self.type = "options"
+        self.name = name
+        self.options = options
+        self.value = value
+        self.index = self.find_index()
+    def find_index(self):
+        return map(lambda x: x[0], self.options).index(self.value)
+    def get_length(self):
+        return len(self.options)
+    def get_display(self, index):
+        return self.options[index][1]
+    def get_value(self, index):
+        return self.options[index][0]
+    def get_cur_display(self):
+        return self.options[self.index][1]
+    def get_cur_value(self):
+        return self.value
+    def get_index(self):
+        return self.index
+    def set_value(self, value):
+        self.value = value
+        self.index = self.find_index()
+
+class BaseUI():
+    def get_string(self, prompt=""):
+        return raw_input(prompt.encode("utf-8"))
+    def menu(self, list):
+        i = 1
+        for l in list:
+            self.show_item(str(i) + ". " + l)
+            i += 1
+        ans = self.get_string("Option: ")
+        try:
+            opt = int(ans)
+            if opt >= i:
+                return -1
+            return opt - 1
+        except Exception, e:
+            return -1
+    def show_item(self, s):
+        print s
+    def form(self, fs):
+        exit = False
+        while not exit:
+            self.show_item("0. Exit")
+            i = 1
+            for f in fs:
+                if f.get_type() == "string":
+                    self.show_item(str(i) + ". " + f.get_name() + ": " + f.get_value())
+                elif f.get_type() == "options":
+                    self.show_item(str(i) + ". " + f.get_name() + ": " + f.get_cur_display())
+                i += 1
+            ans = self.get_string("Option: ")
+            try:
+                opt = int(ans)
+                if opt >= i:
+                    # raise/show error/message about wrong option
+                    opt = -1
+            except Exception, e:
+                raise e
+            if opt == 0:
+                exit = True
+            else:
+                self.show_form(fs[opt - 1])
+    def show_form(self, f):
+        if f.get_type() == "string":
+            self.show_item(f.get_name() + ": " + f.get_value())
+            s = self.get_string(f.get_name() + ": ")
+            f.set_value(s)
+        elif f.get_type() == "options":
+            self.show_options(f)
+    def show_options(self, f):
+        self.show_item(f.get_name() + ": " + f.get_cur_display())
+        for i in range(f.get_length()):
+            self.show_item(str(i) + ". " + f.get_display(i))
+        ans = self.get_string("Option: ")
+        try:
+            opt = int(ans)
+            if opt >= i:
+                # raise/show error/message about wrong option
+                opt = -1
+        except Exception, e:
+            raise e
+        f.set_value(f.get_value(opt))
+
+
+# vim:tabstop=4:expandtab:smartindent
diff --git a/src/menu.py b/src/menu.py
new file mode 100644 (file)
index 0000000..c66d17d
--- /dev/null
@@ -0,0 +1,81 @@
+# -*- mode: python; encoding: utf-8; -*-
+#
+#   Copyright 2013 Thadeu Lima de Souza Cascardo <cascardo@cascardo.info>
+#
+#   This program is free software: you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#   the Free Software Foundation, either version 3 of the License, or
+#   (at your option) any later version.
+#
+#   This program is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License
+#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import baseui
+import contribuinte
+import ocupacoes
+import sys
+
+class OcupacaoForm(baseui.OptionsForm):
+    def __init__(self, ocupacoes, contribuinte):
+        g = ocupacoes.groups()
+        l = []
+        for i in sorted(g):
+            l.extend(g[i])
+        o = map(lambda x: (x[0], x[3]), l)
+        baseui.OptionsForm.__init__(self, u"Ocupações", o, contribuinte.get_campo_contribuinte("ocupacaoPrincipal"))
+        self.ocupacoes = ocupacoes
+        self.contribuinte = contribuinte
+    def set_value(self, value):
+        baseui.OptionsForm.set_value(self, value)
+        self.contribuinte.set_campo_contribuinte("ocupacaoPrincipal", value)
+
+class ContribuinteForm(baseui.StringForm):
+    def __init__(self, name, attr, contribuinte):
+        self.contribuinte = contribuinte
+        self.attr = attr
+        baseui.StringForm.__init__(self, name, self.contribuinte.get_campo_contribuinte(self.attr))
+    def set_value(self, value):
+        baseui.StringForm.set_value(self, value)
+        self.contribuinte.set_campo_contribuinte(self.attr, value)
+
+def DadosPessoais(UI, contrib):
+    form = []
+    ocup = ocupacoes.Ocupacoes()
+    form.append(ContribuinteForm("Nome", "nome", contrib))
+    form.append(OcupacaoForm(ocup, contrib))
+    for i in contribuinte.contribuinte_attributes:
+        form.append(ContribuinteForm(i, i, contrib))
+    UI.form(form)
+    return True
+
+def menu(UI, contrib):
+    m = [ "Sair", "Dados Pessoais" ]
+    f = [ None, DadosPessoais ]
+    exit = False
+    while not exit:
+        r = UI.menu(m)
+        if r <= 0:
+            exit = True
+        else:
+            f[r](UI, contrib)
+
+def main():
+    ret = False
+    UI = baseui.BaseUI()
+    while ret == False:
+        cpf = UI.get_string("Digite seu CPF: ")
+        try:
+            contrib = contribuinte.Contribuinte(cpf)
+            ret = menu(UI, contrib)
+        except RuntimeError, e:
+            print "CPF invalido"
+
+if __name__ == '__main__':
+    main()
+
+# vim:tabstop=4:expandtab:smartindent