|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2010 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Class implementing a specialized application class. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import QCoreApplication |
|
11 from PyQt4.QtGui import QApplication |
|
12 |
|
13 class E5Application(QApplication): |
|
14 """ |
|
15 Eric application class with an object registry. |
|
16 """ |
|
17 def __init__(self, argv): |
|
18 """ |
|
19 Constructor |
|
20 |
|
21 @param argv command line arguments |
|
22 """ |
|
23 QApplication.__init__(self, argv) |
|
24 |
|
25 self.__objectRegistry = {} |
|
26 self.__pluginObjectRegistry = {} |
|
27 |
|
28 def registerObject(self, name, object): |
|
29 """ |
|
30 Public method to register an object in the object registry. |
|
31 |
|
32 @param name name of the object (string) |
|
33 @param object reference to the object |
|
34 @exception KeyError raised when the given name is already in use |
|
35 """ |
|
36 if name in self.__objectRegistry: |
|
37 raise KeyError('Object "%s" already registered.' % name) |
|
38 else: |
|
39 self.__objectRegistry[name] = object |
|
40 |
|
41 def getObject(self, name): |
|
42 """ |
|
43 Public method to get a reference to a registered object. |
|
44 |
|
45 @param name name of the object (string) |
|
46 @return reference to the registered object |
|
47 @exception KeyError raised when the given name is not known |
|
48 """ |
|
49 if name in self.__objectRegistry: |
|
50 return self.__objectRegistry[name] |
|
51 else: |
|
52 raise KeyError('Object "%s" is not registered.' % name) |
|
53 |
|
54 def registerPluginObject(self, name, object, pluginType = None): |
|
55 """ |
|
56 Public method to register a plugin object in the object registry. |
|
57 |
|
58 @param name name of the plugin object (string) |
|
59 @param object reference to the plugin object |
|
60 @keyparam pluginType type of the plugin object (string) |
|
61 @exception KeyError raised when the given name is already in use |
|
62 """ |
|
63 if name in self.__pluginObjectRegistry: |
|
64 raise KeyError('Pluginobject "%s" already registered.' % name) |
|
65 else: |
|
66 self.__pluginObjectRegistry[name] = (object, pluginType) |
|
67 |
|
68 def unregisterPluginObject(self, name): |
|
69 """ |
|
70 Public method to unregister a plugin object in the object registry. |
|
71 |
|
72 @param name name of the plugin object (string) |
|
73 """ |
|
74 if name in self.__pluginObjectRegistry: |
|
75 del self.__pluginObjectRegistry[name] |
|
76 |
|
77 def getPluginObject(self, name): |
|
78 """ |
|
79 Public method to get a reference to a registered plugin object. |
|
80 |
|
81 @param name name of the plugin object (string) |
|
82 @return reference to the registered plugin object |
|
83 @exception KeyError raised when the given name is not known |
|
84 """ |
|
85 if name in self.__pluginObjectRegistry: |
|
86 return self.__pluginObjectRegistry[name][0] |
|
87 else: |
|
88 raise KeyError('Pluginobject "%s" is not registered.' % name) |
|
89 |
|
90 def getPluginObjects(self): |
|
91 """ |
|
92 Public method to get a list of (name, reference) pairs of all |
|
93 registered plugin objects. |
|
94 |
|
95 @return list of (name, reference) pairs |
|
96 """ |
|
97 objects = [] |
|
98 for name in self.__pluginObjectRegistry: |
|
99 objects.append((name, self.__pluginObjectRegistry[name][0])) |
|
100 return objects |
|
101 |
|
102 def getPluginObjectType(self, name): |
|
103 """ |
|
104 Public method to get the type of a registered plugin object. |
|
105 |
|
106 @param name name of the plugin object (string) |
|
107 @return type of the plugin object (string) |
|
108 @exception KeyError raised when the given name is not known |
|
109 """ |
|
110 if name in self.__pluginObjectRegistry: |
|
111 return self.__pluginObjectRegistry[name][1] |
|
112 else: |
|
113 raise KeyError('Pluginobject "%s" is not registered.' % name) |
|
114 |
|
115 e5App = QCoreApplication.instance |