Class: JLDrill::Gtk::WordTable

Inherits:
Gtk::ScrolledWindow
  • Object
show all
Defined in:
lib/jldrill/views/gtk/widgets/WordTable.rb

Direct Known Subclasses

ItemTable, SearchTable

Instance Method Summary collapse

Constructor Details

#initialize(itemList, itemType, &selectAction) ⇒ WordTable

Returns a new instance of WordTable.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 7

def initialize(itemList, itemType, &selectAction)
    super()
    @headings = itemType.const_get(:Headings)
    @selectAction = selectAction
    self.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)
    self.shadow_type = Gtk::SHADOW_IN
    set_size_request(450, 200)

    @listStore = createListStore(itemList)

    @table = Gtk::TreeView.new(@listStore)
    @table.selection.mode = Gtk::SELECTION_SINGLE
    @table.set_rules_hint(true)
    @table.fixed_height_mode = true
    # Initially don't allow searching
    stopSearching
    @table.set_search_equal_func do |model, column, key, iter|
        searchEqual(model, column, key, iter)
    end

    if !itemList.empty?
        attachColumns
    end

    self.add(@table)
    setupSelection
    @mark = nil
end

Instance Method Details

#addItem(item) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 171

def addItem(item)
    # For now we are just going to append the item to the table
    if !item.nil?
        iter = @listStore.append
        setItem(iter, item)
        selectPath(iter.path)
    end
end

#attachColumnsObject



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 88

def attachColumns
    i = 1
    @headings.each do |heading|
        renderer = Gtk::CellRendererText.new
        col = Gtk::TreeViewColumn.new(heading[1], renderer, :text => i)
        col.resizable = true
        col.sizing = Gtk::TreeViewColumn::FIXED
        col.fixed_width = heading[2]
        @table.append_column(col)
        i += 1
    end
end

#callActionOnActivationObject

Call the selectAction block when the row is activated



113
114
115
116
117
118
119
120
121
122
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 113

def callActionOnActivation
    @table.signal_connect('row-activated') do |widget, path, column|
        if iter = @listStore.get_iter(path)
            widget.set_cursor(path,nil,false)
            if !@selectAction.nil?
                @selectAction.call(iter[0])
            end
        end
    end
end

#createListStore(itemList) ⇒ Object

Create the ListStore for the table based on the headings in the item type of the first item.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 67

def createListStore(itemList)
    columnData = [JLDrill::Item]

    if !itemList.empty?
        0.upto(@headings.size) do
            columnData.push(String)
        end
    end

    listStore = eval "Gtk::ListStore.new(#{columnData.join(", ")})"

    if !itemList.empty?
        itemList.each do |item|
            iter = listStore.append
            setItem(iter, item)
        end
    end

    return listStore
end

#focusTableObject

Put focus on the table



255
256
257
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 255

def focusTable
    @table.grab_focus
end

#getContents(item) ⇒ Object

Returns the contents of the item Please redefine in the concrete class



44
45
46
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 44

def getContents(item)
    return nil
end

#getContentsAsVocab(item) ⇒ Object

Transforms the item to a Vocabulary Please redefine in the concrete class



50
51
52
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 50

def getContentsAsVocab(item)
    return nil
end

#getNextIterObject



216
217
218
219
220
221
222
223
224
225
226
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 216

def getNextIter
    retVal = nil
    iter = @table.selection.selected
    if !iter.nil?
        prevPath = iter.path
        if prevPath.next!
            retVal = @listStore.get_iter(prevPath)
        end
    end
    return retVal            
end

#getPreviousIterObject

Gets the iter to the row before the selected one.



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 204

def getPreviousIter
    retVal = nil
    iter = @table.selection.selected
    if !iter.nil?
        prevPath = iter.path
        if prevPath.prev!
            retVal = @listStore.get_iter(prevPath)
        end
    end
    return retVal
end

#getSelectedItemObject



195
196
197
198
199
200
201
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 195

def getSelectedItem
    retVal = nil
    if hasSelection?
        retVal = @table.selection.selected[0]
    end
    return retVal
end

#hasSelection?Boolean

Returns true if an item in the table is selected

Returns:

  • (Boolean)


191
192
193
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 191

def hasSelection?
    !@table.selection.selected.nil?
end

#highlightOnSelectionObject

Highlight the row when it is selected (by clicking on it or by moving the cursor with the arrow keys)



103
104
105
106
107
108
109
110
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 103

def highlightOnSelection
    select = @table.selection
    select.set_select_function do |selection, model, path, 
                                   currently_selected|
        # allow selection state to change
        true
    end
end

#markClearObject



292
293
294
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 292

def markClear
    @mark = nil
end

#markCutObject



281
282
283
284
285
286
287
288
289
290
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 281

def markCut
    if @mark == @table.selection.selected
        # Allow the user to clear the mark by cutting on the
        # same item.  Non-standard, but it's the best I can think
        # of right now
        markClear
    else
        @mark = @table.selection.selected
    end
end

#moveDownObject



241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 241

def moveDown
    if hasSelection?
        iter = @table.selection.selected
        nextIter = getNextIter
        if !iter.nil? && !nextIter.nil? &&
                !iter[0].nil? && !nextIter[0].nil?
            iter[0].swapWith(nextIter[0])
            @listStore.move_after(iter, nextIter)
            selectPath(iter.path)
        end
    end
end

#moveUpObject



228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 228

def moveUp
    if hasSelection?
        iter = @table.selection.selected
        prevIter = getPreviousIter
        if !iter.nil? && !prevIter.nil? &&
                !iter[0].nil? && !prevIter[0].nil?
            iter[0].swapWith(prevIter[0])
            @listStore.move_before(iter, prevIter)
            selectPath(iter.path)
        end
    end
end

#pasteBeforeObject



296
297
298
299
300
301
302
303
304
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 296

def pasteBefore
    target = @table.selection.selected
    if !@mark.nil? && !target.nil?
        @mark[0].insertBefore(target[0])
        @listStore.move_before(@mark, target)
        selectPath(@mark.path)
    end
    markClear
end

#removeItem(item) ⇒ Object



180
181
182
183
184
185
186
187
188
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 180

def removeItem(item)
    if !item.nil? && (item.position != -1)
        path = Gtk::TreePath.new(item.position.to_s)
        iter = @listStore.get_iter(path)
        if !iter.nil?
            @listStore.remove(iter)
        end
    end
end

#searchObject



263
264
265
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 263

def search
    @table.set_enable_search(true)
end

#searchEqual(model, column, key, iter) ⇒ Object

Returns true if the item should be selected when searching Please redefine in the concrete class



38
39
40
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 38

def searchEqual(model, column, key, iter)
    return false
end

#searching?Boolean

Returns:

  • (Boolean)


259
260
261
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 259

def searching?
    @table.enable_search?
end

#selectClosestMatch(vocab) ⇒ Object

Selects the closest match to the given vocabulary



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 137

def selectClosestMatch(vocab)
    iter = @listStore.iter_first
    if !iter.nil?
        pos = iter.path
        rank = vocab.rank(getContentsAsVocab(iter[0]))
        while iter.next!
            newRank = vocab.rank(getContentsAsVocab(iter[0]))
            if newRank > rank
                rank = newRank
                pos = iter.path
            end
        end
        selectPath(pos)
    end
end

#selectItem(item) ⇒ Object

Selects the row with the given item if it exists



154
155
156
157
158
159
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 154

def selectItem(item)
    if !item.nil? && (item.position != -1)
        path = Gtk::TreePath.new(item.position.to_s)
        selectPath(path)
    end
end

#selectPath(path) ⇒ Object

Select the item in the TreePath and scroll to the cell



132
133
134
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 132

def selectPath(path)
    @table.set_cursor(path, nil, false)
end

#setItem(iter, item) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 54

def setItem(iter, item)
    # column 0 isn't rendered.  It's just there for selection
    iter[0] = item
    content = getContents(item) 
    i = 1
    @headings.each do |heading|
        iter[i] = eval("content.#{heading[0]}")
        i += 1
    end
end

#setupSelectionObject

This method sets up the manor in which items are selected in the table.



126
127
128
129
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 126

def setupSelection
    highlightOnSelection
    callActionOnActivation
end

#stopSearchingObject



267
268
269
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 267

def stopSearching
    @table.set_enable_search(false)
end

#toggleSearchObject



271
272
273
274
275
276
277
278
279
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 271

def toggleSearch
    if !@vocabTable.nil?
        if !searching
            @vocabTable.search
        else
            @vocabTable.stopSearching
        end
    end
end

#updateItem(item) ⇒ Object

Updates the item in the tree and selects the row



162
163
164
165
166
167
168
169
# File 'lib/jldrill/views/gtk/widgets/WordTable.rb', line 162

def updateItem(item)
    if !item.nil? && (item.position != -1)
        path = Gtk::TreePath.new(item.position.to_s)
        iter = @listStore.get_iter(path)
        setItem(iter, item)
        selectPath(path)
    end
end