eric7/Preferences/ThemeManager.py

branch
eric7
changeset 8820
149f941d072e
parent 8819
982fb074be98
child 8842
80ce644a6067
equal deleted inserted replaced
8819:982fb074be98 8820:149f941d072e
13 13
14 from PyQt6.QtCore import QObject 14 from PyQt6.QtCore import QObject
15 15
16 from EricWidgets import EricMessageBox, EricFileDialog 16 from EricWidgets import EricMessageBox, EricFileDialog
17 17
18 import Globals
18 import Preferences 19 import Preferences
20
21 from eric7config import getConfig
19 22
20 23
21 class ThemeManager(QObject): 24 class ThemeManager(QObject):
22 """ 25 """
23 Class implementing a manager object for color themes. 26 Class implementing a manager object for color themes.
24 """ 27 """
25 KeyPatternList = [ 28 ColorKeyPatternList = [
26 "Diff/.*Color", 29 "Diff/.*Color",
27 "Editor/Colour/", 30 "Editor/Colour/",
28 "IRC/.*Colou?r", 31 "IRC/.*Colou?r",
29 "Project/Colour", 32 "Project/Colour",
30 "Scintilla/.*color", 33 "Scintilla/.*color",
31 "Scintilla/.*paper", 34 "Scintilla/.*paper",
32 "WebBrowser/.*Colou?r", 35 "WebBrowser/.*Colou?r",
33 ] 36 ]
34 KeyList = [ 37 ColorKeyList = [
35 "Debugger/BgColorChanged", 38 "Debugger/BgColorChanged",
36 "Debugger/BgColorNew", 39 "Debugger/BgColorNew",
37 "UI/IconBarColor", 40 "UI/IconBarColor",
38 "UI/LogStdErrColour", 41 "UI/LogStdErrColour",
39 "UI/NotificationCriticalBackground", 42 "UI/NotificationCriticalBackground",
49 @param parent reference to the parent object (defaults to None) 52 @param parent reference to the parent object (defaults to None)
50 @type QObject (optional) 53 @type QObject (optional)
51 """ 54 """
52 super().__init__(parent) 55 super().__init__(parent)
53 56
54 def importTheme(self: "ThemeManager"): 57 def importTheme(self: "ThemeManager") -> bool:
55 """ 58 """
56 Public method to import a theme file and set the colors. 59 Public method to import a theme file and set the colors.
57 """
58 # TODO: not yet implemented
59 60
60 # TODO: add entry for the current QSS file 61 @return flag indicating a successful import
61 # - on import save it in the eric configuration directory 62 @rtype bool
62 # if such file does not exist already 63 """
63 # - on import set stylesheet key to the saved file 64 filename = EricFileDialog.getOpenFileName(
65 None,
66 self.tr("Import Theme"),
67 getConfig("ericThemesDir"),
68 self.tr("eric Theme Files (*.ethj);;All Files(*)")
69 )
70 if filename:
71 try:
72 with open(filename, "r") as f:
73 jsonString = f.read()
74 themeDict = json.loads(jsonString)
75 except (TypeError, OSError) as err:
76 EricMessageBox.critical(
77 None,
78 self.tr("Import Theme"),
79 self.tr(
80 "<p>The theme file <b>{0}</b> could not"
81 " be read.</p><p>Reason: {1}</p>"
82 ).format(filename, str(err))
83 )
84 return False
85
86 # step 1: process stylesheet data
87 stylesheetDict = themeDict["stylesheet"]
88 if stylesheetDict["name"]:
89 stylesheetsDir = os.path.join(
90 Globals.getConfigDir(), "stylesheets")
91 if not os.path.exists(stylesheetsDir):
92 os.makedirs(stylesheetsDir)
93 stylesheetFile = os.path.join(
94 stylesheetsDir, stylesheetDict["name"])
95 ok = EricMessageBox.yesNo(
96 None,
97 self.tr("Import Theme"),
98 self.tr(
99 "The stylesheet file {0} exists already."
100 " Shall it be overwritten?"
101 ).format(stylesheetDict["name"])
102 ) if os.path.exists(stylesheetFile) else True
103 if ok:
104 try:
105 with open(stylesheetFile, "w") as f:
106 f.write(stylesheetDict["contents"])
107 except OSError as err:
108 EricMessageBox.critical(
109 None,
110 self.tr("Import Theme"),
111 self.tr(
112 "<p>The stylesheet file <b>{0}</b> could"
113 " not be written.</p><p>Reason: {1}</p>"
114 ).format(stylesheetFile, str(err))
115 )
116 stylesheetFile = ""
117 Preferences.setUI("StyleSheet", stylesheetFile)
118
119 # step 2: transfer the color entries
120 settings = Preferences.getSettings()
121 colorsDict = themeDict["colors"]
122 for key, value in colorsDict.items():
123 settings.setValue(key, value)
124
125 Preferences.syncPreferences()
126 return True
127
128 return False
64 129
65 def exportTheme(self: "ThemeManager"): 130 def exportTheme(self: "ThemeManager"):
66 """ 131 """
67 Public method to export the current colors to a theme file. 132 Public method to export the current colors to a theme file.
68 """ 133 """
69 filename, selectedFilter = EricFileDialog.getSaveFileNameAndFilter( 134 filename, selectedFilter = EricFileDialog.getSaveFileNameAndFilter(
70 None, 135 None,
71 self.tr("Export Theme"), 136 self.tr("Export Theme"),
72 os.path.expanduser("~"), 137 os.path.expanduser("~"),
73 self.tr("eric Theme Files (*.ethj);;All Files (*)"), 138 self.tr("eric Theme Files (*.ethj)"),
74 "", 139 "",
75 EricFileDialog.DontConfirmOverwrite 140 EricFileDialog.DontConfirmOverwrite
76 ) 141 )
77 if filename: 142 if filename:
78 ext = os.path.splitext(filename)[1] 143 ext = os.path.splitext(filename)[1]
93 ) 158 )
94 159
95 if ok: 160 if ok:
96 # step 1: generate a dictionary with all color settings 161 # step 1: generate a dictionary with all color settings
97 settings = Preferences.getSettings() 162 settings = Preferences.getSettings()
98 keyFilterRe = re.compile("|".join( 163 colorKeyFilterRe = re.compile("|".join(
99 ThemeManager.KeyPatternList + ThemeManager.KeyList)) 164 ThemeManager.ColorKeyPatternList +
100 165 ThemeManager.ColorKeyList
101 keys = [k for k in settings.allKeys() if keyFilterRe.match(k)] 166 ))
167
168 keys = [k for k in settings.allKeys()
169 if colorKeyFilterRe.match(k)]
102 colorsDict = {} 170 colorsDict = {}
103 for key in keys: 171 for key in keys:
104 colorsDict[key] = settings.value(key) 172 colorsDict[key] = settings.value(key)
105 173
106 # step 2: read the contents of the current stylesheet 174 # step 2: read the contents of the current stylesheet
117 except OSError as err: 185 except OSError as err:
118 EricMessageBox.critical( 186 EricMessageBox.critical(
119 None, 187 None,
120 self.tr("Export Theme"), 188 self.tr("Export Theme"),
121 self.tr( 189 self.tr(
122 "<p>The styleshhet file <b>{0}</b> could not" 190 "<p>The stylesheet file <b>{0}</b> could not"
123 " be read.</p><p>Reason: {1}</p>" 191 " be read.</p><p>Reason: {1}</p>"
124 ).format(stylesheet, str(err)) 192 ).format(stylesheet, str(err))
125 ) 193 )
126 194
127 themeDict = { 195 themeDict = {

eric ide

mercurial