eric6/WebBrowser/SearchWidget.py

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

eric ide

mercurial