Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change switch color depending on state #1182

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion qt/aqt/switch.py
@@ -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 *
Expand All @@ -21,6 +22,8 @@ def __init__(
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)
Expand All @@ -29,6 +32,8 @@ def __init__(
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self._left_label = left_label
self._right_label = right_label
self._left_color = left_color
self._right_color = right_color
self._path_radius = radius
self._knob_radius = radius - self._margin
self._left_position = self._position = self._path_radius + self._margin
Expand All @@ -55,6 +60,11 @@ def end_position(self) -> int:
def label(self) -> str:
return self._right_label if self.isChecked() else self._left_label

@property
def knob_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,
Expand Down Expand Up @@ -93,7 +103,7 @@ def _current_knob_rectangle(self) -> QRectF:
)

def _paint_knob(self, painter: QPainter) -> None:
painter.setBrush(QBrush(theme_manager.qcolor(colors.LINK)))
painter.setBrush(QBrush(self.knob_color))
painter.drawEllipse(self._current_knob_rectangle())

def _paint_label(self, painter: QPainter) -> None:
Expand Down