--- a/PluginWizardDataUriEncoder.py Sun Dec 29 15:40:19 2013 +0100 +++ b/PluginWizardDataUriEncoder.py Sun Dec 29 19:41:52 2013 +0100 @@ -0,0 +1,152 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2013 Detlev Offenbach <detlev@die-offenbachs.de> +# + +""" +Module implementing the base64 data URI encoder wizard plug-in. +""" + +from __future__ import unicode_literals + +import os + +from PyQt4.QtCore import QObject, QTranslator +from PyQt4.QtGui 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 = "0.1.0" +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 (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.trUtf8('Base64 Data Uri Encoder Wizard'), + self.trUtf8('Base&64 Data Uri Encoder Wizard...'), + 0, 0, self, + 'wizards_datauriencoder') + self.__action.setStatusTip(self.trUtf8('.desktop Wizard')) + self.__action.setWhatsThis(self.trUtf8( + """<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.trUtf8('No current editor'), + self.trUtf8('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()