PluginWizardDataUriEncoder.py

changeset 1
2fa6997ff09c
parent 0
3ad485fab7d4
child 3
f01e48994421
equal deleted inserted replaced
0:3ad485fab7d4 1:2fa6997ff09c
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2013 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the base64 data URI encoder wizard plug-in.
8 """
9
10 from __future__ import unicode_literals
11
12 import os
13
14 from PyQt4.QtCore import QObject, QTranslator
15 from PyQt4.QtGui import QDialog
16
17 from E5Gui.E5Application import e5App
18 from E5Gui.E5Action import E5Action
19 from E5Gui import E5MessageBox
20
21 # Start-of-Header
22 name = "Base64 Data URI Encoder Wizard Plug-in"
23 author = "Detlev Offenbach <detlev@die-offenbachs.de>"
24 autoactivate = True
25 deactivateable = True
26 version = "0.1.0"
27 className = "WizardDataUriEncoderPlugin"
28 packageName = "WizardDataUriEncoder"
29 shortDescription = "Wizard for the creation of code for a base64 data URI."
30 longDescription = \
31 """This plug-in implements a wizard to generate code for""" \
32 """ base64 encoded data URIs."""
33 needsRestart = False
34 pyqtApi = 2
35 # End-of-Header
36
37 error = ""
38
39
40 class WizardDataUriEncoderPlugin(QObject):
41 """
42 Class implementing the base64 data URI encoder wizard plug-in.
43 """
44 def __init__(self, ui):
45 """
46 Constructor
47
48 @param ui reference to the user interface object (UI.UserInterface)
49 """
50 QObject.__init__(self, ui)
51 self.__ui = ui
52 self.__action = None
53
54 self.__translator = None
55 self.__loadTranslator()
56
57 def __initialize(self):
58 """
59 Private slot to (re)initialize the plug-in.
60 """
61 self.__act = None
62
63 def activate(self):
64 """
65 Public method to activate this plug-in.
66
67 @return tuple of None and activation status (boolean)
68 """
69 self.__initAction()
70 self.__initMenu()
71
72 return None, True
73
74 def deactivate(self):
75 """
76 Public method to deactivate this plug-in.
77 """
78 menu = self.__ui.getMenu("wizards")
79 if menu:
80 menu.removeAction(self.__action)
81 self.__ui.removeE5Actions([self.__action], 'wizards')
82
83 def __loadTranslator(self):
84 """
85 Private method to load the translation file.
86 """
87 if self.__ui is not None:
88 loc = self.__ui.getLocale()
89 if loc and loc != "C":
90 locale_dir = os.path.join(
91 os.path.dirname(__file__), "WizardDataUriEncoder", "i18n")
92 translation = "datauriencoder_{0}".format(loc)
93 translator = QTranslator(None)
94 loaded = translator.load(translation, locale_dir)
95 if loaded:
96 self.__translator = translator
97 e5App().installTranslator(self.__translator)
98 else:
99 print("Warning: translation file '{0}' could not be"
100 " loaded.".format(translation))
101 print("Using default.")
102
103 def __initAction(self):
104 """
105 Private method to initialize the action.
106 """
107 self.__action = E5Action(
108 self.trUtf8('Base64 Data Uri Encoder Wizard'),
109 self.trUtf8('Base&64 Data Uri Encoder Wizard...'),
110 0, 0, self,
111 'wizards_datauriencoder')
112 self.__action.setStatusTip(self.trUtf8('.desktop Wizard'))
113 self.__action.setWhatsThis(self.trUtf8(
114 """<b>Base64 Data Uri Encoder Wizard</b>"""
115 """<p>This wizard opens a dialog for entering all the parameters"""
116 """ needed to create code for a base64 encoded data URI.</p>"""
117 ))
118 self.__action.triggered[()].connect(self.__handle)
119
120 self.__ui.addE5Actions([self.__action], 'wizards')
121
122 def __initMenu(self):
123 """
124 Private method to add the actions to the right menu.
125 """
126 menu = self.__ui.getMenu("wizards")
127 if menu:
128 menu.addAction(self.__action)
129
130 def __handle(self):
131 """
132 Private method to handle the wizards action.
133 """
134 editor = e5App().getObject("ViewManager").activeWindow()
135
136 if editor is None:
137 E5MessageBox.critical(
138 self.__ui,
139 self.trUtf8('No current editor'),
140 self.trUtf8('Please open or create a file first.'))
141 else:
142 from WizardDataUriEncoder.DataUriEncoderWizardDialog import \
143 DataUriEncoderWizardDialog
144 dlg = DataUriEncoderWizardDialog(None)
145 if dlg.exec_() == QDialog.Accepted:
146 code = dlg.getCode()
147 if code:
148 line, index = editor.getCursorPosition()
149 # It should be done on this way to allow undo
150 editor.beginUndoAction()
151 editor.insertAt(code, line, index)
152 editor.endUndoAction()

eric ide

mercurial