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