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

Zurück zum Archiv-Index

ruby-****@sourc***** ruby-****@sourc*****
2012年 9月 27日 (木) 06:31:46 JST


-------------------------
REMOTE_ADDR = 184.145.80.187
REMOTE_HOST = 
        URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-treev-trees
-------------------------
@@ -678,6 +678,87 @@
 
 
 {{br}}
+{{image_left("treev-MultiDim-load-asci-table.png")}}
+Following is the program, called "treev-MultiDim-load-asci-table.rb", that displays the above array of arrays, we created in a separate module, the screenshot of which you cam see here on the left. Indeed, to run it you have to download both the above module ("initialize-products-from-asci-table.rb") and the following program into the same directory. This program works just as expected. However, because we dynamically process some of its data, namely by nature, the accumulative price, it also requires a special attention, when adding or removing rows. The addition and deletion of records in a tree store is a serious matter, even when we do not have dynamically changed data, and will be dealt with only after we have a second look at Gtk::TreePath, Gtk::TreeIter, and Gtk::TreeRowReference. You can guess, on the following pages we will encounter a second version of this program, demonstrating how to handle adding and deleting rows.
+
+((*treev-MultiDim-load-asci-table.rb*))
+
+
+ #!/usr/bin/env ruby
+ require 'gtk2'
+ 
+ $: << "."
+ require "initialize-products-from-asci-table.rb"
+ include InitializeProductsFromASCItable
+ 
+ product_list = []
+ INIT_ARRAY.each_with_index do |row, i|
+   product_list[i] = Products.new(*row)
+ end
+ product_list.each {|p| print "DEBUG: %-20s ..... %07.2f\n" % [p.product, p.price] }
+ 
+ def setup_tree_view(treeview)
+   renderer = Gtk::CellRendererText.new
+   column   = Gtk::TreeViewColumn.new("Product Name", renderer, :text => NAME_COLUMN)
+   treeview.append_column(column)
+   renderer = Gtk::CellRendererText.new
+   column   = Gtk::TreeViewColumn.new("Price", renderer, :text => PRICE_COLUMN)
+   column.set_cell_data_func(renderer) do |col, renderer, model, iter|
+     renderer.text  = "%07.2f" % iter[PRICE_COLUMN]
+   end
+   treeview.append_column(column)
+ end
+ 
+ NAME_COLUMN, PRICE_COLUMN = 0, 1
+ treeview = Gtk::TreeView.new(store = Gtk::TreeStore.new(String, Float))
+ setup_tree_view(treeview)
+ 
+ def load_products(store, product_list, parent)
+ 
+   # Add all of the products to the GtkTreeStore.
+   product_list.each do |prod_obj|
+ 
+     # If the product has children it's a parent
+     if (prod_obj.children)
+       # Add the category as a new root (parent) row (element).
+       child_parent = store.append(parent)
+       # store.set_value(parent, COL, value) # <= same as below
+       child_parent[NAME_COLUMN]   = prod_obj.product
+       child_parent[PRICE_COLUMN]  = prod_obj.price
+       load_products(store, prod_obj.children, child_parent)
+ 
+     # Otherwise, add the product as a child row of the category.
+     else
+       child = store.append(parent)
+       # store.set_value(child, COL, value) # <= same as below
+       child[NAME_COLUMN]   = prod_obj.product
+       child[PRICE_COLUMN]  = prod_obj.price
+       next
+     end
+   end
+ end
+ 
+ load_products(store, product_list, nil)
+ 
+ window = Gtk::Window.new("Tree View Depth Demo")
+ window.resizable = true
+ 
+ window.border_width = 10
+ window.signal_connect('destroy') { Gtk.main_quit }
+ window.set_size_request(300, -1)
+ 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
+ Gtk.main
+
+
+
+
+
+
+{{br}}
 {{br}}
 
 === Multi-item Super Columns




ruby-gnome2-cvs メーリングリストの案内
Zurück zum Archiv-Index