Sun, 02 Feb 2014 18:03:21 +0100
Implemented the basic framework.
--- a/PluginDjangoTagsMenu.e4p Fri Jan 31 14:00:14 2014 +0100 +++ b/PluginDjangoTagsMenu.e4p Sun Feb 02 18:03:21 2014 +0100 @@ -7,7 +7,7 @@ <ProgLanguage mixed="0">Python3</ProgLanguage> <ProjectType>E4Plugin</ProjectType> <Description>This plug-in adds a menu to select various tag templates to the Django menu.</Description> - <Version>0.1</Version> + <Version>0.x</Version> <Author>Detlev Offenbach</Author> <Email>detlev@die-offenbachs.de</Email> <TranslationPattern>ProjectDjangoTagsMenu/i18n/djangotagsmenu_%language%.ts</TranslationPattern> @@ -142,4 +142,77 @@ <FiletypeAssociation pattern="*.ui" type="FORMS"/> <FiletypeAssociation pattern="*.ui.h" type="FORMS"/> </FiletypeAssociations> + <Checkers> + <CheckersParams> + <dict> + <key> + <string>Pep8Checker</string> + </key> + <value> + <dict> + <key> + <string>DocstringType</string> + </key> + <value> + <string>eric</string> + </value> + <key> + <string>ExcludeFiles</string> + </key> + <value> + <string>*/Ui_*.py</string> + </value> + <key> + <string>ExcludeMessages</string> + </key> + <value> + <string>W293, N802, N803, N807, N808, N821</string> + </value> + <key> + <string>FixCodes</string> + </key> + <value> + <string></string> + </value> + <key> + <string>FixIssues</string> + </key> + <value> + <bool>False</bool> + </value> + <key> + <string>HangClosing</string> + </key> + <value> + <bool>False</bool> + </value> + <key> + <string>IncludeMessages</string> + </key> + <value> + <string></string> + </value> + <key> + <string>MaxLineLength</string> + </key> + <value> + <int>79</int> + </value> + <key> + <string>NoFixCodes</string> + </key> + <value> + <string>E501</string> + </value> + <key> + <string>RepeatMessages</string> + </key> + <value> + <bool>True</bool> + </value> + </dict> + </value> + </dict> + </CheckersParams> + </Checkers> </Project>
--- 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()