103 lines
3.1 KiB
Python
103 lines
3.1 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 create_columns(treeview, columns, keys=0, alignments=None):
|
|
"""
|
|
columns - is a list of column titles
|
|
keys - is the number of the first visible column from the left containing
|
|
the identifying column key
|
|
alignments - l, c or r
|
|
"""
|
|
i = 0
|
|
visible_cols = len(columns) - keys
|
|
if alignments:
|
|
alignments = alignments.lower()
|
|
if len(alignments) < visible_cols:
|
|
alignments += "l" * (visible_cols - len(alignments))
|
|
for column in columns:
|
|
if not alignments:
|
|
add_column(treeview, column, i + keys)
|
|
else:
|
|
if alignments[i] == 'r':
|
|
add_column(treeview, column, i + keys, 1.0)
|
|
elif alignments[i] == 'c':
|
|
add_column(treeview, column, i + keys, 0.5)
|
|
else:
|
|
add_column(treeview, column, i + keys)
|
|
i += 1
|
|
|
|
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
|
|
|
|
def remove_selected_rows(treeview):
|
|
"""
|
|
Removes the currently selected rows from the model
|
|
"""
|
|
selection = treeview.get_selection()
|
|
model, paths = selection.get_selected_rows()
|
|
for path in paths:
|
|
it = model.get_iter(path)
|
|
model.remove(it)
|