diff --git a/.gitignore b/.gitignore index c0cac9c..c652fa3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ __pycache__/ compare_* *.txt - +*.bak diff --git a/yms/combobox.py b/yms/combobox.py new file mode 100644 index 0000000..1bcd7cf --- /dev/null +++ b/yms/combobox.py @@ -0,0 +1,128 @@ +from gi.repository import Gtk + +def load_from_dict(combobox, dictionary, selected=None, sort=False): + """ + Populates a simple combo box based on a given dictionary + Optionally, the list can be sorted in ascending order by value + The key fields must be of a uniform type + """ + if len(dictionary) > 0: + if type(next(iter(dictionary))) == int: + lst = Gtk.ListStore(int, str) + else: + lst = Gtk.ListStore(str, str) + else: + lst = Gtk.ListStore(str, str) + if sort: + for key in sorted(dictionary, key=dictionary.get): + lst.append((key, dictionary[key])) + else: + for item in dictionary.items(): + lst.append(item) + combobox.set_model(lst) + cell = Gtk.CellRendererText() + combobox.pack_start(cell, True) + combobox.add_attribute(cell, 'text', 1) + if selected is not None: + activate_row(combobox, selected) + +def refresh_dict(combobox, dictionary, keep_selection=True): + """ + Populates the data model of a combo box with new values + """ + model = combobox.get_model() + selected = get_value(combobox) + model.clear() + for item in dictionary.iteritems(): + model.append(item) + if keep_selection: + activate_row(combobox, selected) + +def get_list_value(combobox, decode=False): + model = combobox.get_model() + active = combobox.get_active() + if active < 0: + return None + if decode: + return model[active][0].decode('utf-8') + else: + return model[active][0] + +def get_tree_value(combobox, decode=False): + model = combobox.get_model() + it = combobox.get_active_iter() + if it is None: + return None + val = model.get_value(it, 0) + if decode: + return val.decode('utf-8') + else: + return val + +def get_value(combobox, decode=False): + """ + Liefert den Wert der ersten Spalte des verknüpften Modells + TODO Unterscheidung ListStore und TreeStore notwending? + Ggf. alles über den Iter? + """ + model = combobox.get_model() + active = combobox.get_active() + if active < 0: + return None + else: + if type(model) == gtk.TreeStore: + it = combobox.get_active_iter() + val = model.get_value(it, 0) + if decode: + return val.decode('utf-8') + else: + return val + else: + if decode: + return model[active][0].decode('utf-8') + else: + return model[active][0] + +def search(model, treeiter, value, column): + """ + Suche einen TreeIter zu einem gegebenen Spaltenwert + """ + while treeiter: + compare_val = model.get_value(treeiter, column) + if value == compare_val: + return treeiter + result = search(model, model.iter_children(treeiter), value, column) + if result: + return result + treeiter = model.iter_next(treeiter) + return None + +def activate_tree_row(combobox, value, column=0): + model = combobox.get_model() + it = search(model, model.iter_children(None), value, column) + combobox.set_active_iter(it) + +def activate_row(combobox, value, column=0): + """Zeigt einen bestimmten Listeneintrag an""" + model = combobox.get_model() + for row in model: + if row[column] == value: + combobox.set_active_iter(row.iter) + return + combobox.set_active(-1) + +def has_list_value(combobox, value): + """ Prüft, ob in der zugrundeliegenden Liste ein Element vorhanden ist""" + model = combobox.get_model() + for row in model: + if row[0] == value: + return True + return False + +def get_first_iter(combobox, columnindex, value): + """Liefert einen Iter in die Liste, wo der Spalteninhalt mit einem vorgegebenen Wert beginnt""" + model = combobox.get_model() + for row in model: + if row[columnindex].startswith(value): + return row.iter + return None diff --git a/yms/treeview.py b/yms/treeview.py new file mode 100644 index 0000000..baa9a1c --- /dev/null +++ b/yms/treeview.py @@ -0,0 +1,43 @@ +from gi.repository import Gtk + +def add_column(treeview, label, index, xalign=None, col_fg=None, col_bg=None, visible=True): + col = Gtk.TreeViewColumn(label) + cell = Gtk.CellRendererText() + col.pack_start(cell, True) + col.add_attribute(cell, 'text', index) + if xalign: + cell.set_property('xalign', xalign) + if col_fg: + col.add_attribute(cell, 'foreground', col_fg) + if col_bg: + col.add_attribute(cell, 'background', col_bg) + treeview.append_column(col) + +def get_value(treeview, column=0): + """ + Returns the value of the specified column of the linked model + """ + try: + selection = treeview.get_selection() + except: + return None + if selection.get_mode() == Gtk.SelectionMode.SINGLE: + (model, iter) = selection.get_selected() + if iter: + return model.get_value(iter, column) + return None + +def set_value(treeview, column, value): + """ + Sets the value of the specified column of the linked model + """ + try: + selection = treeview.get_selection() + except: + return False + if selection.get_mode() == Gtk.SelectionMode.SINGLE: + (model, iter) = selection.get_selected() + model.set_value(iter, column, value) + return True + else: + return False