diff -r 5ea8f371ff16 -r b2f26d630942 PluginProjectDjangoTagsMenu.py --- a/PluginProjectDjangoTagsMenu.py Fri Jan 31 14:00:14 2014 +0100 +++ b/PluginProjectDjangoTagsMenu.py Sun Feb 02 18:03:21 2014 +0100 @@ -0,0 +1,168 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2014 Detlev Offenbach <detlev@die-offenbachs.de> +# + +""" +Module implementing the Django tags menu plugin. +""" + +from __future__ import unicode_literals # __IGNORE_WARNING__ + +import os + +from PyQt4.QtCore import QObject, QTranslator +from PyQt4.QtGui import QMenu + +from E5Gui.E5Application import e5App + +# Start-of-Header +name = "Django Tags Menu Plugin" +author = "Detlev Offenbach <detlev@die-offenbachs.de>" +autoactivate = True +deactivateable = True +version = "0.1.0" +className = "ProjectDjangoTagsMenuPlugin" +packageName = "ProjectDjangoTagsMenu" +shortDescription = "Tags menu for Django projects." +longDescription = \ + """This plug-in adds a menu to select various tag templates to the""" \ + """ Django menu.""" +needsRestart = False +pyqtApi = 2 +# End-of-Header + +error = "" + + +class ProjectDjangoTagsMenuPlugin(QObject): + """ + Class implementing the Django tags menu plugin. + """ + def __init__(self, ui): + """ + Constructor + + @param ui reference to the user interface object (UI.UserInterface) + """ + QObject.__init__(self, ui) + self.__ui = ui + + self.__initMenu() + + def __initMenu(self): + """ + Private slot to initialize the tags menu. + """ + self.__menuAttached = False + self.__menuAction = None + self.__menuSeparator = None + + self.__menu = QMenu(self.tr("Template Tags")) + + def __attachMenu(self): + """ + Private method to attach the menu to the Django menu. + """ + if not self.__menuAttached: + try: + pluginObject = e5App().getPluginObject("ProjectDjango") + except KeyError: + pluginObject = None + + if pluginObject: + djangoMenu = pluginObject.getMenu("main") + djangoDatabaseMenuAction = \ + pluginObject.getMenu("database").menuAction() + self.__menuAction = djangoMenu.insertMenu( + djangoDatabaseMenuAction, self.__menu) + self.__menuSeparator = djangoMenu.insertSeparator( + djangoDatabaseMenuAction) + self.__menuAttached = True + + def __detachMenu(self): + """ + Private method to detach the menu from the Django menu. + """ + if self.__menuAttached: + try: + pluginObject = e5App().getPluginObject("ProjectDjango") + except KeyError: + pluginObject = None + + if pluginObject: + djangoMenu = pluginObject.getMenu("main") + djangoMenu.removeAction(self.__menuAction) + djangoMenu.removeAction(self.__menuSeparator) + + self.__menuAction = None + self.__menuSeparator = None + self.__menuAttached = False + + def activate(self): + """ + Public method to activate this plugin. + + @return tuple of None and activation status (boolean) + """ + pluginManager = e5App().getObject("PluginManager") + pluginManager.pluginActivated.connect(self.__pluginActivated) + pluginManager.pluginAboutToBeDeactivated.connect( + self.__pluginAboutToBeDeactivated) + + if pluginManager.isPluginActive("PluginProjectDjango"): + self.__attachMenu() + + return None, True + + def deactivate(self): + """ + Public method to deactivate this plugin. + """ + self.__detachMenu() + + pluginManager = e5App().getObject("PluginManager") + pluginManager.pluginActivated.disconnect(self.__pluginActivated) + pluginManager.pluginAboutToBeDeactivated.disconnect( + self.__pluginAboutToBeDeactivated) + + def __loadTranslator(self): + """ + Private method to load the translation file. + """ + if self.__ui is not None: + loc = self.__ui.getLocale() + if loc and loc != "C": + locale_dir = os.path.join( + os.path.dirname(__file__), "ProjectDjangoTagsMenu", "i18n") + translation = "djangotagsmenu_%s" % loc + translator = QTranslator(None) + loaded = translator.load(translation, locale_dir) + if loaded: + self.__translator = translator + e5App().installTranslator(self.__translator) + else: + print("Warning: translation file '{0}' could not be" + " loaded.".format(translation)) + print("Using default.") + + def __pluginActivated(self, moduleName, pluginObject): + """ + Private slot to react on plugin activation of the Django plugin. + + @param moduleName name of the module activated (string) + @param pluginObject reference to the activated plug-in object (object) + """ + if moduleName == "PluginProjectDjango": + self.__attachMenu() + + def __pluginAboutToBeDeactivated(self, moduleName, pluginObject): + """ + Private slot to react on the Django plugin about to be deactivated. + + @param moduleName name of the module about to be deactivated (string) + @param pluginObject reference to the about to be deactivated + plug-in object (object) + """ + if moduleName == "PluginProjectDjango": + self.__detachMenu()