Class: Gtk2_treeview_settings
- Inherits:
-
Object
- Object
- Gtk2_treeview_settings
- Defined in:
- lib/gtk2_treeview_settings.rb
Overview
This class handels appending and reading data from a treeview with dynamic columns. It tracks the columns based on given symbol-IDs.
Examples
@tv_settings = Gtk2_treeview_settings.new(
:tv => @gui["tvTimelogs"],
:col_ids => {
0 => :id,
1 => :name
}
)
Instance Attribute Summary collapse
-
#tv ⇒ Object
readonly
Returns the value of attribute tv.
Instance Method Summary collapse
-
#append(data) ⇒ Object
Appends the given data to the treeview.
-
#append_adv(args) ⇒ Object
Appens the given data the the treeview in hash-form which gives the posibility for a parent element when using TreeStore.
- #cellrenderer_for_id(id) ⇒ Object
-
#col_no_for_id(id) ⇒ Object
Returns a column-number for a specific ID (given at initialize-time).
- #col_orig_no_for_id(id) ⇒ Object
-
#initialize(args) ⇒ Gtk2_treeview_settings
constructor
A new instance of Gtk2_treeview_settings.
Constructor Details
#initialize(args) ⇒ Gtk2_treeview_settings
Returns a new instance of Gtk2_treeview_settings.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/gtk2_treeview_settings.rb', line 13 def initialize(args) @args = args @tv = @args[:tv] @renderers = {} #Set the initial order of the columns so we can recognize them later. @col_ids = {} count = 0 @tv.columns.each do |col| @col_ids[count] = col.__id__ count += 1 end #Remember IDs for columns. if @args[:col_ids] @saved_ids = {} @args[:col_ids].each do |key, val| @saved_ids[val] = @tv.columns[key].__id__ end end end |
Instance Attribute Details
#tv ⇒ Object (readonly)
Returns the value of attribute tv.
11 12 13 |
# File 'lib/gtk2_treeview_settings.rb', line 11 def tv @tv end |
Instance Method Details
#append(data) ⇒ Object
Appends the given data to the treeview. Only works with Gtk::ListStore.
Examples
gset.append(:id => 0, :name => "Kasper") #=> {:iter => Gtk::TreeIter-object}
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/gtk2_treeview_settings.rb', line 80 def append(data) if @tv.model.is_a?(Gtk::TreeStore) iter = @tv.model.append(nil) else iter = @tv.model.append end data.each do |key, val| col_no = self.col_no_for_id(key) col_no_orig = self.col_orig_no_for_id(key) col = tv.columns[col_no] renderer = col.cell_renderers.first if renderer.is_a?(Gtk::CellRendererText) iter[col_no_orig] = val.to_s elsif renderer.is_a?(Gtk::CellRendererToggle) iter[col_no_orig] = Knj::Strings.yn_str(val, 1, 0) elsif renderer.is_a?(Gtk::CellRendererCombo) iter[col_no_orig] = val.to_s else raise "Unknown renderer: '#{renderer.class.name}'." end end return {:iter => iter} end |
#append_adv(args) ⇒ Object
Appens the given data the the treeview in hash-form which gives the posibility for a parent element when using TreeStore.
Examples
gset.append_adv(:parent => parent_iter, :data => {:id => 0, :name => "Kasper"}) #=> {:iter => Gtk::TreeIter-object}
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 |
# File 'lib/gtk2_treeview_settings.rb', line 110 def append_adv(args) if @tv.model.is_a?(Gtk::TreeStore) iter = @tv.model.append(args[:parent]) else iter = @tv.model.append end args[:data].each do |key, val| col_no = self.col_no_for_id(key) col_no_orig = self.col_orig_no_for_id(key) col = tv.columns[col_no] renderer = col.cell_renderers.first if renderer.is_a?(Gtk::CellRendererText) iter[col_no_orig] = val.to_s elsif renderer.is_a?(Gtk::CellRendererToggle) iter[col_no_orig] = Knj::Strings.yn_str(val, 1, 0) elsif renderer.is_a?(Gtk::CellRendererCombo) iter[col_no_orig] = val.to_s else raise "Unknown renderer: '#{renderer.class.name}'." end end return {:iter => iter} end |
#cellrenderer_for_id(id) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/gtk2_treeview_settings.rb', line 35 def cellrenderer_for_id(id) col_no = self.col_no_for_id(id) col = tv.columns[col_no] renderer = col.cell_renderers.first raise Errno::ENOENT, "Could not find cell-renderer for that ID: '#{id}', '#{col_no}'." if !renderer return renderer end |
#col_no_for_id(id) ⇒ Object
Returns a column-number for a specific ID (given at initialize-time).
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/gtk2_treeview_settings.rb', line 44 def col_no_for_id(id) obj_id = @saved_ids[id] raise "No column by that ID: '#{id}'." if !obj_id count = 0 @tv.columns.each do |col| return count if col.__id__ == obj_id count += 1 end raise "Could not find column by that ID: '#{id}'." end |
#col_orig_no_for_id(id) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/gtk2_treeview_settings.rb', line 57 def col_orig_no_for_id(id) col_obj_id = nil @saved_ids.each do |key, val| if key == id col_obj_id = val break end end raise "Could not find column by ID: '#{id}'." if !col_obj_id @col_ids.each do |key, val| if val == col_obj_id return key end end raise "Could not find column number by that ID: '#{id}'." end |