PluginWizardDataUriEncoder.py

Wed, 26 Oct 2022 09:29:22 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 26 Oct 2022 09:29:22 +0200
branch
eric7
changeset 55
40212271d664
parent 53
b1515c7ded27
child 57
2bbbf54c8853
permissions
-rw-r--r--

Modified Python3 template iaw.Black formatting rules.

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

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

"""
Module implementing the base64 data URI encoder wizard plug-in.
"""

import os

from PyQt6.QtCore import QObject, QTranslator
from PyQt6.QtWidgets import QDialog

from eric7.EricGui.EricAction import EricAction
from eric7.EricWidgets import EricMessageBox
from eric7.EricWidgets.EricApplication import ericApp

# Start-of-Header
name = "Base64 Data URI Encoder Wizard Plug-in"
author = "Detlev Offenbach <detlev@die-offenbachs.de>"
autoactivate = True
deactivateable = True
version = "10.1.2"
className = "WizardDataUriEncoderPlugin"
packageName = "WizardDataUriEncoder"
shortDescription = "Wizard for the creation of code for a base64 data URI."
longDescription = (
    """This plug-in implements a wizard to generate code for"""
    """ base64 encoded data URIs."""
)
needsRestart = False
pyqtApi = 2
# End-of-Header

error = ""


class WizardDataUriEncoderPlugin(QObject):
    """
    Class implementing the base64 data URI encoder wizard plug-in.
    """

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

        @param ui reference to the user interface object
        @type UserInterface
        """
        QObject.__init__(self, ui)

        self.__ui = ui
        self.__action = None

        self.__translator = None
        self.__loadTranslator()

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

        @return tuple of None and activation status
        @rtype (None, bool)
        """
        self.__initAction()
        self.__initMenu()

        return None, True

    def deactivate(self):
        """
        Public method to deactivate this plug-in.
        """
        menu = self.__ui.getMenu("wizards")
        if menu:
            menu.removeAction(self.__action)
        self.__ui.removeEricActions([self.__action], "wizards")

    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__), "WizardDataUriEncoder", "i18n"
                )
                translation = "datauriencoder_{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 __initAction(self):
        """
        Private method to initialize the action.
        """
        self.__action = EricAction(
            self.tr("Base64 Data Uri Encoder Wizard"),
            self.tr("Base&64 Data Uri Encoder Wizard..."),
            0,
            0,
            self,
            "wizards_datauriencoder",
        )
        self.__action.setStatusTip(self.tr("Base64 Data Uri Encoder Wizard"))
        self.__action.setWhatsThis(
            self.tr(
                """<b>Base64 Data Uri Encoder Wizard</b>"""
                """<p>This wizard opens a dialog for entering all the parameters"""
                """ needed to create code for a base64 encoded data URI.</p>"""
            )
        )
        self.__action.triggered.connect(self.__handle)

        self.__ui.addEricActions([self.__action], "wizards")

    def __initMenu(self):
        """
        Private method to add the actions to the right menu.
        """
        menu = self.__ui.getMenu("wizards")
        if menu:
            menu.addAction(self.__action)

    def __handle(self):
        """
        Private method to handle the wizards action.
        """
        editor = ericApp().getObject("ViewManager").activeWindow()

        if editor is None:
            EricMessageBox.critical(
                self.__ui,
                self.tr("No current editor"),
                self.tr("Please open or create a file first."),
            )
        else:
            from WizardDataUriEncoder.DataUriEncoderWizardDialog import (
                DataUriEncoderWizardDialog,
            )

            dlg = DataUriEncoderWizardDialog(None)
            if dlg.exec() == QDialog.DialogCode.Accepted:
                code = dlg.getCode()
                if code:
                    line, index = editor.getCursorPosition()
                    # It should be done on this way to allow undo
                    editor.beginUndoAction()
                    editor.insertAt(code, line, index)
                    editor.endUndoAction()


#
# eflag: noqa = M801

eric ide

mercurial