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