From 290df83c80a4bcb45f3a35181b31082d6fe6a5cd Mon Sep 17 00:00:00 2001 From: Yann Leboulanger Date: Tue, 20 Mar 2007 14:09:27 +0000 Subject: [PATCH] show combobox in dataform widget when there are more than 5 options. see #3046 --- src/dataforms_widget.py | 44 +++++++++++++++++++++++++---------------- src/gtkgui_helpers.py | 17 ++++++++++++++++ 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/dataforms_widget.py b/src/dataforms_widget.py index 0d1fc3c8d..ddd44b889 100644 --- a/src/dataforms_widget.py +++ b/src/dataforms_widget.py @@ -312,25 +312,35 @@ class SingleForm(gtk.Table, object): xoptions=gtk.FILL, yoptions=gtk.FILL) elif field.type == 'list-single': - # TODO: When more than few choices, make a list - # TODO: Think of moving that to another function (it could be used - # TODO: in stage2 of adhoc commands too). # TODO: What if we have radio buttons and non-required field? # TODO: We cannot deactivate them all... - widget = gtk.VBox() - first_radio = None - for value, label in field.iter_options(): - radio = gtk.RadioButton(first_radio, label=label) - radio.connect('toggled', self.on_list_single_radiobutton_toggled, - field, value) - if first_radio is None: - first_radio = radio - if field.value=='': # TODO: is None when done - field.value = value - if value == field.value: - radio.set_active(True) - widget.set_sensitive(readwrite) - widget.pack_start(radio, expand=False) + if len(field.options) < 6: + # 5 option max: show radiobutton + widget = gtk.VBox() + first_radio = None + for value, label in field.iter_options(): + radio = gtk.RadioButton(first_radio, label=label) + radio.connect('toggled', + self.on_list_single_radiobutton_toggled, field, value) + if first_radio is None: + first_radio = radio + if field.value == '': # TODO: is None when done + field.value = value + if value == field.value: + radio.set_active(True) + widget.pack_start(radio, expand=False) + else: + # more than 5 options: show combobox + def on_list_single_combobox_changed(combobox, f): + iter = combobox.get_active_iter() + if iter: + model = combobox.get_model() + f.value = model[iter][1] + else: + f.value = '' + widget = gtkgui_helpers.create_combobox(field.options, field.value) + widget.connect('changed', on_list_single_combobox_changed, field) + widget.set_sensitive(readwrite) elif field.type == 'list-multi': # TODO: When more than few choices, make a list diff --git a/src/gtkgui_helpers.py b/src/gtkgui_helpers.py index 83df2c289..e19a90e0a 100644 --- a/src/gtkgui_helpers.py +++ b/src/gtkgui_helpers.py @@ -817,3 +817,20 @@ default_name = ''): def on_bm_header_changed_state(widget, event): widget.set_state(gtk.STATE_NORMAL) #do not allow selected_state + +def create_combobox(value_list, selected_value = None): + '''Value_list is [(label1, value1), ]''' + liststore = gtk.ListStore(str, str) + combobox = gtk.ComboBox(liststore) + cell = gtk.CellRendererText() + combobox.pack_start(cell, True) + combobox.add_attribute(cell, 'text', 0) + i = -1 + for value in value_list: + liststore.append(value) + if selected_value == value[1]: + i = value_list.index(value) + if i > -1: + combobox.set_active(i) + combobox.show_all() + return combobox