eric7/UI/FindFileWidget.py

branch
eric7
changeset 8636
c0a3a6e40815
parent 8632
f25cd4b94eb0
child 8637
394377638256
equal deleted inserted replaced
8635:ea9ba9277670 8636:c0a3a6e40815
8 """ 8 """
9 9
10 import os 10 import os
11 import re 11 import re
12 12
13 from PyQt6.QtCore import pyqtSignal, pyqtSlot, Qt, QPoint 13 from PyQt6.QtCore import pyqtSignal, pyqtSlot, Qt, QPoint, QUrl
14 from PyQt6.QtGui import QCursor 14 from PyQt6.QtGui import QCursor, QDesktopServices, QImageReader
15 from PyQt6.QtWidgets import ( 15 from PyQt6.QtWidgets import (
16 QWidget, QApplication, QMenu, QTreeWidgetItem, QComboBox 16 QWidget, QApplication, QMenu, QTreeWidgetItem, QComboBox
17 ) 17 )
18 18
19 from EricWidgets.EricApplication import ericApp 19 from EricWidgets.EricApplication import ericApp
40 all ticked replacement operations. 40 all ticked replacement operations.
41 41
42 @signal sourceFile(str, int, str, int, int) emitted to open a source file 42 @signal sourceFile(str, int, str, int, int) emitted to open a source file
43 at a specificline 43 at a specificline
44 @signal designerFile(str) emitted to open a Qt-Designer file 44 @signal designerFile(str) emitted to open a Qt-Designer file
45 @signal linguistFile(str) emitted to open a Qt-Linguist (*.ts) file
46 @signal trpreview([str]) emitted to preview Qt-Linguist (*.qm) files
47 @signal pixmapFile(str) emitted to open a pixmap file
48 @signal svgFile(str) emitted to open a SVG file
49 @signal umlFile(str) emitted to open an eric UML file
45 """ 50 """
46 # TODO: add more signals
47 sourceFile = pyqtSignal(str, int, str, int, int) 51 sourceFile = pyqtSignal(str, int, str, int, int)
48 designerFile = pyqtSignal(str) 52 designerFile = pyqtSignal(str)
53 linguistFile = pyqtSignal(str)
54 trpreview = pyqtSignal(list)
55 pixmapFile = pyqtSignal(str)
56 svgFile = pyqtSignal(str)
57 umlFile = pyqtSignal(str)
49 58
50 lineRole = Qt.ItemDataRole.UserRole + 1 59 lineRole = Qt.ItemDataRole.UserRole + 1
51 startRole = Qt.ItemDataRole.UserRole + 2 60 startRole = Qt.ItemDataRole.UserRole + 2
52 endRole = Qt.ItemDataRole.UserRole + 3 61 endRole = Qt.ItemDataRole.UserRole + 3
53 replaceRole = Qt.ItemDataRole.UserRole + 4 62 replaceRole = Qt.ItemDataRole.UserRole + 4
85 self.modeToggleButton.clicked.connect(self.__toggleReplaceMode) 94 self.modeToggleButton.clicked.connect(self.__toggleReplaceMode)
86 95
87 self.findProgressLabel.setMaximumWidth(550) 96 self.findProgressLabel.setMaximumWidth(550)
88 97
89 self.searchHistory = Preferences.toList( 98 self.searchHistory = Preferences.toList(
90 Preferences.Prefs.settings.value( 99 Preferences.getSettings().value(
91 "FindFileWidget/SearchHistory")) 100 "FindFileWidget/SearchHistory"))
92 self.findtextCombo.lineEdit().setClearButtonEnabled(True) 101 self.findtextCombo.lineEdit().setClearButtonEnabled(True)
93 self.findtextCombo.lineEdit().returnPressed.connect(self.__doSearch) 102 self.findtextCombo.lineEdit().returnPressed.connect(self.__doSearch)
94 self.findtextCombo.setCompleter(None) 103 self.findtextCombo.setCompleter(None)
95 self.findtextCombo.addItems(self.searchHistory) 104 self.findtextCombo.addItems(self.searchHistory)
96 self.findtextCombo.setEditText("") 105 self.findtextCombo.setEditText("")
97 106
98 self.replaceHistory = Preferences.toList( 107 self.replaceHistory = Preferences.toList(
99 Preferences.Prefs.settings.value( 108 Preferences.getSettings().value(
100 "FindFileWidget/ReplaceHistory")) 109 "FindFileWidget/ReplaceHistory"))
101 self.replacetextCombo.lineEdit().setClearButtonEnabled(True) 110 self.replacetextCombo.lineEdit().setClearButtonEnabled(True)
102 self.replacetextCombo.lineEdit().returnPressed.connect(self.__doSearch) 111 self.replacetextCombo.lineEdit().returnPressed.connect(self.__doSearch)
103 self.replacetextCombo.setCompleter(None) 112 self.replacetextCombo.setCompleter(None)
104 self.replacetextCombo.addItems(self.replaceHistory) 113 self.replacetextCombo.addItems(self.replaceHistory)
105 self.replacetextCombo.setEditText("") 114 self.replacetextCombo.setEditText("")
106 115
107 self.dirHistory = Preferences.toList( 116 self.dirHistory = Preferences.toList(
108 Preferences.Prefs.settings.value( 117 Preferences.getSettings().value(
109 "FindFileWidget/DirectoryHistory")) 118 "FindFileWidget/DirectoryHistory"))
110 self.dirPicker.addItems(self.dirHistory) 119 self.dirPicker.addItems(self.dirHistory)
111 self.dirPicker.setText("") 120 self.dirPicker.setText("")
112 121
113 self.excludeHiddenCheckBox.setChecked(Preferences.toBool( 122 self.excludeHiddenCheckBox.setChecked(Preferences.toBool(
114 Preferences.Prefs.settings.value( 123 Preferences.getSettings().value(
115 "FindFileWidget/ExcludeHidden", True) 124 "FindFileWidget/ExcludeHidden", True)
116 )) 125 ))
117 126
118 self.project = project 127 self.project = project
119 self.project.projectOpened.connect(self.__projectOpened) 128 self.project.projectOpened.connect(self.__projectOpened)
532 if ct in self.searchHistory: 541 if ct in self.searchHistory:
533 self.searchHistory.remove(ct) 542 self.searchHistory.remove(ct)
534 self.searchHistory.insert(0, ct) 543 self.searchHistory.insert(0, ct)
535 self.findtextCombo.clear() 544 self.findtextCombo.clear()
536 self.findtextCombo.addItems(self.searchHistory) 545 self.findtextCombo.addItems(self.searchHistory)
537 Preferences.Prefs.settings.setValue( 546 Preferences.getSettings().setValue(
538 "FindFileWidget/SearchHistory", 547 "FindFileWidget/SearchHistory",
539 self.searchHistory[:30]) 548 self.searchHistory[:30])
540 Preferences.Prefs.settings.setValue( 549 Preferences.getSettings().setValue(
541 "FindFileWidget/ExcludeHidden", 550 "FindFileWidget/ExcludeHidden",
542 self.excludeHiddenCheckBox.isChecked()) 551 self.excludeHiddenCheckBox.isChecked())
543 552
544 if self.__replaceMode: 553 if self.__replaceMode:
545 replTxt = self.replacetextCombo.currentText() 554 replTxt = self.replacetextCombo.currentText()
558 self.dirHistory.remove(searchDir) 567 self.dirHistory.remove(searchDir)
559 self.dirHistory.insert(0, searchDir) 568 self.dirHistory.insert(0, searchDir)
560 self.dirPicker.clear() 569 self.dirPicker.clear()
561 self.dirPicker.addItems(self.dirHistory) 570 self.dirPicker.addItems(self.dirHistory)
562 self.dirPicker.setText(self.dirHistory[0]) 571 self.dirPicker.setText(self.dirHistory[0])
563 Preferences.Prefs.settings.setValue( 572 Preferences.getSettings().setValue(
564 "FindFileWidget/DirectoryHistory", 573 "FindFileWidget/DirectoryHistory",
565 self.dirHistory[:30]) 574 self.dirHistory[:30])
566 575
567 # set the button states 576 # set the button states
568 self.stopButton.setEnabled(True) 577 self.stopButton.setEnabled(True)
580 break 589 break
581 590
582 self.findProgressLabel.setPath(file) 591 self.findProgressLabel.setPath(file)
583 592
584 fn = ( 593 fn = (
585 os.path.join(self.project.ppath, file) 594 os.path.join(self.project.getProjectPath(), file)
586 if self.projectButton.isChecked() else 595 if self.projectButton.isChecked() else
587 file 596 file
588 ) 597 )
589 # read the file and split it into textlines 598 # read the file and split it into textlines
590 try: 599 try:
650 @pyqtSlot(QTreeWidgetItem, int) 659 @pyqtSlot(QTreeWidgetItem, int)
651 def on_findList_itemDoubleClicked(self, itm, column): 660 def on_findList_itemDoubleClicked(self, itm, column):
652 """ 661 """
653 Private slot to handle the double click on a file item. 662 Private slot to handle the double click on a file item.
654 663
655 It emits the signal sourceFile or designerFile depending on the file 664 It emits a signal depending on the file extension.
656 extension.
657 665
658 @param itm the double clicked tree item 666 @param itm the double clicked tree item
659 @type QTreeWidgetItem 667 @type QTreeWidgetItem
660 @param column column that was double clicked (ignored) 668 @param column column that was double clicked (ignored)
661 @type int 669 @type int
669 file = itm.text(0) 677 file = itm.text(0)
670 line = 1 678 line = 1
671 start = 0 679 start = 0
672 end = 0 680 end = 0
673 681
674 fn = os.path.join(self.project.ppath, file) if self.project else file 682 fileName = (
675 if fn.endswith('.ui'): 683 os.path.join(self.project.getProjectPath(), file)
676 self.designerFile.emit(fn) 684 if self.project.isOpen() else
685 file
686 )
687 fileExt = os.path.splitext(fileName)[1]
688
689 if fileExt == ".ui":
690 self.designerFile.emit(fileName)
691 elif fileExt == ".ts":
692 self.linguistFile.emit(fileName)
693 elif fileExt == ".qm":
694 self.trpreview.emit([fileName])
695 elif fileExt in (".egj", ".e5g"):
696 self.umlFile.emit(fileName)
697 elif fileExt == ".svg":
698 self.svgFile.emit(fileName)
699 elif fileExt[1:] in QImageReader.supportedImageFormats():
700 self.pixmapFile.emit(fileName)
677 else: 701 else:
678 self.sourceFile.emit(fn, line, "", start, end) 702 if Utilities.MimeTypes.isTextFile(fileName):
703 self.sourceFile.emit(fileName, line, "", start, end)
704 else:
705 QDesktopServices.openUrl(QUrl(fileName))
679 706
680 def __getFileList(self, path, filterRe, excludeHiddenDirs=False, 707 def __getFileList(self, path, filterRe, excludeHiddenDirs=False,
681 excludeHiddenFiles=False): 708 excludeHiddenFiles=False):
682 """ 709 """
683 Private method to get a list of files to search. 710 Private method to get a list of files to search.
741 origHash = itm.data(0, self.md5Role) 768 origHash = itm.data(0, self.md5Role)
742 769
743 self.findProgressLabel.setPath(file) 770 self.findProgressLabel.setPath(file)
744 771
745 if self.projectButton.isChecked(): 772 if self.projectButton.isChecked():
746 fn = os.path.join(self.project.ppath, file) 773 fn = os.path.join(self.project.getProjectPath(), file)
747 else: 774 else:
748 fn = file 775 fn = file
749 776
750 # read the file and split it into textlines 777 # read the file and split it into textlines
751 try: 778 try:

eric ide

mercurial