From 9b526142b2745f7f7b8a919333d34ec7e76a0332 Mon Sep 17 00:00:00 2001 From: Marin Visscher Date: Thu, 16 Nov 2023 14:13:36 +0100 Subject: [PATCH 1/3] Added tooltip for table content --- activity_browser/ui/tables/models/base.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/activity_browser/ui/tables/models/base.py b/activity_browser/ui/tables/models/base.py index e2b321d90..62a96f9c7 100644 --- a/activity_browser/ui/tables/models/base.py +++ b/activity_browser/ui/tables/models/base.py @@ -45,7 +45,9 @@ def data(self, index, role=Qt.DisplayRole): 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) @@ -55,7 +57,23 @@ def data(self, index, role=Qt.DisplayRole): value = value.item() elif isinstance(value, tuple): value = str(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()] From 55f90a5857cb2aebda65d6dbe064797e4a57e9b6 Mon Sep 17 00:00:00 2001 From: Marin Visscher Date: Thu, 16 Nov 2023 14:37:32 +0100 Subject: [PATCH 2/3] Added docstring for data method --- activity_browser/ui/tables/models/base.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/activity_browser/ui/tables/models/base.py b/activity_browser/ui/tables/models/base.py index 62a96f9c7..489b70c04 100644 --- a/activity_browser/ui/tables/models/base.py +++ b/activity_browser/ui/tables/models/base.py @@ -42,6 +42,10 @@ 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): + """ + Returns 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 From 07cd9efeab22a2592fcda805c770e65db62f065f Mon Sep 17 00:00:00 2001 From: Marin Visscher Date: Fri, 17 Nov 2023 09:24:38 +0100 Subject: [PATCH 3/3] Cleaning-up documentation --- activity_browser/ui/tables/models/base.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/activity_browser/ui/tables/models/base.py b/activity_browser/ui/tables/models/base.py index 489b70c04..53ccd27b5 100644 --- a/activity_browser/ui/tables/models/base.py +++ b/activity_browser/ui/tables/models/base.py @@ -43,13 +43,14 @@ def columnCount(self, parent=None, *args, **kwargs): def data(self, index, role=Qt.DisplayRole): """ - Returns value for table index based on a certain DisplayRole enum. + 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 - #Instantiate value only in case of DisplayRole or ToolTipRole + # 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()] @@ -62,21 +63,20 @@ def data(self, index, role=Qt.DisplayRole): elif isinstance(value, tuple): value = str(value) - #Immediately return value in case of DisplayRole - if role == Qt.DisplayRole: - 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 + # 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 + # 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 + # 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: