Skip to content

Commit

Permalink
disabled toolbutton
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjinliu committed Feb 12, 2023
1 parent 4a387d5 commit 4aa0909
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
16 changes: 15 additions & 1 deletion tabulous/_qt/_svg.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from __future__ import annotations

from typing import Callable
from pathlib import Path
from qtpy import QtGui, QtCore, QtSvg
from qtpy.QtCore import Qt
Expand Down Expand Up @@ -42,9 +44,12 @@ def __init__(
self,
xml: str,
color: str = "#000000",
converter: Callable[[QtGui.QColor], QtGui.QColor] = lambda x: x,
) -> None:
self._color = QtGui.QColor(color)
col = converter(self._color)
self._xml = xml
colorized = xml.replace(self._COLOR_ARG, f"{color}")
colorized = xml.replace(self._COLOR_ARG, col.name())
super().__init__(SVGBufferIconEngine(colorized))

@lru_cache
Expand All @@ -59,3 +64,12 @@ def fromfile(cls: type[QColoredSVGIcon], path: str | Path, color="#000000"):
with open(path) as f:
xml = f.read()
return cls(xml, color=color)

def color(self) -> QtGui.QColor:
"""Color of the icon"""
return self._color

def with_converted(
self, converter: Callable[[QtGui.QColor], QtGui.QColor]
) -> QColoredSVGIcon:
return QColoredSVGIcon(self._xml, color=self._color.name(), converter=converter)
29 changes: 26 additions & 3 deletions tabulous/_qt/_toolbar/_toolbutton.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations
from pathlib import Path
from qtpy import QtWidgets as QtW
from qtpy import QtWidgets as QtW, QtGui

from tabulous.color import normalize_color
from tabulous._qt._svg import QColoredSVGIcon


Expand All @@ -26,9 +27,31 @@ def setIcon(self, icon: QColoredSVGIcon | str | Path) -> None:
self._icon = icon
return super().setIcon(icon)

def updateColor(self, color):
def updateColor(self, color: str):
"""Update svg color."""
return self.setIcon(self.icon().colored(color))
icon = self.icon().colored(color)
if not self.isEnabled():
icon = icon.with_converted(self._mix_with_background)
self.setIcon(icon)
return None

def setEnabled(self, a0: bool) -> None:
if self.isEnabled() != a0:
# blend icon color with background color
if a0:
self.setIcon(self.icon().with_converted(lambda x: x))
else:
self.setIcon(self.icon().with_converted(self._mix_with_background))
return super().setEnabled(a0)

def backgroundColor(self) -> QtGui.QColor:
"""Get background color."""
return self.palette().color(self.backgroundRole())

def _mix_with_background(self, color: QtGui.QColor) -> QtGui.QColor:
"""Mix color with background color."""
bg = self.palette().color(self.backgroundRole()).getRgb()
return QtGui.QColor(*normalize_color(color.getRgb()).mix(bg, 0.7))


class QMoreToolButton(QColoredToolButton):
Expand Down
1 change: 1 addition & 0 deletions tabulous/style/_style.qss
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ QPushButton:checked {

QPushButton:disabled {
background-color: #[background];
color: #[background1];
border-radius: 2px;
padding: 4px;
border: 0px;
Expand Down

0 comments on commit 4aa0909

Please sign in to comment.