root/Notes.py

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

0.2 release

Added multiple list support, mainly

Line 
1 import gtk
2 import time
3 from utils import getTvText
4
5 class TabNotes:
6     """
7     Class for handling Backpack notes.
8     """
9     def __init__(self):
10         self.preedit_path=-1
11    
12     def render(self, window):
13        
14         # remove any leftover data from a page no longer active
15         cols=window.treeview_notes.get_columns()
16         for col in cols:
17             window.treeview_notes.remove_column(col)
18         buf=gtk.TextBuffer()
19         buf.set_text("")
20         window.textview_notes.set_buffer(buf)
21
22         # list store: (id, title, timestamp, text, title, text, formatted timestamp)--last 2 are
23         # to check for changes to original data before submitting updates
24         list_store = gtk.ListStore(int, str, str, str, str, str, str)
25        
26         # add data to model
27         items=window.bp.notes.list(window.page_id)
28        
29         for item in items:
30             timestamp=time.ctime(item[2])
31             # note: appending items twice to check if actually changed before updating
32             list_store.append([item[0], item[1], item[2], item[3], item[1], item[3], timestamp])
33         window.treeview_notes.set_model(list_store)
34        
35      
36         renderer1 = gtk.CellRendererText()
37         renderer1.set_property('editable', True)
38         renderer1.connect('edited', self.on_treeview_notes_col1_edited, list_store)
39
40         renderer2 = gtk.CellRendererText()
41
42         window.textview_notes.connect('focus_in_event', self.on_textview_notes_focus_in_event, window)
43         #window.textview_notes.connect('focus_out_event', self.on_textview_notes_focus_out_event, window)
44         window.textview_notes.connect('key_release_event', self.on_textview_key_press_event, window)
45        
46         col1=gtk.TreeViewColumn("Note", renderer1, text=1)
47         # the 6th column of the model is the formatted date       
48         col2=gtk.TreeViewColumn("Posted", renderer2, text=6)
49         # sort on the unformatted date tick
50         col1.set_sort_column_id(1)
51         col2.set_sort_column_id(2)
52         col1.set_resizable(True)
53        
54         window.treeview_notes.append_column(col1)
55         window.treeview_notes.append_column(col2)
56
57     def update(self, window):
58         model=window.treeview_notes.get_model()
59         iter=model.get_iter_first()
60         while iter is not None:
61             id=int(model.get_value(iter, 0))
62             title=model.get_value(iter, 1)
63             date=model.get_value(iter, 2)
64             text=model.get_value(iter, 3)
65             check_title=model.get_value(iter, 4)
66             check_text=model.get_value(iter, 5)
67             if id==0:
68                 # insert NEW item
69                 result=window.bp.notes.create(window.page_id, title, text)
70                 id=result[0]
71                 timestamp=result[2]
72                 model.set_value(iter, 0, id)
73                 model.set_value(iter, 2, timestamp)
74                 # TODO: how to make the time stamp appear immediately after saving?
75             # only update if the items were actually changed (check against the
76             # two extra columns
77             if (title != check_title or text != check_text):
78                 window.bp.notes.update(window.page_id, id, title, text)
79                 # TODO: Set the value of check text the in the treemodel
80                 # to the new text
81             iter=model.iter_next(iter)
82        
83     def delete(self, window, note_id):
84         window.bp.notes.destroy(window.page_id, note_id)
85         return
86
87     def create(self, window):
88         # add item to treeview
89         view=window.treeview_notes
90         model=view.get_model()
91         iter=model.append((0, '', '', '', '', '', ''))
92         # grab focus on new item for editing
93         path=model.get_path(iter)
94         col=view.get_column(0)
95         view.grab_focus()
96         view.set_cursor_on_cell(path, col, None, True)
97         return
98
99     def on_treeview_notes_col1_edited( self, cell, path, new_text, model ):
100         """
101         Called when a text cell is edited.  It puts the new text
102         in the model so that it is displayed properly.       
103         """
104         # From the PyGTK reference manual, re. the "edited" gtk.CellRendererText signal:
105         # def callback(cellrenderertext, path, new_text, user_param1, ...)
106        
107         #print "Change '%s' to '%s'" % (model[path][0], new_text)
108         model[path][1] = new_text
109         return
110
111     def on_textview_key_press_event(self, textview, event, window):
112         """
113         Called when a key is pressed inside the notes text area, to
114         update the TreeView without losing text when the user
115         navigates away.  Horrible overhead(?), but how else to do it?
116         """
117         new_text=getTvText(textview)
118         selection=window.treeview_notes.get_selection()
119         (tmp_model, tmp_iter) = selection.get_selected()
120         note_text=tmp_model.get_value(tmp_iter, 3)
121         cur_path=window.treeview_notes.get_cursor()
122         path=self.preedit_path
123         #print ">>>" + str(self.preedit_path)
124         tmp_model[path][3]=new_text
125         return
126
127     def on_textview_notes_focus_in_event(self, textview, event, window):
128         """
129         Called when the body of a note is entered for editing
130         Sets self.preedit to the index of the current row so that
131         when the user loses focus (after editing something), the
132         correct record gets updated with the new text (in focus_out)
133         """
134         selection=window.treeview_notes.get_selection()
135         valid=selection.get_selected()
136         # make sure the user actually selected something
137         if valid[1]:
138             (tmp_model, tmp_iter) = selection.get_selected()
139             note_text=tmp_model.get_value(tmp_iter, 3)
140             cur_path=window.treeview_notes.get_cursor()
141             self.preedit_path=cur_path[0][0]
142         else:
143             # otherwise select first row automatically
144             window.treeview_notes.grab_focus()
145        
146     def on_textview_notes_focus_out_event(self, textview, event, window):
147         """
148         Called after the body of a note is edited (and focus lost).
149         Sets the value of the text correctly in the TreeModel
150         
151         CURRENTLY DISABLED, because if the user clicks on a new note,
152         it inserts the text of that new note instead
153         """
154         new_text=getTvText(textview)
155         selection=window.treeview_notes.get_selection()
156         (tmp_model, tmp_iter) = selection.get_selected()
157         note_text=tmp_model.get_value(tmp_iter, 3)
158         cur_path=window.treeview_notes.get_cursor()
159         path=self.preedit_path
160         tmp_model[path][3]=new_text
161         return
Note: See TracBrowser for help on using the browser.