6 """ |
6 """ |
7 Class implementing a specialized application class. |
7 Class implementing a specialized application class. |
8 """ |
8 """ |
9 |
9 |
10 from PyQt6.QtCore import Qt, QCoreApplication |
10 from PyQt6.QtCore import Qt, QCoreApplication |
11 from PyQt6.QtGui import QPalette |
11 from PyQt6.QtGui import QColor, QPalette |
12 from PyQt6.QtWidgets import QApplication |
12 from PyQt6.QtWidgets import QApplication |
13 |
13 |
14 QCoreApplication.setAttribute( |
14 QCoreApplication.setAttribute( |
15 Qt.ApplicationAttribute.AA_ShareOpenGLContexts, True) |
15 Qt.ApplicationAttribute.AA_ShareOpenGLContexts, True) |
|
16 |
|
17 from . import EricMessageBox |
16 |
18 |
17 |
19 |
18 class EricApplication(QApplication): |
20 class EricApplication(QApplication): |
19 """ |
21 """ |
20 Eric application class with an object registry. |
22 Eric application class with an object registry. |
21 """ |
23 """ |
|
24 PaletteRoleMapping = { |
|
25 "alternate-base": QPalette.ColorRole.AlternateBase, |
|
26 "base": QPalette.ColorRole.Base, |
|
27 "text": QPalette.ColorRole.Text, |
|
28 "bright-text": QPalette.ColorRole.BrightText, |
|
29 "placeholder-text": QPalette.ColorRole.PlaceholderText, |
|
30 "window": QPalette.ColorRole.Window, |
|
31 " window-text": QPalette.ColorRole.WindowText, |
|
32 "tooltip-base": QPalette.ColorRole.ToolTipBase, |
|
33 "tooltip-text": QPalette.ColorRole.ToolTipText, |
|
34 "button": QPalette.ColorRole.Button, |
|
35 "button-text": QPalette.ColorRole.ButtonText, |
|
36 "highlight": QPalette.ColorRole.Highlight, |
|
37 "highlighted-text": QPalette.ColorRole.HighlightedText, |
|
38 "link": QPalette.ColorRole.Link, |
|
39 "link-visited": QPalette.ColorRole.LinkVisited, |
|
40 } |
|
41 |
22 def __init__(self, argv): |
42 def __init__(self, argv): |
23 """ |
43 """ |
24 Constructor |
44 Constructor |
25 |
45 |
26 @param argv command line arguments |
46 @param argv command line arguments |
156 raise KeyError( |
176 raise KeyError( |
157 'Pluginobject "{0}" is not registered.'.format(name)) |
177 'Pluginobject "{0}" is not registered.'.format(name)) |
158 |
178 |
159 return self.__pluginObjectRegistry[name][1] |
179 return self.__pluginObjectRegistry[name][1] |
160 |
180 |
|
181 def setStyleSheetFile(self, filename): |
|
182 """ |
|
183 Public method to read a QSS style sheet file and set the application |
|
184 style sheet based on its contents. |
|
185 |
|
186 @param filename name of the QSS style sheet file |
|
187 @type str |
|
188 """ |
|
189 if filename: |
|
190 try: |
|
191 with open(filename, "r", encoding="utf-8") as f: |
|
192 styleSheet = f.read() |
|
193 except OSError as msg: |
|
194 EricMessageBox.warning( |
|
195 self, |
|
196 QCoreApplication.translate( |
|
197 "EricApplication", "Loading Style Sheet"), |
|
198 QCoreApplication.translate( |
|
199 "EricApplication", |
|
200 """<p>The Qt Style Sheet file <b>{0}</b> could""" |
|
201 """ not be read.<br>Reason: {1}</p>""") |
|
202 .format(filename, str(msg))) |
|
203 return |
|
204 else: |
|
205 filename = "" |
|
206 |
|
207 if "QPalette {" in styleSheet: |
|
208 self.__setPaletteFromStyleSheet(styleSheet) |
|
209 |
|
210 ericApp().setStyleSheet(styleSheet) |
|
211 |
|
212 def __setPaletteFromStyleSheet(self, styleSheet): |
|
213 """ |
|
214 Private method to set the palette from a style sheet. |
|
215 |
|
216 @param styleSheet style sheet |
|
217 @type str |
|
218 """ |
|
219 palette = self.palette() |
|
220 |
|
221 paletteStr = styleSheet.split("QPalette {")[1].split("}")[0] |
|
222 paletteLines = paletteStr.strip().splitlines() |
|
223 for line in paletteLines: |
|
224 role, value = line.strip().split() |
|
225 role = role.strip("\t :").lower() |
|
226 value = value.strip("\t ;") |
|
227 if role in self.PaletteRoleMapping and value.startswith("#"): |
|
228 palette.setColor(self.PaletteRoleMapping[role], QColor(value)) |
|
229 |
|
230 self.setPalette(palette) |
|
231 |
161 def usesDarkPalette(self): |
232 def usesDarkPalette(self): |
162 """ |
233 """ |
163 Public method to check, if the application uses a palette with a dark |
234 Public method to check, if the application uses a palette with a dark |
164 background. |
235 background. |
165 |
236 |