UI/FindFileDialog.py

changeset 682
91114a975eda
parent 654
72d4c6f31157
child 791
9ec2ac20e54e
equal deleted inserted replaced
680:7edb1eff1755 682:91114a975eda
37 37
38 lineRole = Qt.UserRole + 1 38 lineRole = Qt.UserRole + 1
39 startRole = Qt.UserRole + 2 39 startRole = Qt.UserRole + 2
40 endRole = Qt.UserRole + 3 40 endRole = Qt.UserRole + 3
41 replaceRole = Qt.UserRole + 4 41 replaceRole = Qt.UserRole + 4
42 md5Role = Qt.UserRole + 5
42 43
43 def __init__(self, project, replaceMode = False, parent=None): 44 def __init__(self, project, replaceMode = False, parent=None):
44 """ 45 """
45 Constructor 46 Constructor
46 47
113 self.__populating = False 114 self.__populating = False
114 115
115 self.setContextMenuPolicy(Qt.CustomContextMenu) 116 self.setContextMenuPolicy(Qt.CustomContextMenu)
116 self.customContextMenuRequested.connect(self.__contextMenuRequested) 117 self.customContextMenuRequested.connect(self.__contextMenuRequested)
117 118
118 def __createItem(self, file, line, text, start, end, replTxt = ""): 119 def __createItem(self, file, line, text, start, end, replTxt = "", md5 = ""):
119 """ 120 """
120 Private method to create an entry in the file list. 121 Private method to create an entry in the file list.
121 122
122 @param file filename of file (string) 123 @param file filename of file (string)
123 @param line line number (integer) 124 @param line line number (integer)
124 @param text text found (string) 125 @param text text found (string)
125 @param start start position of match (integer) 126 @param start start position of match (integer)
126 @param end end position of match (integer) 127 @param end end position of match (integer)
127 @param replTxt text with replacements applied (string 128 @param replTxt text with replacements applied (string)
129 @keyparam md5 MD5 hash of the file (string)
128 """ 130 """
129 if self.__lastFileItem is None: 131 if self.__lastFileItem is None:
130 # It's a new file 132 # It's a new file
131 self.__lastFileItem = QTreeWidgetItem(self.findList, [file]) 133 self.__lastFileItem = QTreeWidgetItem(self.findList, [file])
132 self.__lastFileItem.setFirstColumnSpanned(True) 134 self.__lastFileItem.setFirstColumnSpanned(True)
134 if self.__replaceMode: 136 if self.__replaceMode:
135 self.__lastFileItem.setFlags(self.__lastFileItem.flags() | \ 137 self.__lastFileItem.setFlags(self.__lastFileItem.flags() | \
136 Qt.ItemFlags(Qt.ItemIsUserCheckable | Qt.ItemIsTristate)) 138 Qt.ItemFlags(Qt.ItemIsUserCheckable | Qt.ItemIsTristate))
137 # Qt bug: 139 # Qt bug:
138 # item is not user checkable if setFirstColumnSpanned is True (< 4.5.0) 140 # item is not user checkable if setFirstColumnSpanned is True (< 4.5.0)
141 self.__lastFileItem.setData(0, self.md5Role, md5)
139 142
140 itm = QTreeWidgetItem(self.__lastFileItem, [' {0:5d} '.format(line), text]) 143 itm = QTreeWidgetItem(self.__lastFileItem, [' {0:5d} '.format(line), text])
141 itm.setTextAlignment(0, Qt.AlignRight) 144 itm.setTextAlignment(0, Qt.AlignRight)
142 itm.setData(0, self.lineRole, line) 145 itm.setData(0, self.lineRole, line)
143 itm.setData(0, self.startRole, start) 146 itm.setData(0, self.startRole, start)
393 fn = os.path.join(self.project.ppath, file) 396 fn = os.path.join(self.project.ppath, file)
394 else: 397 else:
395 fn = file 398 fn = file
396 # read the file and split it into textlines 399 # read the file and split it into textlines
397 try: 400 try:
398 text, encoding = Utilities.readEncodedFile(fn) 401 text, encoding, hash = Utilities.readEncodedFileWithHash(fn)
399 lines = text.splitlines() 402 lines = text.splitlines()
400 except (UnicodeError, IOError): 403 except (UnicodeError, IOError):
401 progress += 1 404 progress += 1
402 self.findProgress.setValue(progress) 405 self.findProgress.setValue(progress)
403 continue 406 continue
421 line = "{0} ...".format(line[:1024]) 424 line = "{0} ...".format(line[:1024])
422 if self.__replaceMode: 425 if self.__replaceMode:
423 if len(rline) > 1024: 426 if len(rline) > 1024:
424 rline = "{0} ...".format(line[:1024]) 427 rline = "{0} ...".format(line[:1024])
425 line = "- {0}\n+ {1}".format(line, rline) 428 line = "- {0}\n+ {1}".format(line, rline)
426 self.__createItem(file, count, line, start, end, rline) 429 self.__createItem(file, count, line, start, end, rline, hash)
427 430
428 if self.feelLikeCheckBox.isChecked(): 431 if self.feelLikeCheckBox.isChecked():
429 fn = os.path.join(self.project.ppath, file) 432 fn = os.path.join(self.project.ppath, file)
430 self.sourceFile.emit(fn, count, "", start, end) 433 self.sourceFile.emit(fn, count, "", start, end)
431 QApplication.processEvents() 434 QApplication.processEvents()
540 progress = 0 543 progress = 0
541 for index in range(self.findList.topLevelItemCount()): 544 for index in range(self.findList.topLevelItemCount()):
542 itm = self.findList.topLevelItem(index) 545 itm = self.findList.topLevelItem(index)
543 if itm.checkState(0) in [Qt.PartiallyChecked, Qt.Checked]: 546 if itm.checkState(0) in [Qt.PartiallyChecked, Qt.Checked]:
544 file = itm.text(0) 547 file = itm.text(0)
548 origHash = itm.data(0, self.md5Role)
545 549
546 self.findProgressLabel.setPath(file) 550 self.findProgressLabel.setPath(file)
547 551
548 if self.projectButton.isChecked(): 552 if self.projectButton.isChecked():
549 fn = os.path.join(self.project.ppath, file) 553 fn = os.path.join(self.project.ppath, file)
550 else: 554 else:
551 fn = file 555 fn = file
552 556
553 # read the file and split it into textlines 557 # read the file and split it into textlines
554 try: 558 try:
555 text, encoding = Utilities.readEncodedFile(fn) 559 text, encoding, hash = Utilities.readEncodedFileWithHash(fn)
556 lines = text.splitlines() 560 lines = text.splitlines()
557 except (UnicodeError, IOError): 561 except (UnicodeError, IOError):
558 E5MessageBox.critical(self, 562 E5MessageBox.critical(self,
559 self.trUtf8("Replace in Files"), 563 self.trUtf8("Replace in Files"),
560 self.trUtf8("""<p>Could not read the file <b>{0}</b>.""" 564 self.trUtf8("""<p>Could not read the file <b>{0}</b>."""
561 """ Skipping it.</p><p>Reason: {1}</p>""")\ 565 """ Skipping it.</p><p>Reason: {1}</p>""")\
562 .format(fn, str(err)) 566 .format(fn, str(err))
567 )
568 progress += 1
569 self.findProgress.setValue(progress)
570 continue
571
572 # Check the original and the current hash. Skip the file,
573 # if hashes are different.
574 if origHash != hash:
575 E5MessageBox.critical(self,
576 self.trUtf8("Replace in Files"),
577 self.trUtf8("""<p>The current and the original hash of the"""
578 """ file <b>{0}</b> are different. Skipping it."""
579 """</p><p>Hash 1: {1}</p><p>Hash 2: {2}</p>""")\
580 .format(fn, origHash, hash)
563 ) 581 )
564 progress += 1 582 progress += 1
565 self.findProgress.setValue(progress) 583 self.findProgress.setValue(progress)
566 continue 584 continue
567 585

eric ide

mercurial