src/eric7/WebBrowser/SearchWidget.py

branch
eric7
changeset 9209
b99e7fd55fd3
parent 8881
54e42bc2437a
child 9221
bf71ee032bb4
equal deleted inserted replaced
9208:3fc8dfeb6ebe 9209:b99e7fd55fd3
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the search bar for the web browser.
8 """
9
10 from PyQt6.QtCore import pyqtSlot, Qt
11 from PyQt6.QtGui import QPalette
12 from PyQt6.QtWidgets import QWidget
13
14 from .Ui_SearchWidget import Ui_SearchWidget
15
16 import UI.PixmapCache
17
18
19 class SearchWidget(QWidget, Ui_SearchWidget):
20 """
21 Class implementing the search bar for the web browser.
22 """
23 def __init__(self, mainWindow, parent=None):
24 """
25 Constructor
26
27 @param mainWindow reference to the main window (QMainWindow)
28 @param parent parent widget of this dialog (QWidget)
29 """
30 super().__init__(parent)
31 self.setupUi(self)
32
33 self.__mainWindow = mainWindow
34
35 self.closeButton.setIcon(UI.PixmapCache.getIcon("close"))
36 self.findPrevButton.setIcon(UI.PixmapCache.getIcon("1leftarrow"))
37 self.findNextButton.setIcon(UI.PixmapCache.getIcon("1rightarrow"))
38
39 self.__defaultBaseColor = (
40 self.findtextCombo.lineEdit().palette().color(
41 QPalette.ColorRole.Base)
42 )
43 self.__defaultTextColor = (
44 self.findtextCombo.lineEdit().palette().color(
45 QPalette.ColorRole.Text)
46 )
47
48 self.__findHistory = []
49 self.__havefound = False
50 self.__findBackwards = False
51
52 self.findtextCombo.setCompleter(None)
53 self.findtextCombo.lineEdit().returnPressed.connect(
54 self.__findByReturnPressed)
55 self.findtextCombo.lineEdit().textEdited.connect(
56 self.__searchTextEdited)
57
58 def on_findtextCombo_editTextChanged(self, txt):
59 """
60 Private slot to enable/disable the find buttons.
61
62 @param txt text of the combobox (string)
63 """
64 self.findPrevButton.setEnabled(txt != "")
65 self.findNextButton.setEnabled(txt != "")
66
67 def __searchTextEdited(self, txt):
68 """
69 Private slot to perform an incremental search.
70
71 @param txt current text of the search combos line edit (string)
72 (unused)
73 """
74 self.__findNextPrev()
75
76 def __findNextPrev(self):
77 """
78 Private slot to find the next occurrence of text.
79 """
80 self.infoLabel.clear()
81 self.__setFindtextComboBackground(False)
82
83 if not self.findtextCombo.currentText():
84 return
85
86 self.__mainWindow.currentBrowser().findNextPrev(
87 self.findtextCombo.currentText(),
88 self.caseCheckBox.isChecked(),
89 self.__findBackwards,
90 self.__findNextPrevCallback)
91
92 def __findNextPrevCallback(self, result):
93 """
94 Private method to process the result of the last search.
95
96 @param result reference to the search result
97 @type QWebEngineFindTextResult
98 """
99 if result.numberOfMatches() == 0:
100 self.infoLabel.setText(self.tr("Expression was not found."))
101 self.__setFindtextComboBackground(True)
102
103 @pyqtSlot()
104 def on_findNextButton_clicked(self):
105 """
106 Private slot to find the next occurrence.
107 """
108 txt = self.findtextCombo.currentText()
109
110 # This moves any previous occurrence of this statement to the head
111 # of the list and updates the combobox
112 if txt in self.__findHistory:
113 self.__findHistory.remove(txt)
114 self.__findHistory.insert(0, txt)
115 self.findtextCombo.clear()
116 self.findtextCombo.addItems(self.__findHistory)
117
118 self.__findBackwards = False
119 self.__findNextPrev()
120
121 def findNext(self):
122 """
123 Public slot to find the next occurrence.
124 """
125 if not self.__havefound or not self.findtextCombo.currentText():
126 self.showFind()
127 return
128
129 self.on_findNextButton_clicked()
130
131 @pyqtSlot()
132 def on_findPrevButton_clicked(self):
133 """
134 Private slot to find the previous occurrence.
135 """
136 txt = self.findtextCombo.currentText()
137
138 # This moves any previous occurrence of this statement to the head
139 # of the list and updates the combobox
140 if txt in self.__findHistory:
141 self.__findHistory.remove(txt)
142 self.__findHistory.insert(0, txt)
143 self.findtextCombo.clear()
144 self.findtextCombo.addItems(self.__findHistory)
145
146 self.__findBackwards = True
147 self.__findNextPrev()
148
149 def findPrevious(self):
150 """
151 Public slot to find the previous occurrence.
152 """
153 if not self.__havefound or not self.findtextCombo.currentText():
154 self.showFind()
155 return
156
157 self.on_findPrevButton_clicked()
158
159 def __findByReturnPressed(self):
160 """
161 Private slot to handle the returnPressed signal of the findtext
162 combobox.
163 """
164 if self.__findBackwards:
165 self.on_findPrevButton_clicked()
166 else:
167 self.on_findNextButton_clicked()
168
169 def showFind(self):
170 """
171 Public method to display this dialog.
172 """
173 self.__havefound = True
174 self.__findBackwards = False
175
176 self.findtextCombo.clear()
177 self.findtextCombo.addItems(self.__findHistory)
178 self.findtextCombo.setEditText('')
179 self.findtextCombo.setFocus()
180
181 self.caseCheckBox.setChecked(False)
182
183 if self.__mainWindow.currentBrowser().hasSelection():
184 self.findtextCombo.setEditText(
185 self.__mainWindow.currentBrowser().selectedText())
186
187 self.__setFindtextComboBackground(False)
188 self.show()
189
190 def __resetSearch(self):
191 """
192 Private method to reset the last search.
193 """
194 self.__mainWindow.currentBrowser().findText("")
195
196 @pyqtSlot()
197 def on_closeButton_clicked(self):
198 """
199 Private slot to close the widget.
200 """
201 self.__resetSearch()
202 self.close()
203
204 def keyPressEvent(self, event):
205 """
206 Protected slot to handle key press events.
207
208 @param event reference to the key press event (QKeyEvent)
209 """
210 if event.key() == Qt.Key.Key_Escape:
211 cb = self.__mainWindow.currentBrowser()
212 if cb:
213 cb.setFocus(Qt.FocusReason.ActiveWindowFocusReason)
214 event.accept()
215 self.__resetSearch()
216 self.close()
217
218 def __setFindtextComboBackground(self, error):
219 """
220 Private slot to change the findtext combo background to indicate
221 errors.
222
223 @param error flag indicating an error condition (boolean)
224 """
225 styleSheet = (
226 "color: #000000; background-color: #ff6666"
227 if error else
228 f"color: {self.__defaultTextColor};"
229 f" background-color: {self.__defaultBaseColor}"
230 )
231 self.findtextCombo.setStyleSheet(styleSheet)

eric ide

mercurial