免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2615 | 回复: 1
打印 上一主题 下一主题

GTk2 ComboBox [复制链接]

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-07-10 10:36 |只看该作者 |倒序浏览
  1. #!/usr/bin/env ruby
  2. require 'gtk2'

  3. def add_product(treeview, list)
  4.   # Create a dialog that will be used to create a new product.

  5.   dialog = Gtk::Dialog.new(
  6.       "Add a Product",
  7.       nil,
  8.       Gtk::Dialog::MODAL,
  9.       [ Gtk::Stock::ADD,    Gtk::Dialog::RESPONSE_OK ],
  10.       [ Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL ]
  11.   )
  12.   # Create widgets that will be packed into the dialog.
  13.   combobox = Gtk::ComboBox.new
  14.   entry = Gtk::Entry.new

  15.   #                         min, max, step
  16.   spin  = Gtk::SpinButton.new(0, 100, 1)
  17.   # Set the precision to be displayed by spin button.
  18.   spin.digits = 0
  19.   check = Gtk::CheckButton.new("_Buy the Product")

  20.   # Add all of the categories to the combo box.
  21.   list.each_with_index do |e, i|
  22.     combobox.append_text(list[i].product) if (e.product_type == P_CATEGORY)
  23.   end

  24.   ### Usually, after initialiying combobox you would set the default
  25.   ### combobox.active = 0    # set active index (1st row)

  26.   table = Gtk::Table.new(4, 2, false)
  27.   table.row_spacings = 5
  28.   table.column_spacings = 5
  29.   table.border_width = 5

  30.   # Pack the table that will hold the dialog widgets.
  31.   fll_shr = Gtk::SHRINK | Gtk::FILL
  32.   fll_exp = Gtk::EXPAND | Gtk::FILL

  33.   table.attach(Gtk::Label.new("Category:"), 0, 1, 0, 1, fll_shr, fll_shr,  0, 0)
  34.   table.attach(combobox,                    1, 2, 0, 1, fll_exp, fll_shr,  0, 0)
  35.   table.attach(Gtk::Label.new("Product:"),  0, 1, 1, 2, fll_shr, fll_shr,  0, 0)
  36.   table.attach(entry,                       1, 2, 1, 2, fll_exp, fll_shr,  0, 0)
  37.   table.attach(Gtk::Label.new("Quantity:"), 0, 1, 2, 3, fll_shr, fll_shr,  0, 0)
  38.   table.attach(spin,                        1, 2, 2, 3, fll_exp, fll_shr,  0, 0)
  39.   table.attach(check,                       1, 2, 3, 4, fll_exp, fll_shr,  0, 0)

  40.   dialog.vbox.pack_start_defaults(table)
  41.   dialog.show_all

  42.   dialog.run do |response|
  43.     # If the user presses OK, verify the entries and add the product.
  44.     if response == Gtk::Dialog::RESPONSE_OK
  45.       quantity = spin.value
  46.       product = entry.text
  47.       category = combobox.active_text
  48.       buy = check.active?

  49.       if product == "" || category == nil
  50.         puts "All of the fields were not correctly filled out!"
  51.         puts "DEBUG:  prod=(#{product}), ctg=(#{category})"
  52.         dialog.destroy
  53.         return
  54.       end

  55.       model = treeview.model
  56.       iter = model.iter_first    #<-- same as: iter = model.get_iter("0")

  57.       # Retrieve an iterator pointing to the selected category.
  58.       begin
  59.         name = iter[PROD_INDEX]  #<-- same as: name=iter.get_value(PROD_INDEX)
  60.         break if name == category
  61.       end while iter.next!

  62.       child = model.append(iter)

  63.       # child[BUY_INDEX]=buy # same as: model.set_value(child, BUY_INDEX, buy)
  64.       child[BUY_INDEX]   = buy
  65.       child[QTY_INDEX]   = quantity
  66.       child[PROD_INDEX]  = product

  67.       # Add the quantity to the running total if it is to be purchased.
  68.       if buy
  69.         qty_value = iter[QTY_INDEX]
  70.         qty_value += quantity
  71.         iter[QTY_INDEX] = qty_value
  72.       end
  73.     end
  74.     dialog.destroy
  75.   end
  76. end

  77. def remove_row(ref, model)
  78.   path = ref.path
  79.   iter = model.get_iter(path)

  80.   # Only remove the row if it is not a root row.
  81.   parent = iter.parent
  82.   if parent
  83.     buy       = iter[BUY_INDEX]
  84.     quantity  = iter[QTY_INDEX]
  85.     pqty      = parent[QTY_INDEX]
  86.     if buy
  87.       pqty -= quantity
  88.       parent[QTY_INDEX] = pqty
  89.     end
  90.     iter = model.get_iter(path)
  91.     model.remove(iter)
  92.   end
  93. end

  94. def remove_products(treeview)
  95.   # Gtk::TreeRowReference.new(model, path)
  96.   selection = treeview.selection

  97.   paths2rm = Array.new
  98.   selection.selected_each do |mod, path, iter|
  99.     ref = Gtk::TreeRowReference.new(mod, path)
  100.     paths2rm << [ref, mod]
  101.   end
  102.   paths2rm.each { |ref, mod| remove_row(ref, mod) }
  103. end

  104. # Add three columns to the GtkTreeView. All three of the
  105. # columns will be displayed as text, although one is a boolean
  106. # value and another is an integer.
  107. def setup_tree_view(treeview)
  108.   # Create a new GtkCellRendererText, add it to the tree
  109.   # view column and append the column to the tree view.
  110.   renderer = Gtk::CellRendererText.new
  111.   column = Gtk::TreeViewColumn.new("Buy", renderer, "text" => BUY_INDEX)
  112.   treeview.append_column(column)
  113.   renderer = Gtk::CellRendererText.new
  114.   column = Gtk::TreeViewColumn.new("Count", renderer, "text" => QTY_INDEX)
  115.   treeview.append_column(column)
  116.   renderer = Gtk::CellRendererText.new
  117.   column = Gtk::TreeViewColumn.new("Product", renderer, "text" => PROD_INDEX)
  118.   treeview.append_column(column)
  119. end

  120. window = Gtk::Window.new(Gtk::Window::TOPLEVEL)
  121. window.resizable = true
  122. window.title = "Grocery List"
  123. window.border_width = 10
  124. window.signal_connect('delete_event') { Gtk.main_quit }
  125. window.set_size_request(275, 300)

  126. class GroceryItem
  127.   attr_accessor :product_type, :buy, :quantity, :product
  128.   def initialize(t,b,q,p)
  129.     @product_type, @buy, @quantity, @product = t, b, q, p
  130.   end
  131. end
  132. BUY_INDEX = 0; QTY_INDEX = 1; PROD_INDEX = 2
  133. P_CATEGORY = 0; P_CHILD = 1

  134. list = Array.new
  135. list[0] = GroceryItem.new(P_CATEGORY, true,  0, "Cleaning Supplies")
  136. list[1] = GroceryItem.new(P_CHILD,    true,  1, "Paper Towels")
  137. list[2] = GroceryItem.new(P_CHILD,    true,  3, "Toilet Paper")
  138. list[3] = GroceryItem.new(P_CATEGORY, true,  0, "Food")
  139. list[4] = GroceryItem.new(P_CHILD,    true,  2, "Bread")
  140. list[5] = GroceryItem.new(P_CHILD,    false, 1, "Butter")
  141. list[6] = GroceryItem.new(P_CHILD,    true,  1, "Milk")
  142. list[7] = GroceryItem.new(P_CHILD,    false, 3, "Chips")
  143. list[8] = GroceryItem.new(P_CHILD,    true,  4, "Soda")

  144. treeview = Gtk::TreeView.new
  145. treeview.tooltip_text = "You can select multiple lines"

  146. setup_tree_view(treeview)

  147. # Create a new tree model with three columns, as Boolean,
  148. # integer and string.
  149. store = Gtk::TreeStore.new(TrueClass, Integer, String)

  150. # Avoid creation of iterators on every iteration, since they
  151. # need to provide state information for all iterations. Hence:
  152. # establish closure variables for iterators parent and child.
  153. parent = child = nil

  154. # Add all of the products to the GtkListStore.
  155. list.each_with_index do |e, i|

  156.   # If the product type is a category, count the quantity
  157.   # of all of the products in the category that are going
  158.   # to be bought.
  159.   if (e.product_type == P_CATEGORY)
  160.     j = i + 1

  161.     # Calculate how many products will be bought in
  162.     # the category.
  163.     while j < list.size && list[j].product_type != P_CATEGORY
  164.       list[i].quantity += list[j].quantity if list[j].buy
  165.       j += 1
  166.     end

  167.     # Add the category as a new root (parent) row (element).
  168.     parent = store.append(nil)
  169.     # store.set_value(parent, BUY_INDEX, list[i].buy # <= same as below
  170.     parent[BUY_INDEX]   = list[i].buy
  171.     parent[QTY_INDEX]   = list[i].quantity
  172.     parent[PROD_INDEX]  = list[i].product

  173.   # Otherwise, add the product as a child row of the category.
  174.   else
  175.     child = store.append(parent)
  176.     # store.set_value(child, BUY_INDEX, list[i].buy # <= same as below
  177.     child[BUY_INDEX]   = list[i].buy
  178.     child[QTY_INDEX]   = list[i].quantity
  179.     child[PROD_INDEX]  = list[i].product
  180.   end
  181. end

  182. # Add the tree model to the tree view
  183. treeview.model = store
  184. treeview.expand_all

  185. # Allow multiple rows to be selected at the same time.
  186. treeview.selection.mode = Gtk::SELECTION_MULTIPLE

  187. scrolled_win = Gtk::ScrolledWindow.new
  188. scrolled_win.add(treeview)
  189. scrolled_win.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)

  190. add    = Gtk::Button.new(Gtk::Stock::ADD)
  191. remove = Gtk::Button.new(Gtk::Stock::REMOVE)
  192. remove.tooltip_text = "You can select multiple\nlines and delete them"

  193. add.signal_connect('clicked')    { add_product(treeview, list) }
  194. remove.signal_connect('clicked') { remove_products(treeview) }

  195. hbox = Gtk::HBox.new(true, 5)
  196. hbox.pack_start(add,    false, true, 0)
  197. hbox.pack_start(remove, false, true, 0)

  198. vbox = Gtk::VBox.new(false, 5)
  199. vbox.pack_start(scrolled_win, true,  true, 0)
  200. vbox.pack_start(hbox,         false, true, 0)

  201. window.add(vbox)
  202. window.show_all
  203. Gtk.main
复制代码

论坛徽章:
5
丑牛
日期:2014-01-21 08:26:26卯兔
日期:2014-03-11 06:37:43天秤座
日期:2014-03-25 08:52:52寅虎
日期:2014-04-19 11:39:48午马
日期:2014-08-06 03:56:58
2 [报告]
发表于 2013-07-12 12:27 |只看该作者
赞!赞!支持LZ。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP