diff --git a/rare/app.py b/rare/app.py index 4bcceecf..7dcb39e2 100644 --- a/rare/app.py +++ b/rare/app.py @@ -1,15 +1,13 @@ import configparser import logging import os -import platform import sys import time import traceback -import qtawesome from PyQt5.QtCore import QThreadPool, QSettings, QTranslator -from PyQt5.QtGui import QIcon, QPalette -from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QStyleFactory, QMessageBox +from PyQt5.QtGui import QIcon +from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QMessageBox from requests import HTTPError import rare.shared as shared @@ -17,7 +15,7 @@ from rare import languages_path, resources_path, cache_dir from rare.components.dialogs.launch_dialog import LaunchDialog from rare.components.main_window import MainWindow from rare.components.tray_icon import TrayIcon -from rare.utils.utils import load_color_scheme +from rare.utils.utils import set_color_pallete, set_style_sheet start_time = time.strftime('%y-%m-%d--%H-%M') # year-month-day-hour-minute file_name = os.path.join(cache_dir, f"logs/Rare_{start_time}.log") @@ -113,27 +111,20 @@ class App(QApplication): self.installTranslator(self.qt_translator) # Style + # lk: this is a bit silly but serves well until we have a class + # lk: store the default qt style name from the system on startup as a property for later reference + self.setProperty('rareDefaultQtStyle', self.style().objectName()) + if self.settings.value("color_scheme", None) is None and self.settings.value("style_sheet", None) is None: self.settings.setValue("color_scheme", "") self.settings.setValue("style_sheet", "RareStyle") - if color := self.settings.value("color_scheme", False): - self.setStyle(QStyleFactory.create("Fusion")) + if color_scheme := self.settings.value("color_scheme", False): self.settings.setValue("style_sheet", "") - custom_palette = load_color_scheme(os.path.join(resources_path, "colors", color + ".scheme")) - if custom_palette is not None: - self.setPalette(custom_palette) - qtawesome.set_defaults(color=custom_palette.color(QPalette.Text)) - - elif style := self.settings.value("style_sheet", False): - self.setStyle(QStyleFactory.create("Fusion")) + set_color_pallete(color_scheme) + elif style_sheet := self.settings.value("style_sheet", False): self.settings.setValue("color_scheme", "") - stylesheet = open(os.path.join(resources_path, "stylesheets", style, "stylesheet.qss")).read() - style_resource_path = os.path.join(resources_path, "stylesheets", style, "") - if platform.system() == "Windows": - style_resource_path = style_resource_path.replace('\\', '/') - self.setStyleSheet(stylesheet.replace("@path@", style_resource_path)) - qtawesome.set_defaults(color="white") + set_style_sheet(style_sheet) self.setWindowIcon(QIcon(os.path.join(resources_path, "images", "Rare.png"))) # launch app diff --git a/rare/components/tabs/settings/rare.py b/rare/components/tabs/settings/rare.py index b601a3de..7720c58f 100644 --- a/rare/components/tabs/settings/rare.py +++ b/rare/components/tabs/settings/rare.py @@ -11,7 +11,7 @@ from rare import cache_dir, shared from rare.components.tabs.settings.rpc import RPCSettings from rare.ui.components.tabs.settings.rare import Ui_RareSettings from rare.utils import utils -from rare.utils.utils import get_translations, get_color_schemes, get_style_sheets +from rare.utils.utils import get_translations, get_color_schemes, set_color_pallete, get_style_sheets, set_style_sheet logger = getLogger("RareSettings") @@ -163,9 +163,11 @@ class RareSettings(QWidget, Ui_RareSettings): self.style_select.setCurrentIndex(0) self.style_select.setDisabled(True) self.settings.setValue("color_scheme", self.color_select.currentText()) + set_color_pallete(self.color_select.currentText()) else: self.settings.setValue("color_scheme", "") self.style_select.setDisabled(False) + set_color_pallete("") self.interface_info.setVisible(True) def on_style_select_changed(self, style): @@ -173,9 +175,11 @@ class RareSettings(QWidget, Ui_RareSettings): self.color_select.setCurrentIndex(0) self.color_select.setDisabled(True) self.settings.setValue("style_sheet", self.style_select.currentText()) + set_style_sheet(self.style_select.currentText()) else: self.settings.setValue("style_sheet", "") self.color_select.setDisabled(False) + set_style_sheet("") self.interface_info.setVisible(True) def open_dir(self): diff --git a/rare/utils/utils.py b/rare/utils/utils.py index a14a2537..fe01b82d 100644 --- a/rare/utils/utils.py +++ b/rare/utils/utils.py @@ -6,10 +6,12 @@ import shutil import subprocess import sys from logging import getLogger -from typing import Tuple +from typing import Tuple, List import requests +import qtawesome from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QRunnable, QSettings, Qt +from PyQt5.QtWidgets import QApplication, QStyleFactory, QStyle from PyQt5.QtGui import QPalette, QColor, QPixmap, QImage from requests import HTTPError @@ -116,7 +118,7 @@ color_group_map = { } -def load_color_scheme(path: str): +def load_color_scheme(path: str) -> QPalette: palette = QPalette() scheme = QSettings(path, QSettings.IniFormat) try: @@ -138,7 +140,20 @@ def load_color_scheme(path: str): return palette -def get_color_schemes(): +def set_color_pallete(color_scheme: str): + if not color_scheme: + QApplication.instance().setStyle(QStyleFactory.create(QApplication.instance().property('rareDefaultQtStyle'))) + QApplication.instance().setStyleSheet("") + QApplication.instance().setPalette(QApplication.instance().style().standardPalette()) + return + QApplication.instance().setStyle(QStyleFactory.create("Fusion")) + custom_palette = load_color_scheme(os.path.join(resources_path, "colors", color_scheme + ".scheme")) + if custom_palette is not None: + QApplication.instance().setPalette(custom_palette) + qtawesome.set_defaults(color=custom_palette.color(QPalette.Text)) + + +def get_color_schemes() -> List[str]: colors = [] for file in os.listdir(os.path.join(resources_path, "colors")): if file.endswith(".scheme") and os.path.isfile(os.path.join(resources_path, "colors", file)): @@ -146,7 +161,21 @@ def get_color_schemes(): return colors -def get_style_sheets(): +def set_style_sheet(style_sheet: str): + if not style_sheet: + QApplication.instance().setStyle(QStyleFactory.create(QApplication.instance().property('rareDefaultQtStyle'))) + QApplication.instance().setStyleSheet("") + return + QApplication.instance().setStyle(QStyleFactory.create("Fusion")) + stylesheet = open(os.path.join(resources_path, "stylesheets", style_sheet, "stylesheet.qss")).read() + style_resource_path = os.path.join(resources_path, "stylesheets", style_sheet, "") + if platform.system() == "Windows": + style_resource_path = style_resource_path.replace('\\', '/') + QApplication.instance().setStyleSheet(stylesheet.replace("@path@", style_resource_path)) + qtawesome.set_defaults(color="white") + + +def get_style_sheets() -> List[str]: styles = [] for folder in os.listdir(os.path.join(resources_path, "stylesheets")): if os.path.isfile(os.path.join(resources_path, "stylesheets", folder, "stylesheet.qss")):