|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2011 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Refactoring dialog base class. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import Qt |
|
11 from PyQt4.QtGui import QDialog, QApplication |
|
12 |
|
13 from E5Gui.E5Application import e5App |
|
14 |
|
15 from ChangesPreviewDialog import ChangesPreviewDialog |
|
16 from ProgressHandle import ProgressHandle |
|
17 |
|
18 |
|
19 class RefactoringDialogBase(QDialog): |
|
20 """ |
|
21 Class implementing the Refactoring dialog base class. |
|
22 """ |
|
23 def __init__(self, refactoring, title, parent=None): |
|
24 """ |
|
25 Constructor |
|
26 |
|
27 @param refactoring reference to the main refactoring object |
|
28 (Refactoring) |
|
29 @param title title of the dialog (string or QString) |
|
30 @param parent reference to the parent widget (QWidget) |
|
31 """ |
|
32 QDialog.__init__(self, parent) |
|
33 self.setAttribute(Qt.WA_DeleteOnClose) |
|
34 self.setWindowTitle(title) |
|
35 |
|
36 self._refactoring = refactoring |
|
37 self._title = title |
|
38 self.__handle = None |
|
39 |
|
40 def __createProgressHandle(self, interruptable=True): |
|
41 """ |
|
42 Private method to create a TaskHandle to update a progress dialog. |
|
43 |
|
44 @param interruptable flag indicating, that the task may be |
|
45 interrupted (boolean) |
|
46 """ |
|
47 self.__handle = ProgressHandle(self._title, interruptable, self) |
|
48 self.__handle.show() |
|
49 QApplication.processEvents() |
|
50 |
|
51 def _calculateChanges(self, handle): |
|
52 """ |
|
53 Protected method to calculate the changes. |
|
54 |
|
55 @param handle reference to the task handle |
|
56 (rope.base.taskhandle.TaskHandle) |
|
57 @return reference to the Changes object (rope.base.change.ChangeSet) |
|
58 """ |
|
59 raise NotImplementedError("_calculateChanges must be overridden.") |
|
60 |
|
61 def __getChanges(self): |
|
62 """ |
|
63 Private method to build the Changes object. |
|
64 |
|
65 @return reference to the Changes object (rope.base.change.ChangeSet) |
|
66 """ |
|
67 self.__createProgressHandle() |
|
68 changes = self._calculateChanges(self.__handle) |
|
69 self.__handle.reset() |
|
70 self.__handle = None |
|
71 |
|
72 return changes |
|
73 |
|
74 def previewChanges(self): |
|
75 """ |
|
76 Public method to preview the changes. |
|
77 """ |
|
78 changes = self.__getChanges() |
|
79 |
|
80 if changes is not None: |
|
81 dlg = ChangesPreviewDialog(changes, self) |
|
82 if dlg.exec_() == QDialog.Accepted: |
|
83 self.applyChanges(changes) |
|
84 |
|
85 def applyChanges(self, changes = None): |
|
86 """ |
|
87 Public method to apply the changes. |
|
88 |
|
89 @param reference to the Changes object (rope.base.change.ChangeSet) |
|
90 """ |
|
91 if changes is None: |
|
92 changes = self.__getChanges() |
|
93 |
|
94 if changes is not None: |
|
95 self.__createProgressHandle(False) |
|
96 try: |
|
97 self._refactoring.getProject().do(changes, self.__handle) |
|
98 except Exception as err: |
|
99 self._refactoring.handleRopeError( |
|
100 err, self._title, self.__handle) |
|
101 self.__handle.reset() |
|
102 self.__handle = None |
|
103 |
|
104 self._refactoring.refreshEditors(changes) |
|
105 p = e5App().getObject("Project") |
|
106 if p.isDirty(): |
|
107 p.saveProject() |
|
108 |
|
109 self.accept() |