src/eric7/EricWidgets/EricApplication.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9400
5494faeaead2
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
11 11
12 from PyQt6.QtCore import Qt, QCoreApplication 12 from PyQt6.QtCore import Qt, QCoreApplication
13 from PyQt6.QtGui import QColor, QPalette 13 from PyQt6.QtGui import QColor, QPalette
14 from PyQt6.QtWidgets import QApplication 14 from PyQt6.QtWidgets import QApplication
15 15
16 QCoreApplication.setAttribute( 16 QCoreApplication.setAttribute(Qt.ApplicationAttribute.AA_ShareOpenGLContexts, True)
17 Qt.ApplicationAttribute.AA_ShareOpenGLContexts, True)
18 17
19 from . import EricMessageBox 18 from . import EricMessageBox
20 19
21 20
22 class EricApplication(QApplication): 21 class EricApplication(QApplication):
23 """ 22 """
24 Eric application class with an object registry. 23 Eric application class with an object registry.
25 """ 24 """
25
26 PaletteRoleMapping = { 26 PaletteRoleMapping = {
27 "alternate-base": QPalette.ColorRole.AlternateBase, 27 "alternate-base": QPalette.ColorRole.AlternateBase,
28 "base": QPalette.ColorRole.Base, 28 "base": QPalette.ColorRole.Base,
29 "text": QPalette.ColorRole.Text, 29 "text": QPalette.ColorRole.Text,
30 "bright-text": QPalette.ColorRole.BrightText, 30 "bright-text": QPalette.ColorRole.BrightText,
38 "highlight": QPalette.ColorRole.Highlight, 38 "highlight": QPalette.ColorRole.Highlight,
39 "highlighted-text": QPalette.ColorRole.HighlightedText, 39 "highlighted-text": QPalette.ColorRole.HighlightedText,
40 "link": QPalette.ColorRole.Link, 40 "link": QPalette.ColorRole.Link,
41 "link-visited": QPalette.ColorRole.LinkVisited, 41 "link-visited": QPalette.ColorRole.LinkVisited,
42 } 42 }
43 43
44 def __init__(self, argv): 44 def __init__(self, argv):
45 """ 45 """
46 Constructor 46 Constructor
47 47
48 @param argv command line arguments 48 @param argv command line arguments
49 @type list 49 @type list
50 """ 50 """
51 super().__init__(argv) 51 super().__init__(argv)
52 52
53 QCoreApplication.setAttribute( 53 QCoreApplication.setAttribute(
54 Qt.ApplicationAttribute.AA_DontCreateNativeWidgetSiblings, True) 54 Qt.ApplicationAttribute.AA_DontCreateNativeWidgetSiblings, True
55 55 )
56
56 self.__objectRegistry = {} 57 self.__objectRegistry = {}
57 self.__pluginObjectRegistry = {} 58 self.__pluginObjectRegistry = {}
58 59
59 self.__smallScreen = False 60 self.__smallScreen = False
60 if "--small-screen" in argv: 61 if "--small-screen" in argv:
61 self.__smallScreen = True 62 self.__smallScreen = True
62 argv.remove("--small-screen") 63 argv.remove("--small-screen")
63 if not self.__smallScreen: 64 if not self.__smallScreen:
64 primaryScreenSize = self.primaryScreen().size() 65 primaryScreenSize = self.primaryScreen().size()
65 self.__smallScreen = ( 66 self.__smallScreen = (
66 primaryScreenSize.width() < 1920 or 67 primaryScreenSize.width() < 1920 or primaryScreenSize.height() < 1080
67 primaryScreenSize.height() < 1080
68 ) 68 )
69 69
70 def usesSmallScreen(self): 70 def usesSmallScreen(self):
71 """ 71 """
72 Public method to determine, if the application is used on a small 72 Public method to determine, if the application is used on a small
73 screen. 73 screen.
74 74
75 @return flag indicating the use of a small screen 75 @return flag indicating the use of a small screen
76 @rtype bool 76 @rtype bool
77 """ 77 """
78 return self.__smallScreen 78 return self.__smallScreen
79 79
80 def registerObject(self, name, objectRef): 80 def registerObject(self, name, objectRef):
81 """ 81 """
82 Public method to register an object in the object registry. 82 Public method to register an object in the object registry.
83 83
84 @param name name of the object 84 @param name name of the object
85 @type str 85 @type str
86 @param objectRef reference to the object 86 @param objectRef reference to the object
87 @type any 87 @type any
88 @exception KeyError raised when the given name is already in use 88 @exception KeyError raised when the given name is already in use
89 """ 89 """
90 if name in self.__objectRegistry: 90 if name in self.__objectRegistry:
91 raise KeyError('Object "{0}" already registered.'.format(name)) 91 raise KeyError('Object "{0}" already registered.'.format(name))
92 else: 92 else:
93 self.__objectRegistry[name] = objectRef 93 self.__objectRegistry[name] = objectRef
94 94
95 def getObject(self, name): 95 def getObject(self, name):
96 """ 96 """
97 Public method to get a reference to a registered object. 97 Public method to get a reference to a registered object.
98 98
99 @param name name of the object 99 @param name name of the object
100 @type str 100 @type str
101 @return reference to the registered object 101 @return reference to the registered object
102 @rtype any 102 @rtype any
103 @exception KeyError raised when the given name is not known 103 @exception KeyError raised when the given name is not known
104 """ 104 """
105 if name not in self.__objectRegistry: 105 if name not in self.__objectRegistry:
106 raise KeyError('Object "{0}" is not registered.'.format(name)) 106 raise KeyError('Object "{0}" is not registered.'.format(name))
107 107
108 return self.__objectRegistry[name] 108 return self.__objectRegistry[name]
109 109
110 def registerPluginObject(self, name, objectRef, pluginType=None): 110 def registerPluginObject(self, name, objectRef, pluginType=None):
111 """ 111 """
112 Public method to register a plugin object in the object registry. 112 Public method to register a plugin object in the object registry.
113 113
114 @param name name of the plugin object 114 @param name name of the plugin object
115 @type str 115 @type str
116 @param objectRef reference to the plugin object 116 @param objectRef reference to the plugin object
117 @type any 117 @type any
118 @param pluginType type of the plugin object 118 @param pluginType type of the plugin object
119 @type str 119 @type str
120 @exception KeyError raised when the given name is already in use 120 @exception KeyError raised when the given name is already in use
121 """ 121 """
122 if name in self.__pluginObjectRegistry: 122 if name in self.__pluginObjectRegistry:
123 raise KeyError( 123 raise KeyError('Pluginobject "{0}" already registered.'.format(name))
124 'Pluginobject "{0}" already registered.'.format(name))
125 else: 124 else:
126 self.__pluginObjectRegistry[name] = (objectRef, pluginType) 125 self.__pluginObjectRegistry[name] = (objectRef, pluginType)
127 126
128 def unregisterPluginObject(self, name): 127 def unregisterPluginObject(self, name):
129 """ 128 """
130 Public method to unregister a plugin object in the object registry. 129 Public method to unregister a plugin object in the object registry.
131 130
132 @param name name of the plugin object 131 @param name name of the plugin object
133 @type str 132 @type str
134 """ 133 """
135 if name in self.__pluginObjectRegistry: 134 if name in self.__pluginObjectRegistry:
136 del self.__pluginObjectRegistry[name] 135 del self.__pluginObjectRegistry[name]
137 136
138 def getPluginObject(self, name): 137 def getPluginObject(self, name):
139 """ 138 """
140 Public method to get a reference to a registered plugin object. 139 Public method to get a reference to a registered plugin object.
141 140
142 @param name name of the plugin object 141 @param name name of the plugin object
143 @type str 142 @type str
144 @return reference to the registered plugin object 143 @return reference to the registered plugin object
145 @rtype any 144 @rtype any
146 @exception KeyError raised when the given name is not known 145 @exception KeyError raised when the given name is not known
147 """ 146 """
148 if name not in self.__pluginObjectRegistry: 147 if name not in self.__pluginObjectRegistry:
149 raise KeyError( 148 raise KeyError('Pluginobject "{0}" is not registered.'.format(name))
150 'Pluginobject "{0}" is not registered.'.format(name)) 149
151
152 return self.__pluginObjectRegistry[name][0] 150 return self.__pluginObjectRegistry[name][0]
153 151
154 def getPluginObjects(self): 152 def getPluginObjects(self):
155 """ 153 """
156 Public method to get a list of (name, reference) pairs of all 154 Public method to get a list of (name, reference) pairs of all
157 registered plugin objects. 155 registered plugin objects.
158 156
159 @return list of (name, reference) pairs 157 @return list of (name, reference) pairs
160 @rtype list of (str, any) 158 @rtype list of (str, any)
161 """ 159 """
162 objects = [] 160 objects = []
163 for name in self.__pluginObjectRegistry: 161 for name in self.__pluginObjectRegistry:
164 objects.append((name, self.__pluginObjectRegistry[name][0])) 162 objects.append((name, self.__pluginObjectRegistry[name][0]))
165 return objects 163 return objects
166 164
167 def getPluginObjectType(self, name): 165 def getPluginObjectType(self, name):
168 """ 166 """
169 Public method to get the type of a registered plugin object. 167 Public method to get the type of a registered plugin object.
170 168
171 @param name name of the plugin object 169 @param name name of the plugin object
172 @type str 170 @type str
173 @return type of the plugin object 171 @return type of the plugin object
174 @rtype str 172 @rtype str
175 @exception KeyError raised when the given name is not known 173 @exception KeyError raised when the given name is not known
176 """ 174 """
177 if name not in self.__pluginObjectRegistry: 175 if name not in self.__pluginObjectRegistry:
178 raise KeyError( 176 raise KeyError('Pluginobject "{0}" is not registered.'.format(name))
179 'Pluginobject "{0}" is not registered.'.format(name)) 177
180
181 return self.__pluginObjectRegistry[name][1] 178 return self.__pluginObjectRegistry[name][1]
182 179
183 def getStyleIconsPath(self, universal=False): 180 def getStyleIconsPath(self, universal=False):
184 """ 181 """
185 Public method to get the path for the style icons. 182 Public method to get the path for the style icons.
186 183
187 @param universal flag indicating a universal file path (defaults to 184 @param universal flag indicating a universal file path (defaults to
188 False) 185 False)
189 @type bool (optional) 186 @type bool (optional)
190 @return directory path containing the style icons 187 @return directory path containing the style icons
191 @rtype str 188 @rtype str
192 """ 189 """
193 import Preferences 190 import Preferences
194 import Utilities 191 import Utilities
195 from eric7config import getConfig 192 from eric7config import getConfig
196 193
197 styleIconsPath = Preferences.getUI("StyleIconsPath") 194 styleIconsPath = Preferences.getUI("StyleIconsPath")
198 if not styleIconsPath: 195 if not styleIconsPath:
199 # default is the 'StyleIcons' sub-directory of the icons 196 # default is the 'StyleIcons' sub-directory of the icons
200 # directory 197 # directory
201 styleIconsPath = os.path.join( 198 styleIconsPath = os.path.join(getConfig("ericIconDir"), "StyleIcons")
202 getConfig('ericIconDir'), "StyleIcons") 199
203
204 if universal: 200 if universal:
205 return Utilities.fromNativeSeparators(styleIconsPath) 201 return Utilities.fromNativeSeparators(styleIconsPath)
206 else: 202 else:
207 return styleIconsPath 203 return styleIconsPath
208 204
209 def setStyleSheetFile(self, filename): 205 def setStyleSheetFile(self, filename):
210 """ 206 """
211 Public method to read a QSS style sheet file and set the application 207 Public method to read a QSS style sheet file and set the application
212 style sheet based on its contents. 208 style sheet based on its contents.
213 209
214 @param filename name of the QSS style sheet file 210 @param filename name of the QSS style sheet file
215 @type str 211 @type str
216 """ 212 """
217 if filename: 213 if filename:
218 try: 214 try:
220 styleSheet = f.read() 216 styleSheet = f.read()
221 except OSError as msg: 217 except OSError as msg:
222 EricMessageBox.warning( 218 EricMessageBox.warning(
223 None, 219 None,
224 QCoreApplication.translate( 220 QCoreApplication.translate(
225 "EricApplication", "Loading Style Sheet"), 221 "EricApplication", "Loading Style Sheet"
222 ),
226 QCoreApplication.translate( 223 QCoreApplication.translate(
227 "EricApplication", 224 "EricApplication",
228 """<p>The Qt Style Sheet file <b>{0}</b> could""" 225 """<p>The Qt Style Sheet file <b>{0}</b> could"""
229 """ not be read.<br>Reason: {1}</p>""") 226 """ not be read.<br>Reason: {1}</p>""",
230 .format(filename, str(msg))) 227 ).format(filename, str(msg)),
228 )
231 return 229 return
232 else: 230 else:
233 styleSheet = "" 231 styleSheet = ""
234 232
235 if styleSheet: 233 if styleSheet:
236 # pre-process the style sheet to replace the placeholder for the 234 # pre-process the style sheet to replace the placeholder for the
237 # path to the icons 235 # path to the icons
238 styleIconsPath = self.getStyleIconsPath(universal=True) 236 styleIconsPath = self.getStyleIconsPath(universal=True)
239 styleSheet = styleSheet.replace("${path}", styleIconsPath) 237 styleSheet = styleSheet.replace("${path}", styleIconsPath)
240 238
241 if "QPalette {" in styleSheet: 239 if "QPalette {" in styleSheet:
242 self.__setPaletteFromStyleSheet(styleSheet) 240 self.__setPaletteFromStyleSheet(styleSheet)
243 241
244 ericApp().setStyleSheet(styleSheet) 242 ericApp().setStyleSheet(styleSheet)
245 243
246 def __setPaletteFromStyleSheet(self, styleSheet): 244 def __setPaletteFromStyleSheet(self, styleSheet):
247 """ 245 """
248 Private method to set the palette from a style sheet. 246 Private method to set the palette from a style sheet.
249 247
250 @param styleSheet style sheet 248 @param styleSheet style sheet
251 @type str 249 @type str
252 """ 250 """
253 palette = self.palette() 251 palette = self.palette()
254 252
255 paletteStr = styleSheet.split("QPalette {")[1].split("}")[0] 253 paletteStr = styleSheet.split("QPalette {")[1].split("}")[0]
256 paletteLines = paletteStr.strip().splitlines() 254 paletteLines = paletteStr.strip().splitlines()
257 for line in paletteLines: 255 for line in paletteLines:
258 role, value = line.strip().split() 256 role, value = line.strip().split()
259 role = role.strip("\t :").lower() 257 role = role.strip("\t :").lower()
260 value = value.strip("\t ;") 258 value = value.strip("\t ;")
261 if role in self.PaletteRoleMapping and value.startswith("#"): 259 if role in self.PaletteRoleMapping and value.startswith("#"):
262 palette.setColor(self.PaletteRoleMapping[role], QColor(value)) 260 palette.setColor(self.PaletteRoleMapping[role], QColor(value))
263 261
264 self.setPalette(palette) 262 self.setPalette(palette)
265 263
266 def usesDarkPalette(self): 264 def usesDarkPalette(self):
267 """ 265 """
268 Public method to check, if the application uses a palette with a dark 266 Public method to check, if the application uses a palette with a dark
269 background. 267 background.
270 268
271 @return flag indicating the use of a palette with a dark background 269 @return flag indicating the use of a palette with a dark background
272 @rtype bool 270 @rtype bool
273 """ 271 """
274 palette = self.palette() 272 palette = self.palette()
275 lightness = palette.color(QPalette.ColorRole.Window).lightness() 273 lightness = palette.color(QPalette.ColorRole.Window).lightness()
276 return lightness <= 128 274 return lightness <= 128
277 275
276
278 ericApp = QCoreApplication.instance 277 ericApp = QCoreApplication.instance

eric ide

mercurial