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

Restrict the length of tooltip #132

Merged
merged 1 commit into from
May 11, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 6 additions & 3 deletions tabulous/_qt/_table/_base/_item_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def __init__(self, parent=None):
self._data_role_map = {
Qt.ItemDataRole.DisplayRole: self._data_display,
Qt.ItemDataRole.EditRole: self._data_edit,
Qt.ItemDataRole.TextColorRole: self._data_text_color,
Qt.ItemDataRole.ForegroundRole: self._data_text_color,
Qt.ItemDataRole.ToolTipRole: self._data_tooltip,
Qt.ItemDataRole.BackgroundColorRole: self._data_background_color_rendered,
Qt.ItemDataRole.BackgroundRole: self._data_background_color_rendered,
Qt.ItemDataRole.DecorationRole: self._data_decoration,
Qt.ItemDataRole.SizeHintRole: self._data_size_hint,
}
Expand Down Expand Up @@ -146,7 +146,10 @@ def _data_tooltip(self, index: QtCore.QModelIndex):
if dtype != object:
return f"{val!r} (dtype: {dtype}){ref}"
else:
return f"{val!r} (dtype: {dtype}; type: {type(val).__name__}){ref}"
val_repr = repr(val)
if len(val_repr) > 64:
val_repr = val_repr[:60] + "..." + val_repr[-4:]
return f"{val_repr} (dtype: {dtype}; type: {type(val).__name__}){ref}"
return QtCore.QVariant()

def _data_background_color(self, index: QtCore.QModelIndex):
Expand Down
7 changes: 5 additions & 2 deletions tabulous/_qt/_table/_spreadsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,13 @@ def _data_tooltip(self, index: QtCore.QModelIndex):
else:
ref = ""
dtype = self._columns_dtype.get(name, None)
val_repr = repr(val)
if len(val_repr) > 64:
val_repr = val_repr[:60] + "..." + val_repr[-4:]
if dtype is None:
return f"{val!r} (dtype: infer){ref}"
return f"{val_repr} (dtype: infer){ref}"
else:
return f"{val!r} (dtype: {dtype}){ref}"
return f"{val_repr} (dtype: {dtype}){ref}"
Comment on lines +152 to +158
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor tooltip truncation logic for efficiency and robustness.

-            val_repr = repr(val)
-            if len(val_repr) > 64:
-                val_repr = val_repr[:60] + "..." + val_repr[-4:]
+            val_repr = (repr(val)[:60] + "..." + repr(val)[-4:]) if len(repr(val)) > 64 else repr(val)

Consider adding a try-except block around the repr(val) to handle any exceptions that might occur during the conversion, ensuring the application's stability.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
val_repr = repr(val)
if len(val_repr) > 64:
val_repr = val_repr[:60] + "..." + val_repr[-4:]
if dtype is None:
return f"{val!r} (dtype: infer){ref}"
return f"{val_repr} (dtype: infer){ref}"
else:
return f"{val!r} (dtype: {dtype}){ref}"
return f"{val_repr} (dtype: {dtype}){ref}"
val_repr = (repr(val)[:60] + "..." + repr(val)[-4:]) if len(repr(val)) > 64 else repr(val)
if dtype is None:
return f"{val_repr} (dtype: infer){ref}"
else:
return f"{val_repr} (dtype: {dtype}){ref}"

return QtCore.QVariant()

def rename_column(self, old_name: str, new_name: str):
Expand Down
2 changes: 1 addition & 1 deletion tabulous/widgets/_component/_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def __getitem__(self, key: tuple[int, int]) -> ColorTuple | None:
self._assert_integers(key)
model = self.parent.native.model()
idx = model.index(*key)
qcolor = model.data(idx, role=Qt.ItemDataRole.TextColorRole)
qcolor = model.data(idx, role=Qt.ItemDataRole.ForegroundRole)
if isinstance(qcolor, QtGui.QColor):
return ColorTuple(*qcolor.getRgb())
return None
Expand Down
Loading