Sun, 30 Jan 2011 17:02:15 +0100
Added these refactoring functions:
- import organization
- restructure
- change signature
- inline argument default
# -*- coding: utf-8 -*- # Copyright (c) 2011 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing the Change Signature dialog. """ import copy from PyQt4.QtCore import pyqtSlot, Qt from PyQt4.QtGui import QDialog, QDialogButtonBox, QListWidgetItem, \ QAbstractButton import rope.refactor.change_signature from Ui_ChangeSignatureDialog import Ui_ChangeSignatureDialog from RefactoringDialogBase import RefactoringDialogBase from AddParameterDialog import AddParameterDialog class ChangeSignatureDialog(RefactoringDialogBase, Ui_ChangeSignatureDialog): """ Class implementing the Change Signature dialog. """ NameRole = Qt.UserRole IsAddedRole = Qt.UserRole + 1 DefaultRole = Qt.UserRole + 2 ValueRole = Qt.UserRole + 3 def __init__(self, refactoring, title, changer, parent=None): """ Constructor @param refactoring reference to the main refactoring object (Refactoring) @param title title of the dialog (string) @param changer reference to the signature changer object (rope.refactor.change_signature.ChangeSignature) @param parent reference to the parent widget (QWidget) """ RefactoringDialogBase.__init__(self, refactoring, title, parent) self.setupUi(self) self.__signature = changer self.__definition_info = self.__signature.get_args() self.__to_be_removed = [] self.__okButton = self.buttonBox.button(QDialogButtonBox.Ok) self.__previewButton = self.buttonBox.addButton( self.trUtf8("Preview"), QDialogButtonBox.ActionRole) self.__previewButton.setDefault(True) # populate the parameters list for arg, default in self.__definition_info: if default is None: itm = QListWidgetItem(arg, self.parameterList) else: itm = QListWidgetItem("{0} = {1}".format(arg, default), self.parameterList) itm.setData(ChangeSignatureDialog.NameRole, arg) itm.setData(ChangeSignatureDialog.IsAddedRole, False) itm.setData(ChangeSignatureDialog.DefaultRole, None) itm.setData(ChangeSignatureDialog.ValueRole, None) if self.parameterList.count(): self.parameterList.setCurrentRow(0) else: self.on_parameterList_currentRowChanged(-1) @pyqtSlot(int) def on_parameterList_currentRowChanged(self, currentRow): """ Private slot called, when the current row is changed. @param currentRow index of the current row (integer) """ if currentRow == -1: self.upButton.setEnabled(False) self.downButton.setEnabled(False) self.removeButton.setEnabled(False) else: maxIndex = self.parameterList.count() - 1 self.upButton.setEnabled(currentRow != 0) self.downButton.setEnabled(currentRow != maxIndex) @pyqtSlot() def on_upButton_clicked(self): """ Private slot called to move the selected item up in the list. """ row = self.parameterList.currentRow() if row == 0: # we're already at the top return itm = self.parameterList.takeItem(row) self.parameterList.insertItem(row - 1, itm) self.parameterList.setCurrentItem(itm) if row == 1: self.upButton.setEnabled(False) else: self.upButton.setEnabled(True) self.downButton.setEnabled(True) @pyqtSlot() def on_downButton_clicked(self): """ Private slot called to move the selected item down in the list. """ rows = self.parameterList.count() row = self.parameterList.currentRow() if row == rows - 1: # we're already at the end return itm = self.parameterList.takeItem(row) self.parameterList.insertItem(row + 1, itm) self.parameterList.setCurrentItem(itm) self.upButton.setEnabled(True) if row == rows - 2: self.downButton.setEnabled(False) else: self.downButton.setEnabled(True) @pyqtSlot() def on_removeButton_clicked(self): """ Private slot to remove a parameter. """ itm = self.parameterList.takeItem(self.parameterList.currentRow()) self.__to_be_removed.append(itm) @pyqtSlot() def on_addButton_clicked(self): """ Private slot to add a new parameter. """ dlg = AddParameterDialog(self) if dlg.exec_() == QDialog.Accepted: name, default, value = dlg.getData() if default: s = "%s = %s" % (name, default) else: s = name itm = QListWidgetItem(s) itm.setData(ChangeSignatureDialog.NameRole, name) itm.setData(ChangeSignatureDialog.IsAddedRole, True) if default: itm.setData(ChangeSignatureDialog.DefaultRole, default) else: itm.setData(ChangeSignatureDialog.DefaultRole, None) if value: itm.setData(ChangeSignatureDialog.ValueRole, value) else: itm.setData(ChangeSignatureDialog.ValueRole, None) if self.parameterList.count(): self.parameterList.insertItem( self.parameterList.currentRow() + 1, itm) else: self.parameterList.addItem(itm) self.parameterList.setCurrentItem(itm) @pyqtSlot(QAbstractButton) def on_buttonBox_clicked(self, button): """ Private slot to act on the button pressed. @param button reference to the button pressed (QAbstractButton) """ if button == self.__previewButton: self.previewChanges() elif button == self.__okButton: self.applyChanges() def __getParameterIndex(self, definition_info, name): """ Private method to calculate the index of the given paramter. @param definition_info object containing the method definition @param name parameter name (string) @return index of the parameter (integer) """ for index, pair in enumerate(definition_info): if pair[0] == name: return index def _calculateChanges(self, handle): """ Protected method to calculate the changes. @param handle reference to the task handle (rope.base.taskhandle.TaskHandle) @return reference to the Changes object (rope.base.change.ChangeSet) """ changers = [] definition_info = copy.deepcopy(self.__definition_info) for itm in self.__to_be_removed: if itm.data(ChangeSignatureDialog.IsAddedRole): continue index = self.__getParameterIndex(definition_info, itm.data(ChangeSignatureDialog.NameRole)) remover = rope.refactor.change_signature.ArgumentRemover(index) changers.append(remover) del definition_info[index] for index in range(self.parameterList.count()): itm = self.parameterList.item(index) if itm.data(ChangeSignatureDialog.IsAddedRole): name = itm.data(ChangeSignatureDialog.NameRole) default = itm.data(ChangeSignatureDialog.DefaultRole) value = itm.data(ChangeSignatureDialog.ValueRole) adder = rope.refactor.change_signature.ArgumentAdder( index, name, default, value) changers.append(adder) try: definition_info.insert(index, (name, default)) except Exception as err: self._refactoring.handleRopeError(err, self._title) new_ordering = [] for row in range(self.parameterList.count()): itm = self.parameterList.item(row) name = itm.data(ChangeSignatureDialog.NameRole) new_ordering.append( self.__getParameterIndex(definition_info, name)) autodef = self.autodefEdit.text() if not autodef: autodef = None changers.append(rope.refactor.change_signature.ArgumentReorderer( new_ordering, autodef=autodef)) try: changes = self.__signature.get_changes( changers, in_hierarchy=self.hierarchyCheckBox.isChecked(), task_handle=handle) return changes except Exception as err: self._refactoring.handleRopeError(err, self._title, handle) return None