From 6e3d7d581b716bcbf5d17a9f0f532b27e54574e6 Mon Sep 17 00:00:00 2001 From: nowrep Date: Sat, 25 Aug 2012 13:08:06 +0200 Subject: [PATCH] Smarter address bar completer now shows better search results - thanks to Franz Fellner closes #506 --- CHANGELOG | 2 + .../completer/locationcompletermodel.cpp | 65 ++++++++++++++++--- .../completer/locationcompletermodel.h | 9 ++- 3 files changed, 64 insertions(+), 12 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 53fd7ed89..041959916 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -11,9 +11,11 @@ Version 1.3.5 * Save x as ... actions will always show file dialog * possibility to choose to use external download manager on every download * remember last section in preferences + * smarter address bar completer will show better search results * new User Agent manager lets you set User Agent per site * new restore session page lets you choose which tabs you want to restore * new scheme handler for file protocol allows browsing through directories + * new option to show loading progress in address bar * fixed visibility of navigation bar in fullscreen * fixed bad position of add tab button when there is a lot of tabs * fixed gui with RTL languages diff --git a/src/lib/navigation/completer/locationcompletermodel.cpp b/src/lib/navigation/completer/locationcompletermodel.cpp index fb3e6766d..32c1a4c3d 100644 --- a/src/lib/navigation/completer/locationcompletermodel.cpp +++ b/src/lib/navigation/completer/locationcompletermodel.cpp @@ -46,15 +46,10 @@ void LocationCompleterModel::refreshCompletions(const QString &string) Type showType = (Type) qzSettings->showLocationSuggestions; int limit = string.size() < 3 ? 25 : 15; - QString searchString = QString("%%1%").arg(string); QList urlList; - QSqlQuery query; if (showType == HistoryAndBookmarks || showType == Bookmarks) { - query.prepare("SELECT id, url, title, icon FROM bookmarks WHERE title LIKE ? OR url LIKE ? LIMIT ?"); - query.addBindValue(searchString); - query.addBindValue(searchString); - query.addBindValue(limit); + QSqlQuery query = createQuery(string, QString(), limit, true, false); query.exec(); while (query.next()) { @@ -66,6 +61,7 @@ void LocationCompleterModel::refreshCompletions(const QString &string) item->setData(query.value(0), IdRole); item->setData(query.value(2), TitleRole); item->setData(QVariant(true), BookmarkRole); + item->setData(string, SearchStringRole); appendRow(item); urlList.append(url); } @@ -74,10 +70,7 @@ void LocationCompleterModel::refreshCompletions(const QString &string) } if (showType == HistoryAndBookmarks || showType == History) { - query.prepare("SELECT id, url, title FROM history WHERE title LIKE ? OR url LIKE ? ORDER BY count DESC LIMIT ?"); - query.addBindValue(searchString); - query.addBindValue(searchString); - query.addBindValue(limit); + QSqlQuery query = createQuery(string, "count DESC", limit, false, false); query.exec(); while (query.next()) { @@ -93,6 +86,7 @@ void LocationCompleterModel::refreshCompletions(const QString &string) item->setData(query.value(0), IdRole); item->setData(query.value(2), TitleRole); item->setData(QVariant(false), BookmarkRole); + item->setData(string, SearchStringRole); appendRow(item); } @@ -119,3 +113,54 @@ void LocationCompleterModel::showMostVisited() appendRow(item); } } + +QSqlQuery LocationCompleterModel::createQuery(QString searchString, QString orderBy, int limit, bool bookmarks, bool exactMatch) +{ + QString query = "SELECT id, url, title"; + QStringList searchList; + + if (bookmarks) { + query.append(", icon FROM bookmarks "); + } + else { + query.append(" FROM history "); + } + + query.append("WHERE "); + if (exactMatch) { + query.append("title LIKE ? OR url LIKE ? "); + } + else { + searchList = searchString.split(' ', QString::SkipEmptyParts); + const int slSize = searchList.size(); + for (int i = 0; i < slSize; ++i) { + query.append("(title LIKE ? OR url LIKE ?) "); + if (i < slSize - 1) { + query.append("AND "); + } + } + } + + if (!orderBy.isEmpty()) { + query.append("ORDER BY " + orderBy); + } + + query.append(" LIMIT ?"); + + QSqlQuery sqlQuery; + sqlQuery.prepare(query); + + if (exactMatch) { + sqlQuery.addBindValue(QString("%%1%").arg(searchString)); + sqlQuery.addBindValue(QString("%%1%").arg(searchString)); + } + else { + foreach(const QString & str, searchList) { + sqlQuery.addBindValue(QString("%%1%").arg(str)); + sqlQuery.addBindValue(QString("%%1%").arg(str)); + } + } + sqlQuery.addBindValue(limit); + + return sqlQuery; +} diff --git a/src/lib/navigation/completer/locationcompletermodel.h b/src/lib/navigation/completer/locationcompletermodel.h index 89546e8da..1bdbdc286 100644 --- a/src/lib/navigation/completer/locationcompletermodel.h +++ b/src/lib/navigation/completer/locationcompletermodel.h @@ -20,13 +20,16 @@ #include +class QSqlQuery; + class LocationCompleterModel : public QStandardItemModel { public: enum Role { TitleRole = Qt::UserRole + 1, BookmarkRole = Qt::UserRole + 2, - IdRole = Qt::UserRole + 3 + IdRole = Qt::UserRole + 3, + SearchStringRole = Qt::UserRole + 4 }; explicit LocationCompleterModel(QObject* parent = 0); @@ -42,9 +45,11 @@ private: HistoryAndBookmarks = 0, History = 1, Bookmarks = 2, - Nothing = 3 + Nothing = 4 }; + QSqlQuery createQuery(QString searchString, QString orderBy, int limit, bool bookmarks, bool exactMatch); + QString m_lastCompletion; };