ruby-****@sourc*****
ruby-****@sourc*****
2012年 8月 20日 (月) 04:36:05 JST
------------------------- REMOTE_ADDR = 70.49.49.99 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-treev-trees ------------------------- @@ -9,7 +9,6 @@ Our example program "treestore.rb" is a revised version of the "Grocery List" program from the previous session, splitting the products into two categories: "Cleaning Supplies" and "Food", which both have children of their own. The quantity of each category is set initially to zero, and is calculated, counting all children, which have the "Buy" column set to TRUE, during the run-time hence displaying 4 and 7 respectively. Note, there are two logical columns under the header ((*Buy.*)) The left logical column represents the parent column with a small expander triangle at the edge, slightly shifted to the right is the row of children. - {{br}} ((*treestore.rb*)) @@ -24,13 +23,12 @@ # Create a new GtkCellRendererText, add it to the tree # view column and append the column to the tree view. renderer = Gtk::CellRendererText.new - column = Gtk::TreeViewColumn.new("Buy", renderer, "text" => $buy_index) + column = Gtk::TreeViewColumn.new("Buy", renderer, "text" => BUY_INDEX) treeview.append_column(column) renderer = Gtk::CellRendererText.new - column = Gtk::TreeViewColumn.new("Count", renderer, "text" => $qty_index) + column = Gtk::TreeViewColumn.new("Count", renderer, "text" => QTY_INDEX) treeview.append_column(column) renderer = Gtk::CellRendererText.new - column = Gtk::TreeViewColumn.new("Product", renderer, "text" => $prod_index) + column = Gtk::TreeViewColumn.new("Product", renderer, "text" => PROD_INDEX) treeview.append_column(column) end @@ -47,19 +46,18 @@ @product_type, @buy, @quantity, @product = t, b, q, p end end - $buy_index = 0; $qty_index = 1; $prod_index = 2 - $p_category = 0; $p_child = 1 + BUY_INDEX = 0; QTY_INDEX = 1; PROD_INDEX = 2 + P_CATEGORY = 0; P_CHILD= 1 list = Array.new - list[0] = GroceryItem.new($p_category, true, 0, "Cleaning Supplies") - list[1] = GroceryItem.new($p_child, true, 1, "Paper Towels") - list[2] = GroceryItem.new($p_child, true, 3, "Toilet Paper") - list[3] = GroceryItem.new($p_category, true, 0, "Food") - list[4] = GroceryItem.new($p_child, true, 2, "Bread") - list[5] = GroceryItem.new($p_child, false, 1, "Butter") - list[6] = GroceryItem.new($p_child, true, 1, "Milk") - list[7] = GroceryItem.new($p_child, false, 3, "Chips") - list[8] = GroceryItem.new($p_child, true, 4, "Soda") + list[0] = GroceryItem.new(P_CATEGORY, true, 0, "Cleaning Supplies") + list[1] = GroceryItem.new(P_CHILD, true, 1, "Paper Towels") + list[2] = GroceryItem.new(P_CHILD, true, 3, "Toilet Paper") + list[3] = GroceryItem.new(P_CATEGORY, true, 0, "Food") + list[4] = GroceryItem.new(P_CHILD, true, 2, "Bread") + list[5] = GroceryItem.new(P_CHILD, false, 1, "Butter") + list[6] = GroceryItem.new(P_CHILD, true, 1, "Milk") + list[7] = GroceryItem.new(P_CHILD, false, 3, "Chips") + list[8] = GroceryItem.new(P_CHILD, true, 4, "Soda") treeview = Gtk::TreeView.new setup_tree_view(treeview) @@ -79,30 +78,29 @@ # If the product type is a category, count the quantity # of all of the products in the category that are going # to be bought. - if (e.product_type == $p_category) + if (e.product_type == P_CATEGORY) j = i + 1 # Calculate how many products will be bought in # the category. - while j < list.size && list[j].product_type != $p_category + while j < list.size && list[j].product_type != P_CATEGORY list[i].quantity += list[j].quantity if list[j].buy j += 1 end # Add the category as a new root (parent) row (element). parent = store.append(nil) - # store.set_value(parent, $buy_index, list[i].buy) # <= same as below - parent[$buy_index] = list[i].buy - parent[$qty_index] = list[i].quantity - parent[$prod_index] = list[i].product + # store.set_value(parent, BUY_INDEX, list[i].buy) # <= same as below + parent[BUY_INDEX] = list[i].buy + parent[QTY_INDEX] = list[i].quantity + parent[PROD_INDEX] = list[i].product # Otherwise, add the product as a child row of the category. else child = store.append(parent) - # store.set_value(child, $buy_index, list[i].buy) # <= same as below - child[$buy_index] = list[i].buy - child[$qty_index] = list[i].quantity - child[$prod_index] = list[i].product + # store.set_value(child, BUY_INDEX, list[i].buy) # <= same as below + child[BUY_INDEX] = list[i].buy + child[QTY_INDEX] = list[i].quantity + child[PROD_INDEX] = list[i].product end end @@ -136,22 +135,21 @@ # Add all of the products to the GtkListStore. list.each_with_index do |e, i| - if (e.product_type == $p_category) + if (e.product_type == P_CATEGORY) . . . # Add the category as a new root (parent) row (element). parent = store.append(nil) - # store.set_value(parent, $buy_index, list[i].buy # <= same as below - parent[$buy_index] = list[i].buy - parent[$qty_index] = list[i].quantity - parent[$prod_index] = list[i].product + # store.set_value(parent, BUY_INDEX, list[i].buy # <= same as below + parent[BUY_INDEX] = list[i].buy + parent[QTY_INDEX] = list[i].quantity + parent[PROD_INDEX] = list[i].product # Otherwise, add the product as a child row of the category. else child = store.append(parent) - # store.set_value(child, $buy_index, list[i].buy # <= same as below - child[$buy_index] = list[i].buy - child[$qty_index] = list[i].quantity - child[$prod_index] = list[i].product + # store.set_value(child, BUY_INDEX, list[i].buy # <= same as below + child[BUY_INDEX] = list[i].buy + child[QTY_INDEX] = list[i].quantity + child[PROD_INDEX] = list[i].product end end @@ -160,15 +159,14 @@ One important thing to be notice is the sequential data organization within the tree store. This may be obvious from the way we set-up our array ((*list:*)) list = Array.new - list[0] = GroceryItem.new($p_category, true, 0, "Cleaning Supplies") - list[1] = GroceryItem.new($p_child, true, 1, "Paper Towels") - list[2] = GroceryItem.new($p_child, true, 3, "Toilet Paper") - list[3] = GroceryItem.new($p_category, true, 0, "Food") - list[4] = GroceryItem.new($p_child, true, 2, "Bread") - list[5] = GroceryItem.new($p_child, false, 1, "Butter") - list[6] = GroceryItem.new($p_child, true, 1, "Milk") - list[7] = GroceryItem.new($p_child, false, 3, "Chips") - list[8] = GroceryItem.new($p_child, true, 4, "Soda") + list[0] = GroceryItem.new(P_CATEGORY, true, 0, "Cleaning Supplies") + list[1] = GroceryItem.new(P_CHILD, true, 1, "Paper Towels") + list[2] = GroceryItem.new(P_CHILD, true, 3, "Toilet Paper") + list[3] = GroceryItem.new(P_CATEGORY, true, 0, "Food") + list[4] = GroceryItem.new(P_CHILD, true, 2, "Bread") + list[5] = GroceryItem.new(P_CHILD, false, 1, "Butter") + list[6] = GroceryItem.new(P_CHILD, true, 1, "Milk") + list[7] = GroceryItem.new(P_CHILD, false, 3, "Chips") + list[8] = GroceryItem.new(P_CHILD, true, 4, "Soda") Beside our initial setup of our array called ((*list*)) you will find numerous hints that this indeed is the case. Parent/child relationship dictates that we bundle together all children of a parent below it. When later on application inserts new rows, particularly children, they have to be inserted into, or better immediately after, the list children of such a group. This makes indices to certain rows ephemeral. For instance if you store (memorize) an index to a certain row that index will became invalid after an insertion or a deletion of a row positioned before the element (row) for which you have stored (memorized) the index. Also the orderly sequential organization of the rows in a tree store allows you to readily process the data, since you can always be sure about locations of rows and the groupings of the children can be determined in a predictable manner. In our example program we exploited this fact in the loop calculating the totals for the parents by iterating th rough their groups of children: @@ -177,11 +176,10 @@ # If the product type is a category, count the quantity # of all of the products in the category that are going # to be bought. - if (e.product_type == $p_category) + if (e.product_type == P_CATEGORY) j = i + 1 # Calculate how many products will be bought in # the category. - while j < list.size && list[j].product_type != $p_category + while j < list.size && list[j].product_type != P_CATEGORY list[i].quantity += list[j].quantity if list[j].buy j += 1 end @@ -192,18 +191,17 @@ # Add all of the products to the GtkListStore. list.each_with_index do |e, i| - if (e.product_type == $p_category) + if (e.product_type == P_CATEGORY) # Add the category as a new root (parent) row (element). parent = store.append(nil) - parent[$buy_index] = list[i].buy - parent[$qty_index] = list[i].quantity - parent[$prod_index] = list[i].product + parent[BUY_INDEX] = list[i].buy + parent[QTY_INDEX] = list[i].quantity + parent[PROD_INDEX] = list[i].product # Otherwise, add the product as a child row of the category. else child = store.append(parent) - child[$buy_index] = list[i].buy - child[$qty_index] = list[i].quantity - child[$prod_index] = list[i].product + child[BUY_INDEX] = list[i].buy + child[QTY_INDEX] = list[i].quantity + child[PROD_INDEX] = list[i].product end end