[ruby-gnome2-doc-cvs] [Ruby-GNOME2 Project Website] update - tut-gtk2-treev-trees

Back to archive index

ruby-****@sourc***** ruby-****@sourc*****
2012年 9月 27日 (木) 02:56:33 JST


-------------------------
REMOTE_ADDR = 184.145.80.187
REMOTE_HOST = 
        URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-treev-trees
-------------------------
@@ -549,134 +549,30 @@
 
 == Tedious Job of Loading Multidimensional Tree Store
 
+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 out array. Following is such an example. For instance we can design a table where the hierarchy is defined as a structure of nested hierarchies of arrays. The signature of this design is the last element in each row, namely we define it either to be the array of children or nil:
 
-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.
+ init_arr = [[ e11, e12, e13, nil ], [e21, e22, e23, [[...],[...]...], ... [en1, en2, en3, nil]] 
 
-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 out array. Following is such an example. For instance we can design a table where the "INDEX" and "PARENT" columns define the hierarchy:
+Following is a more realistic example:
 
- [ INDEX ] [ PARENT ] [ DATA ]
-   0,        0,       "food"          >> ROOT Category
-   1,        0,       "cheeses"       >> Sub-Category
-   2,        1,       "cheddar"       >> Item 1
-   3,        1,       "brie"          >> Item 2
-   4,        0,       "beverages"     >> ROOT Category
-   5,        4,       "non-alcohol"   >> Sub-Category
-   6,        5,       "pepsi"         >> Item 1
-   7,        5,       "coke"          >> Item 2 
-   8,        4,       "alcoholic"     >> Sub-Category
-   9,        8,       "wine"          >> Item 1
-  10,        8,       "brandy"        >> Item 2 
-
-
-
-{{image_right("treestore-multi-dim.png")}}
-
-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? 
-
-It turns out that only top-level parents are created by the call to append with the nil argument. How is then the grouping accomplished? All these issues are addressed in the following example. Note also that we also append the bred sub-category as mentioned above - apparently out of order - i.e. after the beverages.  
-
+ INITIALIZATION_ARRAY = [
+    ['Computer A101dt',  0,  [
+        ['Keyboard',       50.0,  nil],
+        ['Mouse',          40.0,  nil],
+        ['Computer Case',  0,    [
+             ['Front 120mm Fan', 10.0,   nil],
+             ['Back  120mm Fan', 10.0,   nil],
+             ['PW/S',            270.0,  nil],
+             ] ],
+        ['Mother Board',        250.0,    nil],
+        ['CPU',                 300.0,    nil],
+        ['1 TB HD',             170.0,    nil],
+        ] ],
+    ['Swan 27" Monitor ',    210.0,  nil],
+    ['Laser Printer XL3500', 300.0,  nil],
+  ]
 
-{{br}}
-((*treestore-multi-dim.rb*))
 
- #!/usr/bin/env ruby
- require 'gtk2'
- 
- class FoodItem
-   attr_accessor :desc, :qty, :measure, :price
-   def initialize(d, q, m, pr)
-     @desc, @qty, @measure, @price = d, q, m, pr
-   end
-   DESC = 0; QTY = 1; PRC=2
- end
- 
- class MultiColTreeStore
-   attr_accessor :list, :treestore
- 
-   def initialize
-     @list = [     # description   qty  measure price     # index  parent
-       FoodItem.new("food",          0,  nil,   0.0),     #  0,    0
-       FoodItem.new("cheeses",       0,  nil,   0.0),     #  1,    0
-       FoodItem.new("cheddar",      10,  "lb",  2.5),     #  2,    1
-       FoodItem.new("brie",          5,  "lb",  5.0),     #  3,    1,     
-       FoodItem.new("beverages",     0,  nil,   0.0),     #  4,    0,     
-       FoodItem.new("non-alcohol",   0,  nil,   0.0),     #  5,    4,     
-       FoodItem.new("pepsi",        60,  "oz",  0.95),    #  6,    5,     
-       FoodItem.new("coke",         70,  "oz",  0.95),    #  7,    5,     
-       FoodItem.new("alcoholic",     0,  nil,   0.0),     #  8,    4,     
-       FoodItem.new("wine",         20,  "oz",  2.0),     #  9,    8,     
-       FoodItem.new("brandy",        7,  "oz",  3.5),     # 10,    8,     
-       FoodItem.new("breads",        0,  nil,   0.0),     # 11,    1,     
-       FoodItem.new("white grain", 700,  "g",   3.75),    # 12,   11,     
-       FoodItem.new("rye",          65,  "g",   2.5),     # 13,   11,     
-       FoodItem.new("bagel",        30,  "g",   0.5)      # 14,   11,     
-     ]
-     # Create a new tree model with three columns, as Boolean, 
-     # integer and string.
-     @treestore = Gtk::TreeStore.new(String, String, String)
- 
-     window = Gtk::Window.new(Gtk::Window::TOPLEVEL)
-     window.resizable = true
-     window.title = "Multi-dim Tree Store"
-     window.border_width = 10
-     window.signal_connect('delete_event') { Gtk.main_quit }
-     window.set_size_request(275, 300)
- 
-     treeview = Gtk::TreeView.new
-     setup_tree_view(treeview)
- 
-     # Populate trestore
-     food      = fill_row(nil,       0)
-     cheese    = fill_row(food,      1)
-     item      = fill_row(cheese,    2)
-     item      = fill_row(cheese,    3)
-     beverage  = fill_row(nil,       4)
-     nalcho    = fill_row(beverage,  5)
-     item      = fill_row(nalcho,    6)
-     item      = fill_row(nalcho,    7)
-     alcho     = fill_row(beverage,  8)
-     item      = fill_row(alcho,     9)
-     item      = fill_row(alcho,    10)
-     bread     = fill_row(food,     11)
-     item      = fill_row(bread,    12)
-     item      = fill_row(bread,    13)
-     item      = fill_row(bread,    14)
- 
-     # Add the tree model to the tree view
-     treeview.model = treestore
-     scrolled_win = Gtk::ScrolledWindow.new
-     scrolled_win.add(treeview)
-     scrolled_win.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)
-     window.add(scrolled_win)
-     window.show_all
-   end
- 
-   # Add three columns to the GtkTreeView. Both columns will 
-   # be displayed as text.
-   def setup_tree_view(treeview)
-     renderer = Gtk::CellRendererText.new
-     column = Gtk::TreeViewColumn.new("Description", renderer, "text" => FoodItem::DESC)
-     treeview.append_column(column)
-     renderer = Gtk::CellRendererText.new
-     column = Gtk::TreeViewColumn.new("Quantity", renderer, "text" => FoodItem::QTY)
-     treeview.append_column(column) 
-     renderer = Gtk::CellRendererText.new
-     column = Gtk::TreeViewColumn.new("Price", renderer, "text" => FoodItem::PRC)
-     treeview.append_column(column) 
-   end
- 
-   def fill_row(iter, i)
-     row = treestore.append(iter)
-     row[FoodItem::DESC] = list[i].desc 
-     row[FoodItem::QTY]  = list[i].measure ? "%02d %s" % [list[i].qty, list[i].measure] : ""
-     row[FoodItem::PRC]  = list[i].measure ? "$ %02.2f" % [list[i].price] : ""
-     row
-   end
- end 
- 
- MultiColTreeStore.new
- 
- Gtk.main
 
 
 {{br}}




ruby-gnome2-cvs メーリングリストの案内
Back to archive index