Skip to content

Commit

Permalink
Add API to TreeView to be able to save and restore the selection
Browse files Browse the repository at this point in the history
  • Loading branch information
ejeschke committed Sep 4, 2024
1 parent 5ff5162 commit cc71d82
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
1 change: 1 addition & 0 deletions doc/WhatsNew.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Ver 5.2.0 (unreleased)

- Added alternate versions of installed fonts to the default font set
(bold, italic, light)
- Some minor bug fixes for TreeView in Gtk3 backend

Ver 5.1.0 (2024-05-22)
======================
Expand Down
31 changes: 23 additions & 8 deletions ginga/gtk3w/Widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,13 +1018,26 @@ def clear_selection(self):
treeselection = self.tv.get_selection()
treeselection.unselect_all()

def select_path(self, path, state=True):
def get_selected_paths(self):
treeselection = self.tv.get_selection()
item = self._path_to_item(path)
if state:
treeselection.select_iter(item)
else:
treeselection.unselect_iter(item)
model, pathlist = treeselection.get_selected_rows()
paths = []
for path in pathlist:
item = model.get_iter(path)
paths.append(self._get_path(item))
return paths

def select_paths(self, paths, state=True):
treeselection = self.tv.get_selection()
for path in paths:
item = self._path_to_item(path)
if state:
treeselection.select_iter(item)
else:
treeselection.unselect_iter(item)

def select_path(self, path, state=True):
self.select_paths([path], state=state)

def highlight_path(self, path, onoff, font_color='green'):
item = self._path_to_item(path) # noqa
Expand Down Expand Up @@ -1109,7 +1122,8 @@ def fn(*args):
column, cell, model, iter = args[:4]
bnch = model.get_value(iter, 0)
if isinstance(bnch, str):
cell.set_property('text', bnch)
if not isinstance(cell, Gtk.CellRendererPixbuf):
cell.set_property('text', bnch)
elif isinstance(bnch, GdkPixbuf.Pixbuf):
cell.set_property('pixbuf', bnch)
elif isinstance(bnch[idx], GdkPixbuf.Pixbuf):
Expand All @@ -1123,7 +1137,8 @@ def fn(*args):
column, cell, model, iter = args[:4]
bnch = model.get_value(iter, 0)
if isinstance(bnch, str):
cell.set_property('text', '')
if not isinstance(cell, Gtk.CellRendererPixbuf):
cell.set_property('text', '')
elif isinstance(bnch, GdkPixbuf.Pixbuf):
cell.set_property('text', '')
elif isinstance(bnch[idx], GdkPixbuf.Pixbuf):
Expand Down
13 changes: 13 additions & 0 deletions ginga/qtw/Widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,19 @@ def select_path(self, path, state=True):
finally:
self.widget.blockSignals(False)

def get_selected_paths(self):
items = list(self.widget.selectedItems())
return [self._get_path(item) for item in items]

def select_paths(self, paths, state=True):
self.widget.blockSignals(True)
try:
for path in paths:
item = self._path_to_item(path)
item.setSelected(state)
finally:
self.widget.blockSignals(False)

def highlight_path(self, path, onoff, font_color='green'):
item = self._path_to_item(path)

Expand Down
8 changes: 8 additions & 0 deletions ginga/web/pgw/Widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,14 @@ def get_selected(self):
self._get_item(res_dict, rowNum)
return res_dict

def get_selected_paths(self):
return [self._get_path(row_num)
for row_num in self.selectedRows]

def select_paths(self, paths, state=True):
for path in paths:
self.select_path(path, state=state)

def clear(self):
self.rowid = -1
self.rows = []
Expand Down

0 comments on commit cc71d82

Please sign in to comment.