1
0
Fork 0
mirror of synced 2024-06-17 01:54:46 +12:00

RareSettings: Reload themes dynamically on selection

It is a unpolished but it serves well as a way to preview the theme.
Awesome font icons are not re-rendered in the correct colors.
Some styles might look a bit broken until restart.
This commit is contained in:
Stelios Tsampas 2021-11-12 15:17:06 +02:00
parent c952c978af
commit 26bbdf1c0e
No known key found for this signature in database
GPG key ID: 2FAEBF7B5BE5FD7C
3 changed files with 49 additions and 25 deletions

View file

@ -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

View file

@ -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):

View file

@ -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")):