Thu, 09 Dec 2021 20:07:08 +0100
Continued implementing a manager for color themes.
# -*- coding: utf-8 -*- # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing a manager object for color themes. """ import json import os import re from PyQt6.QtCore import QObject from EricWidgets import EricMessageBox, EricFileDialog import Preferences class ThemeManager(QObject): """ Class implementing a manager object for color themes. """ KeyPatternList = [ "Diff/.*Color", "Editor/Colour/", "IRC/.*Colou?r", "Project/Colour", "Scintilla/.*color", "Scintilla/.*paper", "WebBrowser/.*Colou?r", ] KeyList = [ "Debugger/BgColorChanged", "Debugger/BgColorNew", "UI/IconBarColor", "UI/LogStdErrColour", "UI/NotificationCriticalBackground", "UI/NotificationCriticalForeground", "UI/NotificationWarningBackground", "UI/NotificationWarningForeground", ] def __init__(self: "ThemeManager", parent: QObject = None): """ Constructor @param parent reference to the parent object (defaults to None) @type QObject (optional) """ super().__init__(parent) def importTheme(self: "ThemeManager"): """ Public method to import a theme file and set the colors. """ # TODO: not yet implemented # TODO: add entry for the current QSS file # - on import save it in the eric configuration directory # if such file does not exist already # - on import set stylesheet key to the saved file def exportTheme(self: "ThemeManager"): """ Public method to export the current colors to a theme file. """ filename, selectedFilter = EricFileDialog.getSaveFileNameAndFilter( None, self.tr("Export Theme"), os.path.expanduser("~"), self.tr("eric Theme Files (*.ethj);;All Files (*)"), "", EricFileDialog.DontConfirmOverwrite ) if filename: ext = os.path.splitext(filename)[1] if not ext: filename = "{0}{1}".format( filename, selectedFilter.rsplit(None, 1)[-1][2:-1]) ok = ( EricMessageBox.yesNo( None, self.tr("Export Theme"), self.tr( """<p>The theme file <b>{0}</b> exists""" """ already. Overwrite it?</p>""").format(filename)) if os.path.exists(filename) else True ) if ok: # step 1: generate a dictionary with all color settings settings = Preferences.getSettings() keyFilterRe = re.compile("|".join( ThemeManager.KeyPatternList + ThemeManager.KeyList)) keys = [k for k in settings.allKeys() if keyFilterRe.match(k)] colorsDict = {} for key in keys: colorsDict[key] = settings.value(key) # step 2: read the contents of the current stylesheet stylesheetDict = { "contents": "", "name": "" } stylesheet = Preferences.getUI("StyleSheet") if stylesheet and os.path.exists(stylesheet): try: with open(stylesheet, "r") as f: stylesheetDict["contents"] = f.read() stylesheetDict["name"] = os.path.basename(stylesheet) except OSError as err: EricMessageBox.critical( None, self.tr("Export Theme"), self.tr( "<p>The styleshhet file <b>{0}</b> could not" " be read.</p><p>Reason: {1}</p>" ).format(stylesheet, str(err)) ) themeDict = { "colors": colorsDict, "stylesheet": stylesheetDict, } try: jsonString = json.dumps(themeDict, indent=2) with open(filename, "w") as f: f.write(jsonString) except (TypeError, OSError) as err: EricMessageBox.critical( None, self.tr("Export Theme"), self.tr( "<p>The theme file <b>{0}</b> could not" " be written.</p><p>Reason: {1}</p>" ).format(filename, str(err)) )