root/Lists.py

Revision 8, 9.0 kB (checked in by matt_dorn@yahoo.com, 3 years ago)

0.2 release

Added multiple list support, mainly

Line 
1 import gtk
2
3 class TabLists:
4
5     list_item_stores={}   
6    
7     def render(self, window):
8         """
9         """
10         # define the list store for the lists
11         # id, temp id, name, name
12         # (temp id is for GUI only, final name is to check for update)
13         list_store = gtk.ListStore(int, int, str, str)
14        
15         # remove any leftover data from a page no longer active
16         cols=window.treeview_lists.get_columns()
17         for col in cols:
18             window.treeview_lists.remove_column(col)
19
20         # add data to model
21         items=window.bp.lists.lists(window.page_id)
22         i=0
23         first_list_id = None
24         for item in items:
25             if i==0:
26                 first_list_id = item[0]
27             list_store.append([item[0], i, item[1], item[1]])
28             # append item names twice to check if actually changed before updating
29            
30             # each list has a store of list items: id, complete, text
31             # we also need to include the list ID, for deleting from ZODB
32             self.list_item_stores[i]=gtk.ListStore(int, bool, str, bool, str, int)
33             # again, appending items twice for checking
34             if window.bp.__class__.__module__ == "model.db":
35                 list_items = item[2]
36                 for item2 in list_items:
37                     # item[2] holds the dictionary of the list's items
38                     self.list_item_stores[i].append([item2,
39                                                     list_items[item2][0],
40                                                     list_items[item2][1],
41                                                     list_items[item2][0],
42                                                     list_items[item2][1],
43                                                     item[0]])               
44             else:
45                 # module is "model.backpack" -- remote access
46                 # TODO: Unfortunately, requires multiple HTTP requests
47                 # to get name of list.  Not sure if anything can be done about
48                 # this unless 37signals changes the API
49                 list_items = window.bp.lists.list(window.page_id, item[0])
50                 for item2 in list_items:
51                     # not working with a dictionary, but with a list of lists
52                     # TODO: must be a way to convert these lists into a dictionary
53                     # and just use the same loop as above
54                     self.list_item_stores[i].append([item2[0],
55                                                     item2[1],
56                                                     item2[2],
57                                                     item2[1],
58                                                     item2[2],
59                                                     item[0]])
60
61             i = i + 1
62         window.treeview_lists.set_model(list_store)
63         renderer = gtk.CellRendererText()
64         renderer.set_property( 'editable', True )
65         renderer.connect('edited', self.on_treeview_lists_col1_edited, list_store)
66         col1=gtk.TreeViewColumn("List", renderer, text=2)
67         col1.set_sort_column_id(2)
68         window.treeview_lists.append_column(col1)
69         if first_list_id:
70             self.render_items(window, list_store[0][1])
71         else:
72             # no list exists! (not sure if this ever really happens in Backpack)
73             self.render_items(window, 0, True)
74        
75     def render_items(self, window, list_id, no_list=False):
76         #---------- list items pane -------------
77        
78         cols=window.treeview_list_items.get_columns()
79         for col in cols:
80             window.treeview_list_items.remove_column(col)
81        
82         if no_list:
83             # set the store for the first time
84             window.treeview_list_items.set_model(gtk.ListStore(int, bool, str, bool, str, int))
85         else:
86             window.treeview_list_items.set_model(self.list_item_stores[list_id])
87        
88         renderer1 = gtk.CellRendererToggle()
89         renderer1.set_property('activatable', True)
90         if not no_list:
91             renderer1.connect('toggled',
92                               self.on_treeview_list_items_col3_toggle,
93                               self.list_item_stores[list_id])
94
95         renderer2 = gtk.CellRendererText()
96         renderer2.set_property( 'editable', True )
97         if not no_list:
98             renderer2.connect('edited',
99                               self.on_treeview_list_items_edited,
100                               self.list_item_stores[list_id])
101        
102        
103         col3=gtk.TreeViewColumn("Complete", renderer1)
104         col4=gtk.TreeViewColumn("Item", renderer2, text=2)
105         col3.set_sort_column_id(1)
106         col4.set_sort_column_id(2)
107         # the following line is necessary to make the checkbox appear
108         col3.add_attribute(renderer1, "active", 1)
109
110         window.treeview_list_items.append_column(col3)
111         window.treeview_list_items.append_column(col4)
112        
113     def update(self, window):
114         model=window.treeview_lists.get_model()
115         iter=model.get_iter_first()
116         while iter is not None:
117             id=model.get_value(iter, 0)
118             #checked=model.get_value(iter, 1)
119             text=model.get_value(iter, 2)
120             #toggled=model.get_value(iter, 3)
121             check_text=model.get_value(iter, 3)
122             if id==0:
123                 # insert NEW item
124                 result=window.bp.lists.create_list(window.page_id, text)
125                 id=result[0]
126                 model.set_value(iter, 0, id)
127             else:
128                 # UPDATE item
129                 # only update if the items were actually changed (check against the
130                 # two extra columns               
131                 if text != check_text:
132                     window.bp.lists.update_list(window.page_id, id, text)
133             #if checked != toggled:
134                 #window.bp.list.toggle(window.page_id, id)
135
136             # now update the items for each list
137             tmp_list_id = model.get_value(iter, 1)
138             for item in self.list_item_stores[tmp_list_id]:
139                 id2=item[0]
140                 checked2=item[1]
141                 text2=item[2]
142                 toggled2=item[3]
143                 check_text2=item[4]
144                 if id2==0:
145                     # insert NEW item
146                     result2=window.bp.lists.create(window.page_id, text2, id)
147                     id2=result2[0]
148                 else:
149                     # UPDATE item
150                     # only update if the items were actually changed (check against the
151                     # two extra columns
152                     if text2 != check_text2:
153                         window.bp.lists.update(window.page_id, id2, text2, id)
154                 if checked2 != toggled2:
155                     window.bp.lists.toggle(window.page_id, id2, id)
156
157             iter=model.iter_next(iter)
158
159     def delete(self, window, list_id):
160         window.bp.lists.destroy_list(window.page_id, list_id)
161         return
162
163     def create(self, window):
164         # add list to treeview
165         view=window.treeview_lists
166         model=view.get_model()
167         # note: id=0 temporarily (only for UI display purposes)
168         # until some storage action takes place
169         tmp_id = len(model)
170         iter = model.append((0, tmp_id, '', ''))
171         # add the list store object for the list items
172         self.list_item_stores[tmp_id]=gtk.ListStore(int, bool, str, bool, str, int)
173         # grab focus on new item for editing
174         path=model.get_path(iter)
175         col=view.get_column(0)
176         view.grab_focus()       
177         view.set_cursor_on_cell(path, col, None, True)
178         return
179
180     def create_item(self, window):
181         # add item to treeview
182         view=window.treeview_list_items
183         model=view.get_model()
184         # note: id=0 temporarily (only for UI display purposes)
185         # until some storage action takes place
186         iter=model.append((0, False, '', False, '', 0))
187         # grab focus on new item for editing
188         path=model.get_path(iter)
189         col=view.get_column(1)
190         view.grab_focus()
191         view.set_cursor_on_cell(path, col, None, True)
192         return
193
194     def delete_item(self, window, list_id, item_id):
195         window.bp.lists.destroy(window.page_id, item_id, list_id)
196         return
197
198     def on_treeview_list_items_col3_toggle(self, cell, path, model):
199         model[path][1] = not model[path][1]
200         #print model[path][1]
201         return
202
203     def on_treeview_lists_col1_edited( self, cell, path, new_text, model ):
204         """
205         Called when a text cell is edited.  It puts the new text
206         in the model so that it is displayed properly.
207         """
208         #print "Change '%s' to '%s'" % (model[path][0], new_text)
209         model[path][2] = new_text
210         return       
211
212     def on_treeview_list_items_edited( self, cell, path, new_text, model ):
213         """
214         Called when the text cell for a list item is edited.  It puts the new text
215         in the model so that it is displayed properly.
216         """
217         #print "Change '%s' to '%s'" % (model[path][0], new_text)
218         model[path][2] = new_text
219         return               
Note: See TracBrowser for help on using the browser.