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