Thu, 30 Dec 2021 11:20:00 +0100
Updated copyright for 2022.
# -*- coding: utf-8 -*- # Copyright (c) 2014 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing the Django tags menu plugin. """ import contextlib import os from PyQt6.QtCore import QObject, QTranslator from PyQt6.QtWidgets import QMenu from EricWidgets.EricApplication import ericApp 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 = "1.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 # 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 @type UserInterface """ super().__init__(ui) self.__ui = ui self.__translator = None self.__loadTranslator() 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: with contextlib.suppress(KeyError): pluginObject = ericApp().getPluginObject("ProjectDjango") 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: with contextlib.suppress(KeyError): pluginObject = ericApp().getPluginObject("ProjectDjango") 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 @rtype tuple of (None, bool) """ pluginManager = ericApp().getObject("PluginManager") pluginManager.pluginActivated.connect(self.__pluginActivated) pluginManager.pluginAboutToBeDeactivated.connect( self.__pluginAboutToBeDeactivated) if pluginManager.isPluginActive("PluginProjectDjango"): self.__attachMenu() ericApp().getObject("Project").projectClosed.connect( self.__projectClosed) return None, True def deactivate(self): """ Public method to deactivate this plugin. """ ericApp().getObject("Project").projectClosed.disconnect( self.__projectClosed) self.__handler.closeAllWindows() self.__detachMenu() pluginManager = ericApp().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_{0}".format(loc) translator = QTranslator(None) loaded = translator.load(translation, locale_dir) if loaded: self.__translator = translator ericApp().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 @type str @param pluginObject reference to the activated plug-in object @type 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 @type str @param pluginObject reference to the about to be deactivated plug-in object @type object """ if moduleName == "PluginProjectDjango": self.__handler.closeAllWindows() self.__detachMenu() # # eflag: noqa = M801