eric7/Plugins/PluginWizardSetup.py

branch
eric7
changeset 9201
2f1ccadee231
parent 8881
54e42bc2437a
child 9202
81388c6065e8
equal deleted inserted replaced
9199:831e08e94960 9201:2f1ccadee231
4 # 4 #
5 5
6 """ 6 """
7 Module implementing the setup.py wizard plug-in. 7 Module implementing the setup.py wizard plug-in.
8 """ 8 """
9
10 import functools
9 11
10 from PyQt6.QtCore import QObject 12 from PyQt6.QtCore import QObject
11 from PyQt6.QtWidgets import QDialog 13 from PyQt6.QtWidgets import QDialog
12 14
13 from EricWidgets.EricApplication import ericApp 15 from EricWidgets.EricApplication import ericApp
42 """ 44 """
43 def __init__(self, ui): 45 def __init__(self, ui):
44 """ 46 """
45 Constructor 47 Constructor
46 48
47 @param ui reference to the user interface object (UI.UserInterface) 49 @param ui reference to the user interface object
50 @type UI.UserInterface
48 """ 51 """
49 super().__init__(ui) 52 super().__init__(ui)
50 self.__ui = ui 53 self.__ui = ui
51 self.__action = None 54 self.__actions = []
52
53 def __initialize(self):
54 """
55 Private slot to (re)initialize the plug-in.
56 """
57 self.__act = None
58 55
59 def activate(self): 56 def activate(self):
60 """ 57 """
61 Public method to activate this plug-in. 58 Public method to activate this plug-in.
62 59
63 @return tuple of None and activation status (boolean) 60 @return tuple of None and activation status
61 @rtype tuple of (None, boolean)
64 """ 62 """
65 self.__initAction() 63 self.__initActions()
66 self.__initMenu() 64 self.__initMenu()
67 65
68 return None, True 66 return None, True
69 67
70 def deactivate(self): 68 def deactivate(self):
71 """ 69 """
72 Public method to deactivate this plug-in. 70 Public method to deactivate this plug-in.
73 """ 71 """
74 menu = self.__ui.getMenu("wizards") 72 menu = self.__ui.getMenu("wizards")
75 if menu: 73 if menu:
76 menu.removeAction(self.__action) 74 for act in self.__actions:
77 self.__ui.removeEricActions([self.__action], 'wizards') 75 menu.removeAction(act)
76 self.__ui.removeEricActions(self.__actions, 'wizards')
78 77
79 def __initAction(self): 78 def __initActions(self):
80 """ 79 """
81 Private method to initialize the action. 80 Private method to initialize the actions.
82 """ 81 """
83 self.__action = EricAction( 82 # 1. action for 'setup.py' creation
83 act = EricAction(
84 self.tr('setup.py Wizard'), 84 self.tr('setup.py Wizard'),
85 self.tr('setup.py Wizard...'), 85 self.tr('setup.py Wizard...'),
86 0, 0, self, 86 0, 0, self,
87 'wizards_setup_py') 87 'wizards_setup_py')
88 self.__action.setStatusTip(self.tr('setup.py Wizard')) 88 act.setStatusTip(self.tr('setup.py Wizard'))
89 self.__action.setWhatsThis(self.tr( 89 act.setWhatsThis(self.tr(
90 """<b>setup.py Wizard</b>""" 90 """<b>setup.py Wizard</b>"""
91 """<p>This wizard opens a dialog for entering all the parameters""" 91 """<p>This wizard opens a dialog for entering all the parameters"""
92 """ needed to create the basic contents of a setup.py file. The""" 92 """ needed to create the basic contents of a setup.py file. The"""
93 """ generated code is inserted at the current cursor position.""" 93 """ generated code is inserted at the current cursor position."""
94 """</p>""" 94 """</p>"""
95 )) 95 ))
96 self.__action.triggered.connect(self.__handle) 96 act.triggered.connect(functools.partial(self.__handle, "setup.py"))
97 self.__actions.append(act)
97 98
98 self.__ui.addEricActions([self.__action], 'wizards') 99 # 2. action for 'setup.cfg' creation
100 act = EricAction(
101 self.tr('setup.cfg Wizard'),
102 self.tr('setup.cfg Wizard...'),
103 0, 0, self,
104 'wizards_setup_cfg')
105 act.setStatusTip(self.tr('setup.cfg Wizard'))
106 act.setWhatsThis(self.tr(
107 """<b>setup.cfg Wizard</b>"""
108 """<p>This wizard opens a dialog for entering all the parameters"""
109 """ needed to create the basic contents of a setup.cfg file. The"""
110 """ generated code is inserted at the current cursor position."""
111 """</p>"""
112 ))
113 act.triggered.connect(functools.partial(self.__handle, "setup.cfg"))
114 self.__actions.append(act)
115
116 # 3. action for 'pyproject.toml' creation
117 act = EricAction(
118 self.tr('pyproject.toml Wizard'),
119 self.tr('pyproject.toml Wizard...'),
120 0, 0, self,
121 'wizards_pyproject_toml')
122 act.setStatusTip(self.tr('pyproject.toml Wizard'))
123 act.setWhatsThis(self.tr(
124 """<b>pyproject.toml Wizard</b>"""
125 """<p>This wizard opens a dialog for entering all the parameters"""
126 """ needed to create the basic contents of a pyproject.toml file. The"""
127 """ generated code is inserted at the current cursor position."""
128 """</p>"""
129 ))
130 act.triggered.connect(functools.partial(self.__handle, "pyproject.toml"))
131 self.__actions.append(act)
132
133 self.__ui.addEricActions(self.__actions, 'wizards')
99 134
100 def __initMenu(self): 135 def __initMenu(self):
101 """ 136 """
102 Private method to add the actions to the right menu. 137 Private method to add the actions to the right menu.
103 """ 138 """
104 menu = self.__ui.getMenu("wizards") 139 menu = self.__ui.getMenu("wizards")
105 if menu: 140 if menu:
106 menu.addAction(self.__action) 141 menu.addActions(self.__actions)
107 142
108 def __callForm(self, editor): 143 def __callForm(self, editor, category):
109 """ 144 """
110 Private method to display a dialog and get the code. 145 Private method to display a dialog and get the code.
111 146
112 @param editor reference to the current editor 147 @param editor reference to the current editor
113 @return the generated code (string) 148 @type Editor
149 @param category category of setup file to create
150 @type str
151 @return tuple containing the generated code and a flag indicating an error
152 @rtype tuple of (str, bool)
114 """ 153 """
115 from WizardPlugins.SetupWizard.SetupWizardDialog import ( 154 from WizardPlugins.SetupWizard.SetupWizardDialog import (
116 SetupWizardDialog 155 SetupWizardDialog
117 ) 156 )
118 dlg = SetupWizardDialog(None) 157 dlg = SetupWizardDialog(category, self.__ui)
119 if dlg.exec() == QDialog.DialogCode.Accepted: 158 if dlg.exec() == QDialog.DialogCode.Accepted:
120 line, index = editor.getCursorPosition() 159 line, index = editor.getCursorPosition()
121 indLevel = editor.indentation(line) // editor.indentationWidth() 160 indLevel = editor.indentation(line) // editor.indentationWidth()
122 if editor.indentationsUseTabs(): 161 if editor.indentationsUseTabs():
123 indString = '\t' 162 indString = '\t'
124 else: 163 else:
125 indString = editor.indentationWidth() * ' ' 164 indString = editor.indentationWidth() * ' '
126 return (dlg.getCode(indLevel, indString), True) 165 return (dlg.getCode(indLevel, indString), True)
127 else: 166 else:
128 return (None, False) 167 return ("", False)
129 168
130 def __handle(self): 169 def __handle(self, category):
131 """ 170 """
132 Private method to handle the wizards action. 171 Private method to handle the wizards action.
172
173 @param category category of setup file to create
174 @type str
133 """ 175 """
134 editor = ericApp().getObject("ViewManager").activeWindow() 176 editor = ericApp().getObject("ViewManager").activeWindow()
135 177
136 if editor is None: 178 if editor is None:
137 EricMessageBox.critical( 179 EricMessageBox.critical(
138 self.__ui, 180 self.__ui,
139 self.tr('No current editor'), 181 self.tr('No current editor'),
140 self.tr('Please open or create a file first.')) 182 self.tr('Please open or create a file first.'))
141 else: 183 else:
142 code, ok = self.__callForm(editor) 184 sourceCode, ok = self.__callForm(editor, category)
143 if ok: 185 if ok:
144 line, index = editor.getCursorPosition() 186 line, index = editor.getCursorPosition()
145 # It should be done on this way to allow undo 187 # It should be done this way to allow undo
146 editor.beginUndoAction() 188 editor.beginUndoAction()
147 editor.insertAt(code, line, index) 189 editor.insertAt(sourceCode, line, index)
148 editor.endUndoAction() 190 editor.endUndoAction()
149 191
150 # 192 #
151 # eflag: noqa = M801 193 # eflag: noqa = M801

eric ide

mercurial