Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
lib/jldrill/views/gtk/widgets/WordTable.rb | 307 | 240 | 27.36%
|
13.75%
|
Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.
1 require 'gtk2' |
2 |
3 module JLDrill::Gtk |
4 class WordTable < Gtk::ScrolledWindow |
5 |
6 def initialize(itemList, itemType, &selectAction) |
7 super() |
8 @itemType = itemType |
9 @headings = @itemType.headings |
10 @selectAction = selectAction |
11 self.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC) |
12 self.shadow_type = Gtk::SHADOW_IN |
13 set_size_request(450, 200) |
14 |
15 @listStore = createListStore(itemList) |
16 |
17 @table = Gtk::TreeView.new(@listStore) |
18 @table.selection.mode = Gtk::SELECTION_SINGLE |
19 @table.set_rules_hint(true) |
20 @table.fixed_height_mode = true |
21 # Initially don't allow searching |
22 stopSearching |
23 @table.set_search_equal_func do |model, column, key, iter| |
24 searchEqual(model, column, key, iter) |
25 end |
26 |
27 if !itemList.empty? |
28 attachColumns |
29 end |
30 |
31 self.add(@table) |
32 setupSelection |
33 @mark = nil |
34 end |
35 |
36 # Returns true if the item should be selected when searching |
37 # Please redefine in the concrete class |
38 def searchEqual(model, column, key, iter) |
39 return false |
40 end |
41 |
42 # Returns the contents of the item |
43 # Please redefine in the concrete class |
44 def getContents(item) |
45 return nil |
46 end |
47 |
48 # Transforms the item to a Vocabulary |
49 # Please redefine in the concrete class |
50 def getContentsAsVocab(item) |
51 return nil |
52 end |
53 |
54 def setItem(iter, item) |
55 # column 0 isn't rendered. It's just there for selection |
56 iter[0] = item |
57 content = getContents(item) |
58 i = 1 |
59 @headings.each do |heading| |
60 iter[i] = eval("content.#{heading[0]}") |
61 i += 1 |
62 end |
63 end |
64 |
65 # Create the ListStore for the table based on the headings in |
66 # the item type of the first item. |
67 def createListStore(itemList) |
68 columnData = [JLDrill::Item] |
69 |
70 if !itemList.empty? |
71 0.upto(@headings.size) do |
72 columnData.push(String) |
73 end |
74 end |
75 |
76 listStore = eval "Gtk::ListStore.new(#{columnData.join(", ")})" |
77 |
78 if !itemList.empty? |
79 itemList.each do |item| |
80 iter = listStore.append |
81 setItem(iter, item) |
82 end |
83 end |
84 |
85 return listStore |
86 end |
87 |
88 def attachColumns |
89 i = 1 |
90 @headings.each do |heading| |
91 renderer = Gtk::CellRendererText.new |
92 col = Gtk::TreeViewColumn.new(heading[1], renderer, :text => i) |
93 col.resizable = true |
94 col.sizing = Gtk::TreeViewColumn::FIXED |
95 col.fixed_width = heading[2] |
96 @table.append_column(col) |
97 i += 1 |
98 end |
99 end |
100 |
101 # Highlight the row when it is selected (by clicking on it or |
102 # by moving the cursor with the arrow keys) |
103 def highlightOnSelection |
104 select = @table.selection |
105 select.set_select_function do |selection, model, path, |
106 currently_selected| |
107 # allow selection state to change |
108 true |
109 end |
110 end |
111 |
112 # Call the selectAction block when the row is activated |
113 def callActionOnActivation |
114 @table.signal_connect('row-activated') do |widget, path, column| |
115 if iter = @listStore.get_iter(path) |
116 widget.set_cursor(path,nil,false) |
117 if !@selectAction.nil? |
118 @selectAction.call(iter[0]) |
119 end |
120 end |
121 end |
122 end |
123 |
124 # This method sets up the manor in which items are selected |
125 # in the table. |
126 def setupSelection |
127 highlightOnSelection |
128 callActionOnActivation |
129 end |
130 |
131 # Select the item in the TreePath and scroll to the cell |
132 def selectPath(path) |
133 @table.set_cursor(path, nil, false) |
134 end |
135 |
136 # Selects the closest match to the given vocabulary |
137 def selectClosestMatch(vocab) |
138 iter = @listStore.iter_first |
139 if !iter.nil? |
140 pos = iter.path |
141 rank = vocab.rank(getContentsAsVocab(iter[0])) |
142 while iter.next! |
143 newRank = vocab.rank(getContentsAsVocab(iter[0])) |
144 if newRank > rank |
145 rank = newRank |
146 pos = iter.path |
147 end |
148 end |
149 selectPath(pos) |
150 end |
151 end |
152 |
153 # Selects the row with the given item if it exists |
154 def selectItem(item) |
155 if !item.nil? && (item.position != -1) |
156 path = Gtk::TreePath.new(item.position.to_s) |
157 selectPath(path) |
158 end |
159 end |
160 |
161 # Updates the item in the tree and selects the row |
162 def updateItem(item) |
163 if !item.nil? && (item.position != -1) |
164 path = Gtk::TreePath.new(item.position.to_s) |
165 iter = @listStore.get_iter(path) |
166 setItem(iter, item) |
167 selectPath(path) |
168 end |
169 end |
170 |
171 def addItem(item) |
172 # For now we are just going to append the item to the table |
173 if !item.nil? |
174 iter = @listStore.append |
175 setItem(iter, item) |
176 selectPath(iter.path) |
177 end |
178 end |
179 |
180 def removeItem(item) |
181 if !item.nil? && (item.position != -1) |
182 path = Gtk::TreePath.new(item.position.to_s) |
183 iter = @listStore.get_iter(path) |
184 if !iter.nil? |
185 @listStore.remove(iter) |
186 end |
187 end |
188 end |
189 |
190 # Returns true if an item in the table is selected |
191 def hasSelection? |
192 !@table.selection.selected.nil? |
193 end |
194 |
195 def getSelectedItem |
196 retVal = nil |
197 if hasSelection? |
198 retVal = @table.selection.selected[0] |
199 end |
200 return retVal |
201 end |
202 |
203 # Gets the iter to the row before the selected one. |
204 def getPreviousIter |
205 retVal = nil |
206 iter = @table.selection.selected |
207 if !iter.nil? |
208 prevPath = iter.path |
209 if prevPath.prev! |
210 retVal = @listStore.get_iter(prevPath) |
211 end |
212 end |
213 return retVal |
214 end |
215 |
216 def getNextIter |
217 retVal = nil |
218 iter = @table.selection.selected |
219 if !iter.nil? |
220 prevPath = iter.path |
221 if prevPath.next! |
222 retVal = @listStore.get_iter(prevPath) |
223 end |
224 end |
225 return retVal |
226 end |
227 |
228 def moveUp |
229 if hasSelection? |
230 iter = @table.selection.selected |
231 prevIter = getPreviousIter |
232 if !iter.nil? && !prevIter.nil? && |
233 !iter[0].nil? && !prevIter[0].nil? |
234 iter[0].swapWith(prevIter[0]) |
235 @listStore.move_before(iter, prevIter) |
236 selectPath(iter.path) |
237 end |
238 end |
239 end |
240 |
241 def moveDown |
242 if hasSelection? |
243 iter = @table.selection.selected |
244 nextIter = getNextIter |
245 if !iter.nil? && !nextIter.nil? && |
246 !iter[0].nil? && !nextIter[0].nil? |
247 iter[0].swapWith(nextIter[0]) |
248 @listStore.move_after(iter, nextIter) |
249 selectPath(iter.path) |
250 end |
251 end |
252 end |
253 |
254 # Put focus on the table |
255 def focusTable |
256 @table.grab_focus |
257 end |
258 |
259 def searching? |
260 @table.enable_search? |
261 end |
262 |
263 def search |
264 @table.set_enable_search(true) |
265 end |
266 |
267 def stopSearching |
268 @table.set_enable_search(false) |
269 end |
270 |
271 def toggleSearch |
272 if !@vocabTable.nil? |
273 if !searching |
274 @vocabTable.search |
275 else |
276 @vocabTable.stopSearching |
277 end |
278 end |
279 end |
280 |
281 def markCut |
282 if @mark == @table.selection.selected |
283 # Allow the user to clear the mark by cutting on the |
284 # same item. Non-standard, but it's the best I can think |
285 # of right now |
286 markClear |
287 else |
288 @mark = @table.selection.selected |
289 end |
290 end |
291 |
292 def markClear |
293 @mark = nil |
294 end |
295 |
296 def pasteBefore |
297 target = @table.selection.selected |
298 if !@mark.nil? && !target.nil? |
299 @mark[0].insertBefore(target[0]) |
300 @listStore.move_before(@mark, target) |
301 selectPath(@mark.path) |
302 end |
303 markClear |
304 end |
305 |
306 end |
307 end |
Generated on Mon May 23 16:17:47 +0900 2011 with rcov 0.9.8