Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tooltip to table cells with content overflow #1130

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions activity_browser/ui/tables/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,17 @@ def columnCount(self, parent=None, *args, **kwargs):
return 0 if self._dataframe is None else self._dataframe.shape[1]

def data(self, index, role=Qt.DisplayRole):
"""
Return value for table index based on a certain DisplayRole enum.

More on DisplayRole enums: https://doc.qt.io/qt-5/qt.html#ItemDataRole-enum
"""
if not index.isValid():
return None

if role == Qt.DisplayRole:
# instantiate value only in case of DisplayRole or ToolTipRole
value = None
if role == Qt.DisplayRole or role == Qt.ToolTipRole:
value = self._dataframe.iat[index.row(), index.column()]
if isinstance(value, np.float64):
value = float(value)
Expand All @@ -55,7 +62,22 @@ def data(self, index, role=Qt.DisplayRole):
value = value.item()
elif isinstance(value, tuple):
value = str(value)
return value

# immediately return value in case of DisplayRole
if role == Qt.DisplayRole: return value

# in case of ToolTipRole, check whether content fits the cell
if role == Qt.ToolTipRole:
parent = self.parent()
fontMetrics = parent.fontMetrics()

# get the width of both the cell, and the text
column_width = parent.columnWidth(index.column())
text_width = fontMetrics.horizontalAdvance(value)
margin = 10

# only show tooltip if the text is wider then the cell minus the margin
if text_width > column_width - margin: return value

if role == Qt.ForegroundRole:
col_name = self._dataframe.columns[index.column()]
Expand Down