def show_add_product_dialog(parent, categories, data=None, \ selected_category_id=None): """Shows AddProduct dialog.""" dialog = gtk.Dialog(parent=parent, flags=gtk.DIALOG_NO_SEPARATOR) if data is None: dialog.add_button(_('Add'), gtk.RESPONSE_OK) dialog.set_title(_('Add new product')) else: dialog.add_button(_('Modify'), gtk.RESPONSE_OK) dialog.set_title(_('Modify product')) dialog.add_button(_('Cancel'), gtk.RESPONSE_CANCEL) # creating widgets table = gtk.Table(rows=3, columns=2, homogeneous=True) table.set_border_width(8) table.set_col_spacings(10) table.set_row_spacings(8) pname_label = gtk.Label(_('Product name')) pname_entry = gtk.Entry() pname_entry.set_name('entry_widget') pu_label = gtk.Label(_('Carbohydrates')) pu_entry = gtk.Entry() pu_entry.set_name('entry_widget') #pi_label = gtk.Label(_('Index')) #pi_entry = gtk.Entry() #pi_entry.set_name('entry_widget') category_label = gtk.Label(_('Category')) combobox = create_combobox(categories, selected_category_id) # disable combobox if user enter to category if selected_category_id is not None: combobox.set_sensitive(False) # cheking for edit mode if data is not None: # edit mode pname_entry.set_text(data[0]) pu_entry.set_text(str(data[1])) #pi_entry.set_text(str(data[2])) model = combobox.get_model() for index in range(len(model)): if model[index][1] == data[3]: combobox.set_active(index) break # packing widgets table.attach(pname_label, 0, 1, 0, 1) table.attach(pname_entry, 1, 2, 0, 1, xoptions=gtk.EXPAND|gtk.FILL) table.attach(pu_label, 0, 1, 1, 2) table.attach(pu_entry, 1, 2, 1, 2, xoptions=gtk.EXPAND|gtk.FILL) # temporary disable pi widgets #table.attach(pi_label, 0, 1, 2, 3) #table.attach(pi_entry, 1, 2, 2, 3, xoptions=gtk.EXPAND|gtk.FILL) #table.attach(category_label, 0, 1, 3, 4) #table.attach(combobox, 1, 2, 3, 4, xoptions=gtk.EXPAND|gtk.FILL) table.attach(category_label, 0, 1, 2, 3) table.attach(combobox, 1, 2, 2, 3, xoptions=gtk.EXPAND|gtk.FILL) dialog.vbox.pack_start(table, expand=True) dialog.vbox.show_all() response = dialog.run() if response == gtk.RESPONSE_OK: pname = pname_entry.get_text() try: pu = float(pu_entry.get_text().replace(',', '.')) #pi = float(pi_entry.get_text().replace(',', '.')) pi = 1.0 except: pass else: model, active = combobox.get_model(), combobox.get_active() cid = model[active][1] dialog.destroy() return pname, pu, pi, cid dialog.destroy() return None, None, None, None
def show_add_product_to_composition_dialog(parent, controller, data=None): """ Shows AddProductToCompositions dialog. data: (pname, pweight, pid) """ def on_change_category_cb(widget, controller, pcombobox): """Emits when user changes category.""" cmodel, active = widget.get_model(), widget.get_active() cid = cmodel[active][1] pmodel = pcombobox.get_model() pmodel.clear() for product in controller.get_products_by_category(cid): pmodel.append(product) pcombobox.set_active(0) dialog = gtk.Dialog(parent=parent, flags=gtk.DIALOG_NO_SEPARATOR) if data is None: dialog.add_button(_('Add'), gtk.RESPONSE_OK) dialog.set_title(_('Add new product to composition')) else: dialog.add_button(_('Modify'), gtk.RESPONSE_OK) dialog.set_title(_('Modify product in composition')) dialog.add_button(_('Cancel'), gtk.RESPONSE_CANCEL) # creating widgets table = gtk.Table(rows=3, columns=2, homogeneous=True) table.set_border_width(8) table.set_col_spacings(10) cname_label = gtk.Label(_('Category name')) pname_label = gtk.Label(_('Product name')) pweight_label = gtk.Label(_('Product weight')) pweight_entry = gtk.Entry() pweight_entry.set_text('0') pweight_entry.set_name('entry_widget') # cheking for edit mode if data is not None: pweight, pid = data[1], data[2] pweight_entry.set_text(str(pweight)) cid = controller.get_product_category(pid) ccombobox = create_combobox(controller.get_categories(), cid) pcombobox = create_combobox(controller.get_products_by_category(cid), \ pid) ccombobox.set_sensitive(False) pcombobox.set_sensitive(False) else: pcombobox = create_combobox(controller.get_products_list()) ccombobox = create_combobox(controller.get_categories()) ccombobox.connect('changed', on_change_category_cb, controller, \ pcombobox) on_change_category_cb(ccombobox, controller, pcombobox) # packing widgets table.attach(cname_label, 0, 1, 0, 1) table.attach(ccombobox, 1, 2, 0, 1, ypadding=gtk.EXPAND|gtk.FILL) table.attach(pname_label, 0, 1, 1, 2) table.attach(pcombobox, 1, 2, 1, 2, ypadding=gtk.EXPAND|gtk.FILL) table.attach(pweight_label, 0, 1, 2, 3) table.attach(pweight_entry, 1, 2, 2, 3, ypadding=gtk.EXPAND|gtk.FILL) dialog.vbox.pack_start(table, padding=8) dialog.vbox.show_all() response = dialog.run() if response == gtk.RESPONSE_OK: try: pweight = int(pweight_entry.get_text()) model, active = pcombobox.get_model(), pcombobox.get_active() pid = model[active][1] except: pass else: dialog.destroy() return pid, pweight dialog.destroy() return None, None