ruby-****@sourc*****
ruby-****@sourc*****
2012年 8月 26日 (日) 04:21:23 JST
------------------------- REMOTE_ADDR = 70.49.49.99 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-treev-addrnhs ------------------------- @@ -307,12 +307,14 @@ # Add all of the categories to the combo box. list.each_with_index do |e, i| - combobox.append_text(list[i].product) if (e.product_type == $p_category) + combobox.append_text(list[i].product) if (e.product_type == P_CATEGORY) end - ### Usually you would set the default selection, but for the - ### sake of learning we leave it unset: - ### combobox.active = 0 + ### Usually, after initialiying combobox you would set the default + ### selection, but for the sake of learning we leave it unset: + ### combobox.active = 0 # set active index (1st row) + ### puts "Combobox-text:#{combobox.active_text}" + table = Gtk::Table.new(4, 2, false) table.row_spacings = 5 table.column_spacings = 5 @@ -335,7 +337,9 @@ 1, 2, 2, 3, fll_exp, fll_shr, 0, 0) table.attach(check, 1, 2, 3, 4, fll_exp, fll_shr, 0, 0) - + dialog.vbox.pack_start_defaults(table) dialog.show_all @@ -346,22 +348,24 @@ product = entry.text category = combobox.active_text buy = check.active? - + if product == "" || category == nil puts "All of the fields were not correctly filled out!" puts "DEBUG: prod=(#{product}), ctg=(#{category})" dialog.destroy return end - + model = treeview.model iter = model.get_iter("0") # Retrieve an iterator pointing to the selected category. begin - name = iter[$prod_index] #<-- same as: name=iter.get_value($prod_index) + name = iter[PROD_INDEX] #<-- same as: name=iter.get_value(PROD_INDEX) break if name == category - + # Gtk::TreeIter#next! - advances iter to point at the # node following its original position at the current # level. If there is no next iter, false is returned @@ -369,24 +371,26 @@ # same as Gtk::TreeIter#first!. # Returns: true if iter is advanced, false otherwise. end while iter.next! - + # Convert the category iterator to a path so that it will not # become invalid and add the new product as a child of the category. path = iter.path - + child = model.append(iter) - # child[$buy_index]=buy # same as: model.set_value(child, $buy_index, buy) - child[$buy_index] = buy - child[$qty_index] = quantity - child[$prod_index] = product + # child[BUY_INDEX]=buy # same as: model.set_value(child, BUY_INDEX, buy) + child[BUY_INDEX] = buy + child[QTY_INDEX] = quantity + child[PROD_INDEX] = product # Add the quantity to the running total if it is to be purchased. if buy iter = model.get_iter(path) - qty_value = iter[$qty_index] + qty_value = iter[QTY_INDEX] qty_value += quantity - iter[$qty_index] = qty_value + iter[QTY_INDEX] = qty_value end end dialog.destroy @@ -400,12 +402,14 @@ # Only remove the row if it is not a root row. parent = iter.parent if parent - buy = iter[$buy_index] - quantity = iter[$qty_index] - pqty = parent[$qty_index] + buy = iter[BUY_INDEX] + quantity = iter[QTY_INDEX] + pqty = parent[QTY_INDEX] if buy pqty -= quantity - parent[$qty_index] = pqty + parent[QTY_INDEX] = pqty end iter = model.get_iter(path) model.remove(iter) @@ -431,13 +433,15 @@ # 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 @@ -454,21 +456,25 @@ @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 + treeview.tooltip_text = "You can select multiple lines" + setup_tree_view(treeview) # Create a new tree model with three columns, as Boolean, @@ -486,37 +490,41 @@ # 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 # Add the tree model to the tree view treeview.model = store treeview.expand_all - + # Allow multiple rows to be selected at the same time. treeview.selection.mode = Gtk::SELECTION_MULTIPLE @@ -526,6 +530,11 @@ add = Gtk::Button.new(Gtk::Stock::ADD) remove = Gtk::Button.new(Gtk::Stock::REMOVE) + remove.tooltip_text = "You can select multiple\nlines and delete them" add.signal_connect('clicked') { add_product(treeview, list) } remove.signal_connect('clicked') { remove_products(treeview) }