ruby-****@sourc*****
ruby-****@sourc*****
2012年 8月 23日 (木) 04:44:08 JST
------------------------- REMOTE_ADDR = 70.49.49.99 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-treev-trees ------------------------- @@ -333,8 +333,96 @@ == Multidimensional Tree Store -Recently I was inspired by a discussion on Ruby Forum to write a more elaborate Tree Store example implementing a multidimensional array with categories and subcategories, to reveal the mechanics behind it. It may not be immediately apparent how to create a deeper hierarchy of categories and subcategories. We learned so far that Gtk::TreeStore#append(nil) creates a parent row. For instance we have a table with the following data: +Now that we have learned the basics of list and tree store mechanics we can look at a very simple example in which all rows are built by the most basic tools without any automation. We also employ the cell data function to display the cell position within the hierarchy utilizing the((*path*))and((*depth*))instance methods defined in Gtk::TreeIter and Gtk::TreePath classes respectively. + +{{br}} +((*treestore-multi-pathNdepth.rb*)) + + #!/usr/bin/env ruby + require 'gtk2' + + treestore = Gtk::TreeStore.new(String, String) + + # Append a toplevel row and fill in some data + parent = treestore.append(nil) + parent[0] = "Maria" + parent[1] = "Incognito" + # Append another toplevel row and fill in some data + parent = treestore.append(nil) + parent[0] = "Tony" + parent[1] = "Zus" + # Append a second toplevel row and fill in some data + parent = treestore.append(nil) + parent[0] = "Jane" + parent[1] = "Average" + # Append a child to the second toplevel row and fill in some data + child = treestore.append(parent) + child[0] = "Janinita" + child[1] = "Average" + # Append a grandchild to the third level row and fill in some data + grandchild = treestore.append(child) + grandchild[0] = "Janinitica" + grandchild[1] = "Average" + # Append the 2nd grandchild to the third level row and fill in some data + grandchild = treestore.append(child) + grandchild[0] = "Janinitica2" + grandchild[1] = "Average" + # Append again a second toplevel row and fill in some data + parent = treestore.append(nil) + parent[0] = "Gustav" + parent[1] = "Crocky" + # Append a child to the second toplevel row and fill in some data + child = treestore.append(parent) + child[0] = "Gustlek" + child[1] = "Crocky" + # Append a child to the second toplevel row and fill in some data + child = treestore.append(parent) + child[0] = "Zyla" + child[1] = "Crocky" + + view = Gtk::TreeView.new(treestore) + view.selection.mode = Gtk::SELECTION_NONE + + # Create a renderer + renderer = Gtk::CellRendererText.new + # Add column using our renderer + col = Gtk::TreeViewColumn.new("First Name", renderer, :text => 0) + view.append_column(col) + + # Create another renderer and set the weight property + renderer = Gtk::CellRendererText.new + # Add column using the second renderer + col = Gtk::TreeViewColumn.new("Last Name", renderer, :text => 1) + view.append_column(col) + + # - add another (virtual) VIEW column to TreeView + # Create one last renderer and set its display value to row's path + renderer = Gtk::CellRendererText.new + # Add column using the third renderer + col = Gtk::TreeViewColumn.new("Depth & Path", renderer) + view.append_column(col) + + # Create a cell data function to display row path and depth. + col.set_cell_data_func(renderer) do |col, renderer, model, iter| + renderer.text = "D=#{iter.path.depth}\tP=#{iter.path.to_str}" + end + + window = Gtk::Window.new("Multilevel Tree View") + window.signal_connect("destroy") { Gtk.main_quit } + window.add(view) + window.show_all + Gtk.main + + + +== Tedious Job of Loading Multidimensional Tree Store + + +The Gtk::TreeView and models should allow us to manage and maintain deeply nested tree structures automatically. But there are times when the intent of a tree view is only to give us a visual representation of some fixed or permanent tree layout. In that case we have to load the data at the program start up time. + +Loading initial values into a tree store may become a daunting task for large tree hierarchies. We usually can help ourselves with some kind of organized structure, most likely a nicely laid array. Following is such an example. For instance we can design a table where the "INDEX" and "PARENT" columns define the hierarchy: + [ INDEX ] [ PARENT ] [ DATA ] 0, 0, "food" >> ROOT Category 1, 0, "cheeses" >> Sub-Category @@ -347,8 +435,97 @@ 8, 4, "alcoholic" >> Sub-Category 9, 8, "wine" >> Item 1 10, 8, "brandy" >> Item 2 + +Now we may have the following questions! Are the Sub-Categories also created by passing argument nil to the append method? Is there a requirement that the rows be ordered in a certain way. Can we append to the above table another food category for instance bread, after the non-food categories? How are the rows categories, subcategories and individual items related? -are the Sub-Categories also created by passing argument nil to the append method? Is there a requirement that the rows be ordered in a certain way. Can we append to the above table another food category for instance bread, after the non-food categories? How are the rows categories, subcategories and individual items related? {{image_right("treestore-multi-dim.png")}}