Class: Gtk3assist::Treeview

Inherits:
Object
  • Object
show all
Defined in:
lib/gtk3assist_treeview.rb

Overview

This class contains various code that can greatly speed up the building and handeling of treeviews.

Constant Summary collapse

ALLOWED_ARGS =

An array of allowed arguments for the ‘initialize’-method.

[:tv, :cols, :model, :sort_col]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Treeview

Constructor.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gtk3assist_treeview.rb', line 13

def initialize(args)
  raise "'args' was not a hash." if !args.is_a?(Hash)
  args.each do |key, val|
    raise "Invalid argument: '#{key}'." if !ALLOWED_ARGS.include?(key)
  end
  
  @columns = []
  @column_count = 0
  @tv = args[:tv]
  raise "No ':tv' argument was given." if !@tv.is_a?(Gtk::TreeView)
  
  if args[:cols]
    args[:cols].each do |val|
      self.add_column(val)
    end
  end
  
  if args[:model]
    if args[:model] == :liststore
      self.init_liststore
    else
      raise "Unknown model: '#{args[:model]}'."
    end
  end
  
  self.sort_col = args[:sort_col] if args.key?(:sort_col)
end

Instance Attribute Details

#modelObject (readonly)

The model-object that is used by the treeview.



7
8
9
# File 'lib/gtk3assist_treeview.rb', line 7

def model
  @model
end

#tvObject (readonly)

The treeview that this object manipulates.



4
5
6
# File 'lib/gtk3assist_treeview.rb', line 4

def tv
  @tv
end

Instance Method Details

#add_column(args) ⇒ Object

Adds a new column to the treeview.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/gtk3assist_treeview.rb', line 71

def add_column(args)
  if args.is_a?(Hash)
    #ignore.
  elsif args.is_a?(String)
    args = {:type => :string, :title => args, :id => args.to_s.downcase.to_sym}
  else
    raise "Unknown argument given: '#{argument.class.name}'."
  end
  
  renderer = Gtk::CellRendererText.new
  lab = Gtk::Label.new(args[:title])
  
  col = Gtk::TreeViewColumn.new
  col.set_widget(lab)
  col.pack_start(renderer, true)
  col.add_attribute(renderer, "text", @columns.length)
  
  lab.show
  @tv.append_column(col)
  count = @column_count
  
  @columns << {
    :col => col,
    :lab => lab,
    :id => args[:id],
    :type => args[:type]
  }
end

#add_row(args) ⇒ Object

Add a new row to the treeview.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/gtk3assist_treeview.rb', line 101

def add_row(args)
  raise "''args' wasnt a hash." if !args.is_a?(Hash)
  raise "No ':data'-array was given." if !args[:data]
  
  if @model.is_a?(Gtk::ListStore)
    data = []
    @columns.each do |col_data|
      found = false
      args[:data].each do |key, val|
        if key == col_data[:id]
          data << val
          found = true
          break
        end
      end
      
      raise "Not found: '#{col_data[:id]}' (#{col_data})." if !found
    end
    
    iter = @model.append
    count = 0
    data.each do |val|
      col_data = @columns[count]
      
      if col_data[:type] == :string or !col_data[:type]
        @model.set_value(iter, count, val.to_s)
      else
        raise "Unknown column-type: '#{col_data[:type]}'."
      end
      
      count += 1
    end
    
    return {:iter => iter}
  else
    raise "Unknown model: '#{@model.class.name}'."
  end
end

#init_liststoreObject

Initializes a new list-store on the treeview.



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gtk3assist_treeview.rb', line 42

def init_liststore
  liststore_args = []
  @columns.each do |col_data|
    if col_data[:type] == :string or !col_data[:type]
      liststore_args << GObject::TYPE_STRING
    else
      raise "Unknown column-type: '#{col_data[:type]}'."
    end
  end
  
  @model = Gtk::ListStore.new(liststore_args)
  @tv.set_model(@model)
end

#rows(args = nil, &block) ⇒ Object

Enumerates over every row in the treeview.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/gtk3assist_treeview.rb', line 168

def rows(args = nil, &block)
  enum = Enumerator.new do |y|
    iter_cur = @model.iter_first.last
    
    while iter_cur
      match = true
      sel_val = @tv.get_selection.iter_is_selected(iter_cur) rescue false
      match = false if args and args[:selected] and !sel_val
      
      if match
        data = {}
        count = 0
        
        @columns.each do |col_data|
          if col_data[:type] == :string or !col_data[:type]
            data[col_data[:id]] = @model.get_value(iter_cur, count).get_string
          else
            raise "Unknown column-type: '#{col_data[:type]}'."
          end
          
          count += 1
        end
        
        y << {
          :sel => sel_val,
          :data => data,
          :iter => iter_cur
        }
      end
      
      break if !@model.iter_next(iter_cur)
    end
  end
  
  if block
    enum.each(&block)
    return nil
  else
    return enum
  end
end

#rows_remove(args = nil) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/gtk3assist_treeview.rb', line 140

def rows_remove(args = nil)
  #Containing the ID's that should be removed (minus length to account for cursor changes).
  removes = []
  
  #Avoid counting length of 'removes' all the time.
  removes_count = 0
  
  #Calculate which rows should be removed by yield'ing and checking for true. Then adding ID (minus remove-count) to 'removes'-array.
  self.rows(args) do |data|
    res = yield(data)
    
    if res == true
      removes << @model.path(data[:iter]).to_string.to_i - removes_count
      removes_count += 1
    end
  end
  
  #Remove rows by their IDs (minus removes-count).
  removes.each do |id|
    path = Gtk::TreePath.new_from_string(id.to_s)
    iter = @model.iter(path).last
    @model.remove(iter)
  end
  
  return nil
end

#selObject

Returns the first selected row found.



211
212
213
214
215
216
217
# File 'lib/gtk3assist_treeview.rb', line 211

def sel
  self.rows(:selected => true).each do |data|
    return data if data[:sel]
  end
  
  return nil
end

#sort_col=(col_id) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gtk3assist_treeview.rb', line 56

def sort_col=(col_id)
  count = 0
  @columns.each do |col_data|
    if col_data[:id] == col_id
      @model.set_sort_column_id(count, Gtk::SortType[:ascending])
      return nil
    end
    
    count += 1
  end
  
  raise "Could not find a column by that ID: '#{col_id}'."
end