|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 - 2017 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the eric plug-in wizard plug-in. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 try: |
|
12 str = unicode # __IGNORE_EXCEPTION__ |
|
13 except NameError: |
|
14 pass |
|
15 |
|
16 import os |
|
17 |
|
18 from PyQt5.QtCore import QObject |
|
19 from PyQt5.QtWidgets import QDialog |
|
20 |
|
21 from E5Gui.E5Application import e5App |
|
22 from E5Gui.E5Action import E5Action |
|
23 from E5Gui import E5MessageBox |
|
24 |
|
25 import UI.Info |
|
26 |
|
27 # Start-of-Header |
|
28 name = "eric plug-in Wizard Plug-in" |
|
29 author = "Detlev Offenbach <detlev@die-offenbachs.de>" |
|
30 autoactivate = True |
|
31 deactivateable = True |
|
32 version = UI.Info.VersionOnly |
|
33 className = "WizardEricPluginWizard" |
|
34 packageName = "__core__" |
|
35 shortDescription = "Wizard for the creation of an eric plug-in file." |
|
36 longDescription = \ |
|
37 """This plug-in implements a wizard to generate code for""" \ |
|
38 """ an eric plug-in main script file.""" |
|
39 needsRestart = False |
|
40 pyqtApi = 2 |
|
41 python2Compatible = True |
|
42 # End-of-Header |
|
43 |
|
44 error = "" |
|
45 |
|
46 |
|
47 class WizardEricPluginWizard(QObject): |
|
48 """ |
|
49 Class implementing the eric plug-in wizard plug-in. |
|
50 """ |
|
51 def __init__(self, ui): |
|
52 """ |
|
53 Constructor |
|
54 |
|
55 @param ui reference to the user interface object (UI.UserInterface) |
|
56 """ |
|
57 super(WizardEricPluginWizard, self).__init__(ui) |
|
58 self.__ui = ui |
|
59 self.__action = None |
|
60 |
|
61 def __initialize(self): |
|
62 """ |
|
63 Private slot to (re)initialize the plug-in. |
|
64 """ |
|
65 self.__act = None |
|
66 |
|
67 def activate(self): |
|
68 """ |
|
69 Public method to activate this plug-in. |
|
70 |
|
71 @return tuple of None and activation status (boolean) |
|
72 """ |
|
73 self.__initAction() |
|
74 self.__initMenu() |
|
75 |
|
76 return None, True |
|
77 |
|
78 def deactivate(self): |
|
79 """ |
|
80 Public method to deactivate this plug-in. |
|
81 """ |
|
82 menu = self.__ui.getMenu("wizards") |
|
83 if menu: |
|
84 menu.removeAction(self.__action) |
|
85 self.__ui.removeE5Actions([self.__action], 'wizards') |
|
86 |
|
87 def __initAction(self): |
|
88 """ |
|
89 Private method to initialize the action. |
|
90 """ |
|
91 self.__action = E5Action( |
|
92 self.tr('eric Plug-in Wizard'), |
|
93 self.tr('&eric Plug-in Wizard...'), |
|
94 0, 0, self, |
|
95 'wizards_eric_plugin') |
|
96 self.__action.setStatusTip(self.tr('eric Plug-in Wizard')) |
|
97 self.__action.setWhatsThis(self.tr( |
|
98 """<b>eric Plug-in Wizard</b>""" |
|
99 """<p>This wizard opens a dialog for entering all the parameters""" |
|
100 """ needed to create the basic contents of an eric plug-in file.""" |
|
101 """ The generated code is inserted at the current cursor""" |
|
102 """ position.</p>""" |
|
103 )) |
|
104 self.__action.triggered.connect(self.__handle) |
|
105 |
|
106 self.__ui.addE5Actions([self.__action], 'wizards') |
|
107 |
|
108 def __initMenu(self): |
|
109 """ |
|
110 Private method to add the actions to the right menu. |
|
111 """ |
|
112 menu = self.__ui.getMenu("wizards") |
|
113 if menu: |
|
114 menu.addAction(self.__action) |
|
115 |
|
116 def __callForm(self, editor): |
|
117 """ |
|
118 Private method to display a dialog and get the code. |
|
119 |
|
120 @param editor reference to the current editor |
|
121 @return generated code (string), the plug-in package name (string) |
|
122 and a flag indicating success (boolean) |
|
123 """ |
|
124 from WizardPlugins.EricPluginWizard.PluginWizardDialog import \ |
|
125 PluginWizardDialog |
|
126 dlg = PluginWizardDialog(None) |
|
127 if dlg.exec_() == QDialog.Accepted: |
|
128 return (dlg.getCode(), dlg.packageName(), True) |
|
129 else: |
|
130 return (None, "", False) |
|
131 |
|
132 def __handle(self): |
|
133 """ |
|
134 Private method to handle the wizards action. |
|
135 """ |
|
136 editor = e5App().getObject("ViewManager").activeWindow() |
|
137 |
|
138 if editor is None: |
|
139 E5MessageBox.critical( |
|
140 self.__ui, |
|
141 self.tr('No current editor'), |
|
142 self.tr('Please open or create a file first.')) |
|
143 else: |
|
144 code, packageName, ok = self.__callForm(editor) |
|
145 if ok: |
|
146 line, index = editor.getCursorPosition() |
|
147 # It should be done on this way to allow undo |
|
148 editor.beginUndoAction() |
|
149 editor.insertAt(code, line, index) |
|
150 editor.endUndoAction() |
|
151 if not editor.getFileName(): |
|
152 editor.setLanguage("dummy.py") |
|
153 |
|
154 if packageName: |
|
155 project = e5App().getObject("Project") |
|
156 packagePath = os.path.join(project.getProjectPath(), |
|
157 packageName) |
|
158 if not os.path.exists(packagePath): |
|
159 try: |
|
160 os.mkdir(packagePath) |
|
161 except OSError as err: |
|
162 E5MessageBox.critical( |
|
163 self, |
|
164 self.tr("Create Package"), |
|
165 self.tr( |
|
166 """<p>The package directory <b>{0}</b>""" |
|
167 """ could not be created. Aborting...""" |
|
168 """</p><p>Reason: {1}</p>""") |
|
169 .format(packagePath, str(err))) |
|
170 return |
|
171 packageFile = os.path.join(packagePath, "__init__.py") |
|
172 if not os.path.exists(packageFile): |
|
173 try: |
|
174 f = open(packageFile, "w", encoding="utf-8") |
|
175 f.close() |
|
176 except IOError as err: |
|
177 E5MessageBox.critical( |
|
178 self, |
|
179 self.tr("Create Package"), |
|
180 self.tr( |
|
181 """<p>The package file <b>{0}</b> could""" |
|
182 """ not be created. Aborting...</p>""" |
|
183 """<p>Reason: {1}</p>""") |
|
184 .format(packageFile, str(err))) |
|
185 return |
|
186 project.appendFile(packageFile) |
|
187 project.saveProject() |
|
188 e5App().getObject("ViewManager").openSourceFile( |
|
189 packageFile) |
|
190 |
|
191 # |
|
192 # eflag: noqa = M801 |