UI/CompareDialog.py

changeset 4589
b648ccbdbef9
parent 4021
195a471c327b
child 4631
5c1a96925da4
equal deleted inserted replaced
4586:9221c0c5c66f 4589:b648ccbdbef9
19 19
20 from PyQt5.QtCore import QTimer, QEvent, pyqtSlot 20 from PyQt5.QtCore import QTimer, QEvent, pyqtSlot
21 from PyQt5.QtGui import QColor, QFontMetrics, QBrush, QTextCursor 21 from PyQt5.QtGui import QColor, QFontMetrics, QBrush, QTextCursor
22 from PyQt5.QtWidgets import QWidget, QApplication, QDialogButtonBox 22 from PyQt5.QtWidgets import QWidget, QApplication, QDialogButtonBox
23 23
24 from E5Gui.E5Completers import E5FileCompleter 24 from E5Gui import E5MessageBox
25 from E5Gui import E5MessageBox, E5FileDialog
26 from E5Gui.E5MainWindow import E5MainWindow 25 from E5Gui.E5MainWindow import E5MainWindow
26 from E5Gui.E5PathPicker import E5PathPickerModes
27 27
28 import UI.PixmapCache 28 import UI.PixmapCache
29 29
30 from .Ui_CompareDialog import Ui_CompareDialog 30 from .Ui_CompareDialog import Ui_CompareDialog
31 31
32 import Utilities
33 import Preferences 32 import Preferences
34 33
35 34
36 def sbsdiff(a, b, linenumberwidth=4): 35 def sbsdiff(a, b, linenumberwidth=4):
37 """ 36 """
100 @param parent parent widget (QWidget) 99 @param parent parent widget (QWidget)
101 """ 100 """
102 super(CompareDialog, self).__init__(parent) 101 super(CompareDialog, self).__init__(parent)
103 self.setupUi(self) 102 self.setupUi(self)
104 103
105 self.file1Button.setIcon(UI.PixmapCache.getIcon("open.png")) 104 self.file1Picker.setMode(E5PathPickerModes.OpenFileMode)
106 self.file2Button.setIcon(UI.PixmapCache.getIcon("open.png")) 105 self.file2Picker.setMode(E5PathPickerModes.OpenFileMode)
107
108 self.file1Completer = E5FileCompleter(self.file1Edit)
109 self.file2Completer = E5FileCompleter(self.file2Edit)
110 106
111 self.diffButton = self.buttonBox.addButton( 107 self.diffButton = self.buttonBox.addButton(
112 self.tr("Compare"), QDialogButtonBox.ActionRole) 108 self.tr("Compare"), QDialogButtonBox.ActionRole)
113 self.diffButton.setToolTip( 109 self.diffButton.setToolTip(
114 self.tr("Press to perform the comparison of the two files")) 110 self.tr("Press to perform the comparison of the two files"))
148 self.cDeletedFormat.setBackground(QBrush(QColor(237, 190, 190))) 144 self.cDeletedFormat.setBackground(QBrush(QColor(237, 190, 190)))
149 self.cReplacedFormat = self.contents_1.currentCharFormat() 145 self.cReplacedFormat = self.contents_1.currentCharFormat()
150 self.cReplacedFormat.setBackground(QBrush(QColor(190, 190, 237))) 146 self.cReplacedFormat.setBackground(QBrush(QColor(190, 190, 237)))
151 147
152 # connect some of our widgets explicitly 148 # connect some of our widgets explicitly
153 self.file1Edit.textChanged.connect(self.__fileChanged) 149 self.file1Picker.textChanged.connect(self.__fileChanged)
154 self.file2Edit.textChanged.connect(self.__fileChanged) 150 self.file2Picker.textChanged.connect(self.__fileChanged)
155 self.vsb1.valueChanged.connect(self.__scrollBarMoved) 151 self.vsb1.valueChanged.connect(self.__scrollBarMoved)
156 self.vsb1.valueChanged.connect(self.vsb2.setValue) 152 self.vsb1.valueChanged.connect(self.vsb2.setValue)
157 self.vsb2.valueChanged.connect(self.vsb1.setValue) 153 self.vsb2.valueChanged.connect(self.vsb1.setValue)
158 154
159 self.diffParas = [] 155 self.diffParas = []
161 157
162 self.markerPattern = "\0\+|\0\^|\0\-" 158 self.markerPattern = "\0\+|\0\^|\0\-"
163 159
164 if len(files) == 2: 160 if len(files) == 2:
165 self.filesGroup.hide() 161 self.filesGroup.hide()
166 self.file1Edit.setText(files[0][1]) 162 self.file1Picker.setText(files[0][1])
167 self.file2Edit.setText(files[1][1]) 163 self.file2Picker.setText(files[1][1])
168 self.file1Label.setText(files[0][0]) 164 self.file1Label.setText(files[0][0])
169 self.file2Label.setText(files[1][0]) 165 self.file2Label.setText(files[1][0])
170 self.diffButton.hide() 166 self.diffButton.hide()
171 QTimer.singleShot(0, self.on_diffButton_clicked) 167 QTimer.singleShot(0, self.on_diffButton_clicked)
172 else: 168 else:
178 Public slot to show the dialog. 174 Public slot to show the dialog.
179 175
180 @param filename name of a file to use as the first file (string) 176 @param filename name of a file to use as the first file (string)
181 """ 177 """
182 if filename: 178 if filename:
183 self.file1Edit.setText(filename) 179 self.file1Picker.setText(filename)
184 super(CompareDialog, self).show() 180 super(CompareDialog, self).show()
185 181
186 def __appendText(self, pane, linenumber, line, format, interLine=False): 182 def __appendText(self, pane, linenumber, line, format, interLine=False):
187 """ 183 """
188 Private method to append text to the end of the contents pane. 184 Private method to append text to the end of the contents pane.
228 @pyqtSlot() 224 @pyqtSlot()
229 def on_diffButton_clicked(self): 225 def on_diffButton_clicked(self):
230 """ 226 """
231 Private slot to handle the Compare button press. 227 Private slot to handle the Compare button press.
232 """ 228 """
233 filename1 = Utilities.toNativeSeparators(self.file1Edit.text()) 229 filename1 = self.file1Picker.text()
234 try: 230 try:
235 f1 = open(filename1, "r", encoding="utf-8") 231 f1 = open(filename1, "r", encoding="utf-8")
236 lines1 = f1.readlines() 232 lines1 = f1.readlines()
237 f1.close() 233 f1.close()
238 except IOError: 234 except IOError:
242 self.tr( 238 self.tr(
243 """<p>The file <b>{0}</b> could not be read.</p>""") 239 """<p>The file <b>{0}</b> could not be read.</p>""")
244 .format(filename1)) 240 .format(filename1))
245 return 241 return
246 242
247 filename2 = Utilities.toNativeSeparators(self.file2Edit.text()) 243 filename2 = self.file2Picker.text()
248 try: 244 try:
249 f2 = open(filename2, "r", encoding="utf-8") 245 f2 = open(filename2, "r", encoding="utf-8")
250 lines2 = f2.readlines() 246 lines2 = f2.readlines()
251 f2.close() 247 f2.close()
252 except IOError: 248 except IOError:
272 if name1 == "" or name2 == "": 268 if name1 == "" or name2 == "":
273 self.filesGroup.hide() 269 self.filesGroup.hide()
274 else: 270 else:
275 self.file1Button.hide() 271 self.file1Button.hide()
276 self.file2Button.hide() 272 self.file2Button.hide()
277 self.file1Edit.setText(name1) 273 self.file1Picker.setText(name1)
278 self.file1Edit.setReadOnly(True) 274 self.file1Picker.setReadOnly(True)
279 self.file2Edit.setText(name2) 275 self.file2Picker.setText(name2)
280 self.file2Edit.setReadOnly(True) 276 self.file2Picker.setReadOnly(True)
281 self.diffButton.setEnabled(False) 277 self.diffButton.setEnabled(False)
282 self.diffButton.hide() 278 self.diffButton.hide()
283 279
284 if isinstance(lines1, basestring): 280 if isinstance(lines1, basestring):
285 lines1 = lines1.splitlines(True) 281 lines1 = lines1.splitlines(True)
428 424
429 def __fileChanged(self): 425 def __fileChanged(self):
430 """ 426 """
431 Private slot to enable/disable the Compare button. 427 Private slot to enable/disable the Compare button.
432 """ 428 """
433 if not self.file1Edit.text() or \ 429 if not self.file1Picker.text() or \
434 not self.file2Edit.text(): 430 not self.file2Picker.text():
435 self.diffButton.setEnabled(False) 431 self.diffButton.setEnabled(False)
436 else: 432 else:
437 self.diffButton.setEnabled(True) 433 self.diffButton.setEnabled(True)
438
439 def __selectFile(self, lineEdit):
440 """
441 Private slot to display a file selection dialog.
442
443 @param lineEdit field for the display of the selected filename
444 (QLineEdit)
445 """
446 filename = E5FileDialog.getOpenFileName(
447 self,
448 self.tr("Select file to compare"),
449 lineEdit.text(),
450 "")
451
452 if filename:
453 lineEdit.setText(Utilities.toNativeSeparators(filename))
454
455 @pyqtSlot()
456 def on_file1Button_clicked(self):
457 """
458 Private slot to handle the file 1 file selection button press.
459 """
460 self.__selectFile(self.file1Edit)
461
462 @pyqtSlot()
463 def on_file2Button_clicked(self):
464 """
465 Private slot to handle the file 2 file selection button press.
466 """
467 self.__selectFile(self.file2Edit)
468 434
469 @pyqtSlot(bool) 435 @pyqtSlot(bool)
470 def on_synchronizeCheckBox_toggled(self, sync): 436 def on_synchronizeCheckBox_toggled(self, sync):
471 """ 437 """
472 Private slot to connect or disconnect the scrollbars of the displays. 438 Private slot to connect or disconnect the scrollbars of the displays.

eric ide

mercurial