ruby-****@sourc*****
ruby-****@sourc*****
2012年 10月 9日 (火) 08:57:51 JST
------------------------- REMOTE_ADDR = 184.145.82.7 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-treev-spbttr ------------------------- @@ -17,16 +17,18 @@ #!/usr/bin/env ruby require 'gtk2' - # Add three columns to the tree view. The first of the three + + # Add three columns to the tree view. The first of the three # columns will be displayed as the toggle button, the second # column will be an editable spin button and the last column # will be the name of the product category or the product to buy. - def setup_tree_view(treeview) - renderer = Gtk::CellRendererToggle.new - column = Gtk::TreeViewColumn.new("Buy", renderer, "active" => BUY_COLUMN) - renderer.activatable = true + renderer = Gtk::CellRendererToggle.new + column = Gtk::TreeViewColumn.new("Buy", renderer, "active" => BUY_COLUMN) + column.set_cell_data_func(renderer) do |tvc, cell, model, iter| + iter[BUY_COLUMN] = any_child_set_to_buy(iter) if iter.has_child? + end renderer.signal_connect('toggled') do |w, path| iter = treeview.model.get_iter(path) iter[BUY_COLUMN] = !iter[BUY_COLUMN] if (iter) @@ -43,8 +45,10 @@ iter[COUNT_COLUMN] = new_text.to_i end - # renderer.editable = true - column = Gtk::TreeViewColumn.new("Count-spin", renderer, "text" => COUNT_COLUMN) + # renderer.editable = true # not needed! + column = Gtk::TreeViewColumn.new("Count", renderer, "text" => COUNT_COLUMN) column.set_cell_data_func(renderer) do |tvc, cell, model, iter| cell.editable = iter.has_child? ? false : true fix_parents_total(iter) if !iter.has_child? @@ -56,14 +58,24 @@ treeview.append_column(column) end + def any_child_set_to_buy(parent) + tmp_iter = parent.first_child + return true if tmp_iter[BUY_COLUMN] + (return true if tmp_iter[BUY_COLUMN]) while tmp_iter.next! + return false + end + def fix_parents_total(iter) parent = iter.parent tmp_iter = parent.first_child - total = 0 - total = tmp_iter[COUNT_COLUMN] if tmp_iter[BUY_COLUMN] + total = tmp_iter[BUY_COLUMN] ? tmp_iter[COUNT_COLUMN] : 0 (total += tmp_iter[COUNT_COLUMN] if tmp_iter[BUY_COLUMN]) while tmp_iter.next! parent[COUNT_COLUMN] = total end + + class GroceryItem attr_accessor :product_type, :buy, :quantity, :product