|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2011 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Rename dialog. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import pyqtSlot |
|
11 from PyQt4.QtGui import QDialogButtonBox, QAbstractButton, QMessageBox |
|
12 |
|
13 from E5Gui.E5Application import e5App |
|
14 from E5Gui import E5MessageBox |
|
15 |
|
16 from Ui_RenameDialog import Ui_RenameDialog |
|
17 |
|
18 from RefactoringDialogBase import RefactoringDialogBase |
|
19 |
|
20 |
|
21 class RenameDialog(RefactoringDialogBase, Ui_RenameDialog): |
|
22 """ |
|
23 Class implementing the Rename dialog. |
|
24 """ |
|
25 def __init__(self, refactoring, title, renamer, resource=None, |
|
26 parent=None): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param refactoring reference to the main refactoring object |
|
31 (Refactoring) |
|
32 @param title title of the dialog (string) |
|
33 @param renamer reference to the renamer object |
|
34 (rope.refactor.rename.Rename) |
|
35 @param resource reference to a resource object, if the action is to |
|
36 be applied to the local file only (rope.base.resources.File) |
|
37 @param parent reference to the parent widget (QWidget) |
|
38 """ |
|
39 RefactoringDialogBase.__init__(self, refactoring, title, parent) |
|
40 self.setupUi(self) |
|
41 |
|
42 self.__renamer = renamer |
|
43 if resource is not None: |
|
44 self.__resources = [resource] |
|
45 else: |
|
46 self.__resources = None |
|
47 |
|
48 self.__okButton = self.buttonBox.button(QDialogButtonBox.Ok) |
|
49 self.__okButton.setEnabled(False) |
|
50 self.__previewButton = self.buttonBox.addButton(\ |
|
51 self.trUtf8("Preview"), QDialogButtonBox.ActionRole) |
|
52 self.__previewButton.setDefault(True) |
|
53 |
|
54 @pyqtSlot(str) |
|
55 def on_newNameEdit_textChanged(self, text): |
|
56 """ |
|
57 Private slot to react to changes of the new name. |
|
58 |
|
59 @param text text entered into the edit (QString) |
|
60 """ |
|
61 self.__okButton.setEnabled(text != "") |
|
62 |
|
63 @pyqtSlot(QAbstractButton) |
|
64 def on_buttonBox_clicked(self, button): |
|
65 """ |
|
66 Private slot to act on the button pressed. |
|
67 |
|
68 @param button reference to the button pressed (QAbstractButton) |
|
69 """ |
|
70 if button == self.__previewButton: |
|
71 self.previewChanges() |
|
72 elif button == self.__okButton: |
|
73 self.applyChanges() |
|
74 |
|
75 def __confirmUnsure(self, occurrence): |
|
76 """ |
|
77 Private method to confirm unsure occurrences. |
|
78 |
|
79 @parameter occurrence reference to the occurrence object |
|
80 (rope.refactor.occurrences.Occurrence) |
|
81 @return flag indicating an occurrence (boolean) |
|
82 """ |
|
83 if self.ignoreButton.isChecked(): |
|
84 return False |
|
85 if self.matchButton.isChecked(): |
|
86 return True |
|
87 |
|
88 filename = occurrence.resource.real_path |
|
89 start, end = occurrence.get_primary_range() |
|
90 |
|
91 vm = e5App().getObject("ViewManager") |
|
92 |
|
93 # display the file and select the match |
|
94 vm.openSourceFile(filename) |
|
95 aw = vm.activeWindow() |
|
96 cline, cindex = aw.getCursorPosition() |
|
97 sline, sindex = aw.lineIndexFromPosition(start) |
|
98 eline, eindex = aw.lineIndexFromPosition(end) |
|
99 aw.ensureLineVisible(sline) |
|
100 aw.gotoLine(sline) |
|
101 aw.setSelection(sline, sindex, eline, eindex) |
|
102 ans = E5MessageBox.question(self, self.trUtf8("Rename"), |
|
103 self.trUtf8("<p>Is the highlighted code a match?</p>"), |
|
104 QMessageBox.StandardButtons(\ |
|
105 QMessageBox.No | \ |
|
106 QMessageBox.Yes), |
|
107 QMessageBox.Yes) |
|
108 aw.setCursorPosition(cline, cindex) |
|
109 aw.ensureCursorVisible() |
|
110 |
|
111 return ans == QMessageBox.Yes |
|
112 |
|
113 def _calculateChanges(self, handle): |
|
114 """ |
|
115 Protected method to calculate the changes. |
|
116 |
|
117 @param handle reference to the task handle |
|
118 (rope.base.taskhandle.TaskHandle) |
|
119 @return reference to the Changes object (rope.base.change.ChangeSet) |
|
120 """ |
|
121 try: |
|
122 changes = self.__renamer.get_changes(self.newNameEdit.text(), |
|
123 resources=self.__resources, |
|
124 in_hierarchy=self.allCheckBox.isChecked(), |
|
125 unsure=self.__confirmUnsure, |
|
126 docs=self.stringsCheckBox.isChecked(), |
|
127 task_handle=handle) |
|
128 return changes |
|
129 except Exception as err: |
|
130 self._refactoring.handleRopeError(err, self._title, handle) |
|
131 return None |