5 |
5 |
6 """ |
6 """ |
7 Module implementing the Django tags menu plugin. |
7 Module implementing the Django tags menu plugin. |
8 """ |
8 """ |
9 |
9 |
|
10 import contextlib |
10 import os |
11 import os |
11 |
12 |
12 from PyQt5.QtCore import QObject, QTranslator |
13 from PyQt6.QtCore import QObject, QTranslator |
13 from PyQt5.QtWidgets import QMenu |
14 from PyQt6.QtWidgets import QMenu |
14 |
15 |
15 from E5Gui.E5Application import e5App |
16 from EricWidgets.EricApplication import ericApp |
16 |
17 |
17 from ProjectDjangoTagsMenu.DjangoTagsMenuHandler import DjangoTagsMenuHandler |
18 from ProjectDjangoTagsMenu.DjangoTagsMenuHandler import DjangoTagsMenuHandler |
18 |
19 |
19 # Start-of-Header |
20 # Start-of-Header |
20 name = "Django Tags Menu Plugin" |
21 name = "Django Tags Menu Plugin" |
21 author = "Detlev Offenbach <detlev@die-offenbachs.de>" |
22 author = "Detlev Offenbach <detlev@die-offenbachs.de>" |
22 autoactivate = True |
23 autoactivate = True |
23 deactivateable = True |
24 deactivateable = True |
24 version = "3.2.0" |
25 version = "1.0.0" |
25 className = "ProjectDjangoTagsMenuPlugin" |
26 className = "ProjectDjangoTagsMenuPlugin" |
26 packageName = "ProjectDjangoTagsMenu" |
27 packageName = "ProjectDjangoTagsMenu" |
27 shortDescription = "Tags menu for Django projects." |
28 shortDescription = "Tags menu for Django projects." |
28 longDescription = ( |
29 longDescription = ( |
29 """This plug-in adds a menu to select various tag templates to the""" |
30 """This plug-in adds a menu to select various tag templates to the""" |
70 def __attachMenu(self): |
72 def __attachMenu(self): |
71 """ |
73 """ |
72 Private method to attach the menu to the Django menu. |
74 Private method to attach the menu to the Django menu. |
73 """ |
75 """ |
74 if not self.__menuAttached: |
76 if not self.__menuAttached: |
75 try: |
77 with contextlib.suppress(KeyError): |
76 pluginObject = e5App().getPluginObject("ProjectDjango") |
78 pluginObject = ericApp().getPluginObject("ProjectDjango") |
77 except KeyError: |
|
78 pluginObject = None |
|
79 |
|
80 if pluginObject: |
|
81 djangoMenu = pluginObject.getMenu("main") |
79 djangoMenu = pluginObject.getMenu("main") |
82 djangoDatabaseMenuAction = pluginObject.getMenu( |
80 djangoDatabaseMenuAction = pluginObject.getMenu( |
83 "database").menuAction() |
81 "database").menuAction() |
84 self.__menuAction = djangoMenu.insertMenu( |
82 self.__menuAction = djangoMenu.insertMenu( |
85 djangoDatabaseMenuAction, self.__menu) |
83 djangoDatabaseMenuAction, self.__menu) |
90 def __detachMenu(self): |
88 def __detachMenu(self): |
91 """ |
89 """ |
92 Private method to detach the menu from the Django menu. |
90 Private method to detach the menu from the Django menu. |
93 """ |
91 """ |
94 if self.__menuAttached: |
92 if self.__menuAttached: |
95 try: |
93 with contextlib.suppress(KeyError): |
96 pluginObject = e5App().getPluginObject("ProjectDjango") |
94 pluginObject = ericApp().getPluginObject("ProjectDjango") |
97 except KeyError: |
|
98 pluginObject = None |
|
99 |
|
100 if pluginObject: |
|
101 djangoMenu = pluginObject.getMenu("main") |
95 djangoMenu = pluginObject.getMenu("main") |
102 djangoMenu.removeAction(self.__menuAction) |
96 djangoMenu.removeAction(self.__menuAction) |
103 djangoMenu.removeAction(self.__menuSeparator) |
97 djangoMenu.removeAction(self.__menuSeparator) |
104 |
98 |
105 self.__menuAction = None |
99 self.__menuAction = None |
108 |
102 |
109 def activate(self): |
103 def activate(self): |
110 """ |
104 """ |
111 Public method to activate this plugin. |
105 Public method to activate this plugin. |
112 |
106 |
113 @return tuple of None and activation status (boolean) |
107 @return tuple of None and activation status |
|
108 @rtype tuple of (None, bool) |
114 """ |
109 """ |
115 pluginManager = e5App().getObject("PluginManager") |
110 pluginManager = ericApp().getObject("PluginManager") |
116 pluginManager.pluginActivated.connect(self.__pluginActivated) |
111 pluginManager.pluginActivated.connect(self.__pluginActivated) |
117 pluginManager.pluginAboutToBeDeactivated.connect( |
112 pluginManager.pluginAboutToBeDeactivated.connect( |
118 self.__pluginAboutToBeDeactivated) |
113 self.__pluginAboutToBeDeactivated) |
119 |
114 |
120 if pluginManager.isPluginActive("PluginProjectDjango"): |
115 if pluginManager.isPluginActive("PluginProjectDjango"): |
121 self.__attachMenu() |
116 self.__attachMenu() |
122 |
117 |
123 e5App().getObject("Project").projectClosed.connect( |
118 ericApp().getObject("Project").projectClosed.connect( |
124 self.__projectClosed) |
119 self.__projectClosed) |
125 |
120 |
126 return None, True |
121 return None, True |
127 |
122 |
128 def deactivate(self): |
123 def deactivate(self): |
129 """ |
124 """ |
130 Public method to deactivate this plugin. |
125 Public method to deactivate this plugin. |
131 """ |
126 """ |
132 e5App().getObject("Project").projectClosed.disconnect( |
127 ericApp().getObject("Project").projectClosed.disconnect( |
133 self.__projectClosed) |
128 self.__projectClosed) |
134 |
129 |
135 self.__handler.closeAllWindows() |
130 self.__handler.closeAllWindows() |
136 self.__detachMenu() |
131 self.__detachMenu() |
137 |
132 |
138 pluginManager = e5App().getObject("PluginManager") |
133 pluginManager = ericApp().getObject("PluginManager") |
139 pluginManager.pluginActivated.disconnect(self.__pluginActivated) |
134 pluginManager.pluginActivated.disconnect(self.__pluginActivated) |
140 pluginManager.pluginAboutToBeDeactivated.disconnect( |
135 pluginManager.pluginAboutToBeDeactivated.disconnect( |
141 self.__pluginAboutToBeDeactivated) |
136 self.__pluginAboutToBeDeactivated) |
142 |
137 |
143 def __loadTranslator(self): |
138 def __loadTranslator(self): |
152 translation = "djangotagsmenu_{0}".format(loc) |
147 translation = "djangotagsmenu_{0}".format(loc) |
153 translator = QTranslator(None) |
148 translator = QTranslator(None) |
154 loaded = translator.load(translation, locale_dir) |
149 loaded = translator.load(translation, locale_dir) |
155 if loaded: |
150 if loaded: |
156 self.__translator = translator |
151 self.__translator = translator |
157 e5App().installTranslator(self.__translator) |
152 ericApp().installTranslator(self.__translator) |
158 else: |
153 else: |
159 print("Warning: translation file '{0}' could not be" |
154 print("Warning: translation file '{0}' could not be" |
160 " loaded.".format(translation)) |
155 " loaded.".format(translation)) |
161 print("Using default.") |
156 print("Using default.") |
162 |
157 |
168 |
163 |
169 def __pluginActivated(self, moduleName, pluginObject): |
164 def __pluginActivated(self, moduleName, pluginObject): |
170 """ |
165 """ |
171 Private slot to react on plugin activation of the Django plugin. |
166 Private slot to react on plugin activation of the Django plugin. |
172 |
167 |
173 @param moduleName name of the module activated (string) |
168 @param moduleName name of the module activated |
174 @param pluginObject reference to the activated plug-in object (object) |
169 @type str |
|
170 @param pluginObject reference to the activated plug-in object |
|
171 @type object |
175 """ |
172 """ |
176 if moduleName == "PluginProjectDjango": |
173 if moduleName == "PluginProjectDjango": |
177 self.__attachMenu() |
174 self.__attachMenu() |
178 |
175 |
179 def __pluginAboutToBeDeactivated(self, moduleName, pluginObject): |
176 def __pluginAboutToBeDeactivated(self, moduleName, pluginObject): |
180 """ |
177 """ |
181 Private slot to react on the Django plugin about to be deactivated. |
178 Private slot to react on the Django plugin about to be deactivated. |
182 |
179 |
183 @param moduleName name of the module about to be deactivated (string) |
180 @param moduleName name of the module about to be deactivated |
|
181 @type str |
184 @param pluginObject reference to the about to be deactivated |
182 @param pluginObject reference to the about to be deactivated |
185 plug-in object (object) |
183 plug-in object |
|
184 @type object |
186 """ |
185 """ |
187 if moduleName == "PluginProjectDjango": |
186 if moduleName == "PluginProjectDjango": |
188 self.__handler.closeAllWindows() |
187 self.__handler.closeAllWindows() |
189 self.__detachMenu() |
188 self.__detachMenu() |
190 |
189 |