mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 22:12:21 -04:00
Change switch color depending on state
Make knob overlap path.
This commit is contained in:
parent
3d4cf26758
commit
cba5c2253a
1 changed files with 17 additions and 8 deletions
|
@ -1,6 +1,7 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Copyright: Ankitects Pty Ltd and contributors
|
||||
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||
from typing import Tuple
|
||||
|
||||
from aqt import colors
|
||||
from aqt.qt import *
|
||||
|
@ -21,6 +22,8 @@ class Switch(QAbstractButton):
|
|||
radius: int = 10,
|
||||
left_label: str = "",
|
||||
right_label: str = "",
|
||||
left_color: Tuple[str, str] = colors.FLAG4_BG,
|
||||
right_color: Tuple[str, str] = colors.FLAG3_BG,
|
||||
parent: QWidget = None,
|
||||
) -> None:
|
||||
super().__init__(parent=parent)
|
||||
|
@ -29,9 +32,10 @@ class Switch(QAbstractButton):
|
|||
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
|
||||
self._left_label = left_label
|
||||
self._right_label = right_label
|
||||
self._path_radius = radius
|
||||
self._knob_radius = radius - self._margin
|
||||
self._left_position = self._position = self._path_radius + self._margin
|
||||
self._left_color = left_color
|
||||
self._right_color = right_color
|
||||
self._path_radius = radius - self._margin
|
||||
self._knob_radius = self._left_position = self._position = radius
|
||||
self._right_position = 3 * self._path_radius + self._margin
|
||||
|
||||
@pyqtProperty(int) # type: ignore
|
||||
|
@ -55,6 +59,11 @@ class Switch(QAbstractButton):
|
|||
def label(self) -> str:
|
||||
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:
|
||||
return QSize(
|
||||
4 * self._path_radius + 2 * self._margin,
|
||||
|
@ -75,7 +84,7 @@ class Switch(QAbstractButton):
|
|||
self._paint_label(painter)
|
||||
|
||||
def _paint_path(self, painter: QPainter) -> None:
|
||||
painter.setBrush(QBrush(theme_manager.qcolor(colors.FRAME_BG)))
|
||||
painter.setBrush(QBrush(self.path_color))
|
||||
rectangle = QRectF(
|
||||
self._margin,
|
||||
self._margin,
|
||||
|
@ -87,19 +96,19 @@ class Switch(QAbstractButton):
|
|||
def _current_knob_rectangle(self) -> QRectF:
|
||||
return QRectF(
|
||||
self.position - self._knob_radius, # type: ignore
|
||||
2 * self._margin,
|
||||
0,
|
||||
2 * self._knob_radius,
|
||||
2 * self._knob_radius,
|
||||
)
|
||||
|
||||
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())
|
||||
|
||||
def _paint_label(self, painter: QPainter) -> None:
|
||||
painter.setPen(QColor("white"))
|
||||
painter.setPen(theme_manager.qcolor(colors.SLIGHTLY_GREY_TEXT))
|
||||
font = painter.font()
|
||||
font.setPixelSize(int(1.5 * self._knob_radius))
|
||||
font.setPixelSize(int(1.2 * self._knob_radius))
|
||||
painter.setFont(font)
|
||||
painter.drawText(self._current_knob_rectangle(), Qt.AlignCenter, self.label)
|
||||
|
||||
|
|
Loading…
Reference in a new issue