Change switch color depending on state

Make knob overlap path.
This commit is contained in:
RumovZ 2021-05-22 09:14:50 +02:00
parent 3d4cf26758
commit cba5c2253a

View file

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright: Ankitects Pty Ltd and contributors # Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from typing import Tuple
from aqt import colors from aqt import colors
from aqt.qt import * from aqt.qt import *
@ -21,6 +22,8 @@ class Switch(QAbstractButton):
radius: int = 10, radius: int = 10,
left_label: str = "", left_label: str = "",
right_label: str = "", right_label: str = "",
left_color: Tuple[str, str] = colors.FLAG4_BG,
right_color: Tuple[str, str] = colors.FLAG3_BG,
parent: QWidget = None, parent: QWidget = None,
) -> None: ) -> None:
super().__init__(parent=parent) super().__init__(parent=parent)
@ -29,9 +32,10 @@ class Switch(QAbstractButton):
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self._left_label = left_label self._left_label = left_label
self._right_label = right_label self._right_label = right_label
self._path_radius = radius self._left_color = left_color
self._knob_radius = radius - self._margin self._right_color = right_color
self._left_position = self._position = self._path_radius + self._margin self._path_radius = radius - self._margin
self._knob_radius = self._left_position = self._position = radius
self._right_position = 3 * self._path_radius + self._margin self._right_position = 3 * self._path_radius + self._margin
@pyqtProperty(int) # type: ignore @pyqtProperty(int) # type: ignore
@ -55,6 +59,11 @@ class Switch(QAbstractButton):
def label(self) -> str: def label(self) -> str:
return self._right_label if self.isChecked() else self._left_label return self._right_label if self.isChecked() else self._left_label
@property
def path_color(self) -> QColor:
color = self._right_color if self.isChecked() else self._left_color
return theme_manager.qcolor(color)
def sizeHint(self) -> QSize: def sizeHint(self) -> QSize:
return QSize( return QSize(
4 * self._path_radius + 2 * self._margin, 4 * self._path_radius + 2 * self._margin,
@ -75,7 +84,7 @@ class Switch(QAbstractButton):
self._paint_label(painter) self._paint_label(painter)
def _paint_path(self, painter: QPainter) -> None: def _paint_path(self, painter: QPainter) -> None:
painter.setBrush(QBrush(theme_manager.qcolor(colors.FRAME_BG))) painter.setBrush(QBrush(self.path_color))
rectangle = QRectF( rectangle = QRectF(
self._margin, self._margin,
self._margin, self._margin,
@ -87,19 +96,19 @@ class Switch(QAbstractButton):
def _current_knob_rectangle(self) -> QRectF: def _current_knob_rectangle(self) -> QRectF:
return QRectF( return QRectF(
self.position - self._knob_radius, # type: ignore self.position - self._knob_radius, # type: ignore
2 * self._margin, 0,
2 * self._knob_radius, 2 * self._knob_radius,
2 * self._knob_radius, 2 * self._knob_radius,
) )
def _paint_knob(self, painter: QPainter) -> None: def _paint_knob(self, painter: QPainter) -> None:
painter.setBrush(QBrush(theme_manager.qcolor(colors.LINK))) painter.setBrush(QBrush(theme_manager.qcolor(colors.FRAME_BG)))
painter.drawEllipse(self._current_knob_rectangle()) painter.drawEllipse(self._current_knob_rectangle())
def _paint_label(self, painter: QPainter) -> None: def _paint_label(self, painter: QPainter) -> None:
painter.setPen(QColor("white")) painter.setPen(theme_manager.qcolor(colors.SLIGHTLY_GREY_TEXT))
font = painter.font() font = painter.font()
font.setPixelSize(int(1.5 * self._knob_radius)) font.setPixelSize(int(1.2 * self._knob_radius))
painter.setFont(font) painter.setFont(font)
painter.drawText(self._current_knob_rectangle(), Qt.AlignCenter, self.label) painter.drawText(self._current_knob_rectangle(), Qt.AlignCenter, self.label)