|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2011 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the History dialog. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import Qt, pyqtSlot |
|
11 from PyQt4.QtGui import QDialogButtonBox, QListWidgetItem, QMessageBox, \ |
|
12 QApplication |
|
13 |
|
14 from E5Gui import E5MessageBox |
|
15 from E5Gui.E5Application import e5App |
|
16 |
|
17 from PreviewDialogBase import PreviewDialogBase |
|
18 from ProgressHandle import ProgressHandle |
|
19 |
|
20 import Utilities |
|
21 |
|
22 class HistoryDialog(PreviewDialogBase): |
|
23 """ |
|
24 Class implementing the History dialog. |
|
25 """ |
|
26 ChangeIDRole = Qt.UserRole |
|
27 |
|
28 def __init__(self, refactoring, changes, isUndo, parent = None): |
|
29 """ |
|
30 Constructor |
|
31 |
|
32 @param refactoring reference to the main refactoring object |
|
33 (Refactoring) |
|
34 @param changes list of ChangeSet objects |
|
35 (list of rope.base.change.ChangeSet) |
|
36 @param isUndo flag indicating an undo history dialog (boolean) |
|
37 @param parent reference to the parent widget (QWidget) |
|
38 """ |
|
39 PreviewDialogBase.__init__(self, parent) |
|
40 |
|
41 self.__refactoring = refactoring |
|
42 self.__isUndo = isUndo |
|
43 |
|
44 if self.__isUndo: |
|
45 self.__actionButton = self.buttonBox.addButton( |
|
46 self.trUtf8("&Undo"), |
|
47 QDialogButtonBox.AcceptRole) |
|
48 self.description.setText(self.trUtf8("Undoable Changes")) |
|
49 title = self.trUtf8("Undo History") |
|
50 else: |
|
51 self.__actionButton = self.buttonBox.addButton( |
|
52 self.trUtf8("&Redo"), |
|
53 QDialogButtonBox.AcceptRole) |
|
54 self.description.setText(self.trUtf8("Redoable Changes")) |
|
55 title = self.trUtf8("Redo History") |
|
56 self.buttonBox.addButton(QDialogButtonBox.Close) |
|
57 self.setWindowTitle(title) |
|
58 |
|
59 # populate the list |
|
60 self.__changes = {} |
|
61 for change in changes: |
|
62 self.__changes[id(change)] = change |
|
63 itm = QListWidgetItem(str(change), self.changesList) |
|
64 itm.setData(HistoryDialog.ChangeIDRole, id(change)) |
|
65 if self.changesList.count() > 0: |
|
66 self.changesList.item(0).setSelected(True) |
|
67 |
|
68 @pyqtSlot(QListWidgetItem, QListWidgetItem) |
|
69 def on_changesList_currentItemChanged(self, current, previous): |
|
70 """ |
|
71 Private slot called when a change is selected. |
|
72 |
|
73 @param current reference to the new current item (QListWidgetItem) |
|
74 @param previous reference to the old current item (QListWidgetItem) |
|
75 """ |
|
76 if current is None: |
|
77 return |
|
78 |
|
79 self.previewEdit.clear() |
|
80 id = current.data(HistoryDialog.ChangeIDRole) |
|
81 change = self.__changes[id] |
|
82 for line in change.get_description().splitlines(True): |
|
83 try: |
|
84 format = self.formats[line[0]] |
|
85 except (IndexError, KeyError): |
|
86 format = self.formats[' '] |
|
87 self._appendText(line, format) |
|
88 |
|
89 def accept(self): |
|
90 """ |
|
91 Public slot to undo the selected set of changes. |
|
92 """ |
|
93 id = self.changesList.currentItem()\ |
|
94 .data(HistoryDialog.ChangeIDRole) |
|
95 change = self.__changes[id] |
|
96 |
|
97 if self.__isUndo: |
|
98 res = E5MessageBox.question(None, |
|
99 self.trUtf8("Undo refactorings"), |
|
100 self.trUtf8("""Shall all refactorings up to <b>{0}</b>""" |
|
101 """ be undone?""")\ |
|
102 .format(Utilities.html_encode(str(change))), |
|
103 QMessageBox.StandardButtons(\ |
|
104 QMessageBox.No | \ |
|
105 QMessageBox.Yes), |
|
106 QMessageBox.No) |
|
107 else: |
|
108 res = E5MessageBox.question(None, |
|
109 self.trUtf8("Redo refactorings"), |
|
110 self.trUtf8("""Shall all refactorings up to <b>{0}</b>""" |
|
111 """ be redone?""")\ |
|
112 .format(Utilities.html_encode(str(change))), |
|
113 QMessageBox.StandardButtons(\ |
|
114 QMessageBox.No | \ |
|
115 QMessageBox.Yes), |
|
116 QMessageBox.No) |
|
117 if res == QMessageBox.Yes: |
|
118 if not self.__refactoring.confirmAllBuffersSaved(): |
|
119 return |
|
120 |
|
121 handle = ProgressHandle(change.description, False, self) |
|
122 handle.show() |
|
123 QApplication.processEvents() |
|
124 if self.__isUndo: |
|
125 self.__refactoring.getProject().history.undo( |
|
126 change, task_handle=handle) |
|
127 else: |
|
128 self.__refactoring.getProject().history.redo( |
|
129 change, task_handle=handle) |
|
130 handle.reset() |
|
131 |
|
132 self.__refactoring.refreshEditors(change) |
|
133 p = e5App().getObject("Project") |
|
134 if p.isDirty(): |
|
135 p.saveProject() |
|
136 |
|
137 PreviewDialogBase.accept(self) |
|
138 else: |
|
139 self.reject() |