Skip to content

Commit

Permalink
show tooltip function
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjinliu committed Nov 2, 2024
1 parent 70a042d commit 634e502
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 14 deletions.
6 changes: 0 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,3 @@ filterwarnings = [
"ignore:path is deprecated:DeprecationWarning",
"ignore:Jupyter is migrating its paths to use standard platformdirs:DeprecationWarning",
]

[tool.pyright.defineConstant]
PYQT5 = true
PYSIDE2 = false
PYQT6 = false
PYSIDE6 = false
5 changes: 1 addition & 4 deletions tabulous/_qt/_table/_base/_enhanced_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
from typing import TYPE_CHECKING, Iterable, Iterator, cast, Literal
from qtpy import QtWidgets as QtW, QtGui, QtCore
from qtpy.QtCore import Qt
from qtpy.QtCore import Qt, Signal, Property

from ._item_model import AbstractDataFrameModel
from ._header_view import QHorizontalHeaderView, QVerticalHeaderView
Expand All @@ -19,9 +19,6 @@
from tabulous._qt._mainwindow import _QtMainWidgetBase
from tabulous._map_model import SlotRefMapping
from tabulous._utils import TabulousConfig
from qtpy.QtCore import pyqtSignal as Signal, pyqtProperty as Property
else:
from qtpy.QtCore import Signal, Property

# Flags
_SCROLL_PER_PIXEL = QtW.QAbstractItemView.ScrollMode.ScrollPerPixel
Expand Down
28 changes: 28 additions & 0 deletions tabulous/_qt/_table/_base/_table_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,34 @@ def createHandle(self) -> QTableHandle:
"""Create custom handle."""
return QTableHandle(Qt.Orientation.Horizontal, self)

def showToolTip(self, row: int, col: int) -> None:
"""Show tooltip at the given position."""
if row >= 0 and col >= 0:
index = self.model().index(row, col)
qtable_view = self._qtable_view
pos = qtable_view.mapToGlobal(qtable_view.visualRect(index).center())
tooltip = self.model().data(index, Qt.ItemDataRole.ToolTipRole)
if isinstance(tooltip, str):
QtW.QToolTip.showText(pos, tooltip, qtable_view)
elif row < 0:
header = self._qtable_view.horizontalHeader()
pos = header.mapToGlobal(header.visualRectAtIndex(col).center())
tooltip = self.model().headerData(
col, Qt.Orientation.Horizontal, Qt.ItemDataRole.ToolTipRole
)
if isinstance(tooltip, str):
QtW.QToolTip.showText(pos, tooltip, header)
elif col < 0:
header = self._qtable_view.verticalHeader()
pos = header.mapToGlobal(header.visualRectAtIndex(row).center())
tooltip = self.model().headerData(
row, Qt.Orientation.Vertical, Qt.ItemDataRole.ToolTipRole
)
if isinstance(tooltip, str):
QtW.QToolTip.showText(pos, tooltip, header)
else:
return None

def showContextMenu(self, pos: QtCore.QPoint) -> None:
"""Show contextmenu at the given position."""
index = self._qtable_view.indexAt(pos)
Expand Down
1 change: 1 addition & 0 deletions tabulous/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def wrapper(f):
(table.zoom_in, "Ctrl+Shift+>"),
(table.zoom_out, "Ctrl+Shift+<"),
(table.jump_to_cell, "Ctrl+G"),
(table.show_tooltip, "Ctrl+K, Ctrl+I"),
(file.open_table, "Ctrl+O"),
(file.open_spreadsheet, "Ctrl+K, Ctrl+O"),
(file.save_table, "Ctrl+S"),
Expand Down
7 changes: 7 additions & 0 deletions tabulous/commands/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,10 @@ def jump_to_cell(viewer: TableViewerBase):
return
jw = QJumpWidget(viewer.native)
jw.show()


def show_tooltip(viewer: TableViewerBase):
"""Show tooltip"""
table = _utils.get_table(viewer)
r, c = table.current_index
table._qwidget.showToolTip(r, c)
15 changes: 11 additions & 4 deletions tabulous/widgets/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import ast
from enum import Enum
from pathlib import Path
from typing import Any, Callable, Hashable, TYPE_CHECKING, Mapping, overload
from typing import Any, Callable, Hashable, TYPE_CHECKING, Mapping, NamedTuple, overload
import weakref
from psygnal import SignalGroup, Signal

Expand Down Expand Up @@ -59,6 +59,13 @@ def __repr__(self) -> str:
return f"<{type(self).__name__}.{self.name}>"


class TableIndex(NamedTuple):
"""Tuple of row and column indices."""

row: int
col: int


# #####################################################################
# Property-like
# #####################################################################
Expand Down Expand Up @@ -185,9 +192,9 @@ def source(self) -> Source:
return self._source

@property
def current_index(self) -> tuple[int, int]:
"""The current index of the table."""
return self._qwidget._qtable_view._selection_model.current_index
def current_index(self) -> TableIndex:
"""The current index (row, col) of the table."""
return TableIndex(*self._qwidget._qtable_view._selection_model.current_index)

@current_index.setter
def current_index(self, index: tuple[int, int]) -> None:
Expand Down

0 comments on commit 634e502

Please sign in to comment.