Files
YMS/yms/treeview.py
T

68 lines
1.9 KiB
Python

# SPDX-License-Identifier: WTFPL
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
def set_row_values(treeview, data):
"""
Set every column in row to new value
"""
try:
selection = treeview.get_selection()
except:
return False
if not selection.get_mode() == Gtk.SelectionMode.SINGLE:
return False
(model, iter) = selection.get_selected()
# check if data count matches treeview columns
if len(data) != model.get_n_columns():
print("treeview:refresh_row column count mismatch")
return False
column = 0
for value in data:
# TODO data type checks?
model.set_value(iter, column, value)
column += 1
return True