Skip to content

Commit

Permalink
fix colorrole, painter
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjinliu committed May 10, 2024
1 parent 7d500ed commit 53d3797
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
3 changes: 2 additions & 1 deletion tabulous/_qt/_preference/_shared.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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())
Expand Down
20 changes: 12 additions & 8 deletions tabulous/_qt/_preference/_theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 21 additions & 0 deletions tabulous/_qt/_qt_const.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
from pathlib import Path
from qtpy import QtGui, QT6

# Monospace font
if sys.platform == "win32":
Expand All @@ -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)
3 changes: 2 additions & 1 deletion tabulous/_qt/_table/_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 53d3797

Please sign in to comment.