PluginWizardDataUriEncoder.py

branch
eric7
changeset 50
4d34c264a71d
parent 48
d9cd58b1e0c8
child 51
619765dd3aa8
equal deleted inserted replaced
49:ef86e0082122 50:4d34c264a71d
38 38
39 class WizardDataUriEncoderPlugin(QObject): 39 class WizardDataUriEncoderPlugin(QObject):
40 """ 40 """
41 Class implementing the base64 data URI encoder wizard plug-in. 41 Class implementing the base64 data URI encoder wizard plug-in.
42 """ 42 """
43
43 def __init__(self, ui): 44 def __init__(self, ui):
44 """ 45 """
45 Constructor 46 Constructor
46 47
47 @param ui reference to the user interface object 48 @param ui reference to the user interface object
48 @type UserInterface 49 @type UserInterface
49 """ 50 """
50 QObject.__init__(self, ui) 51 QObject.__init__(self, ui)
51 52
52 self.__ui = ui 53 self.__ui = ui
53 self.__action = None 54 self.__action = None
54 55
55 self.__translator = None 56 self.__translator = None
56 self.__loadTranslator() 57 self.__loadTranslator()
57 58
58 def activate(self): 59 def activate(self):
59 """ 60 """
60 Public method to activate this plug-in. 61 Public method to activate this plug-in.
61 62
62 @return tuple of None and activation status 63 @return tuple of None and activation status
63 @rtype (None, bool) 64 @rtype (None, bool)
64 """ 65 """
65 self.__initAction() 66 self.__initAction()
66 self.__initMenu() 67 self.__initMenu()
67 68
68 return None, True 69 return None, True
69 70
70 def deactivate(self): 71 def deactivate(self):
71 """ 72 """
72 Public method to deactivate this plug-in. 73 Public method to deactivate this plug-in.
73 """ 74 """
74 menu = self.__ui.getMenu("wizards") 75 menu = self.__ui.getMenu("wizards")
75 if menu: 76 if menu:
76 menu.removeAction(self.__action) 77 menu.removeAction(self.__action)
77 self.__ui.removeEricActions([self.__action], 'wizards') 78 self.__ui.removeEricActions([self.__action], "wizards")
78 79
79 def __loadTranslator(self): 80 def __loadTranslator(self):
80 """ 81 """
81 Private method to load the translation file. 82 Private method to load the translation file.
82 """ 83 """
83 if self.__ui is not None: 84 if self.__ui is not None:
84 loc = self.__ui.getLocale() 85 loc = self.__ui.getLocale()
85 if loc and loc != "C": 86 if loc and loc != "C":
86 locale_dir = os.path.join( 87 locale_dir = os.path.join(
87 os.path.dirname(__file__), "WizardDataUriEncoder", "i18n") 88 os.path.dirname(__file__), "WizardDataUriEncoder", "i18n"
89 )
88 translation = "datauriencoder_{0}".format(loc) 90 translation = "datauriencoder_{0}".format(loc)
89 translator = QTranslator(None) 91 translator = QTranslator(None)
90 loaded = translator.load(translation, locale_dir) 92 loaded = translator.load(translation, locale_dir)
91 if loaded: 93 if loaded:
92 self.__translator = translator 94 self.__translator = translator
93 ericApp().installTranslator(self.__translator) 95 ericApp().installTranslator(self.__translator)
94 else: 96 else:
95 print("Warning: translation file '{0}' could not be" 97 print(
96 " loaded.".format(translation)) 98 "Warning: translation file '{0}' could not be"
99 " loaded.".format(translation)
100 )
97 print("Using default.") 101 print("Using default.")
98 102
99 def __initAction(self): 103 def __initAction(self):
100 """ 104 """
101 Private method to initialize the action. 105 Private method to initialize the action.
102 """ 106 """
103 self.__action = EricAction( 107 self.__action = EricAction(
104 self.tr('Base64 Data Uri Encoder Wizard'), 108 self.tr("Base64 Data Uri Encoder Wizard"),
105 self.tr('Base&64 Data Uri Encoder Wizard...'), 109 self.tr("Base&64 Data Uri Encoder Wizard..."),
106 0, 0, self, 110 0,
107 'wizards_datauriencoder') 111 0,
108 self.__action.setStatusTip(self.tr( 112 self,
109 'Base64 Data Uri Encoder Wizard')) 113 "wizards_datauriencoder",
110 self.__action.setWhatsThis(self.tr( 114 )
111 """<b>Base64 Data Uri Encoder Wizard</b>""" 115 self.__action.setStatusTip(self.tr("Base64 Data Uri Encoder Wizard"))
112 """<p>This wizard opens a dialog for entering all the parameters""" 116 self.__action.setWhatsThis(
113 """ needed to create code for a base64 encoded data URI.</p>""" 117 self.tr(
114 )) 118 """<b>Base64 Data Uri Encoder Wizard</b>"""
119 """<p>This wizard opens a dialog for entering all the parameters"""
120 """ needed to create code for a base64 encoded data URI.</p>"""
121 )
122 )
115 self.__action.triggered.connect(self.__handle) 123 self.__action.triggered.connect(self.__handle)
116 124
117 self.__ui.addEricActions([self.__action], 'wizards') 125 self.__ui.addEricActions([self.__action], "wizards")
118 126
119 def __initMenu(self): 127 def __initMenu(self):
120 """ 128 """
121 Private method to add the actions to the right menu. 129 Private method to add the actions to the right menu.
122 """ 130 """
123 menu = self.__ui.getMenu("wizards") 131 menu = self.__ui.getMenu("wizards")
124 if menu: 132 if menu:
125 menu.addAction(self.__action) 133 menu.addAction(self.__action)
126 134
127 def __handle(self): 135 def __handle(self):
128 """ 136 """
129 Private method to handle the wizards action. 137 Private method to handle the wizards action.
130 """ 138 """
131 editor = ericApp().getObject("ViewManager").activeWindow() 139 editor = ericApp().getObject("ViewManager").activeWindow()
132 140
133 if editor is None: 141 if editor is None:
134 EricMessageBox.critical( 142 EricMessageBox.critical(
135 self.__ui, 143 self.__ui,
136 self.tr('No current editor'), 144 self.tr("No current editor"),
137 self.tr('Please open or create a file first.')) 145 self.tr("Please open or create a file first."),
146 )
138 else: 147 else:
139 from WizardDataUriEncoder.DataUriEncoderWizardDialog import ( 148 from WizardDataUriEncoder.DataUriEncoderWizardDialog import (
140 DataUriEncoderWizardDialog 149 DataUriEncoderWizardDialog,
141 ) 150 )
151
142 dlg = DataUriEncoderWizardDialog(None) 152 dlg = DataUriEncoderWizardDialog(None)
143 if dlg.exec() == QDialog.DialogCode.Accepted: 153 if dlg.exec() == QDialog.DialogCode.Accepted:
144 code = dlg.getCode() 154 code = dlg.getCode()
145 if code: 155 if code:
146 line, index = editor.getCursorPosition() 156 line, index = editor.getCursorPosition()
147 # It should be done on this way to allow undo 157 # It should be done on this way to allow undo
148 editor.beginUndoAction() 158 editor.beginUndoAction()
149 editor.insertAt(code, line, index) 159 editor.insertAt(code, line, index)
150 editor.endUndoAction() 160 editor.endUndoAction()
151 161
162
152 # 163 #
153 # eflag: noqa = M801 164 # eflag: noqa = M801

eric ide

mercurial