File indexing completed on 2025-01-19 03:59:52

0001 from PyQt5.QtGui import QIcon
0002 from PyQt5.QtCore import Qt, QTimer
0003 from PyQt5.QtCore import pyqtSignal as Signal
0004 from PyQt5.QtCore import pyqtSlot as Slot
0005 from PyQt5.QtWidgets import *
0006 
0007 
0008 class SearchWidget(QWidget):
0009     frame_changed = Signal(int)
0010 
0011     def __init__(self, target):
0012         super().__init__()
0013         self.target = target
0014 
0015         layout_slider = QHBoxLayout()
0016         layout_slider.setContentsMargins(0, 0, 0, 0)
0017         self.setLayout(layout_slider)
0018 
0019         self.line_find = QLineEdit()
0020         self.line_find.setPlaceholderText("Find")
0021         self.line_find.returnPressed.connect(self.find_next)
0022         layout_slider.addWidget(self.line_find)
0023 
0024         self.line_replace = QLineEdit()
0025         self.line_replace.setPlaceholderText("Replace")
0026         self.line_replace.returnPressed.connect(self.replace)
0027         layout_slider.addWidget(self.line_replace)
0028 
0029         self.button_next = QPushButton()
0030         self.button_next.setIcon(QIcon.fromTheme("go-down-search"))
0031         self.button_next.clicked.connect(self.find_next)
0032         self.button_next.setToolTip("Next")
0033         layout_slider.addWidget(self.button_next)
0034 
0035         self.button_prev = QPushButton()
0036         self.button_prev.setIcon(QIcon.fromTheme("go-up-search"))
0037         self.button_prev.clicked.connect(self.find_prev)
0038         self.button_next.setToolTip("Previous")
0039         layout_slider.addWidget(self.button_prev)
0040 
0041         self.button_repl = QPushButton()
0042         self.button_repl.setText("Replace")
0043         self.button_repl.clicked.connect(self.replace)
0044         layout_slider.addWidget(self.button_repl)
0045 
0046         self.button_case = QPushButton()
0047         self.button_case.setCheckable(True)
0048         self.button_case.setIcon(QIcon.fromTheme("format-text-superscript"))
0049         self.button_case.setToolTip("Case Sensitive")
0050         layout_slider.addWidget(self.button_case)
0051 
0052         self.combo_mode = QComboBox()
0053         self.combo_mode.addItem("Text")
0054         self.combo_mode.addItem("RegExp")
0055         layout_slider.addWidget(self.combo_mode)
0056 
0057         self.button_selection = QPushButton()
0058         self.button_selection.setCheckable(True)
0059         self.button_selection.setIcon(QIcon.fromTheme("edit-select-all"))
0060         self.button_selection.setToolTip("Only in selection")
0061         layout_slider.addWidget(self.button_selection)
0062 
0063         self.set_replace_enabled(False)
0064 
0065     def start_search(self, replace):
0066         if self.target.hasSelectedText():
0067             st = self.target.selectedText()
0068             if "\n" in st:
0069                 self.button_selection.setChecked(True)
0070             else:
0071                 self.button_selection.setChecked(False)
0072                 self.line_find.setText(st)
0073         self.line_find.selectAll()
0074         self.line_find.setFocus(True)
0075         self.set_replace_enabled(replace)
0076 
0077     def set_replace_enabled(self, show):
0078         #self.button_selection.setChecked(show != self.button_selection.isVisible())
0079         #self.button_selection.setVisible(show)
0080         self.line_replace.setVisible(show)
0081         self.button_repl.setVisible(show)
0082 
0083     def find(self, forward):
0084         func = self.target.findFirst if not self.button_selection.isChecked() else self.target.findFirstInSelection
0085         func(
0086             self.line_find.text(),
0087             self.combo_mode.currentIndex() != 0,
0088             self.button_case.isChecked(),
0089             False,
0090             True,
0091             forward,
0092             True
0093         )
0094 
0095     def find_next(self):
0096         return self.find(True)
0097 
0098     def find_prev(self):
0099         return self.find(False)
0100 
0101     def replace(self):
0102         self.target.replace(self.line_replace.text())
0103         self.find_next()