PluginProjectDjangoTagsMenu.py

branch
eric7
changeset 63
85418cf03fdb
parent 61
e4ec6063d2e2
child 65
2616383c037c
equal deleted inserted replaced
62:dba433b4f3d6 63:85418cf03fdb
39 39
40 class ProjectDjangoTagsMenuPlugin(QObject): 40 class ProjectDjangoTagsMenuPlugin(QObject):
41 """ 41 """
42 Class implementing the Django tags menu plugin. 42 Class implementing the Django tags menu plugin.
43 """ 43 """
44
44 def __init__(self, ui): 45 def __init__(self, ui):
45 """ 46 """
46 Constructor 47 Constructor
47 48
48 @param ui reference to the user interface object 49 @param ui reference to the user interface object
49 @type UserInterface 50 @type UserInterface
50 """ 51 """
51 super().__init__(ui) 52 super().__init__(ui)
52 self.__ui = ui 53 self.__ui = ui
53 54
54 self.__translator = None 55 self.__translator = None
55 self.__loadTranslator() 56 self.__loadTranslator()
56 57
57 self.__handler = DjangoTagsMenuHandler(ui, self) 58 self.__handler = DjangoTagsMenuHandler(ui, self)
58 59
59 self.__initMenu() 60 self.__initMenu()
60 61
61 def __initMenu(self): 62 def __initMenu(self):
62 """ 63 """
63 Private slot to initialize the tags menu. 64 Private slot to initialize the tags menu.
64 """ 65 """
65 self.__menuAttached = False 66 self.__menuAttached = False
66 self.__menuAction = None 67 self.__menuAction = None
67 self.__menuSeparator = None 68 self.__menuSeparator = None
68 69
69 self.__menu = QMenu(self.tr("Template Tags")) 70 self.__menu = QMenu(self.tr("Template Tags"))
70 self.__handler.initMenus(self.__menu) 71 self.__handler.initMenus(self.__menu)
71 72
72 def __attachMenu(self): 73 def __attachMenu(self):
73 """ 74 """
74 Private method to attach the menu to the Django menu. 75 Private method to attach the menu to the Django menu.
75 """ 76 """
76 if not self.__menuAttached: 77 if not self.__menuAttached:
77 with contextlib.suppress(KeyError): 78 with contextlib.suppress(KeyError):
78 pluginObject = ericApp().getPluginObject("ProjectDjango") 79 pluginObject = ericApp().getPluginObject("ProjectDjango")
79 djangoMenu = pluginObject.getMenu("main") 80 djangoMenu = pluginObject.getMenu("main")
80 djangoDatabaseMenuAction = pluginObject.getMenu( 81 djangoDatabaseMenuAction = pluginObject.getMenu("database").menuAction()
81 "database").menuAction()
82 self.__menuAction = djangoMenu.insertMenu( 82 self.__menuAction = djangoMenu.insertMenu(
83 djangoDatabaseMenuAction, self.__menu) 83 djangoDatabaseMenuAction, self.__menu
84 )
84 self.__menuSeparator = djangoMenu.insertSeparator( 85 self.__menuSeparator = djangoMenu.insertSeparator(
85 djangoDatabaseMenuAction) 86 djangoDatabaseMenuAction
87 )
86 self.__menuAttached = True 88 self.__menuAttached = True
87 89
88 def __detachMenu(self): 90 def __detachMenu(self):
89 """ 91 """
90 Private method to detach the menu from the Django menu. 92 Private method to detach the menu from the Django menu.
91 """ 93 """
92 if self.__menuAttached: 94 if self.__menuAttached:
93 with contextlib.suppress(KeyError): 95 with contextlib.suppress(KeyError):
94 pluginObject = ericApp().getPluginObject("ProjectDjango") 96 pluginObject = ericApp().getPluginObject("ProjectDjango")
95 djangoMenu = pluginObject.getMenu("main") 97 djangoMenu = pluginObject.getMenu("main")
96 djangoMenu.removeAction(self.__menuAction) 98 djangoMenu.removeAction(self.__menuAction)
97 djangoMenu.removeAction(self.__menuSeparator) 99 djangoMenu.removeAction(self.__menuSeparator)
98 100
99 self.__menuAction = None 101 self.__menuAction = None
100 self.__menuSeparator = None 102 self.__menuSeparator = None
101 self.__menuAttached = False 103 self.__menuAttached = False
102 104
103 def activate(self): 105 def activate(self):
104 """ 106 """
105 Public method to activate this plugin. 107 Public method to activate this plugin.
106 108
107 @return tuple of None and activation status 109 @return tuple of None and activation status
108 @rtype tuple of (None, bool) 110 @rtype tuple of (None, bool)
109 """ 111 """
110 pluginManager = ericApp().getObject("PluginManager") 112 pluginManager = ericApp().getObject("PluginManager")
111 pluginManager.pluginActivated.connect(self.__pluginActivated) 113 pluginManager.pluginActivated.connect(self.__pluginActivated)
112 pluginManager.pluginAboutToBeDeactivated.connect( 114 pluginManager.pluginAboutToBeDeactivated.connect(
113 self.__pluginAboutToBeDeactivated) 115 self.__pluginAboutToBeDeactivated
114 116 )
117
115 if pluginManager.isPluginActive("PluginProjectDjango"): 118 if pluginManager.isPluginActive("PluginProjectDjango"):
116 self.__attachMenu() 119 self.__attachMenu()
117 120
118 ericApp().getObject("Project").projectClosed.connect( 121 ericApp().getObject("Project").projectClosed.connect(self.__projectClosed)
119 self.__projectClosed) 122
120
121 return None, True 123 return None, True
122 124
123 def deactivate(self): 125 def deactivate(self):
124 """ 126 """
125 Public method to deactivate this plugin. 127 Public method to deactivate this plugin.
126 """ 128 """
127 ericApp().getObject("Project").projectClosed.disconnect( 129 ericApp().getObject("Project").projectClosed.disconnect(self.__projectClosed)
128 self.__projectClosed) 130
129
130 self.__handler.closeAllWindows() 131 self.__handler.closeAllWindows()
131 self.__detachMenu() 132 self.__detachMenu()
132 133
133 pluginManager = ericApp().getObject("PluginManager") 134 pluginManager = ericApp().getObject("PluginManager")
134 pluginManager.pluginActivated.disconnect(self.__pluginActivated) 135 pluginManager.pluginActivated.disconnect(self.__pluginActivated)
135 pluginManager.pluginAboutToBeDeactivated.disconnect( 136 pluginManager.pluginAboutToBeDeactivated.disconnect(
136 self.__pluginAboutToBeDeactivated) 137 self.__pluginAboutToBeDeactivated
137 138 )
139
138 def __loadTranslator(self): 140 def __loadTranslator(self):
139 """ 141 """
140 Private method to load the translation file. 142 Private method to load the translation file.
141 """ 143 """
142 if self.__ui is not None: 144 if self.__ui is not None:
143 loc = self.__ui.getLocale() 145 loc = self.__ui.getLocale()
144 if loc and loc != "C": 146 if loc and loc != "C":
145 locale_dir = os.path.join( 147 locale_dir = os.path.join(
146 os.path.dirname(__file__), "ProjectDjangoTagsMenu", "i18n") 148 os.path.dirname(__file__), "ProjectDjangoTagsMenu", "i18n"
149 )
147 translation = "djangotagsmenu_{0}".format(loc) 150 translation = "djangotagsmenu_{0}".format(loc)
148 translator = QTranslator(None) 151 translator = QTranslator(None)
149 loaded = translator.load(translation, locale_dir) 152 loaded = translator.load(translation, locale_dir)
150 if loaded: 153 if loaded:
151 self.__translator = translator 154 self.__translator = translator
152 ericApp().installTranslator(self.__translator) 155 ericApp().installTranslator(self.__translator)
153 else: 156 else:
154 print("Warning: translation file '{0}' could not be" 157 print(
155 " loaded.".format(translation)) 158 "Warning: translation file '{0}' could not be"
159 " loaded.".format(translation)
160 )
156 print("Using default.") 161 print("Using default.")
157 162
158 def __projectClosed(self): 163 def __projectClosed(self):
159 """ 164 """
160 Private slot to handle the projectClosed signal. 165 Private slot to handle the projectClosed signal.
161 """ 166 """
162 self.__handler.closeAllWindows() 167 self.__handler.closeAllWindows()
163 168
164 def __pluginActivated(self, moduleName, pluginObject): 169 def __pluginActivated(self, moduleName, pluginObject):
165 """ 170 """
166 Private slot to react on plugin activation of the Django plugin. 171 Private slot to react on plugin activation of the Django plugin.
167 172
168 @param moduleName name of the module activated 173 @param moduleName name of the module activated
169 @type str 174 @type str
170 @param pluginObject reference to the activated plug-in object 175 @param pluginObject reference to the activated plug-in object
171 @type object 176 @type object
172 """ 177 """
173 if moduleName == "PluginProjectDjango": 178 if moduleName == "PluginProjectDjango":
174 self.__attachMenu() 179 self.__attachMenu()
175 180
176 def __pluginAboutToBeDeactivated(self, moduleName, pluginObject): 181 def __pluginAboutToBeDeactivated(self, moduleName, pluginObject):
177 """ 182 """
178 Private slot to react on the Django plugin about to be deactivated. 183 Private slot to react on the Django plugin about to be deactivated.
179 184
180 @param moduleName name of the module about to be deactivated 185 @param moduleName name of the module about to be deactivated
181 @type str 186 @type str
182 @param pluginObject reference to the about to be deactivated 187 @param pluginObject reference to the about to be deactivated
183 plug-in object 188 plug-in object
184 @type object 189 @type object
185 """ 190 """
186 if moduleName == "PluginProjectDjango": 191 if moduleName == "PluginProjectDjango":
187 self.__handler.closeAllWindows() 192 self.__handler.closeAllWindows()
188 self.__detachMenu() 193 self.__detachMenu()
189 194
195
190 # 196 #
191 # eflag: noqa = M801 197 # eflag: noqa = M801

eric ide

mercurial