rework icons, codechanges, bugfixes, typofixes
This commit is contained in:
@@ -1,31 +1,89 @@
|
||||
import darkdetect
|
||||
from omegaconf import OmegaConf
|
||||
from PyQt6 import QtGui
|
||||
|
||||
import re
|
||||
config = OmegaConf.load("config.yaml")
|
||||
config = OmegaConf.load(f"{config.icon_path}/icons.yaml")
|
||||
|
||||
path = config.icon_path
|
||||
dark = darkdetect.isDark()
|
||||
mode = "dark" if dark else "light"
|
||||
path = f"{path}/{mode}"
|
||||
class Icon:
|
||||
def __init__(self, icon_type, widget=None):
|
||||
|
||||
|
||||
class Icon:
|
||||
def __init__(self, icon_type, widget=None,recolor=False):
|
||||
dark = darkdetect.isDark()
|
||||
if dark:
|
||||
self.color = config.dark_color
|
||||
else:
|
||||
self.color = config.light_color
|
||||
self.icon = QtGui.QIcon()
|
||||
self.add_icon(icon_type)
|
||||
self.icon_path = path + config["icons"][icon_type]
|
||||
self.add_icon(self.icon_path,recolor)
|
||||
if widget is not None:
|
||||
widget.setIcon(self.icon)
|
||||
|
||||
def add_icon(self, icon_type):
|
||||
icon = config[icon_type]
|
||||
icon = f"{path}/{icon}"
|
||||
def add_icon(self, icon_path,recolor=False):
|
||||
icon = self.changeColor(icon_path,recolor)
|
||||
|
||||
# use icon bytes to create a pixmap
|
||||
pixmap = QtGui.QPixmap()
|
||||
pixmap.loadFromData(icon)
|
||||
|
||||
self.icon.addPixmap(
|
||||
QtGui.QPixmap(icon),
|
||||
pixmap,
|
||||
QtGui.QIcon.Mode.Normal,
|
||||
QtGui.QIcon.State.Off,
|
||||
)
|
||||
|
||||
def overwriteColor(self, color):
|
||||
# take the icon, read it as bytes and change the color in fill
|
||||
icon = self.changeColor(self.icon_path)
|
||||
cicon = str(icon)
|
||||
fill = re.search(r"fill=\"(.*?)\"", cicon).group(1)
|
||||
stroke = re.search(r"stroke=\"(.*?)\"", cicon)
|
||||
if stroke:
|
||||
stroke = stroke.group(1)
|
||||
|
||||
if fill and stroke:
|
||||
# replace stroke
|
||||
newicon = icon.replace(stroke.encode(), color.encode())
|
||||
else:
|
||||
newicon = icon.replace(fill.encode(), color.encode())
|
||||
|
||||
pixmap = QtGui.QPixmap()
|
||||
pixmap.loadFromData(newicon)
|
||||
|
||||
self.icon.addPixmap(
|
||||
pixmap,
|
||||
QtGui.QIcon.Mode.Normal,
|
||||
QtGui.QIcon.State.Off,
|
||||
)
|
||||
return self.icon
|
||||
|
||||
|
||||
def changeColor(self,icon_path,recolor) -> bytes:
|
||||
"""change the color of the svg icon to the color set in the config file
|
||||
|
||||
Args:
|
||||
icon_path (str): the path to the icon, usually icons/[name].svg
|
||||
|
||||
Returns:
|
||||
icon: a byte representation of the icon with the new color
|
||||
"""
|
||||
color = self.color
|
||||
with open(icon_path, "rb") as file:
|
||||
icon = file.read()
|
||||
if recolor:
|
||||
cicon = str(icon)
|
||||
|
||||
try:
|
||||
fill = re.search(r"fill=\"(.*?)\"", cicon).group(1)
|
||||
except AttributeError:
|
||||
fill = None
|
||||
if fill:
|
||||
icon = icon.replace(fill.encode(), color.encode())
|
||||
return icon
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("This is a module and can not be executed directly.")
|
||||
|
||||
Reference in New Issue
Block a user