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:
parent
c952c978af
commit
26bbdf1c0e
3 changed files with 49 additions and 25 deletions
31
rare/app.py
31
rare/app.py
|
@ -1,15 +1,13 @@
|
||||||
import configparser
|
import configparser
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import platform
|
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
import qtawesome
|
|
||||||
from PyQt5.QtCore import QThreadPool, QSettings, QTranslator
|
from PyQt5.QtCore import QThreadPool, QSettings, QTranslator
|
||||||
from PyQt5.QtGui import QIcon, QPalette
|
from PyQt5.QtGui import QIcon
|
||||||
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QStyleFactory, QMessageBox
|
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QMessageBox
|
||||||
from requests import HTTPError
|
from requests import HTTPError
|
||||||
|
|
||||||
import rare.shared as shared
|
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.dialogs.launch_dialog import LaunchDialog
|
||||||
from rare.components.main_window import MainWindow
|
from rare.components.main_window import MainWindow
|
||||||
from rare.components.tray_icon import TrayIcon
|
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
|
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")
|
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)
|
self.installTranslator(self.qt_translator)
|
||||||
|
|
||||||
# Style
|
# 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:
|
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("color_scheme", "")
|
||||||
self.settings.setValue("style_sheet", "RareStyle")
|
self.settings.setValue("style_sheet", "RareStyle")
|
||||||
|
|
||||||
if color := self.settings.value("color_scheme", False):
|
if color_scheme := self.settings.value("color_scheme", False):
|
||||||
self.setStyle(QStyleFactory.create("Fusion"))
|
|
||||||
self.settings.setValue("style_sheet", "")
|
self.settings.setValue("style_sheet", "")
|
||||||
custom_palette = load_color_scheme(os.path.join(resources_path, "colors", color + ".scheme"))
|
set_color_pallete(color_scheme)
|
||||||
if custom_palette is not None:
|
elif style_sheet := self.settings.value("style_sheet", False):
|
||||||
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"))
|
|
||||||
self.settings.setValue("color_scheme", "")
|
self.settings.setValue("color_scheme", "")
|
||||||
stylesheet = open(os.path.join(resources_path, "stylesheets", style, "stylesheet.qss")).read()
|
set_style_sheet(style_sheet)
|
||||||
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")
|
|
||||||
self.setWindowIcon(QIcon(os.path.join(resources_path, "images", "Rare.png")))
|
self.setWindowIcon(QIcon(os.path.join(resources_path, "images", "Rare.png")))
|
||||||
|
|
||||||
# launch app
|
# launch app
|
||||||
|
|
|
@ -11,7 +11,7 @@ from rare import cache_dir, shared
|
||||||
from rare.components.tabs.settings.rpc import RPCSettings
|
from rare.components.tabs.settings.rpc import RPCSettings
|
||||||
from rare.ui.components.tabs.settings.rare import Ui_RareSettings
|
from rare.ui.components.tabs.settings.rare import Ui_RareSettings
|
||||||
from rare.utils import utils
|
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")
|
logger = getLogger("RareSettings")
|
||||||
|
|
||||||
|
@ -163,9 +163,11 @@ class RareSettings(QWidget, Ui_RareSettings):
|
||||||
self.style_select.setCurrentIndex(0)
|
self.style_select.setCurrentIndex(0)
|
||||||
self.style_select.setDisabled(True)
|
self.style_select.setDisabled(True)
|
||||||
self.settings.setValue("color_scheme", self.color_select.currentText())
|
self.settings.setValue("color_scheme", self.color_select.currentText())
|
||||||
|
set_color_pallete(self.color_select.currentText())
|
||||||
else:
|
else:
|
||||||
self.settings.setValue("color_scheme", "")
|
self.settings.setValue("color_scheme", "")
|
||||||
self.style_select.setDisabled(False)
|
self.style_select.setDisabled(False)
|
||||||
|
set_color_pallete("")
|
||||||
self.interface_info.setVisible(True)
|
self.interface_info.setVisible(True)
|
||||||
|
|
||||||
def on_style_select_changed(self, style):
|
def on_style_select_changed(self, style):
|
||||||
|
@ -173,9 +175,11 @@ class RareSettings(QWidget, Ui_RareSettings):
|
||||||
self.color_select.setCurrentIndex(0)
|
self.color_select.setCurrentIndex(0)
|
||||||
self.color_select.setDisabled(True)
|
self.color_select.setDisabled(True)
|
||||||
self.settings.setValue("style_sheet", self.style_select.currentText())
|
self.settings.setValue("style_sheet", self.style_select.currentText())
|
||||||
|
set_style_sheet(self.style_select.currentText())
|
||||||
else:
|
else:
|
||||||
self.settings.setValue("style_sheet", "")
|
self.settings.setValue("style_sheet", "")
|
||||||
self.color_select.setDisabled(False)
|
self.color_select.setDisabled(False)
|
||||||
|
set_style_sheet("")
|
||||||
self.interface_info.setVisible(True)
|
self.interface_info.setVisible(True)
|
||||||
|
|
||||||
def open_dir(self):
|
def open_dir(self):
|
||||||
|
|
|
@ -6,10 +6,12 @@ import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
from typing import Tuple
|
from typing import Tuple, List
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
import qtawesome
|
||||||
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QRunnable, QSettings, Qt
|
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 PyQt5.QtGui import QPalette, QColor, QPixmap, QImage
|
||||||
from requests import HTTPError
|
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()
|
palette = QPalette()
|
||||||
scheme = QSettings(path, QSettings.IniFormat)
|
scheme = QSettings(path, QSettings.IniFormat)
|
||||||
try:
|
try:
|
||||||
|
@ -138,7 +140,20 @@ def load_color_scheme(path: str):
|
||||||
return palette
|
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 = []
|
colors = []
|
||||||
for file in os.listdir(os.path.join(resources_path, "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)):
|
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
|
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 = []
|
styles = []
|
||||||
for folder in os.listdir(os.path.join(resources_path, "stylesheets")):
|
for folder in os.listdir(os.path.join(resources_path, "stylesheets")):
|
||||||
if os.path.isfile(os.path.join(resources_path, "stylesheets", folder, "stylesheet.qss")):
|
if os.path.isfile(os.path.join(resources_path, "stylesheets", folder, "stylesheet.qss")):
|
||||||
|
|
Loading…
Reference in a new issue