Plugins/PluginWizardQRegularExpression.py

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

eric ide

mercurial