RefactoringRope/RenameDialog.py

changeset 3
3be1b4662b48
child 9
8cee612bcc28
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RefactoringRope/RenameDialog.py	Sat Jan 29 15:10:40 2011 +0100
@@ -0,0 +1,131 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2011 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing the Rename dialog.
+"""
+
+from PyQt4.QtCore import pyqtSlot
+from PyQt4.QtGui import QDialogButtonBox, QAbstractButton, QMessageBox
+
+from E5Gui.E5Application import e5App
+from E5Gui import E5MessageBox
+
+from Ui_RenameDialog import Ui_RenameDialog
+
+from RefactoringDialogBase import RefactoringDialogBase
+
+
+class RenameDialog(RefactoringDialogBase, Ui_RenameDialog):
+    """
+    Class implementing the Rename dialog.
+    """
+    def __init__(self, refactoring, title, renamer, resource=None, 
+                 parent=None):
+        """
+        Constructor
+        
+        @param refactoring reference to the main refactoring object
+            (Refactoring)
+        @param title title of the dialog (string)
+        @param renamer reference to the renamer object
+            (rope.refactor.rename.Rename)
+        @param resource reference to a resource object, if the action is to
+            be applied to the local file only (rope.base.resources.File)
+        @param parent reference to the parent widget (QWidget)
+        """
+        RefactoringDialogBase.__init__(self, refactoring, title, parent)
+        self.setupUi(self)
+        
+        self.__renamer = renamer
+        if resource is not None:
+            self.__resources = [resource]
+        else:
+            self.__resources = None
+        
+        self.__okButton = self.buttonBox.button(QDialogButtonBox.Ok)
+        self.__okButton.setEnabled(False)
+        self.__previewButton = self.buttonBox.addButton(\
+            self.trUtf8("Preview"), QDialogButtonBox.ActionRole)
+        self.__previewButton.setDefault(True)
+    
+    @pyqtSlot(str)
+    def on_newNameEdit_textChanged(self, text):
+        """
+        Private slot to react to changes of the new name.
+        
+        @param text text entered into the edit (QString)
+        """
+        self.__okButton.setEnabled(text != "")
+    
+    @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 __confirmUnsure(self, occurrence):
+        """
+        Private method to confirm unsure occurrences.
+        
+        @parameter occurrence reference to the occurrence object
+            (rope.refactor.occurrences.Occurrence)
+        @return flag indicating an occurrence (boolean)
+        """
+        if self.ignoreButton.isChecked():
+            return False
+        if self.matchButton.isChecked():
+            return True
+        
+        filename = occurrence.resource.real_path
+        start, end = occurrence.get_primary_range()
+        
+        vm = e5App().getObject("ViewManager")
+        
+        # display the file and select the match
+        vm.openSourceFile(filename)
+        aw = vm.activeWindow()
+        cline, cindex = aw.getCursorPosition()
+        sline, sindex = aw.lineIndexFromPosition(start)
+        eline, eindex = aw.lineIndexFromPosition(end)
+        aw.ensureLineVisible(sline)
+        aw.gotoLine(sline)
+        aw.setSelection(sline, sindex, eline, eindex)
+        ans = E5MessageBox.question(self, self.trUtf8("Rename"),
+            self.trUtf8("<p>Is the highlighted code a match?</p>"),
+            QMessageBox.StandardButtons(\
+                QMessageBox.No | \
+                QMessageBox.Yes),
+            QMessageBox.Yes)
+        aw.setCursorPosition(cline, cindex)
+        aw.ensureCursorVisible()
+        
+        return ans == QMessageBox.Yes
+    
+    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)
+        """
+        try:
+            changes = self.__renamer.get_changes(self.newNameEdit.text(), 
+                resources=self.__resources,
+                in_hierarchy=self.allCheckBox.isChecked(),
+                unsure=self.__confirmUnsure,
+                docs=self.stringsCheckBox.isChecked(), 
+                task_handle=handle)
+            return changes
+        except Exception as err:
+            self._refactoring.handleRopeError(err, self._title, handle)
+            return None

eric ide

mercurial