eric6/UI/FindFileDialog.py

branch
maintenance
changeset 8273
698ae46f40a4
parent 8176
31965986ecd1
parent 8265
0090cfa83159
equal deleted inserted replaced
8190:fb0ef164f536 8273:698ae46f40a4
53 53
54 @param project reference to the project object 54 @param project reference to the project object
55 @param replaceMode flag indicating the replace dialog mode (boolean) 55 @param replaceMode flag indicating the replace dialog mode (boolean)
56 @param parent parent widget of this dialog (QWidget) 56 @param parent parent widget of this dialog (QWidget)
57 """ 57 """
58 super(FindFileDialog, self).__init__(parent) 58 super().__init__(parent)
59 self.setupUi(self) 59 self.setupUi(self)
60 self.setWindowFlags(Qt.WindowType.Window) 60 self.setWindowFlags(Qt.WindowType.Window)
61 61
62 self.dirPicker.setMode(E5PathPickerModes.DirectoryMode) 62 self.dirPicker.setMode(E5PathPickerModes.DirectoryMode)
63 self.dirPicker.setInsertPolicy(QComboBox.InsertPolicy.InsertAtTop) 63 self.dirPicker.setInsertPolicy(QComboBox.InsertPolicy.InsertAtTop)
195 195
196 if self.__replaceMode: 196 if self.__replaceMode:
197 self.findList.clear() 197 self.findList.clear()
198 self.replacetextCombo.setEditText("") 198 self.replacetextCombo.setEditText("")
199 199
200 super(FindFileDialog, self).show() 200 super().show()
201 201
202 def on_findtextCombo_editTextChanged(self, text): 202 def on_findtextCombo_editTextChanged(self, text):
203 """ 203 """
204 Private slot to handle the editTextChanged signal of the find 204 Private slot to handle the editTextChanged signal of the find
205 text combo. 205 text combo.
436 # retrieve the values 436 # retrieve the values
437 reg = self.regexpCheckBox.isChecked() 437 reg = self.regexpCheckBox.isChecked()
438 wo = self.wordCheckBox.isChecked() 438 wo = self.wordCheckBox.isChecked()
439 cs = self.caseCheckBox.isChecked() 439 cs = self.caseCheckBox.isChecked()
440 ct = self.findtextCombo.currentText() 440 ct = self.findtextCombo.currentText()
441 if reg: 441 txt = ct if reg else re.escape(ct)
442 txt = ct
443 else:
444 txt = re.escape(ct)
445 if wo: 442 if wo:
446 txt = "\\b{0}\\b".format(txt) 443 txt = "\\b{0}\\b".format(txt)
447 flags = re.UNICODE 444 flags = re.UNICODE
448 if not cs: 445 if not cs:
449 flags |= re.IGNORECASE 446 flags |= re.IGNORECASE
500 self.findButton.setEnabled(False) 497 self.findButton.setEnabled(False)
501 498
502 # now go through all the files 499 # now go through all the files
503 self.__populating = True 500 self.__populating = True
504 self.findList.setUpdatesEnabled(False) 501 self.findList.setUpdatesEnabled(False)
505 progress = 0
506 breakSearch = False 502 breakSearch = False
507 occurrences = 0 503 occurrences = 0
508 fileOccurrences = 0 504 fileOccurrences = 0
509 for file in files: 505 for progress, file in enumerate(files, start=1):
510 self.__lastFileItem = None 506 self.__lastFileItem = None
511 found = False 507 found = False
512 if self.__cancelSearch or breakSearch: 508 if self.__cancelSearch or breakSearch:
513 break 509 break
514 510
515 self.findProgressLabel.setPath(file) 511 self.findProgressLabel.setPath(file)
516 512
517 if self.projectButton.isChecked(): 513 fn = (
518 fn = os.path.join(self.project.ppath, file) 514 os.path.join(self.project.ppath, file)
519 else: 515 if self.projectButton.isChecked() else
520 fn = file 516 file
517 )
521 # read the file and split it into textlines 518 # read the file and split it into textlines
522 try: 519 try:
523 text, encoding, hashStr = Utilities.readEncodedFileWithHash(fn) 520 text, encoding, hashStr = Utilities.readEncodedFileWithHash(fn)
524 lines = text.splitlines(True) 521 lines = text.splitlines(True)
525 except (UnicodeError, OSError): 522 except (UnicodeError, OSError):
526 progress += 1
527 self.findProgress.setValue(progress) 523 self.findProgress.setValue(progress)
528 continue 524 continue
529 525
530 # now perform the search and display the lines found 526 # now perform the search and display the lines found
531 count = 0 527 for count, line in enumerate(lines, start=1):
532 for line in lines:
533 if self.__cancelSearch: 528 if self.__cancelSearch:
534 break 529 break
535 530
536 count += 1
537 contains = search.search(line) 531 contains = search.search(line)
538 if contains: 532 if contains:
539 occurrences += 1 533 occurrences += 1
540 found = True 534 found = True
541 start = contains.start() 535 start = contains.start()
564 558
565 QApplication.processEvents() 559 QApplication.processEvents()
566 560
567 if found: 561 if found:
568 fileOccurrences += 1 562 fileOccurrences += 1
569 progress += 1
570 self.findProgress.setValue(progress) 563 self.findProgress.setValue(progress)
571 564
572 if not files: 565 if not files:
573 self.findProgress.setMaximum(1) 566 self.findProgress.setMaximum(1)
574 self.findProgress.setValue(1) 567 self.findProgress.setValue(1)
613 file = itm.text(0) 606 file = itm.text(0)
614 line = 1 607 line = 1
615 start = 0 608 start = 0
616 end = 0 609 end = 0
617 610
618 if self.project: 611 fn = os.path.join(self.project.ppath, file) if self.project else file
619 fn = os.path.join(self.project.ppath, file)
620 else:
621 fn = file
622 if fn.endswith('.ui'): 612 if fn.endswith('.ui'):
623 self.designerFile.emit(fn) 613 self.designerFile.emit(fn)
624 else: 614 else:
625 self.sourceFile.emit(fn, line, "", start, end) 615 self.sourceFile.emit(fn, line, "", start, end)
626 616
676 Private slot to perform the requested replace actions. 666 Private slot to perform the requested replace actions.
677 """ 667 """
678 self.findProgress.setMaximum(self.findList.topLevelItemCount()) 668 self.findProgress.setMaximum(self.findList.topLevelItemCount())
679 self.findProgress.setValue(0) 669 self.findProgress.setValue(0)
680 670
681 progress = 0
682 for index in range(self.findList.topLevelItemCount()): 671 for index in range(self.findList.topLevelItemCount()):
683 itm = self.findList.topLevelItem(index) 672 itm = self.findList.topLevelItem(index)
684 if itm.checkState(0) in [Qt.CheckState.PartiallyChecked, 673 if itm.checkState(0) in [Qt.CheckState.PartiallyChecked,
685 Qt.CheckState.Checked]: 674 Qt.CheckState.Checked]:
686 file = itm.text(0) 675 file = itm.text(0)
706 self.tr( 695 self.tr(
707 """<p>Could not read the file <b>{0}</b>.""" 696 """<p>Could not read the file <b>{0}</b>."""
708 """ Skipping it.</p><p>Reason: {1}</p>""") 697 """ Skipping it.</p><p>Reason: {1}</p>""")
709 .format(fn, str(err)) 698 .format(fn, str(err))
710 ) 699 )
711 progress += 1 700 self.findProgress.setValue(index)
712 self.findProgress.setValue(progress)
713 continue 701 continue
714 702
715 # Check the original and the current hash. Skip the file, 703 # Check the original and the current hash. Skip the file,
716 # if hashes are different. 704 # if hashes are different.
717 if origHash != hashStr: 705 if origHash != hashStr:
722 """<p>The current and the original hash of the""" 710 """<p>The current and the original hash of the"""
723 """ file <b>{0}</b> are different. Skipping it.""" 711 """ file <b>{0}</b> are different. Skipping it."""
724 """</p><p>Hash 1: {1}</p><p>Hash 2: {2}</p>""") 712 """</p><p>Hash 1: {1}</p><p>Hash 2: {2}</p>""")
725 .format(fn, origHash, hashStr) 713 .format(fn, origHash, hashStr)
726 ) 714 )
727 progress += 1 715 self.findProgress.setValue(index)
728 self.findProgress.setValue(progress)
729 continue 716 continue
730 717
731 # replace the lines authorized by the user 718 # replace the lines authorized by the user
732 for cindex in range(itm.childCount()): 719 for cindex in range(itm.childCount()):
733 citm = itm.child(cindex) 720 citm = itm.child(cindex)
748 """<p>Could not save the file <b>{0}</b>.""" 735 """<p>Could not save the file <b>{0}</b>."""
749 """ Skipping it.</p><p>Reason: {1}</p>""") 736 """ Skipping it.</p><p>Reason: {1}</p>""")
750 .format(fn, str(err)) 737 .format(fn, str(err))
751 ) 738 )
752 739
753 progress += 1 740 self.findProgress.setValue(index + 1)
754 self.findProgress.setValue(progress)
755 741
756 self.findProgressLabel.setPath("") 742 self.findProgressLabel.setPath("")
757 743
758 self.findList.clear() 744 self.findList.clear()
759 self.replaceButton.setEnabled(False) 745 self.replaceButton.setEnabled(False)
784 def __copyToClipboard(self): 770 def __copyToClipboard(self):
785 """ 771 """
786 Private method to copy the path of an entry to the clipboard. 772 Private method to copy the path of an entry to the clipboard.
787 """ 773 """
788 itm = self.findList.selectedItems()[0] 774 itm = self.findList.selectedItems()[0]
789 if itm.parent(): 775 fn = itm.parent().text(0) if itm.parent() else itm.text(0)
790 fn = itm.parent().text(0)
791 else:
792 fn = itm.text(0)
793 776
794 cb = QApplication.clipboard() 777 cb = QApplication.clipboard()
795 cb.setText(fn) 778 cb.setText(fn)

eric ide

mercurial