eric6/Plugins/PluginWizardQRegularExpression.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7199
c71bd6f21748
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2013 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the QRegularExpression 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 = "QRegularExpression Wizard Plugin"
23 author = "Detlev Offenbach <detlev@die-offenbachs.de>"
24 autoactivate = True
25 deactivateable = True
26 version = UI.Info.VersionOnly
27 className = "QRegularExpressionWizard"
28 packageName = "__core__"
29 shortDescription = "Show the QRegularExpression wizard."
30 longDescription = """This plugin shows the QRegularExpression wizard."""
31 pyqtApi = 2
32 python2Compatible = True
33 # End-Of-Header
34
35 error = ""
36
37
38 class QRegularExpressionWizard(QObject):
39 """
40 Class implementing the QRegularExpression 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(QRegularExpressionWizard, 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('QRegularExpression Wizard'),
77 self.tr('QRegularE&xpression Wizard...'), 0, 0, self,
78 'wizards_qregularexpression')
79 self.action.setStatusTip(self.tr('QRegularExpression Wizard'))
80 self.action.setWhatsThis(self.tr(
81 """<b>QRegularExpression Wizard</b>"""
82 """<p>This wizard opens a dialog for entering all the parameters"""
83 """ needed to create a QRegularExpression string. The generated"""
84 """ code is 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.QRegularExpressionWizard\
106 .QRegularExpressionWizardDialog import \
107 QRegularExpressionWizardDialog
108 dlg = QRegularExpressionWizardDialog(None, True)
109 if dlg.exec_() == QDialog.Accepted:
110 line, index = editor.getCursorPosition()
111 indLevel = editor.indentation(line) // editor.indentationWidth()
112 if editor.indentationsUseTabs():
113 indString = '\t'
114 else:
115 indString = editor.indentationWidth() * ' '
116 return (dlg.getCode(indLevel, indString), 1)
117 else:
118 return (None, False)
119
120 def __handle(self):
121 """
122 Private method to handle the wizards action.
123 """
124 editor = e5App().getObject("ViewManager").activeWindow()
125
126 if editor is None:
127 E5MessageBox.critical(
128 self.__ui,
129 self.tr('No current editor'),
130 self.tr('Please open or create a file first.'))
131 else:
132 code, ok = self.__callForm(editor)
133 if ok:
134 line, index = editor.getCursorPosition()
135 # It should be done on this way to allow undo
136 editor.beginUndoAction()
137 editor.insertAt(code, line, index)
138 editor.endUndoAction()

eric ide

mercurial