Merge lp://qastaging/~rick-rickspencer3/desktopcouch/couchwidget into lp://qastaging/desktopcouch

Proposed by Rick Spencer
Status: Merged
Approved by: Elliot Murphy
Approved revision: 17
Merged at revision: not available
Proposed branch: lp://qastaging/~rick-rickspencer3/desktopcouch/couchwidget
Merge into: lp://qastaging/desktopcouch
Diff against target: None lines
To merge this branch: bzr merge lp://qastaging/~rick-rickspencer3/desktopcouch/couchwidget
Reviewer Review Type Date Requested Status
Elliot Murphy (community) Approve
dobey (community) Approve
Review via email: mp+9255@code.qastaging.launchpad.net

Commit message

Improvements to the CouchWidget:
Added exception handling to test app. Also made test methods private to not expose them in the api.
Made selected_record_ids setable
Added convenience properties and updated test method. Properties are still only read only.

To post a comment you must log in.
Revision history for this message
dobey (dobey) :
review: Approve
Revision history for this message
Elliot Murphy (statik) wrote :

looks good, lets get this merged.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'desktopcouch/records/couchwidget.py'
2--- desktopcouch/records/couchwidget.py 2009-07-15 19:05:43 +0000
3+++ desktopcouch/records/couchwidget.py 2009-07-24 16:54:41 +0000
4@@ -45,6 +45,7 @@
5 self.__record_type = None
6 self.__headings = None
7 self.__editable = False
8+ self.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
9
10 @property
11 def headings(self):
12@@ -210,6 +211,96 @@
13 #add it to the list store
14 self.list_store.append(row)
15
16+ @property
17+ def selected_rows(self):
18+ """ selected_rows - returns a list of dictionaries
19+ for each row selected. Note that the values are not
20+ necessarily the complete dictionary for the records, but rather
21+ are determined by the headings used for the CouchWidget.
22+
23+ To get the complete record, use selected_records, or for the record
24+ ids, use selected_record_ids
25+
26+ This property is read only.
27+ """
28+
29+ #get the selected rows in the ListStore
30+ selection = self.get_selection()
31+ model, model_rows = selection.get_selected_rows()
32+
33+ rows = [] #list of rows to return
34+ for mr in model_rows:
35+ row = {} #a row to be added to the list of rows
36+ iter = model.get_iter(mr)
37+
38+ #get the value for each heading and add it to the row
39+ for i, h in enumerate(self.headings):
40+ row[h] = model.get_value(iter,i)
41+
42+ #add the row to the list
43+ rows.append(row)
44+ return rows
45+
46+ @property
47+ def selected_record_ids(self):
48+ """ selected_record_ids - a list of document ids that are
49+ selected in the CouchWidget. Throws an IndexError if
50+ a specified id is not found in the list when setting
51+ this property.
52+
53+ This property is read/write
54+
55+ """
56+
57+ #get the selected rows in the ListStore
58+ selection = self.get_selection()
59+ model, model_rows = selection.get_selected_rows()
60+
61+ ids = [] # a list of ids to return
62+ for mr in model_rows:
63+ iter = model.get_iter(mr)
64+
65+ #add the id to the list
66+ id_index = len(self.headings) #id is always last column
67+ ids.append(model.get_value(iter,id_index))
68+ return ids
69+
70+
71+ @selected_record_ids.setter
72+ def selected_record_ids(self, indexes):
73+ rows = [] #a list of rows to select
74+ for id in indexes:
75+ id_found = False #track if the id was found
76+
77+ for i,r in enumerate(self.list_store):
78+ id_index = len(self.headings) #id is always last column
79+ if r[id_index] == id: #id matched in ListModel
80+ id_found = True #id was good
81+ if r not in rows: #don't have duplicates to select
82+ rows.append(i)
83+ if not id_found: #stop if a requested id was not in the list
84+ raise IndexError("id %s not found" %id)
85+
86+ #select the requested ids
87+ selection = self.get_selection()
88+ selection.unselect_all()
89+ for r in rows:
90+ selection.select_path(r)
91+
92+ @property
93+ def selected_records(self):
94+ """ selected_records - returns a list of Record objects
95+ for those selected in the CouchWidget.
96+
97+ This property is read only.
98+
99+ """
100+ recs = [] #a list of records to return
101+ for id in self.selected_record_ids:
102+ #retrieve a record for each id
103+ recs.append(Record(record_id = id, record_type = self.record_type))
104+ return recs
105+
106 def __reset_model(self):
107 """ __reset_model - internal funciton, do not call directly.
108 This function is typically called when properties are set
109@@ -278,6 +369,30 @@
110 else: #it has been saved
111 self.__db.update_fields(id,{key:new_text})
112
113+def __show_selected(widget, widgets):
114+ """Test function for selection properties of CouchWidget"""
115+ tv, cw = widgets
116+ disp = "Rows:\n"
117+ for r in cw.selected_rows:
118+ disp += str(r) + "\n"
119+
120+ disp += "\n\n_Ids:\n"
121+ for r in cw.selected_record_ids:
122+ disp += str(r) + "\n"
123+
124+ disp += "\n\nRecords:\n"
125+ for r in cw.selected_records:
126+ disp += str(r) + "\n"
127+
128+ tv.get_buffer().set_text(disp)
129+
130+def __select_ids(widget, widgets):
131+ entry, cw, lbl = widgets
132+ try:
133+ cw.selected_record_ids = entry.get_text().split(",")
134+ except Exception, inst:
135+ lbl.set_text(str(inst))
136+
137 if __name__ == "__main__":
138 """creates a test CouchWidget if called directly"""
139
140@@ -287,6 +402,11 @@
141 win.connect("destroy",gtk.main_quit)
142 win.show()
143
144+ #create a top level container
145+ vbox = gtk.VBox(False, False)
146+ vbox.show()
147+ win.add(vbox)
148+
149 #create a test widget with test database values
150 cw = CouchWidget()
151 cw.database = "couch_widget_test"
152@@ -313,6 +433,40 @@
153
154 #show the control, add it to the window, and run the main loop
155 cw.show()
156- win.add(cw)
157+ vbox.pack_start(cw, False, True)
158+
159+ #create a test display area
160+ hbox = gtk.HBox(False, 5)
161+ hbox.show()
162+ tv = gtk.TextView()
163+ tv.show()
164+ cw.connect("cursor-changed",__show_selected, (tv,cw))
165+
166+
167+ #create ui for testing selection
168+ id_vbox = gtk.VBox(False, 5)
169+ id_vbox.show()
170+
171+
172+ fb_lbl = gtk.Label("paste ids into the edit box to select them")
173+ fb_lbl.show()
174+
175+ entry = gtk.Entry()
176+ entry.show()
177+
178+ btn = gtk.Button("select ids")
179+ btn.show()
180+ btn.connect("clicked", __select_ids, (entry,cw, fb_lbl))
181+
182+ id_vbox.pack_start(fb_lbl, False, False)
183+ id_vbox.pack_start(entry, False, False)
184+ id_vbox.pack_end(btn, False, False)
185+
186+ #pack up the window
187+ hbox.pack_start(tv, False, False)
188+ vbox.pack_end(hbox, False, False)
189+ hbox.pack_end(id_vbox, False, False)
190+
191+ #run the test app
192 gtk.main()
193

Subscribers

People subscribed via source and target branches