Class: VR::ListView

Inherits:
Gtk::TreeView
  • Object
show all
Includes:
GladeGUI, ViewCommon
Defined in:
lib/treeview/ListView.rb

Instance Attribute Summary

Attributes included from ViewCommon

#column_keys, #vr_cols, #vr_renderer

Attributes included from GladeGUI

#builder

Instance Method Summary collapse

Methods included from ViewCommon

#col_attr, #column, #delete_selected, #each_renderer, #each_row, #flatten_hash, #get_iter, #id, #load_columns, #method_missing, #ren_attr, #renderer, #selected_rows, #turn_on_comboboxes, #vr_row

Methods included from GladeGUI

#buttonCancel__clicked, #extract_key, #fill_control, #get_control_value, #get_glade_active_record, #get_glade_all, #get_glade_variables, included, #load_glade, #parse_signals, #set_drag_drop, #set_glade_active_record, #set_glade_all, #set_glade_hash, #set_glade_variables, #show_glade, #try_to_select_text_in_combobox, #window1__destroy, #window1__key_press_event

Constructor Details

#initialize(cls) ⇒ ListView

The new() constructor takes a Hash that defines the columns as its only argument. The Hash defines symbols as the keys to give an ID to each column. The Hash also defines the type (class) of the column. A simple constructor looks like this: You can create columns with any type of data including your own classes, and classes subclassed from ActiveRecordBase. The common types that are included by default are:

  • String - Displays and edits as a String.

  • Integer - Displays numadd_activeber, edits like a String.

  • FixNum - ditto

  • Integer - ditto

  • Float - ditto

  • DateTime - Displays in a default date format(editable), edits like a String

  • TrueClass - Displays as a GtkCheckButton, click checkbox to edit

  • GdkPixbuf - Just an Image, uneditable

  • VR::Col::CalendarCol - Displays in a default date format(editable), calendar window to edit.

  • VR::Col::SpinCol - Displays as a number with default number of digits, edits like a GtkSpinButton

  • VR::Col::ComboCol - Displays String, edits like a GtkComboBoxEntry

  • VR::Col::ProgressCol - Displays a GtkProgressBar, uneditable

  • VR::Col::BlobCol - For long strings. Displays first 20 characters in view, edits with simple text editor.

You can also add your own user-defined column types. See: Adding Your Own Objects to ListView.

Examples:

@view = VR::ListView.new(:name => String, :date => DateTime)


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

def initialize(cls)
  super()
  if cls.respond_to?(:column_names)
    hash = {}
    cls.column_names.each { |key| hash[key.to_sym] = String }
    cols = hash
  else
    hash = flatten_hash(cls)
    cols = cls
  end
  vals = hash.values
  self.model = Gtk::ListStore.new(*vals)
  load_columns(cols)
  add_active_record_rows(cls) if cls.respond_to?(:column_names)    
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class VR::ViewCommon

Instance Method Details

#[](row, col) ⇒ Object

:nodoc:



75
76
77
# File 'lib/treeview/ListView.rb', line 75

def []( row )  # :nodoc:
  model.get_iter(Gtk::TreePath.new("#{row}"))
end

#add_active_record_rows(ar) ⇒ Object

:nodoc:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/treeview/ListView.rb', line 58

def add_active_record_rows(ar) # :nodoc:
  fields = ar.column_names.map { |x| x.to_sym }
  matches = fields & @column_keys #intersection
  ar.each do |obj|
    row = add_row()
    matches.each do |f|
#          begin  
        row[f] = obj[f] unless obj[f].nil?
#          rescue
#            row[f] = obj[f].to_s
#          end
    end          
  end
  self.visible = true
end

#add_row(hash = {}) ⇒ Object

This will add a row to the data model, and fill-in the values from a Hash. This example would add a row to the model and set the name and email fields:

@view.add_row(:name => “Chester”, :email => “[email protected]”)

  • hash: A ruby Hash object with pairs of column ISs (symbols) and values.



102
103
104
105
106
# File 'lib/treeview/ListView.rb', line 102

def add_row(hash = {})
  row = vr_row(model.append)
  hash.each_pair { |key, val| row[key] = val }
  return row
end

#before_showObject



10
11
12
13
# File 'lib/treeview/ListView.rb', line 10

def before_show
  @builder[:scrolledwindow1].add self
  self.visible = true
end

#select_row(row_number = 0) ⇒ Object

This method will select a given row number. The row will be hilighted, and the GtkSelection object will point to that row. It uses the GtkTreeView#set_cursor method to move the cursor to the specified row.

  • row_number: Integer (FixNum)



90
91
92
# File 'lib/treeview/ListView.rb', line 90

def select_row(row_number = 0)
  set_cursor(Gtk::TreePath.new(row_number), nil, false)
end