RefactoringRope/MoveDialog.py

branch
eric7
changeset 389
4f53795beff0
parent 374
958f34e97952
child 396
933b8fcd854f
equal deleted inserted replaced
388:cb044ec27c24 389:4f53795beff0
24 24
25 class MoveDialog(RefactoringDialogBase, Ui_MoveDialog): 25 class MoveDialog(RefactoringDialogBase, Ui_MoveDialog):
26 """ 26 """
27 Class implementing the Move Method or Module dialog. 27 Class implementing the Move Method or Module dialog.
28 """ 28 """
29
29 def __init__(self, refactoring, title, filename, offset, parent=None): 30 def __init__(self, refactoring, title, filename, offset, parent=None):
30 """ 31 """
31 Constructor 32 Constructor
32 33
33 @param refactoring reference to the main refactoring object 34 @param refactoring reference to the main refactoring object
34 @type RefactoringServer 35 @type RefactoringServer
35 @param title title of the dialog 36 @param title title of the dialog
36 @type str 37 @type str
37 @param filename file name to be worked on 38 @param filename file name to be worked on
41 @param parent reference to the parent widget 42 @param parent reference to the parent widget
42 @type QWidget 43 @type QWidget
43 """ 44 """
44 RefactoringDialogBase.__init__(self, refactoring, title, parent) 45 RefactoringDialogBase.__init__(self, refactoring, title, parent)
45 self.setupUi(self) 46 self.setupUi(self)
46 47
47 self._changeGroupName = "Move" 48 self._changeGroupName = "Move"
48 49
49 self.__destinationCompleter = EricFileCompleter(self.destinationEdit) 50 self.__destinationCompleter = EricFileCompleter(self.destinationEdit)
50 51
51 self.__filename = filename 52 self.__filename = filename
52 self.__offset = offset 53 self.__offset = offset
53 54
54 self.__project = ericApp().getObject("Project") 55 self.__project = ericApp().getObject("Project")
55 56
56 self.__okButton = self.buttonBox.button( 57 self.__okButton = self.buttonBox.button(QDialogButtonBox.StandardButton.Ok)
57 QDialogButtonBox.StandardButton.Ok)
58 self.__okButton.setEnabled(False) 58 self.__okButton.setEnabled(False)
59 self.__previewButton = self.buttonBox.addButton( 59 self.__previewButton = self.buttonBox.addButton(
60 self.tr("Preview"), QDialogButtonBox.ButtonRole.ActionRole) 60 self.tr("Preview"), QDialogButtonBox.ButtonRole.ActionRole
61 )
61 self.__previewButton.setDefault(True) 62 self.__previewButton.setDefault(True)
62 63
63 self.moveStackWidget.setCurrentIndex(0) 64 self.moveStackWidget.setCurrentIndex(0)
64 65
65 self.__moveType = "" 66 self.__moveType = ""
66 67
67 if offset is None: 68 if offset is None:
68 # it is a 'move module' refactoring, no need to determine 69 # it is a 'move module' refactoring, no need to determine
69 # the move type via the client 70 # the move type via the client
70 self.__processMoveType({"Kind": "move_module"}) 71 self.__processMoveType({"Kind": "move_module"})
71 else: 72 else:
72 self._refactoring.sendJson("RequestMoveType", { 73 self._refactoring.sendJson(
73 "ChangeGroup": self._changeGroupName, 74 "RequestMoveType",
74 "Title": self._title, 75 {
75 "FileName": self.__filename, 76 "ChangeGroup": self._changeGroupName,
76 "Offset": self.__offset, 77 "Title": self._title,
77 }) 78 "FileName": self.__filename,
78 79 "Offset": self.__offset,
80 },
81 )
82
79 def __processMoveType(self, data): 83 def __processMoveType(self, data):
80 """ 84 """
81 Private method to process the move type data sent by the refactoring 85 Private method to process the move type data sent by the refactoring
82 client in order to polish the dialog. 86 client in order to polish the dialog.
83 87
84 @param data dictionary containing the move type data 88 @param data dictionary containing the move type data
85 @type dict 89 @type dict
86 """ 90 """
87 self.__moveType = data["Kind"] 91 self.__moveType = data["Kind"]
88 92
89 if self.__moveType == "move_method": 93 if self.__moveType == "move_method":
90 self.setWindowTitle(self.tr("Move Method")) 94 self.setWindowTitle(self.tr("Move Method"))
91 self.moveStackWidget.setCurrentIndex(1) 95 self.moveStackWidget.setCurrentIndex(1)
92 self.methodEdit.setText(data["Method"]) 96 self.methodEdit.setText(data["Method"])
93 self.methodEdit.selectAll() 97 self.methodEdit.selectAll()
94 elif self.__moveType == "move_global_method": 98 elif self.__moveType == "move_global_method":
95 self.setWindowTitle(self.tr("Move Global Method")) 99 self.setWindowTitle(self.tr("Move Global Method"))
96 self.moveStackWidget.setCurrentIndex(2) 100 self.moveStackWidget.setCurrentIndex(2)
97 self.destinationLabel.setText(self.tr("Destination Module:")) 101 self.destinationLabel.setText(self.tr("Destination Module:"))
98 self.destinationEdit.setToolTip(self.tr( 102 self.destinationEdit.setToolTip(
99 "Enter the destination module for the method")) 103 self.tr("Enter the destination module for the method")
100 self.selectButton.setToolTip(self.tr( 104 )
101 "Select the destination module via a file selection dialog")) 105 self.selectButton.setToolTip(
106 self.tr("Select the destination module via a file selection dialog")
107 )
102 elif self.__moveType == "move_module": 108 elif self.__moveType == "move_module":
103 self.setWindowTitle(self.tr("Move Module")) 109 self.setWindowTitle(self.tr("Move Module"))
104 self.moveStackWidget.setCurrentIndex(2) 110 self.moveStackWidget.setCurrentIndex(2)
105 self.destinationLabel.setText(self.tr("Destination Package:")) 111 self.destinationLabel.setText(self.tr("Destination Package:"))
106 self.destinationEdit.setToolTip(self.tr( 112 self.destinationEdit.setToolTip(
107 "Enter the destination package for the module")) 113 self.tr("Enter the destination package for the module")
108 self.selectButton.setToolTip(self.tr( 114 )
109 "Select the destination package via a directory selection" 115 self.selectButton.setToolTip(
110 " dialog")) 116 self.tr(
117 "Select the destination package via a directory selection" " dialog"
118 )
119 )
111 else: 120 else:
112 self.setWindowTitle(self.tr("Move")) 121 self.setWindowTitle(self.tr("Move"))
113 self.moveStackWidget.setCurrentIndex(0) 122 self.moveStackWidget.setCurrentIndex(0)
114 123
115 self.__updateUI() 124 self.__updateUI()
116 125
117 msh = self.minimumSizeHint() 126 msh = self.minimumSizeHint()
118 self.resize(max(self.width(), msh.width()), msh.height()) 127 self.resize(max(self.width(), msh.width()), msh.height())
119 128
120 @pyqtSlot(str) 129 @pyqtSlot(str)
121 def on_attributeEdit_textChanged(self, text): 130 def on_attributeEdit_textChanged(self, text):
122 """ 131 """
123 Private slot to react to changes of the attribute. 132 Private slot to react to changes of the attribute.
124 133
125 @param text text entered into the edit 134 @param text text entered into the edit
126 @type str 135 @type str
127 """ 136 """
128 self.__updateUI() 137 self.__updateUI()
129 138
130 @pyqtSlot(str) 139 @pyqtSlot(str)
131 def on_methodEdit_textChanged(self, text): 140 def on_methodEdit_textChanged(self, text):
132 """ 141 """
133 Private slot to react to changes of the method. 142 Private slot to react to changes of the method.
134 143
135 @param text text entered into the edit 144 @param text text entered into the edit
136 @type str 145 @type str
137 """ 146 """
138 self.__updateUI() 147 self.__updateUI()
139 148
140 @pyqtSlot(str) 149 @pyqtSlot(str)
141 def on_destinationEdit_textChanged(self, text): 150 def on_destinationEdit_textChanged(self, text):
142 """ 151 """
143 Private slot to react to changes of the destination module. 152 Private slot to react to changes of the destination module.
144 153
145 @param text text entered into the edit 154 @param text text entered into the edit
146 @type str 155 @type str
147 """ 156 """
148 self.__updateUI() 157 self.__updateUI()
149 158
150 def __updateUI(self): 159 def __updateUI(self):
151 """ 160 """
152 Private method to perform various UI updates. 161 Private method to perform various UI updates.
153 """ 162 """
154 if self.__moveType == "move_method": 163 if self.__moveType == "move_method":
155 enable = ( 164 enable = self.attributeEdit.text() != "" and self.methodEdit.text() != ""
156 self.attributeEdit.text() != "" and
157 self.methodEdit.text() != ""
158 )
159 elif self.__moveType in ["move_global_method", "move_module"]: 165 elif self.__moveType in ["move_global_method", "move_module"]:
160 enable = self.destinationEdit.text() != "" 166 enable = self.destinationEdit.text() != ""
161 else: 167 else:
162 enable = False 168 enable = False
163 169
164 self.__okButton.setEnabled(enable) 170 self.__okButton.setEnabled(enable)
165 self.__previewButton.setEnabled(enable) 171 self.__previewButton.setEnabled(enable)
166 172
167 @pyqtSlot() 173 @pyqtSlot()
168 def on_selectButton_clicked(self): 174 def on_selectButton_clicked(self):
169 """ 175 """
170 Private slot called to select the destination module via a file 176 Private slot called to select the destination module via a file
171 selection dialog. 177 selection dialog.
178 destination = ( 184 destination = (
179 EricFileDialog.getOpenFileName( 185 EricFileDialog.getOpenFileName(
180 self, 186 self,
181 self.windowTitle(), 187 self.windowTitle(),
182 dest, 188 dest,
183 self.tr("Python Files (*.py *.py3);;All Files (*)")) 189 self.tr("Python Files (*.py *.py3);;All Files (*)"),
184 if self.__moveType == "move_global_method" else 190 )
191 if self.__moveType == "move_global_method"
192 else
185 # move_module 193 # move_module
186 EricFileDialog.getExistingDirectory( 194 EricFileDialog.getExistingDirectory(self, self.windowTitle(), dest)
187 self,
188 self.windowTitle(),
189 dest)
190 ) 195 )
191 196
192 if destination: 197 if destination:
193 destination = Utilities.toNativeSeparators(destination) 198 destination = Utilities.toNativeSeparators(destination)
194 if not self.__project.startswithProjectPath(destination): 199 if not self.__project.startswithProjectPath(destination):
195 if self.__moveType == "move_global_method": 200 if self.__moveType == "move_global_method":
196 errorMessage = self.tr("""The selected module must be """ 201 errorMessage = self.tr(
197 """inside the project.""") 202 """The selected module must be """ """inside the project."""
203 )
198 else: 204 else:
199 # move_module 205 # move_module
200 errorMessage = self.tr("""The selected directory must""" 206 errorMessage = self.tr(
201 """ be inside the project.""") 207 """The selected directory must""" """ be inside the project."""
202 EricMessageBox.critical( 208 )
203 self, 209 EricMessageBox.critical(self, self.windowTitle(), errorMessage)
204 self.windowTitle(),
205 errorMessage)
206 return 210 return
207 211
208 if self.__moveType == "move_global_method": 212 if self.__moveType == "move_global_method":
209 if not os.path.exists(destination): 213 if not os.path.exists(destination):
210 EricMessageBox.critical( 214 EricMessageBox.critical(
211 self, 215 self,
212 self.windowTitle(), 216 self.windowTitle(),
213 self.tr("""The selected module <b>{0}</b> does""" 217 self.tr(
214 """ not exist.""").format(destination)) 218 """The selected module <b>{0}</b> does""" """ not exist."""
219 ).format(destination),
220 )
215 return 221 return
216 else: 222 else:
217 # move_module 223 # move_module
218 if not os.path.exists( 224 if not os.path.exists(os.path.join(destination, "__init__.py")):
219 os.path.join(destination, "__init__.py")):
220 EricMessageBox.critical( 225 EricMessageBox.critical(
221 self, 226 self,
222 self.windowTitle(), 227 self.windowTitle(),
223 self.tr("""The selected directory <b>{0}</b> is""" 228 self.tr(
224 """ not a package.""").format(destination)) 229 """The selected directory <b>{0}</b> is"""
230 """ not a package."""
231 ).format(destination),
232 )
225 return 233 return
226 234
227 destination = self.__project.getRelativePath(destination) 235 destination = self.__project.getRelativePath(destination)
228 self.destinationEdit.setText(destination) 236 self.destinationEdit.setText(destination)
229 237
230 def __checkDestination(self): 238 def __checkDestination(self):
231 """ 239 """
232 Private method to check the destination entered. 240 Private method to check the destination entered.
233 241
234 @return flag indicating a valid entry 242 @return flag indicating a valid entry
235 @rtype bool 243 @rtype bool
236 """ 244 """
237 destination = os.path.join( 245 destination = os.path.join(
238 self.__project.getProjectPath(), 246 self.__project.getProjectPath(), self.destinationEdit.text()
239 self.destinationEdit.text()) 247 )
240 if self.__moveType == "move_global_method": 248 if self.__moveType == "move_global_method":
241 if not os.path.exists(destination): 249 if not os.path.exists(destination):
242 EricMessageBox.critical( 250 EricMessageBox.critical(
243 self, 251 self,
244 self.windowTitle(), 252 self.windowTitle(),
245 self.tr("""The selected module <b>{0}</b> does""" 253 self.tr(
246 """ not exist.""").format(destination)) 254 """The selected module <b>{0}</b> does""" """ not exist."""
255 ).format(destination),
256 )
247 return False 257 return False
248 else: 258 else:
249 # move_module 259 # move_module
250 if not os.path.exists(os.path.join(destination, "__init__.py")): 260 if not os.path.exists(os.path.join(destination, "__init__.py")):
251 EricMessageBox.critical( 261 EricMessageBox.critical(
252 self, 262 self,
253 self.windowTitle(), 263 self.windowTitle(),
254 self.tr("""The selected directory <b>{0}</b> is""" 264 self.tr(
255 """ not a package.""").format(destination)) 265 """The selected directory <b>{0}</b> is""" """ not a package."""
266 ).format(destination),
267 )
256 return False 268 return False
257 269
258 return True 270 return True
259 271
260 @pyqtSlot(QAbstractButton) 272 @pyqtSlot(QAbstractButton)
261 def on_buttonBox_clicked(self, button): 273 def on_buttonBox_clicked(self, button):
262 """ 274 """
263 Private slot to act on the button pressed. 275 Private slot to act on the button pressed.
264 276
265 @param button reference to the button pressed 277 @param button reference to the button pressed
266 @type QAbstractButton 278 @type QAbstractButton
267 """ 279 """
268 if self.__moveType == "move_method" or ( 280 if self.__moveType == "move_method" or (
269 self.__moveType in ["move_global_method", "move_module"] and 281 self.__moveType in ["move_global_method", "move_module"]
270 self.__checkDestination()): 282 and self.__checkDestination()
283 ):
271 if button == self.__previewButton: 284 if button == self.__previewButton:
272 self.requestPreview() 285 self.requestPreview()
273 elif button == self.__okButton: 286 elif button == self.__okButton:
274 self.applyChanges() 287 self.applyChanges()
275 288
276 def _calculateChanges(self): 289 def _calculateChanges(self):
277 """ 290 """
278 Protected method to initiate the calculation of the changes. 291 Protected method to initiate the calculation of the changes.
279 """ 292 """
280 newName = self.methodEdit.text() 293 newName = self.methodEdit.text()
281 if not newName: 294 if not newName:
282 newName = None 295 newName = None
283 296
284 self._refactoring.sendJson("CalculateMoveChanges", { 297 self._refactoring.sendJson(
285 "ChangeGroup": self._changeGroupName, 298 "CalculateMoveChanges",
286 "Title": self.windowTitle(), 299 {
287 "FileName": self.__filename, 300 "ChangeGroup": self._changeGroupName,
288 "Offset": self.__offset, 301 "Title": self.windowTitle(),
289 "Kind": self.__moveType, 302 "FileName": self.__filename,
290 "NewName": newName, 303 "Offset": self.__offset,
291 "Attribute": self.attributeEdit.text(), 304 "Kind": self.__moveType,
292 "DestinationModule": self.destinationEdit.text(), 305 "NewName": newName,
293 }) 306 "Attribute": self.attributeEdit.text(),
294 307 "DestinationModule": self.destinationEdit.text(),
308 },
309 )
310
295 def processChangeData(self, data): 311 def processChangeData(self, data):
296 """ 312 """
297 Public method to process the change data sent by the refactoring 313 Public method to process the change data sent by the refactoring
298 client. 314 client.
299 315
300 @param data dictionary containing the change data 316 @param data dictionary containing the change data
301 @type dict 317 @type dict
302 """ 318 """
303 subcommand = data["Subcommand"] 319 subcommand = data["Subcommand"]
304 if subcommand == "MoveType": 320 if subcommand == "MoveType":

eric ide

mercurial