Skip to content

Commit

Permalink
GenericTableModel/View improvements (#1163)
Browse files Browse the repository at this point in the history
* [dataviews] GenericTableModel/View improvements ...

* GenericTableView got a new argument specifying whether the ellipsis
for long cell content should be right (old behavior, default) or left
useful for long content such as the filenames in the video table.
* GenericTableView uses all the space that available to the table.
* The model's data function returns the full cell content to be shown as
tool tip text.

* [gui/app] set the ellipsis to be on the left for long table contents

---------

Co-authored-by: Liezl Maree <[email protected]>
  • Loading branch information
jgrewe and roomrys authored Feb 16, 2023
1 parent 05683d5 commit 57ee08a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions sleap/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,7 @@ def _add_button(to, label, action, key=None):
row_name="video",
is_activatable=True,
model=VideosTableModel(items=self.labels.videos, context=self.commands),
ellipsis_left=True,
)
videos_layout.addWidget(self.videosTable)

Expand Down
18 changes: 17 additions & 1 deletion sleap/gui/dataviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def __init__(
super(GenericTableModel, self).__init__()
self.properties = properties or self.properties or []
self.context = context

self.items = items

def object_to_items(self, item_list):
Expand Down Expand Up @@ -142,6 +141,13 @@ def data(self, index: QtCore.QModelIndex, role=QtCore.Qt.DisplayRole):
elif role == QtCore.Qt.ForegroundRole:
return self.get_item_color(self.original_items[idx], key)

elif role == QtCore.Qt.ToolTipRole:
if isinstance(item, dict) and key in item:
return item[key]

if hasattr(item, key):
return getattr(item, key)

return None

def setData(self, index: QtCore.QModelIndex, value: str, role=QtCore.Qt.EditRole):
Expand Down Expand Up @@ -272,6 +278,11 @@ class GenericTableView(QtWidgets.QTableView):
for some reason you need this to be different. For instance, the table
of instances in the GUI sets this to "" so that the row for an instance
is automatically selected when `state["instance"]` is set outside the table.
"ellipsis_left" can be used to make the TableView truncate cell content on
the left instead of the right side. By default, the argument is set to
False, i.e. truncation on the right side, which is also the default for
QTableView.
"""

row_name: Optional[str] = None
Expand All @@ -287,6 +298,7 @@ def __init__(
name_prefix: Optional[str] = None,
is_sortable: bool = False,
is_activatable: bool = False,
ellipsis_left: bool = False,
):
super(GenericTableView, self).__init__()

Expand All @@ -298,6 +310,10 @@ def __init__(

self.setModel(model)

if ellipsis_left:
self.setTextElideMode(QtCore.Qt.ElideLeft)
self.setWordWrap(False)
self.horizontalHeader().setStretchLastSection(True)
self.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
self.setSortingEnabled(self.is_sortable)
Expand Down

0 comments on commit 57ee08a

Please sign in to comment.