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