Class: VR::ListView

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

Direct Known Subclasses

ObjectInspector::MethodsListView, ObjectInspector::VariablesListView

Instance Attribute Summary

Attributes included from ViewCommon

#vr_renderer

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

Constructor Details

- (ListView) initialize(cls)

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 ActiveRecord::Base. The common types that are included by default are:

  • String - Displays and edits as a String.

  • Integer - Displays number, 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

  • Gdk::Pixbuf - 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 Gtk::SpinButton

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

  • VR::Col::ProgressCol - Displays a Gtk::ProgressBar, 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)


34
35
36
37
38
39
40
# File 'lib/treeview/ListView.rb', line 34

def initialize(cls)
  super()
  hash = flatten_hash(cls)
  vals = hash.values
  self.model = Gtk::ListStore.new(*vals)
  load_columns(cls)
end

Dynamic Method Handling

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

Instance Method Details

- (Object) [](row, col)

:nodoc:



50
51
52
# File 'lib/treeview/ListView.rb', line 50

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

- (Object) add_active_record_rows(ar)

:nodoc:



42
43
44
45
46
47
48
# File 'lib/treeview/ListView.rb', line 42

def add_active_record_rows(ar) # :nodoc:
  ar.each do |obj|
    row = add_row()
    matches = obj.attributes.keys & @column_keys #intersection
    matches.each { |field| row[field] = obj.attributes[field] }   
  end
end

- (Object) add_row(hash = {})

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 => “chester@chester.com”)

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



77
78
79
80
81
# File 'lib/treeview/ListView.rb', line 77

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

- (Object) select_row(row_number = 0)

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

  • row_number: Integer (FixNum)



65
66
67
# File 'lib/treeview/ListView.rb', line 65

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