7 Module implementing a web search widget for the web browser. |
7 Module implementing a web search widget for the web browser. |
8 """ |
8 """ |
9 |
9 |
10 |
10 |
11 from PyQt5.QtCore import pyqtSignal, QUrl, QModelIndex, QTimer, Qt |
11 from PyQt5.QtCore import pyqtSignal, QUrl, QModelIndex, QTimer, Qt |
12 from PyQt5.QtGui import QStandardItem, QStandardItemModel, QFont, QIcon, \ |
12 from PyQt5.QtGui import ( |
13 QPixmap |
13 QStandardItem, QStandardItemModel, QFont, QIcon, QPixmap |
|
14 ) |
14 from PyQt5.QtWidgets import QMenu, QCompleter |
15 from PyQt5.QtWidgets import QMenu, QCompleter |
15 from PyQt5.QtWebEngineWidgets import QWebEnginePage |
16 from PyQt5.QtWebEngineWidgets import QWebEnginePage |
16 |
17 |
17 import UI.PixmapCache |
18 import UI.PixmapCache |
18 |
19 |
110 |
111 |
111 if searchText in self.__recentSearches: |
112 if searchText in self.__recentSearches: |
112 self.__recentSearches.remove(searchText) |
113 self.__recentSearches.remove(searchText) |
113 self.__recentSearches.insert(0, searchText) |
114 self.__recentSearches.insert(0, searchText) |
114 if len(self.__recentSearches) > self.__maxSavedSearches: |
115 if len(self.__recentSearches) > self.__maxSavedSearches: |
115 self.__recentSearches = \ |
116 self.__recentSearches = self.__recentSearches[ |
116 self.__recentSearches[:self.__maxSavedSearches] |
117 :self.__maxSavedSearches] |
117 self.__setupCompleterMenu() |
118 self.__setupCompleterMenu() |
118 |
119 |
119 self.__mw.currentBrowser().setFocus() |
120 self.__mw.currentBrowser().setFocus() |
120 self.__mw.currentBrowser().load( |
121 self.__mw.currentBrowser().load( |
121 self.__openSearchManager.currentEngine().searchUrl(searchText)) |
122 self.__openSearchManager.currentEngine().searchUrl(searchText)) |
122 |
123 |
123 def __setupCompleterMenu(self): |
124 def __setupCompleterMenu(self): |
124 """ |
125 """ |
125 Private method to create the completer menu. |
126 Private method to create the completer menu. |
126 """ |
127 """ |
127 if not self.__suggestions or \ |
128 if ( |
128 (self.__model.rowCount() > 0 and |
129 not self.__suggestions or |
129 self.__model.item(0) != self.__suggestionsItem): |
130 (self.__model.rowCount() > 0 and |
|
131 self.__model.item(0) != self.__suggestionsItem) |
|
132 ): |
130 self.__model.clear() |
133 self.__model.clear() |
131 self.__suggestionsItem = None |
134 self.__suggestionsItem = None |
132 else: |
135 else: |
133 self.__model.removeRows(1, self.__model.rowCount() - 1) |
136 self.__model.removeRows(1, self.__model.rowCount() - 1) |
134 |
137 |
170 """ |
173 """ |
171 Private slot handling the selection of an entry from the completer. |
174 Private slot handling the selection of an entry from the completer. |
172 |
175 |
173 @param index index of the item (QModelIndex) |
176 @param index index of the item (QModelIndex) |
174 """ |
177 """ |
175 if self.__suggestionsItem and \ |
178 if ( |
176 self.__suggestionsItem.index().row() == index.row(): |
179 self.__suggestionsItem and |
|
180 self.__suggestionsItem.index().row() == index.row() |
|
181 ): |
177 return |
182 return |
178 |
183 |
179 if self.__recentSearchesItem and \ |
184 if ( |
180 self.__recentSearchesItem.index().row() == index.row(): |
185 self.__recentSearchesItem and |
|
186 self.__recentSearchesItem.index().row() == index.row() |
|
187 ): |
181 return |
188 return |
182 |
189 |
183 self.__searchNow() |
190 self.__searchNow() |
184 |
191 |
185 def __completerHighlighted(self, index): |
192 def __completerHighlighted(self, index): |
187 Private slot handling the highlighting of an entry of the completer. |
194 Private slot handling the highlighting of an entry of the completer. |
188 |
195 |
189 @param index index of the item (QModelIndex) |
196 @param index index of the item (QModelIndex) |
190 @return flah indicating a successful highlighting (boolean) |
197 @return flah indicating a successful highlighting (boolean) |
191 """ |
198 """ |
192 if self.__suggestionsItem and \ |
199 if ( |
193 self.__suggestionsItem.index().row() == index.row(): |
200 self.__suggestionsItem and |
|
201 self.__suggestionsItem.index().row() == index.row() |
|
202 ): |
194 return False |
203 return False |
195 |
204 |
196 if self.__recentSearchesItem and \ |
205 if ( |
197 self.__recentSearchesItem.index().row() == index.row(): |
206 self.__recentSearchesItem and |
|
207 self.__recentSearchesItem.index().row() == index.row() |
|
208 ): |
198 return False |
209 return False |
199 |
210 |
200 self.setText(index.data()) |
211 self.setText(index.data()) |
201 return True |
212 return True |
202 |
213 |
222 Private slot to get search suggestions from the configured search |
233 Private slot to get search suggestions from the configured search |
223 engine. |
234 engine. |
224 """ |
235 """ |
225 searchText = self.text() |
236 searchText = self.text() |
226 if searchText: |
237 if searchText: |
227 self.__openSearchManager.currentEngine()\ |
238 self.__openSearchManager.currentEngine().requestSuggestions( |
228 .requestSuggestions(searchText) |
239 searchText) |
229 |
240 |
230 def __newSuggestions(self, suggestions): |
241 def __newSuggestions(self, suggestions): |
231 """ |
242 """ |
232 Private slot to receive a new list of suggestions. |
243 Private slot to receive a new list of suggestions. |
233 |
244 |