Reshuffle the gui class structure a bit more.
[matthijs/upstream/mobilegtd.git] / src / gui / project_details / project_view.py
1 import model, config, gui
2 import appuifw,os
3 import thread
4 from model.model import *
5 from config.config import gtd_directory,Configuration
6 from config.defaultconfig import default_actions_menu
7 from gui.gui import SearchableListView
8 from infos_widget import InfosWidget
9 from info_widget import InfoWidget
10 from context_widget import ContextWidget
11 from action_widget import ActionWidget,edit_action
12 from action_view import ActionView
13 from model import action
14 from model import info
15 from model import project
16 from model.action import Action
17 from log.logging import logger
18 from e32 import Ao_lock, in_emulator
19 from key_codes import *
20 import key_codes
21
22 from gui import *
23
24 ACTION_LIST_KEYS_AND_MENU = Configuration(os.path.join(gtd_directory,"actions.cfg"), default_actions_menu)
25
26
27 def ask_for_action(project_name,proposition=None):
28
29     action = Action(project_name,u'')
30     was_saved = edit_action(action)
31     if was_saved:
32         return action
33     else:
34         return None
35
36
37 #def ask_for_action(project_name,proposition=None):
38 #    if proposition == None:
39 #        proposition = u'Context %s'%(project_name)
40 #    action_line = appuifw.query(u'Enter action %s'%project_name,'text',proposition)
41 #    if action_line == None:
42 #        return None
43 #    else:
44 #        return parse_action(action_line)
45
46
47 #    text = u'Enter action'
48 #    if info:
49 #        text = text+info
50 #    new_description = appuifw.query(text,'text',action.description)
51 #    if new_description:
52 #        action.set_description(new_description)
53 #        return True
54 #    return False
55
56
57
58 def ask_for_info(proposition):
59     return appuifw.query(u'Enter info','text',u'%s'%(proposition))
60
61
62 class ProjectView(SearchableListView):
63     def __init__(self,project):
64         self.project = project
65         self.project.observers.append(self)
66         super(ProjectView, self).__init__()
67         self.set_menu(ACTION_LIST_KEYS_AND_MENU)
68         self.set_title(self.project.name)
69         self.set_filters([lambda:self.project.actions.with_property(lambda a:a.status==action.active)])
70
71     def exit(self):
72         self.project.observers.remove(self)
73 #        self.project.status = project.active
74 #        self.project.status.update(self.project)
75         super(ProjectView, self).exit()
76 #        self.project.dirty = True
77 #        self.project.write()
78 #        self.projects.update_status(self.project)
79     def edit_menu(self):
80         show_config(ACTION_LIST_KEYS_AND_MENU)
81         ACTION_LIST_KEYS_AND_MENU.write()
82
83     def actions(self):
84         return self.project.actions
85
86     def generate_widgets(self):
87         widgets = []
88         widgets.append(InfosWidget(self.project))
89         for info in self.project.infos:
90             widgets.append(InfoWidget(info,self.project))
91         for (context,actions) in self.actions_by_context().items():
92             widgets.append(ContextWidget(context,self.project))
93             for action in actions:
94                 widgets.append(ActionWidget(action))
95         return widgets
96     def actions_by_context(self):
97         context_actions_map = {}
98         for action in self.actions():
99             if not action.context in context_actions_map:
100                 context_actions_map[action.context]=[]
101             context_actions_map[action.context].append(action)
102         return context_actions_map
103
104     def add_action(self):
105         a = ask_for_action(self.project.name)
106         if a:
107             self.project.add_action(a)
108             self.project.status = project.active
109     def add_info(self):
110         i = ask_for_info(self.project.name)
111         if i:
112             selected = self.selected_index()
113             # First position is "Infos"
114             if selected>=0 and selected <= len(self.project.infos):
115                 position = selected
116             else:
117                 position = None
118             self.project.add_info(info.Info(i),position)
119             if position:
120                 self.set_index(position+1)
121
122
123
124     def notify(self,project,attribute,new=None,old=None):
125         self.refresh()
126
127
128
129
130 __all__= ('ProjectView','ask_for_action','ask_for_info')