PluginProjectDjangoTagsMenu.py

Sat, 23 Dec 2023 15:48:51 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 23 Dec 2023 15:48:51 +0100
branch
eric7
changeset 70
ce1c2effa0e0
parent 69
9acb6987ce60
child 71
fc1ab84c242f
permissions
-rw-r--r--

Updated copyright for 2024.

# -*- coding: utf-8 -*-

# Copyright (c) 2014 - 2024 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 eric7.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 = "10.2.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

eric ide

mercurial