|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2007 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Python re wizard plugin. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
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 = "Python re Wizard Plugin" |
|
23 author = "Detlev Offenbach <detlev@die-offenbachs.de>" |
|
24 autoactivate = True |
|
25 deactivateable = True |
|
26 version = UI.Info.VersionOnly |
|
27 className = "PyRegExpWizard" |
|
28 packageName = "__core__" |
|
29 shortDescription = "Show the Python re wizard." |
|
30 longDescription = """This plugin shows the Python re wizard.""" |
|
31 pyqtApi = 2 |
|
32 python2Compatible = True |
|
33 # End-Of-Header |
|
34 |
|
35 error = "" |
|
36 |
|
37 |
|
38 class PyRegExpWizard(QObject): |
|
39 """ |
|
40 Class implementing the Python re wizard plugin. |
|
41 """ |
|
42 def __init__(self, ui): |
|
43 """ |
|
44 Constructor |
|
45 |
|
46 @param ui reference to the user interface object (UI.UserInterface) |
|
47 """ |
|
48 super(PyRegExpWizard, self).__init__(ui) |
|
49 self.__ui = ui |
|
50 |
|
51 def activate(self): |
|
52 """ |
|
53 Public method to activate this plugin. |
|
54 |
|
55 @return tuple of None and activation status (boolean) |
|
56 """ |
|
57 self.__initAction() |
|
58 self.__initMenu() |
|
59 |
|
60 return None, True |
|
61 |
|
62 def deactivate(self): |
|
63 """ |
|
64 Public method to deactivate this plugin. |
|
65 """ |
|
66 menu = self.__ui.getMenu("wizards") |
|
67 if menu: |
|
68 menu.removeAction(self.action) |
|
69 self.__ui.removeE5Actions([self.action], 'wizards') |
|
70 |
|
71 def __initAction(self): |
|
72 """ |
|
73 Private method to initialize the action. |
|
74 """ |
|
75 self.action = E5Action( |
|
76 self.tr('Python re Wizard'), |
|
77 self.tr('&Python re Wizard...'), 0, 0, self, |
|
78 'wizards_python_re') |
|
79 self.action.setStatusTip(self.tr('Python re Wizard')) |
|
80 self.action.setWhatsThis(self.tr( |
|
81 """<b>Python re Wizard</b>""" |
|
82 """<p>This wizard opens a dialog for entering all the parameters""" |
|
83 """ needed to create a Python re string. The generated code is""" |
|
84 """ inserted at the current cursor position.</p>""" |
|
85 )) |
|
86 self.action.triggered.connect(self.__handle) |
|
87 |
|
88 self.__ui.addE5Actions([self.action], 'wizards') |
|
89 |
|
90 def __initMenu(self): |
|
91 """ |
|
92 Private method to add the actions to the right menu. |
|
93 """ |
|
94 menu = self.__ui.getMenu("wizards") |
|
95 if menu: |
|
96 menu.addAction(self.action) |
|
97 |
|
98 def __callForm(self, editor): |
|
99 """ |
|
100 Private method to display a dialog and get the code. |
|
101 |
|
102 @param editor reference to the current editor |
|
103 @return the generated code (string) |
|
104 """ |
|
105 from WizardPlugins.PyRegExpWizard.PyRegExpWizardDialog import \ |
|
106 PyRegExpWizardDialog |
|
107 dlg = PyRegExpWizardDialog(None, True) |
|
108 if dlg.exec_() == QDialog.Accepted: |
|
109 line, index = editor.getCursorPosition() |
|
110 indLevel = editor.indentation(line) // editor.indentationWidth() |
|
111 if editor.indentationsUseTabs(): |
|
112 indString = '\t' |
|
113 else: |
|
114 indString = editor.indentationWidth() * ' ' |
|
115 return (dlg.getCode(indLevel, indString), 1) |
|
116 else: |
|
117 return (None, False) |
|
118 |
|
119 def __handle(self): |
|
120 """ |
|
121 Private method to handle the wizards action. |
|
122 """ |
|
123 editor = e5App().getObject("ViewManager").activeWindow() |
|
124 |
|
125 if editor is None: |
|
126 E5MessageBox.critical( |
|
127 self.__ui, |
|
128 self.tr('No current editor'), |
|
129 self.tr('Please open or create a file first.')) |
|
130 else: |
|
131 code, ok = self.__callForm(editor) |
|
132 if ok: |
|
133 line, index = editor.getCursorPosition() |
|
134 # It should be done on this way to allow undo |
|
135 editor.beginUndoAction() |
|
136 editor.insertAt(code, line, index) |
|
137 editor.endUndoAction() |