55 def sizeHintForRow(self, row): |
55 def sizeHintForRow(self, row): |
56 """ |
56 """ |
57 Public method to give a size hint for rows. |
57 Public method to give a size hint for rows. |
58 |
58 |
59 @param row row number (integer) |
59 @param row row number (integer) |
|
60 @return desired row height (integer) |
60 """ |
61 """ |
61 metrics = self.fontMetrics() |
62 metrics = self.fontMetrics() |
62 return metrics.height() |
63 return metrics.height() |
63 |
64 |
64 |
65 |
75 @param parent reference to the parent object (QObject) |
76 @param parent reference to the parent object (QObject) |
76 """ |
77 """ |
77 super(HistoryCompletionModel, self).__init__(parent) |
78 super(HistoryCompletionModel, self).__init__(parent) |
78 |
79 |
79 self.__searchString = "" |
80 self.__searchString = "" |
80 self.__searchMatcher = QRegExp("", Qt.CaseInsensitive, QRegExp.FixedString) |
81 self.__searchMatcher = QRegExp( |
|
82 "", Qt.CaseInsensitive, QRegExp.FixedString) |
81 self.__wordMatcher = QRegExp("", Qt.CaseInsensitive) |
83 self.__wordMatcher = QRegExp("", Qt.CaseInsensitive) |
82 self.__isValid = False |
84 self.__isValid = False |
83 |
85 |
84 self.setDynamicSortFilter(True) |
86 self.setDynamicSortFilter(True) |
85 |
87 |
89 |
91 |
90 @param index index of history entry to get data for (QModelIndex) |
92 @param index index of history entry to get data for (QModelIndex) |
91 @param role data role (integer) |
93 @param role data role (integer) |
92 @return history entry data |
94 @return history entry data |
93 """ |
95 """ |
94 # If the model is valid, tell QCompleter that everything we have filtered |
96 # If the model is valid, tell QCompleter that everything we have |
95 # matches what the user typed; if not, nothing matches |
97 # filtered matches what the user typed; if not, nothing matches |
96 if role == self.HistoryCompletionRole and index.isValid(): |
98 if role == self.HistoryCompletionRole and index.isValid(): |
97 if self.isValid(): |
99 if self.isValid(): |
98 return "t" |
100 return "t" |
99 else: |
101 else: |
100 return "f" |
102 return "f" |
124 if string == self.__searchString: |
126 if string == self.__searchString: |
125 return |
127 return |
126 |
128 |
127 self.__searchString = string |
129 self.__searchString = string |
128 self.__searchMatcher.setPattern(self.__searchString) |
130 self.__searchMatcher.setPattern(self.__searchString) |
129 self.__wordMatcher.setPattern("\\b" + QRegExp.escape(self.__searchString)) |
131 self.__wordMatcher.setPattern( |
|
132 "\\b" + QRegExp.escape(self.__searchString)) |
130 self.invalidateFilter() |
133 self.invalidateFilter() |
131 |
134 |
132 def isValid(self): |
135 def isValid(self): |
133 """ |
136 """ |
134 Public method to check the model for validity. |
137 Public method to check the model for validity. |
135 |
138 |
136 @param flag indicating a valid status (boolean) |
139 @return flag indicating a valid status (boolean) |
137 """ |
140 """ |
138 return self.__isValid |
141 return self.__isValid |
139 |
142 |
140 def setValid(self, valid): |
143 def setValid(self, valid): |
141 """ |
144 """ |
147 return |
150 return |
148 |
151 |
149 self.__isValid = valid |
152 self.__isValid = valid |
150 |
153 |
151 # tell the history completer that the model has changed |
154 # tell the history completer that the model has changed |
152 self.dataChanged.emit(self.index(0, 0), self.index(0, self.rowCount() - 1)) |
155 self.dataChanged.emit(self.index(0, 0), self.index(0, |
|
156 self.rowCount() - 1)) |
153 |
157 |
154 def filterAcceptsRow(self, sourceRow, sourceParent): |
158 def filterAcceptsRow(self, sourceRow, sourceParent): |
155 """ |
159 """ |
156 Protected method to determine, if the row is acceptable. |
160 Protected method to determine, if the row is acceptable. |
157 |
161 |
211 # Sort results in descending frequency-derived score. |
215 # Sort results in descending frequency-derived score. |
212 return frequency_R < frequency_L |
216 return frequency_R < frequency_L |
213 |
217 |
214 |
218 |
215 class HistoryCompleter(QCompleter): |
219 class HistoryCompleter(QCompleter): |
|
220 """ |
|
221 Class implementing a completer for the browser history. |
|
222 """ |
216 def __init__(self, model, parent=None): |
223 def __init__(self, model, parent=None): |
217 """ |
224 """ |
218 Constructor |
225 Constructor |
219 |
226 |
220 @param model reference to the model (QAbstractItemModel) |
227 @param model reference to the model (QAbstractItemModel) |
246 """ |
253 """ |
247 return self.model().data(idx, HistoryModel.UrlStringRole) |
254 return self.model().data(idx, HistoryModel.UrlStringRole) |
248 |
255 |
249 def splitPath(self, path): |
256 def splitPath(self, path): |
250 """ |
257 """ |
251 Public method to split the given path into strings, that are used to match |
258 Public method to split the given path into strings, that are used to |
252 at each level in the model. |
259 match at each level in the model. |
253 |
260 |
254 @param path path to be split (string) |
261 @param path path to be split (string) |
255 @return list of path elements (list of strings) |
262 @return list of path elements (list of strings) |
256 """ |
263 """ |
257 if path == self.__searchString: |
264 if path == self.__searchString: |
258 return ["t"] |
265 return ["t"] |
259 |
266 |
260 # Queue an update to the search string. Wait a bit, so that if the user |
267 # Queue an update to the search string. Wait a bit, so that if the user |
261 # is quickly typing, the completer doesn't try to complete until they pause. |
268 # is quickly typing, the completer doesn't try to complete until they |
|
269 # pause. |
262 if self.__filterTimer.isActive(): |
270 if self.__filterTimer.isActive(): |
263 self.__filterTimer.stop() |
271 self.__filterTimer.stop() |
264 self.__filterTimer.start(150) |
272 self.__filterTimer.start(150) |
265 |
273 |
266 # If the previous search results are not a superset of the current |
274 # If the previous search results are not a superset of the current |
269 self.model().setValid(False) |
277 self.model().setValid(False) |
270 |
278 |
271 self.__searchString = path |
279 self.__searchString = path |
272 |
280 |
273 # The actual filtering is done by the HistoryCompletionModel. Just |
281 # The actual filtering is done by the HistoryCompletionModel. Just |
274 # return a short dummy here so that QCompleter thinks everything matched. |
282 # return a short dummy here so that QCompleter thinks everything |
|
283 # matched. |
275 return ["t"] |
284 return ["t"] |
276 |
285 |
277 def __updateFilter(self): |
286 def __updateFilter(self): |
278 """ |
287 """ |
279 Private slot to update the search string. |
288 Private slot to update the search string. |