PluginWizardDataUriEncoder.py

Sun, 10 Jul 2016 12:26:24 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 10 Jul 2016 12:26:24 +0200
changeset 26
6d808b18fbc6
parent 25
aaae1b4373c9
child 27
3583168da814
permissions
-rw-r--r--

Created global tag <release-2.0.2>.

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

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

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

from __future__ import unicode_literals

import os

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

from E5Gui.E5Application import e5App
from E5Gui.E5Action import E5Action
from E5Gui import E5MessageBox

# Start-of-Header
name = "Base64 Data URI Encoder Wizard Plug-in"
author = "Detlev Offenbach <detlev@die-offenbachs.de>"
autoactivate = True
deactivateable = True
version = "2.0.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
python2Compatible = True
# 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 (UI.UserInterface)
        """
        QObject.__init__(self, ui)
        self.__ui = ui
        self.__action = None
        
        self.__translator = None
        self.__loadTranslator()
    
    def __initialize(self):
        """
        Private slot to (re)initialize the plug-in.
        """
        self.__act = None
    
    def activate(self):
        """
        Public method to activate this plug-in.
        
        @return tuple of None and activation status (boolean)
        """
        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.removeE5Actions([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
                    e5App().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 = E5Action(
            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.addE5Actions([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 = e5App().getObject("ViewManager").activeWindow()
        
        if editor is None:
                E5MessageBox.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.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()

eric ide

mercurial