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