|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Class implementing a specialized application class. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import Qt, QCoreApplication |
|
11 from PyQt6.QtGui import QPalette |
|
12 from PyQt6.QtWidgets import QApplication |
|
13 |
|
14 QCoreApplication.setAttribute( |
|
15 Qt.ApplicationAttribute.AA_ShareOpenGLContexts, True) |
|
16 |
|
17 |
|
18 class EricApplication(QApplication): |
|
19 """ |
|
20 Eric application class with an object registry. |
|
21 """ |
|
22 def __init__(self, argv): |
|
23 """ |
|
24 Constructor |
|
25 |
|
26 @param argv command line arguments |
|
27 @type list |
|
28 """ |
|
29 super().__init__(argv) |
|
30 |
|
31 QCoreApplication.setAttribute( |
|
32 Qt.ApplicationAttribute.AA_DontCreateNativeWidgetSiblings, True) |
|
33 |
|
34 self.__objectRegistry = {} |
|
35 self.__pluginObjectRegistry = {} |
|
36 |
|
37 def registerObject(self, name, objectRef): |
|
38 """ |
|
39 Public method to register an object in the object registry. |
|
40 |
|
41 @param name name of the object |
|
42 @type str |
|
43 @param objectRef reference to the object |
|
44 @type any |
|
45 @exception KeyError raised when the given name is already in use |
|
46 """ |
|
47 if name in self.__objectRegistry: |
|
48 raise KeyError('Object "{0}" already registered.'.format(name)) |
|
49 else: |
|
50 self.__objectRegistry[name] = objectRef |
|
51 |
|
52 def getObject(self, name): |
|
53 """ |
|
54 Public method to get a reference to a registered object. |
|
55 |
|
56 @param name name of the object |
|
57 @type str |
|
58 @return reference to the registered object |
|
59 @rtype any |
|
60 @exception KeyError raised when the given name is not known |
|
61 """ |
|
62 if name not in self.__objectRegistry: |
|
63 raise KeyError('Object "{0}" is not registered.'.format(name)) |
|
64 |
|
65 return self.__objectRegistry[name] |
|
66 |
|
67 def registerPluginObject(self, name, objectRef, pluginType=None): |
|
68 """ |
|
69 Public method to register a plugin object in the object registry. |
|
70 |
|
71 @param name name of the plugin object |
|
72 @type str |
|
73 @param objectRef reference to the plugin object |
|
74 @type any |
|
75 @param pluginType type of the plugin object |
|
76 @type str |
|
77 @exception KeyError raised when the given name is already in use |
|
78 """ |
|
79 if name in self.__pluginObjectRegistry: |
|
80 raise KeyError( |
|
81 'Pluginobject "{0}" already registered.'.format(name)) |
|
82 else: |
|
83 self.__pluginObjectRegistry[name] = (objectRef, pluginType) |
|
84 |
|
85 def unregisterPluginObject(self, name): |
|
86 """ |
|
87 Public method to unregister a plugin object in the object registry. |
|
88 |
|
89 @param name name of the plugin object |
|
90 @type str |
|
91 """ |
|
92 if name in self.__pluginObjectRegistry: |
|
93 del self.__pluginObjectRegistry[name] |
|
94 |
|
95 def getPluginObject(self, name): |
|
96 """ |
|
97 Public method to get a reference to a registered plugin object. |
|
98 |
|
99 @param name name of the plugin object |
|
100 @type str |
|
101 @return reference to the registered plugin object |
|
102 @rtype any |
|
103 @exception KeyError raised when the given name is not known |
|
104 """ |
|
105 if name not in self.__pluginObjectRegistry: |
|
106 raise KeyError( |
|
107 'Pluginobject "{0}" is not registered.'.format(name)) |
|
108 |
|
109 return self.__pluginObjectRegistry[name][0] |
|
110 |
|
111 def getPluginObjects(self): |
|
112 """ |
|
113 Public method to get a list of (name, reference) pairs of all |
|
114 registered plugin objects. |
|
115 |
|
116 @return list of (name, reference) pairs |
|
117 @rtype list of (str, any) |
|
118 """ |
|
119 objects = [] |
|
120 for name in self.__pluginObjectRegistry: |
|
121 objects.append((name, self.__pluginObjectRegistry[name][0])) |
|
122 return objects |
|
123 |
|
124 def getPluginObjectType(self, name): |
|
125 """ |
|
126 Public method to get the type of a registered plugin object. |
|
127 |
|
128 @param name name of the plugin object |
|
129 @type str |
|
130 @return type of the plugin object |
|
131 @rtype str |
|
132 @exception KeyError raised when the given name is not known |
|
133 """ |
|
134 if name not in self.__pluginObjectRegistry: |
|
135 raise KeyError( |
|
136 'Pluginobject "{0}" is not registered.'.format(name)) |
|
137 |
|
138 return self.__pluginObjectRegistry[name][1] |
|
139 |
|
140 def usesDarkPalette(self): |
|
141 """ |
|
142 Public method to check, if the application uses a palette with a dark |
|
143 background. |
|
144 |
|
145 @return flag indicating the use of a palette with a dark background |
|
146 @rtype bool |
|
147 """ |
|
148 palette = self.palette() |
|
149 lightness = palette.color(QPalette.ColorRole.Window).lightness() |
|
150 return lightness <= 128 |
|
151 |
|
152 ericApp = QCoreApplication.instance |