Fri, 11 Jul 2014 19:25:03 +0200
Ported to PyQt5 and eric6.
# -*- coding: utf-8 -*- # Copyright (c) 2014 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing the Django tags menu plugin. """ from __future__ import unicode_literals import os from PyQt5.QtCore import QObject, QTranslator from PyQt5.QtWidgets import QMenu from E5Gui.E5Application import e5App from ProjectDjangoTagsMenu.DjangoTagsMenuHandler import DjangoTagsMenuHandler # Start-of-Header name = "Django Tags Menu Plugin" author = "Detlev Offenbach <detlev@die-offenbachs.de>" autoactivate = True deactivateable = True version = "2.0.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 python2Compatible = True # 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) """ super(ProjectDjangoTagsMenuPlugin, self).__init__(ui) self.__ui = ui self.__handler = DjangoTagsMenuHandler(ui, self) 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")) self.__handler.initMenus(self.__menu) 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() e5App().getObject("Project").projectClosed.connect( self.__projectClosed) return None, True def deactivate(self): """ Public method to deactivate this plugin. """ e5App().getObject("Project").projectClosed.disconnect( self.__projectClosed) self.__handler.closeAllWindows() 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 __projectClosed(self): """ Private slot to handle the projectClosed signal. """ self.__handler.closeAllWindows() 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.__handler.closeAllWindows() self.__detachMenu()