14 |
14 |
15 from PyQt6.QtCore import QFileInfo, QEvent, pyqtSlot |
15 from PyQt6.QtCore import QFileInfo, QEvent, pyqtSlot |
16 from PyQt6.QtGui import QTextCursor |
16 from PyQt6.QtGui import QTextCursor |
17 from PyQt6.QtWidgets import QWidget, QApplication, QDialogButtonBox |
17 from PyQt6.QtWidgets import QWidget, QApplication, QDialogButtonBox |
18 |
18 |
19 from E5Gui import E5MessageBox, E5FileDialog |
19 from E5Gui import EricMessageBox, EricFileDialog |
20 from E5Gui.E5MainWindow import E5MainWindow |
20 from E5Gui.EricMainWindow import EricMainWindow |
21 from E5Gui.E5PathPicker import E5PathPickerModes |
21 from E5Gui.EricPathPicker import EricPathPickerModes |
22 |
22 |
23 from .Ui_DiffDialog import Ui_DiffDialog |
23 from .Ui_DiffDialog import Ui_DiffDialog |
24 from .DiffHighlighter import DiffHighlighter |
24 from .DiffHighlighter import DiffHighlighter |
25 |
25 |
26 import Utilities |
26 import Utilities |
38 @param parent reference to the parent widget (QWidget) |
38 @param parent reference to the parent widget (QWidget) |
39 """ |
39 """ |
40 super().__init__(parent) |
40 super().__init__(parent) |
41 self.setupUi(self) |
41 self.setupUi(self) |
42 |
42 |
43 self.file1Picker.setMode(E5PathPickerModes.OPEN_FILE_MODE) |
43 self.file1Picker.setMode(EricPathPickerModes.OPEN_FILE_MODE) |
44 self.file2Picker.setMode(E5PathPickerModes.OPEN_FILE_MODE) |
44 self.file2Picker.setMode(EricPathPickerModes.OPEN_FILE_MODE) |
45 |
45 |
46 self.diffButton = self.buttonBox.addButton( |
46 self.diffButton = self.buttonBox.addButton( |
47 self.tr("Compare"), QDialogButtonBox.ButtonRole.ActionRole) |
47 self.tr("Compare"), QDialogButtonBox.ButtonRole.ActionRole) |
48 self.diffButton.setToolTip( |
48 self.diffButton.setToolTip( |
49 self.tr("Press to perform the comparison of the two files")) |
49 self.tr("Press to perform the comparison of the two files")) |
101 filesystem. |
101 filesystem. |
102 """ |
102 """ |
103 dname, fname = Utilities.splitPath(self.filename2) |
103 dname, fname = Utilities.splitPath(self.filename2) |
104 fname = "{0}.diff".format(self.filename2) if fname != '.' else dname |
104 fname = "{0}.diff".format(self.filename2) if fname != '.' else dname |
105 |
105 |
106 fname, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( |
106 fname, selectedFilter = EricFileDialog.getSaveFileNameAndFilter( |
107 self, |
107 self, |
108 self.tr("Save Diff"), |
108 self.tr("Save Diff"), |
109 fname, |
109 fname, |
110 self.tr("Patch Files (*.diff)"), |
110 self.tr("Patch Files (*.diff)"), |
111 None, |
111 None, |
112 E5FileDialog.DontConfirmOverwrite) |
112 EricFileDialog.DontConfirmOverwrite) |
113 |
113 |
114 if not fname: |
114 if not fname: |
115 return |
115 return |
116 |
116 |
117 ext = QFileInfo(fname).suffix() |
117 ext = QFileInfo(fname).suffix() |
118 if not ext: |
118 if not ext: |
119 ex = selectedFilter.split("(*")[1].split(")")[0] |
119 ex = selectedFilter.split("(*")[1].split(")")[0] |
120 if ex: |
120 if ex: |
121 fname += ex |
121 fname += ex |
122 if QFileInfo(fname).exists(): |
122 if QFileInfo(fname).exists(): |
123 res = E5MessageBox.yesNo( |
123 res = EricMessageBox.yesNo( |
124 self, |
124 self, |
125 self.tr("Save Diff"), |
125 self.tr("Save Diff"), |
126 self.tr("<p>The patch file <b>{0}</b> already exists." |
126 self.tr("<p>The patch file <b>{0}</b> already exists." |
127 " Overwrite it?</p>").format(fname), |
127 " Overwrite it?</p>").format(fname), |
128 icon=E5MessageBox.Warning) |
128 icon=EricMessageBox.Warning) |
129 if not res: |
129 if not res: |
130 return |
130 return |
131 fname = Utilities.toNativeSeparators(fname) |
131 fname = Utilities.toNativeSeparators(fname) |
132 |
132 |
133 txt = self.contents.toPlainText() |
133 txt = self.contents.toPlainText() |
134 try: |
134 try: |
135 with open(fname, "w", encoding="utf-8") as f, \ |
135 with open(fname, "w", encoding="utf-8") as f, \ |
136 contextlib.suppress(UnicodeError): |
136 contextlib.suppress(UnicodeError): |
137 f.write(txt) |
137 f.write(txt) |
138 except OSError as why: |
138 except OSError as why: |
139 E5MessageBox.critical( |
139 EricMessageBox.critical( |
140 self, self.tr('Save Diff'), |
140 self, self.tr('Save Diff'), |
141 self.tr( |
141 self.tr( |
142 '<p>The patch file <b>{0}</b> could not be saved.<br />' |
142 '<p>The patch file <b>{0}</b> could not be saved.<br />' |
143 'Reason: {1}</p>').format(fname, str(why))) |
143 'Reason: {1}</p>').format(fname, str(why))) |
144 |
144 |
154 filemtime1 = "" |
154 filemtime1 = "" |
155 try: |
155 try: |
156 with open(self.filename1, "r", encoding="utf-8") as f1: |
156 with open(self.filename1, "r", encoding="utf-8") as f1: |
157 lines1 = f1.readlines() |
157 lines1 = f1.readlines() |
158 except OSError: |
158 except OSError: |
159 E5MessageBox.critical( |
159 EricMessageBox.critical( |
160 self, |
160 self, |
161 self.tr("Compare Files"), |
161 self.tr("Compare Files"), |
162 self.tr( |
162 self.tr( |
163 """<p>The file <b>{0}</b> could not be read.</p>""") |
163 """<p>The file <b>{0}</b> could not be read.</p>""") |
164 .format(self.filename1)) |
164 .format(self.filename1)) |
171 filemtime2 = "" |
171 filemtime2 = "" |
172 try: |
172 try: |
173 with open(self.filename2, "r", encoding="utf-8") as f2: |
173 with open(self.filename2, "r", encoding="utf-8") as f2: |
174 lines2 = f2.readlines() |
174 lines2 = f2.readlines() |
175 except OSError: |
175 except OSError: |
176 E5MessageBox.critical( |
176 EricMessageBox.critical( |
177 self, |
177 self, |
178 self.tr("Compare Files"), |
178 self.tr("Compare Files"), |
179 self.tr( |
179 self.tr( |
180 """<p>The file <b>{0}</b> could not be read.</p>""") |
180 """<p>The file <b>{0}</b> could not be read.</p>""") |
181 .format(self.filename2)) |
181 .format(self.filename2)) |