RefactoringRope/ChangeSignatureDialog.py

branch
eric7
changeset 389
4f53795beff0
parent 374
958f34e97952
child 409
65153bf17e8d
equal deleted inserted replaced
388:cb044ec27c24 389:4f53795beff0
8 """ 8 """
9 9
10 import copy 10 import copy
11 11
12 from PyQt6.QtCore import pyqtSlot, Qt 12 from PyQt6.QtCore import pyqtSlot, Qt
13 from PyQt6.QtWidgets import ( 13 from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QListWidgetItem, QAbstractButton
14 QDialog, QDialogButtonBox, QListWidgetItem, QAbstractButton
15 )
16 14
17 from .Ui_ChangeSignatureDialog import Ui_ChangeSignatureDialog 15 from .Ui_ChangeSignatureDialog import Ui_ChangeSignatureDialog
18 from .RefactoringDialogBase import RefactoringDialogBase 16 from .RefactoringDialogBase import RefactoringDialogBase
19 17
20 18
21 class ChangeSignatureDialog(RefactoringDialogBase, Ui_ChangeSignatureDialog): 19 class ChangeSignatureDialog(RefactoringDialogBase, Ui_ChangeSignatureDialog):
22 """ 20 """
23 Class implementing the Change Signature dialog. 21 Class implementing the Change Signature dialog.
24 """ 22 """
23
25 NameRole = Qt.ItemDataRole.UserRole 24 NameRole = Qt.ItemDataRole.UserRole
26 IsAddedRole = Qt.ItemDataRole.UserRole + 1 25 IsAddedRole = Qt.ItemDataRole.UserRole + 1
27 DefaultRole = Qt.ItemDataRole.UserRole + 2 26 DefaultRole = Qt.ItemDataRole.UserRole + 2
28 ValueRole = Qt.ItemDataRole.UserRole + 3 27 ValueRole = Qt.ItemDataRole.UserRole + 3
29 28
30 def __init__(self, refactoring, title, filename, offset, parent=None): 29 def __init__(self, refactoring, title, filename, offset, parent=None):
31 """ 30 """
32 Constructor 31 Constructor
33 32
34 @param refactoring reference to the main refactoring object 33 @param refactoring reference to the main refactoring object
35 @type RefactoringServer 34 @type RefactoringServer
36 @param title title of the dialog 35 @param title title of the dialog
37 @type str 36 @type str
38 @param filename file name to be worked on 37 @param filename file name to be worked on
42 @param parent reference to the parent widget 41 @param parent reference to the parent widget
43 @type QWidget 42 @type QWidget
44 """ 43 """
45 RefactoringDialogBase.__init__(self, refactoring, title, parent) 44 RefactoringDialogBase.__init__(self, refactoring, title, parent)
46 self.setupUi(self) 45 self.setupUi(self)
47 46
48 self._changeGroupName = "ChangeSignature" 47 self._changeGroupName = "ChangeSignature"
49 48
50 self.__filename = filename 49 self.__filename = filename
51 self.__offset = offset 50 self.__offset = offset
52 51
53 self.__definition_info = [] 52 self.__definition_info = []
54 self.__to_be_removed = [] 53 self.__to_be_removed = []
55 54
56 self.__okButton = self.buttonBox.button( 55 self.__okButton = self.buttonBox.button(QDialogButtonBox.StandardButton.Ok)
57 QDialogButtonBox.StandardButton.Ok)
58 self.__previewButton = self.buttonBox.addButton( 56 self.__previewButton = self.buttonBox.addButton(
59 self.tr("Preview"), QDialogButtonBox.ButtonRole.ActionRole) 57 self.tr("Preview"), QDialogButtonBox.ButtonRole.ActionRole
58 )
60 self.__previewButton.setDefault(True) 59 self.__previewButton.setDefault(True)
61 60
62 self._refactoring.sendJson("RequestSignature", { 61 self._refactoring.sendJson(
63 "ChangeGroup": self._changeGroupName, 62 "RequestSignature",
64 "Title": self._title, 63 {
65 "FileName": self.__filename, 64 "ChangeGroup": self._changeGroupName,
66 "Offset": self.__offset, 65 "Title": self._title,
67 }) 66 "FileName": self.__filename,
68 67 "Offset": self.__offset,
68 },
69 )
70
69 def __processSignature(self, data): 71 def __processSignature(self, data):
70 """ 72 """
71 Private method to process the inline type data sent by the refactoring 73 Private method to process the inline type data sent by the refactoring
72 client in order to polish the dialog. 74 client in order to polish the dialog.
73 75
74 @param data dictionary containing the inline type data 76 @param data dictionary containing the inline type data
75 @type dict 77 @type dict
76 """ 78 """
77 self.__definition_info = data["DefinitionInfo"] 79 self.__definition_info = data["DefinitionInfo"]
78 80
79 # populate the parameters list 81 # populate the parameters list
80 for arg, default in self.__definition_info: 82 for arg, default in self.__definition_info:
81 itm = ( 83 itm = (
82 QListWidgetItem(arg, self.parameterList) 84 QListWidgetItem(arg, self.parameterList)
83 if default is None else 85 if default is None
84 QListWidgetItem("{0}={1}".format(arg, default), 86 else QListWidgetItem("{0}={1}".format(arg, default), self.parameterList)
85 self.parameterList)
86 ) 87 )
87 itm.setData(ChangeSignatureDialog.NameRole, arg) 88 itm.setData(ChangeSignatureDialog.NameRole, arg)
88 itm.setData(ChangeSignatureDialog.IsAddedRole, False) 89 itm.setData(ChangeSignatureDialog.IsAddedRole, False)
89 itm.setData(ChangeSignatureDialog.DefaultRole, None) 90 itm.setData(ChangeSignatureDialog.DefaultRole, None)
90 itm.setData(ChangeSignatureDialog.ValueRole, None) 91 itm.setData(ChangeSignatureDialog.ValueRole, None)
91 92
92 if self.parameterList.count(): 93 if self.parameterList.count():
93 self.parameterList.setCurrentRow(0) 94 self.parameterList.setCurrentRow(0)
94 else: 95 else:
95 self.on_parameterList_currentRowChanged(-1) 96 self.on_parameterList_currentRowChanged(-1)
96 97
97 @pyqtSlot(int) 98 @pyqtSlot(int)
98 def on_parameterList_currentRowChanged(self, currentRow): 99 def on_parameterList_currentRowChanged(self, currentRow):
99 """ 100 """
100 Private slot called, when the current row is changed. 101 Private slot called, when the current row is changed.
101 102
102 @param currentRow index of the current row 103 @param currentRow index of the current row
103 @type int 104 @type int
104 """ 105 """
105 if currentRow == -1: 106 if currentRow == -1:
106 self.upButton.setEnabled(False) 107 self.upButton.setEnabled(False)
108 self.removeButton.setEnabled(False) 109 self.removeButton.setEnabled(False)
109 else: 110 else:
110 maxIndex = self.parameterList.count() - 1 111 maxIndex = self.parameterList.count() - 1
111 self.upButton.setEnabled(currentRow != 0) 112 self.upButton.setEnabled(currentRow != 0)
112 self.downButton.setEnabled(currentRow != maxIndex) 113 self.downButton.setEnabled(currentRow != maxIndex)
113 114
114 @pyqtSlot() 115 @pyqtSlot()
115 def on_upButton_clicked(self): 116 def on_upButton_clicked(self):
116 """ 117 """
117 Private slot called to move the selected item up in the list. 118 Private slot called to move the selected item up in the list.
118 """ 119 """
119 row = self.parameterList.currentRow() 120 row = self.parameterList.currentRow()
120 if row == 0: 121 if row == 0:
121 # we're already at the top 122 # we're already at the top
122 return 123 return
123 124
124 itm = self.parameterList.takeItem(row) 125 itm = self.parameterList.takeItem(row)
125 self.parameterList.insertItem(row - 1, itm) 126 self.parameterList.insertItem(row - 1, itm)
126 self.parameterList.setCurrentItem(itm) 127 self.parameterList.setCurrentItem(itm)
127 if row == 1: 128 if row == 1:
128 self.upButton.setEnabled(False) 129 self.upButton.setEnabled(False)
129 else: 130 else:
130 self.upButton.setEnabled(True) 131 self.upButton.setEnabled(True)
131 self.downButton.setEnabled(True) 132 self.downButton.setEnabled(True)
132 133
133 @pyqtSlot() 134 @pyqtSlot()
134 def on_downButton_clicked(self): 135 def on_downButton_clicked(self):
135 """ 136 """
136 Private slot called to move the selected item down in the list. 137 Private slot called to move the selected item down in the list.
137 """ 138 """
138 rows = self.parameterList.count() 139 rows = self.parameterList.count()
139 row = self.parameterList.currentRow() 140 row = self.parameterList.currentRow()
140 if row == rows - 1: 141 if row == rows - 1:
141 # we're already at the end 142 # we're already at the end
142 return 143 return
143 144
144 itm = self.parameterList.takeItem(row) 145 itm = self.parameterList.takeItem(row)
145 self.parameterList.insertItem(row + 1, itm) 146 self.parameterList.insertItem(row + 1, itm)
146 self.parameterList.setCurrentItem(itm) 147 self.parameterList.setCurrentItem(itm)
147 self.upButton.setEnabled(True) 148 self.upButton.setEnabled(True)
148 if row == rows - 2: 149 if row == rows - 2:
149 self.downButton.setEnabled(False) 150 self.downButton.setEnabled(False)
150 else: 151 else:
151 self.downButton.setEnabled(True) 152 self.downButton.setEnabled(True)
152 153
153 @pyqtSlot() 154 @pyqtSlot()
154 def on_removeButton_clicked(self): 155 def on_removeButton_clicked(self):
155 """ 156 """
156 Private slot to remove a parameter. 157 Private slot to remove a parameter.
157 """ 158 """
158 itm = self.parameterList.takeItem(self.parameterList.currentRow()) 159 itm = self.parameterList.takeItem(self.parameterList.currentRow())
159 self.__to_be_removed.append(itm) 160 self.__to_be_removed.append(itm)
160 161
161 @pyqtSlot() 162 @pyqtSlot()
162 def on_addButton_clicked(self): 163 def on_addButton_clicked(self):
163 """ 164 """
164 Private slot to add a new parameter. 165 Private slot to add a new parameter.
165 """ 166 """
166 from .AddParameterDialog import AddParameterDialog 167 from .AddParameterDialog import AddParameterDialog
168
167 dlg = AddParameterDialog(self) 169 dlg = AddParameterDialog(self)
168 if dlg.exec() == QDialog.DialogCode.Accepted: 170 if dlg.exec() == QDialog.DialogCode.Accepted:
169 name, default, value = dlg.getData() 171 name, default, value = dlg.getData()
170 if default: 172 if default:
171 s = "{0}={1}".format(name, default) 173 s = "{0}={1}".format(name, default)
181 if value: 183 if value:
182 itm.setData(ChangeSignatureDialog.ValueRole, value) 184 itm.setData(ChangeSignatureDialog.ValueRole, value)
183 else: 185 else:
184 itm.setData(ChangeSignatureDialog.ValueRole, None) 186 itm.setData(ChangeSignatureDialog.ValueRole, None)
185 if self.parameterList.count(): 187 if self.parameterList.count():
186 self.parameterList.insertItem( 188 self.parameterList.insertItem(self.parameterList.currentRow() + 1, itm)
187 self.parameterList.currentRow() + 1, itm)
188 else: 189 else:
189 self.parameterList.addItem(itm) 190 self.parameterList.addItem(itm)
190 self.parameterList.setCurrentItem(itm) 191 self.parameterList.setCurrentItem(itm)
191 192
192 @pyqtSlot(QAbstractButton) 193 @pyqtSlot(QAbstractButton)
193 def on_buttonBox_clicked(self, button): 194 def on_buttonBox_clicked(self, button):
194 """ 195 """
195 Private slot to act on the button pressed. 196 Private slot to act on the button pressed.
196 197
197 @param button reference to the button pressed 198 @param button reference to the button pressed
198 @type QAbstractButton 199 @type QAbstractButton
199 """ 200 """
200 if button == self.__previewButton: 201 if button == self.__previewButton:
201 self.requestPreview() 202 self.requestPreview()
202 elif button == self.__okButton: 203 elif button == self.__okButton:
203 self.applyChanges() 204 self.applyChanges()
204 205
205 def __getParameterIndex(self, definition_info, name): 206 def __getParameterIndex(self, definition_info, name):
206 """ 207 """
207 Private method to calculate the index of the given paramter. 208 Private method to calculate the index of the given paramter.
208 209
209 @param definition_info list of lists containing the method signature 210 @param definition_info list of lists containing the method signature
210 definition 211 definition
211 @type list of lists of two str 212 @type list of lists of two str
212 @param name parameter name 213 @param name parameter name
213 @type str 214 @type str
215 @rtype int 216 @rtype int
216 """ 217 """
217 for index, pair in enumerate(definition_info): 218 for index, pair in enumerate(definition_info):
218 if pair[0] == name: 219 if pair[0] == name:
219 return index 220 return index
220 221
221 return -1 222 return -1
222 223
223 def _calculateChanges(self): 224 def _calculateChanges(self):
224 """ 225 """
225 Protected method to initiate the calculation of the changes. 226 Protected method to initiate the calculation of the changes.
228 definition_info = copy.deepcopy(self.__definition_info) 229 definition_info = copy.deepcopy(self.__definition_info)
229 for itm in self.__to_be_removed: 230 for itm in self.__to_be_removed:
230 if itm.data(ChangeSignatureDialog.IsAddedRole): 231 if itm.data(ChangeSignatureDialog.IsAddedRole):
231 continue 232 continue
232 index = self.__getParameterIndex( 233 index = self.__getParameterIndex(
233 definition_info, 234 definition_info, itm.data(ChangeSignatureDialog.NameRole)
234 itm.data(ChangeSignatureDialog.NameRole)) 235 )
235 if index >= 0: 236 if index >= 0:
236 removals.append(index) 237 removals.append(index)
237 del definition_info[index] 238 del definition_info[index]
238 239
239 additions = [] 240 additions = []
240 for index in range(self.parameterList.count()): 241 for index in range(self.parameterList.count()):
241 itm = self.parameterList.item(index) 242 itm = self.parameterList.item(index)
242 if itm.data(ChangeSignatureDialog.IsAddedRole): 243 if itm.data(ChangeSignatureDialog.IsAddedRole):
243 name = itm.data(ChangeSignatureDialog.NameRole) 244 name = itm.data(ChangeSignatureDialog.NameRole)
246 additions.append([index, name, default, value]) 247 additions.append([index, name, default, value])
247 try: 248 try:
248 definition_info.insert(index, [name, default]) 249 definition_info.insert(index, [name, default])
249 except Exception as err: 250 except Exception as err:
250 self._refactoring.handleRopeError(err, self._title) 251 self._refactoring.handleRopeError(err, self._title)
251 252
252 newOrdering = [] 253 newOrdering = []
253 for row in range(self.parameterList.count()): 254 for row in range(self.parameterList.count()):
254 itm = self.parameterList.item(row) 255 itm = self.parameterList.item(row)
255 name = itm.data(ChangeSignatureDialog.NameRole) 256 name = itm.data(ChangeSignatureDialog.NameRole)
256 index = self.__getParameterIndex(definition_info, name) 257 index = self.__getParameterIndex(definition_info, name)
257 if index >= 0: 258 if index >= 0:
258 newOrdering.append(index) 259 newOrdering.append(index)
259 autodef = self.autodefEdit.text() 260 autodef = self.autodefEdit.text()
260 if not autodef: 261 if not autodef:
261 autodef = None 262 autodef = None
262 263
263 self._refactoring.sendJson("CalculateSignatureChanges", { 264 self._refactoring.sendJson(
264 "ChangeGroup": self._changeGroupName, 265 "CalculateSignatureChanges",
265 "Title": self._title, 266 {
266 "FileName": self.__filename, 267 "ChangeGroup": self._changeGroupName,
267 "Offset": self.__offset, 268 "Title": self._title,
268 "Removals": removals, 269 "FileName": self.__filename,
269 "Additions": additions, 270 "Offset": self.__offset,
270 "Ordering": newOrdering, 271 "Removals": removals,
271 "AutoDef": autodef, 272 "Additions": additions,
272 "Hierarchy": self.hierarchyCheckBox.isChecked(), 273 "Ordering": newOrdering,
273 }) 274 "AutoDef": autodef,
274 275 "Hierarchy": self.hierarchyCheckBox.isChecked(),
276 },
277 )
278
275 def processChangeData(self, data): 279 def processChangeData(self, data):
276 """ 280 """
277 Public method to process the change data sent by the refactoring 281 Public method to process the change data sent by the refactoring
278 client. 282 client.
279 283
280 @param data dictionary containing the change data 284 @param data dictionary containing the change data
281 @type dict 285 @type dict
282 """ 286 """
283 subcommand = data["Subcommand"] 287 subcommand = data["Subcommand"]
284 if subcommand == "Signature": 288 if subcommand == "Signature":

eric ide

mercurial