ruby-****@sourc*****
ruby-****@sourc*****
2012年 8月 10日 (金) 04:02:17 JST
------------------------- REMOTE_ADDR = 184.145.90.35 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-treeview-selection-handle ------------------------- @@ -104,3 +104,148 @@ selection.selected_each { count += 1 } count end + +Following is the entire program in which some of the code samples from above paragraphs are included: + + + #!/usr/bin/env ruby + =begin + SEARCHING TREE VIEW FOR SELECTIONS + ================================== + Into this program I packed things we've learned so far about + Gtk::TreeView, and the pertinent data model. + + Included items are: + + 1) The connection strategy of model an view: + Gtk::TreeViewColumn.new("Buy", renderer, :text => BUY_IT [, ...] ) + where [, ...] are in the form of :property => COLUMN_NUM, ... + + 2) The use of {{ set_cell_data_func(renderer) {|col, renderer, model, iter| ...} }} + + 3) Catching double-clicks on a row. It's quite easy and is done by + connecting to a tree view's "row-activated" signal + + 4) Traversing through selected rows via + {{ selection.selected_each do |model, path, iter| ... end }} + =end + + require 'gtk2' + + def setup_tree_view(treeview) + renderer = Gtk::CellRendererText.new + column = Gtk::TreeViewColumn.new("Buy", renderer, :text => BUY_IT) + column.set_cell_data_func(renderer) do |col, renderer, model, iter| + renderer.background = iter[BUY_IT] ? "red" : nil + end + treeview.append_column(column) + renderer = Gtk::CellRendererText.new + column = Gtk::TreeViewColumn.new("Count", renderer, :text => QUANTITY) + treeview.append_column(column) + renderer = Gtk::CellRendererText.new + column = Gtk::TreeViewColumn.new("Product", renderer, :text => PRODUCT) + treeview.append_column(column) + end + + def traverse_list_to_print_all_rows(store) + # get first row in list store + return unless iter = store.iter_first + + begin + puts "Product: #{iter[2]} Buy:#{iter[0]}" + end while iter.next! + end + + def print_only_unselected_rows(store, treev) + # get first row in list store + return unless iter = store.iter_first + + # - You can check whether a given row is selected or not using the + # - + # - Gtk::TreeSelection#iter_is_selected? + # - or + # - Gtk::TreeSelection#path_is_selected? + # - + # - methods. If you want to know all rows that are not selected, for + # - example, you could just traverse the whole list or tree, and use + # - the above methods to check for each row whether it is selected or not. + + all_selections = treev.selection # <--- obtain all selections from TreeView + begin + puts "Product: #{iter[2]} Buy:#{iter[0]}" if ! all_selections.iter_is_selected?(iter) + end while iter.next! # =-==============-======================= + end + + + # Print all selected rows (2nd way /using VIEW/ + def print_all_selected_rows(treev) + + treev.selection.selected_each do |model, path, iter| + puts "Product: #{iter[2]} Buy:#{iter[0]}" + end + end + + + class GroceryItem + attr_accessor :buy, :quantity, :product + def initialize(b, q, p); @buy, @quantity, @product = b, q, p; end + end + BUY_IT = 0; QUANTITY = 1; PRODUCT = 2 + + list = Array.new + list[0] = GroceryItem.new(true, 1, "Paper Towels") + list[1] = GroceryItem.new(true, 2, "Bread") + list[2] = GroceryItem.new(false, 1, "Butter") + list[3] = GroceryItem.new(true, 1, "Milk") + list[4] = GroceryItem.new(false, 3, "Chips") + list[5] = GroceryItem.new(true, 4, "Soda") + + store = Gtk::ListStore.new(TrueClass, Integer, String) + treeview = Gtk::TreeView.new(store) # Add the tree model (store) to the tree view + treeview.selection.mode = Gtk::SELECTION_MULTIPLE + + treeview.signal_connect("row-activated") do |view, path, column| + puts "Row #{path.to_str} was clicked!" + + if iter = view.model.get_iter(path) + puts "Double-clicked row contains product: #{iter[2]}!" + end + end + + setup_tree_view(treeview) + + list.each_with_index do |e, i| + iter = store.append + store.set_value(iter, BUY_IT, list[i].buy) # iter[BUY_IT] = list[i].buy + store.set_value(iter, QUANTITY, list[i].quantity) # iter[QUANTITY] = list[i].quantity + store.set_value(iter, PRODUCT, list[i].product) # iter[PRODUCT] = list[i].product + end + + vbox = Gtk::VBox.new(homogeneous = false, spacing = nil) + button = Gtk::Button.new("Collect and analize selections") + button.signal_connect("clicked") do |w| + + puts "==== All rows regardless of selection (1st way /using MODEL/" + traverse_list_to_print_all_rows(store) + + puts "==== All NON-selected rows" + print_only_unselected_rows(store, treeview) + + puts "==== All selected rows (2nd way /using VIEW/)" + print_all_selected_rows(treeview) + end + + scrolled_win = Gtk::ScrolledWindow.new + scrolled_win.add(treeview) + scrolled_win.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC) + + vbox.pack_start_defaults(scrolled_win) + vbox.pack_start_defaults(button) + window = Gtk::Window.new("Grocery List") + window.resizable = true + window.border_width = 10 + window.signal_connect('destroy') { Gtk.main_quit } + window.set_size_request(250, 165) + window.add(vbox) + window.show_all + Gtk.main