2
* Copyright 2013 Canonical Ltd.
4
* This file is part of webbrowser-app.
6
* webbrowser-app is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 3.
10
* webbrowser-app is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#include "history-matches-model.h"
20
#include "history-model.h"
23
#include <QtCore/QRegExp>
26
\class HistoryMatchesModel
27
\brief Proxy model that filters the contents of the history model
28
based on a query string
30
HistoryMatchesModel is a proxy model that filters the contents of a
31
HistoryModel based on a query string.
33
The query string may contain several terms (or words).
35
An entry in the history model matches if all the terms are contained in
36
either its URL or its title (inclusive OR).
38
HistoryMatchesModel::HistoryMatchesModel(QObject* parent)
39
: QSortFilterProxyModel(parent)
43
HistoryModel* HistoryMatchesModel::sourceModel() const
45
return qobject_cast<HistoryModel*>(QSortFilterProxyModel::sourceModel());
48
void HistoryMatchesModel::setSourceModel(HistoryModel* sourceModel)
50
if (sourceModel != this->sourceModel()) {
51
QSortFilterProxyModel::setSourceModel(sourceModel);
52
Q_EMIT sourceModelChanged();
56
const QString& HistoryMatchesModel::query() const
61
void HistoryMatchesModel::setQuery(const QString& query)
63
if (query != m_query) {
65
m_terms = query.split(QRegExp("\\s+"), QString::SkipEmptyParts);
67
Q_EMIT queryChanged();
68
Q_EMIT termsChanged();
72
const QStringList& HistoryMatchesModel::terms() const
77
bool HistoryMatchesModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
79
if (m_terms.isEmpty()) {
82
QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
83
QString url = sourceModel()->data(index, HistoryModel::Url).toUrl().toString();
84
QString title = sourceModel()->data(index, HistoryModel::Title).toString();
85
Q_FOREACH (const QString& term, m_terms) {
86
if (!url.contains(term, Qt::CaseInsensitive) &&
87
!title.contains(term, Qt::CaseInsensitive)) {