ruby-****@sourc*****
ruby-****@sourc*****
2012年 10月 8日 (月) 10:40:55 JST
------------------------- REMOTE_ADDR = 184.145.82.7 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-treev-crs ------------------------- @@ -318,3 +318,22 @@ window.add(scrolled_win) window.show_all Gtk.main + +All the differences between the two programs above are found in two places in the((*setup_tree_view*))method in which the three tree view columns 'Buy', 'Count' and 'Product' are set. However, though only two modifications were made in the second example program, a user can observe three behavioural differences: (1) neither of the two product category's 'Buy' columns respond to clicks ob their respective check boxes, (2) toggling clickable products now adjusts the 'Count' value of their product category header record to reflect the number of selected items from the category, and (3) if all the products of a category are deselected, the check-box in their control record is also unchecked and as soon a s any item is re-checked the product category control record's check-box is also set as checked. + +The first modification is found in setting up the 'Buy' column and the second one is in how the 'Count' column is now set up. Let's start with the second one first. That is the new code found in the place where 'Count' column is set is: + + column.set_cell_data_func(renderer) do |tvc, cell, model, iter| + fix_parents_total(iter) if !iter.has_child? + end + +Indeed, all the work here is done in the new((*fix_parents_total*))method, which is called if the check-box is clicked for an item which is not the parent node, i.e. does not have children rows. In the new 'fix_parents_total' helper method we obtain parent's iter (parent), and the iter for its first child (tmp_iter), which is subsequently advanced through the list of all children rows for this product category accumulating the total value of all selected 'Count' cells in the column identified by the GItm::QTY_INDEX. If a cell is selected is determined by the status of its 'Buy' column. Namely, when checked, the model's column identified by the GItm::BUY_INDEX contains value true. Finally, the parent's 'Count' value is set to the newly accumulated total value. + + def fix_parents_total(iter) + parent = iter.parent + tmp_iter = parent.first_child + total = 0 + total = tmp_iter[GItm::QTY_INDEX] if tmp_iter[GItm::BUY_INDEX] + (total += tmp_iter[GItm::QTY_INDEX] if tmp_iter[GItm::BUY_INDEX]) while tmp_iter.next! + parent[GItm::QTY_INDEX] = total + end