1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2013 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing the QRegularExpression wizard plugin.
8"""
9
10from PyQt5.QtCore import QObject
11from PyQt5.QtWidgets import QDialog
12
13from E5Gui.E5Application import e5App
14from E5Gui.E5Action import E5Action
15from E5Gui import E5MessageBox
16
17import UI.Info
18
19# Start-Of-Header
20name = "QRegularExpression Wizard Plugin"
21author = "Detlev Offenbach <detlev@die-offenbachs.de>"
22autoactivate = True
23deactivateable = True
24version = UI.Info.VersionOnly
25className = "QRegularExpressionWizard"
26packageName = "__core__"
27shortDescription = "Show the QRegularExpression wizard."
28longDescription = """This plugin shows the QRegularExpression wizard."""
29pyqtApi = 2
30# End-Of-Header
31
32error = ""
33
34
35class 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().__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(
73            self.tr('QRegularExpression Wizard'),
74            self.tr('QRegularE&xpression Wizard...'), 0, 0, self,
75            'wizards_qregularexpression')
76        self.action.setStatusTip(self.tr('QRegularExpression Wizard'))
77        self.action.setWhatsThis(self.tr(
78            """<b>QRegularExpression Wizard</b>"""
79            """<p>This wizard opens a dialog for entering all the parameters"""
80            """ needed to create a QRegularExpression string. The generated"""
81            """ code is inserted at the current cursor position.</p>"""
82        ))
83        self.action.triggered.connect(self.__handle)
84
85        self.__ui.addE5Actions([self.action], 'wizards')
86
87    def __initMenu(self):
88        """
89        Private method to add the actions to the right menu.
90        """
91        menu = self.__ui.getMenu("wizards")
92        if menu:
93            menu.addAction(self.action)
94
95    def __callForm(self, editor):
96        """
97        Private method to display a dialog and get the code.
98
99        @param editor reference to the current editor
100        @return the generated code (string)
101        """
102        from WizardPlugins.QRegularExpressionWizard import (
103            QRegularExpressionWizardDialog
104        )
105        dlg = QRegularExpressionWizardDialog.QRegularExpressionWizardDialog(
106            None, True)
107        if dlg.exec() == QDialog.DialogCode.Accepted:
108            line, index = editor.getCursorPosition()
109            indLevel = editor.indentation(line) // editor.indentationWidth()
110            if editor.indentationsUseTabs():
111                indString = '\t'
112            else:
113                indString = editor.indentationWidth() * ' '
114            return (dlg.getCode(indLevel, indString), 1)
115        else:
116            return (None, False)
117
118    def __handle(self):
119        """
120        Private method to handle the wizards action.
121        """
122        editor = e5App().getObject("ViewManager").activeWindow()
123
124        if editor is None:
125            E5MessageBox.critical(
126                self.__ui,
127                self.tr('No current editor'),
128                self.tr('Please open or create a file first.'))
129        else:
130            code, ok = self.__callForm(editor)
131            if ok:
132                line, index = editor.getCursorPosition()
133                # It should be done on this way to allow undo
134                editor.beginUndoAction()
135                editor.insertAt(code, line, index)
136                editor.endUndoAction()
137