PluginExtensionCorba.py

Wed, 07 Dec 2022 13:08:40 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 07 Dec 2022 13:08:40 +0100
changeset 6
dbc40938587e
parent 4
ff4fc402f193
child 10
c88be5019d60
permissions
-rw-r--r--

Added missing code to load the translations.

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

# Copyright (c) 2022 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a plugin to add support for CORBA development.
"""

import os

from PyQt6.QtCore import QCoreApplication, QObject, QTranslator

from eric7 import Globals, Preferences
from eric7.EricWidgets import EricMessageBox
from eric7.EricWidgets.EricApplication import ericApp
from ExtensionCorba.ProjectInterfacesBrowser import ProjectInterfacesBrowser

# Start-Of-Header
name = "Corba Extension Plugin"
author = "Detlev Offenbach <detlev@die-offenbachs.de>"
autoactivate = True
deactivateable = True
version = "10.0.1"
className = "CorbaExtensionPlugin"
packageName = "ExtensionCorba"
shortDescription = "Support for the development of CORBA projects"
longDescription = (
    "This plugin adds support for the development of CORBA related projects."
)
needsRestart = False
pyqtApi = 2
# End-Of-Header

error = ""

corbaExtensionPluginObject = None


def exeDisplayData():
    """
    Module function to support the display of some executable info.

    @return dictionary containing the data to query the presence of
        the executable
    @rtype dict
    """
    global corbaExtensionPluginObject

    if corbaExtensionPluginObject is None:
        data = {
            "programEntry": False,
            "header": QCoreApplication.translate(
                "CorbaExtensionPlugin", "CORBA IDL Compiler"
            ),
            "text": QCoreApplication.translate(
                "CorbaExtensionPlugin", "CORBA Support plugin is not activated"
            ),
            "version": QCoreApplication.translate("CorbaExtensionPlugin", "(inactive)"),
        }
    else:
        exe = corbaExtensionPluginObject.getPreferences("omniidl")
        if not exe:
            exe = "omniidl"
            if Globals.isWindowsPlatform():
                exe += ".exe"

        data = {
            "programEntry": True,
            "header": QCoreApplication.translate(
                "CorbaExtensionPlugin", "CORBA IDL Compiler"
            ),
            "exe": exe,
            "versionCommand": "-V",
            "versionStartsWith": "omniidl",
            "versionRe": "",
            "versionPosition": -1,
            "version": "",
            "versionCleanup": None,
            "exeModule": None,
        }

    return data


def getConfigData():
    """
    Module function returning data as required by the configuration dialog.

    @return dictionary containing the relevant data
    @rtype dict
    """
    iconSuffix = "dark" if ericApp().usesDarkPalette() else "light"

    return {
        "corbaPage": [
            QCoreApplication.translate("CorbaExtensionPlugin", "CORBA"),
            os.path.join(
                "ExtensionCorba", "icons", "preferences-corba-{0}".format(iconSuffix)
            ),
            createCorbaPage,
            None,
            None,
        ],
    }


def createCorbaPage(configDlg):
    """
    Module function to create the CORBA configuration page.

    @param configDlg reference to the configuration dialog
    @type ConfigurationWidget
    @return reference to the configuration page
    @rtype CorbaPage
    """
    from ExtensionCorba.ConfigurationPage.CorbaPage import CorbaPage

    global corbaExtensionPluginObject

    page = CorbaPage(corbaExtensionPluginObject)
    return page


def prepareUninstall():
    """
    Module function to prepare for an un-installation.
    """
    Preferences.getSettings().remove(CorbaExtensionPlugin.PreferencesKey)


class CorbaExtensionPlugin(QObject):
    """
    Class implementing a plugin to add support for CORBA development.
    """

    PreferencesKey = "Corba"

    def __init__(self, ui):
        """
        Constructor

        @param ui reference to the user interface object
        @type UI.UserInterface
        """
        super().__init__(ui)
        self.__ui = ui
        self.__initialize()

        self.__defaults = {
            "omniidl": "",
        }

        self.__translator = None
        self.__loadTranslator()

    def __initialize(self):
        """
        Private slot to (re)initialize the plugin.
        """
        global corbaExtensionPluginObject
        corbaExtensionPluginObject = None

        self.__browser = None

    def activate(self):
        """
        Public method to activate this plug-in.

        @return tuple of None and activation status
        @rtype bool
        """
        global error, corbaExtensionPluginObject
        error = ""  # clear previous error

        if self.__ui.versionIsNewer("22.12"):
            corbaExtensionPluginObject = self

            self.__browser = ProjectInterfacesBrowser(self)

            return None, True
        else:
            EricMessageBox.warning(
                self.__ui,
                self.tr("CORBA Extension"),
                self.tr(
                    "The CORBA extension cannot be activated because it requires eric7"
                    " 23.1 or newer."
                ),
            )
            error = self.tr(
                "The CORBA extension cannot be activated because it requires eric7"
                " 23.1 or newer."
            )

            return None, False

    def deactivate(self):
        """
        Public method to deactivate this plug-in.
        """
        self.__browser.deactivate()

        self.__initialize()

    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__), "ExtensionCorba", "i18n"
                )
                translation = "corba_{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 getPreferences(self, key):
        """
        Public method to retrieve the various settings values.

        @param key the key of the value to get
        @type str
        @return the requested setting value
        @rtype any
        """
        return Preferences.Prefs.settings.value(
            self.PreferencesKey + "/" + key, self.__defaults[key]
        )

    def setPreferences(self, key, value):
        """
        Public method to store the various settings values.

        @param key the key of the setting to be set
        @type str
        @param value the value to be set
        @type any
        """
        Preferences.Prefs.settings.setValue(self.PreferencesKey + "/" + key, value)


#
# eflag: noqa = M801

eric ide

mercurial