From 53d3797004b36e59dda718caf08950268791e7a8 Mon Sep 17 00:00:00 2001 From: Hanjin Liu Date: Fri, 10 May 2024 23:54:51 +0900 Subject: [PATCH 1/2] fix colorrole, painter --- tabulous/_qt/_preference/_shared.py | 3 ++- tabulous/_qt/_preference/_theme.py | 20 ++++++++++++-------- tabulous/_qt/_qt_const.py | 21 +++++++++++++++++++++ tabulous/_qt/_table/_animation.py | 3 ++- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/tabulous/_qt/_preference/_shared.py b/tabulous/_qt/_preference/_shared.py index fe9aaf64..faf81041 100644 --- a/tabulous/_qt/_preference/_shared.py +++ b/tabulous/_qt/_preference/_shared.py @@ -1,6 +1,7 @@ from __future__ import annotations from qtpy import QtWidgets as QtW, QtGui, QtCore +from tabulous._qt._qt_const import foreground_color_role class QTitleLabel(QtW.QLabel): @@ -13,7 +14,7 @@ def __init__(self, text: str, size: int) -> None: def paintEvent(self, a0: QtGui.QPaintEvent) -> None: painter = QtGui.QPainter(self) - color = self.palette().color(QtGui.QPalette.ColorRole.Foreground) + color = foreground_color_role(self.palette()) painter.setPen(QtGui.QPen(color, 1)) bottom_left = self.rect().bottomLeft() bottom_right = QtCore.QPoint(bottom_left.x() + 300, bottom_left.y()) diff --git a/tabulous/_qt/_preference/_theme.py b/tabulous/_qt/_preference/_theme.py index 0ae0e922..96367528 100644 --- a/tabulous/_qt/_preference/_theme.py +++ b/tabulous/_qt/_preference/_theme.py @@ -71,20 +71,24 @@ def paintEvent(self, a0: QtGui.QPaintEvent) -> None: painter = QtGui.QPainter(self) geo = self.rect() - grad = QtGui.QLinearGradient(geo.topLeft(), geo.bottomRight()) + grad = QtGui.QLinearGradient( + QtCore.QPointF(geo.topLeft()), QtCore.QPointF(geo.bottomRight()) + ) grad.setColorAt(0, QtGui.QColor(self._style_theme.background0)) grad.setColorAt(1, QtGui.QColor(self._style_theme.background1)) - path = QtGui.QPainterPath(geo.topLeft()) - path.lineTo(geo.topRight()) - path.lineTo(geo.bottomLeft()) + path = QtGui.QPainterPath(QtCore.QPointF(geo.topLeft())) + path.lineTo(QtCore.QPointF(geo.topRight())) + path.lineTo(QtCore.QPointF(geo.bottomLeft())) painter.fillPath(path, grad) - grad = QtGui.QLinearGradient(geo.topLeft(), geo.bottomRight()) + grad = QtGui.QLinearGradient( + QtCore.QPointF(geo.topLeft()), QtCore.QPointF(geo.bottomRight()) + ) grad.setColorAt(0, QtGui.QColor(self._style_theme.highlight0)) grad.setColorAt(1, QtGui.QColor(self._style_theme.highlight1)) - path = QtGui.QPainterPath(geo.topRight()) - path.lineTo(geo.bottomLeft()) - path.lineTo(geo.bottomRight()) + path = QtGui.QPainterPath(QtCore.QPointF(geo.topRight())) + path.lineTo(QtCore.QPointF(geo.bottomLeft())) + path.lineTo(QtCore.QPointF(geo.bottomRight())) painter.fillPath(path, grad) if self._checked: diff --git a/tabulous/_qt/_qt_const.py b/tabulous/_qt/_qt_const.py index b5a231f4..3ef83501 100644 --- a/tabulous/_qt/_qt_const.py +++ b/tabulous/_qt/_qt_const.py @@ -1,5 +1,6 @@ import sys from pathlib import Path +from qtpy import QtGui, QT6 # Monospace font if sys.platform == "win32": @@ -10,3 +11,23 @@ MonospaceFontFamily = "Monospace" ICON_DIR = Path(__file__).parent / "_icons" + +if QT6: + + def foreground_color_role(qpalette: QtGui.QPalette) -> QtGui.QColor: + return qpalette.color( + QtGui.QPalette.ColorGroup.Normal, QtGui.QPalette.ColorRole.Text + ) + + def background_color_role(qpalette: QtGui.QPalette) -> QtGui.QColor: + return qpalette.color( + QtGui.QPalette.ColorGroup.Normal, QtGui.QPalette.ColorRole.Base + ) + +else: + + def foreground_color_role(qpalette: QtGui.QPalette) -> QtGui.QColor: + return qpalette.color(QtGui.QPalette.ColorRole.Foreground) + + def background_color_role(qpalette: QtGui.QPalette) -> QtGui.QColor: + return qpalette.color(QtGui.QPalette.ColorRole.Background) diff --git a/tabulous/_qt/_table/_animation.py b/tabulous/_qt/_table/_animation.py index f2239609..ca7cc73f 100644 --- a/tabulous/_qt/_table/_animation.py +++ b/tabulous/_qt/_table/_animation.py @@ -5,6 +5,7 @@ from contextlib import contextmanager from tabulous._utils import get_config from tabulous._range import MultiRectRange, RectRange +from tabulous._qt._qt_const import background_color_role from qtpy import QtCore, QtWidgets as QtW, QtGui @@ -123,7 +124,7 @@ def get_brush(self, size: QtCore.QSize, time: float, bg) -> QtGui.QBrush: qtable = self._parent.parent() is_qvariant = not isinstance(bg, QtGui.QColor) if is_qvariant: - bg = qtable.palette().color(QtGui.QPalette.ColorRole.Background) + bg = background_color_role(qtable.palette()) col = qtable._qtable_view.selectionColor length = max(size.width(), size.height()) dh = 4 / length From cfed3af9e76a0b618a0b8bfb27106c62972fa436 Mon Sep 17 00:00:00 2001 From: Hanjin Liu Date: Sat, 11 May 2024 00:03:07 +0900 Subject: [PATCH 2/2] fix mouse move event --- tabulous/_qt/_table/_base/_enhanced_table.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tabulous/_qt/_table/_base/_enhanced_table.py b/tabulous/_qt/_table/_base/_enhanced_table.py index 2e5199cf..6936380e 100644 --- a/tabulous/_qt/_table/_base/_enhanced_table.py +++ b/tabulous/_qt/_table/_base/_enhanced_table.py @@ -360,6 +360,8 @@ def mousePressEvent(self, e: QtGui.QMouseEvent) -> None: def mouseMoveEvent(self, e: QtGui.QMouseEvent) -> None: """Scroll table plane when mouse is moved with right click.""" + if e.buttons() == Qt.MouseButton.NoButton: + return None if self._mouse_track.last_button == "right": pos = e.pos() dy = pos.y() - self._mouse_track.last_rightclick_pos.y()