eric7/Preferences/ThemeManager.py

branch
eric7
changeset 8819
982fb074be98
parent 8817
92214d84beef
child 8820
149f941d072e
equal deleted inserted replaced
8818:2c14c7a09032 8819:982fb074be98
5 5
6 """ 6 """
7 Module implementing a manager object for color themes. 7 Module implementing a manager object for color themes.
8 """ 8 """
9 9
10 import json
11 import os
10 import re 12 import re
11 13
12 from PyQt6.QtCore import QObject 14 from PyQt6.QtCore import QObject
15
16 from EricWidgets import EricMessageBox, EricFileDialog
13 17
14 import Preferences 18 import Preferences
15 19
16 20
17 class ThemeManager(QObject): 21 class ThemeManager(QObject):
36 "UI/NotificationCriticalForeground", 40 "UI/NotificationCriticalForeground",
37 "UI/NotificationWarningBackground", 41 "UI/NotificationWarningBackground",
38 "UI/NotificationWarningForeground", 42 "UI/NotificationWarningForeground",
39 ] 43 ]
40 44
41 def __init__(self, parent=None): 45 def __init__(self: "ThemeManager", parent: QObject = None):
42 """ 46 """
43 Constructor 47 Constructor
44 48
45 @param parent reference to the parent object (defaults to None) 49 @param parent reference to the parent object (defaults to None)
46 @type QObject (optional) 50 @type QObject (optional)
47 """ 51 """
48 super().__init__(parent) 52 super().__init__(parent)
49 53
50 def importTheme(self): 54 def importTheme(self: "ThemeManager"):
51 """ 55 """
52 Public method to import a theme file and set the colors. 56 Public method to import a theme file and set the colors.
53 """ 57 """
54 # TODO: not yet implemented 58 # TODO: not yet implemented
59
60 # TODO: add entry for the current QSS file
61 # - on import save it in the eric configuration directory
62 # if such file does not exist already
63 # - on import set stylesheet key to the saved file
55 64
56 def exportTheme(self): 65 def exportTheme(self: "ThemeManager"):
57 """ 66 """
58 Public method to export the current colors to a theme file. 67 Public method to export the current colors to a theme file.
59 """ 68 """
60 # TODO: not yet implemented 69 filename, selectedFilter = EricFileDialog.getSaveFileNameAndFilter(
61 settings = Preferences.getSettings() 70 None,
62 keyFilterRe = re.compile("|".join( 71 self.tr("Export Theme"),
63 ThemeManager.KeyPatternList + ThemeManager.KeyList)) 72 os.path.expanduser("~"),
64 73 self.tr("eric Theme Files (*.ethj);;All Files (*)"),
65 keys = [k for k in settings.allKeys() if keyFilterRe.match(k)] 74 "",
66 themeDict = {} 75 EricFileDialog.DontConfirmOverwrite
67 for key in keys: 76 )
68 themeDict[key] = settings.value(key) 77 if filename:
69 78 ext = os.path.splitext(filename)[1]
70 # TODO: save to a json file *.ethj 79 if not ext:
80 filename = "{0}{1}".format(
81 filename,
82 selectedFilter.rsplit(None, 1)[-1][2:-1])
83
84 ok = (
85 EricMessageBox.yesNo(
86 None,
87 self.tr("Export Theme"),
88 self.tr(
89 """<p>The theme file <b>{0}</b> exists"""
90 """ already. Overwrite it?</p>""").format(filename))
91 if os.path.exists(filename) else
92 True
93 )
94
95 if ok:
96 # step 1: generate a dictionary with all color settings
97 settings = Preferences.getSettings()
98 keyFilterRe = re.compile("|".join(
99 ThemeManager.KeyPatternList + ThemeManager.KeyList))
100
101 keys = [k for k in settings.allKeys() if keyFilterRe.match(k)]
102 colorsDict = {}
103 for key in keys:
104 colorsDict[key] = settings.value(key)
105
106 # step 2: read the contents of the current stylesheet
107 stylesheetDict = {
108 "contents": "",
109 "name": ""
110 }
111 stylesheet = Preferences.getUI("StyleSheet")
112 if stylesheet and os.path.exists(stylesheet):
113 try:
114 with open(stylesheet, "r") as f:
115 stylesheetDict["contents"] = f.read()
116 stylesheetDict["name"] = os.path.basename(stylesheet)
117 except OSError as err:
118 EricMessageBox.critical(
119 None,
120 self.tr("Export Theme"),
121 self.tr(
122 "<p>The styleshhet file <b>{0}</b> could not"
123 " be read.</p><p>Reason: {1}</p>"
124 ).format(stylesheet, str(err))
125 )
126
127 themeDict = {
128 "colors": colorsDict,
129 "stylesheet": stylesheetDict,
130 }
131
132 try:
133 jsonString = json.dumps(themeDict, indent=2)
134 with open(filename, "w") as f:
135 f.write(jsonString)
136 except (TypeError, OSError) as err:
137 EricMessageBox.critical(
138 None,
139 self.tr("Export Theme"),
140 self.tr(
141 "<p>The theme file <b>{0}</b> could not"
142 " be written.</p><p>Reason: {1}</p>"
143 ).format(filename, str(err))
144 )

eric ide

mercurial