diff options
author | Andras Becsi <andras.becsi@theqtcompany.com> | 2015-08-07 20:45:47 +0200 |
---|---|---|
committer | Andras Becsi <andras.becsi@theqtcompany.com> | 2015-08-12 17:22:04 +0200 |
commit | 01c9037f797bac0012b013c355a8a2c7c1c0cae1 (patch) | |
tree | 9b325984264ab7b2013735f50f8046a5326815b9 /src | |
parent | bd5799ee730f1b3789c75e29e8406bf8017bb559 (diff) |
Add SearchProxyModel to filter history based on search text
This patch also adds highlighting by making the seach string bold.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 2 | ||||
-rw-r--r-- | src/navigationhistoryproxymodel.cpp | 76 | ||||
-rw-r--r-- | src/navigationhistoryproxymodel.h | 75 | ||||
-rw-r--r-- | src/qml/BrowserWindow.qml | 24 | ||||
-rw-r--r-- | src/qml/Utils.js | 24 | ||||
-rw-r--r-- | src/resources.qrc | 1 | ||||
-rw-r--r-- | src/src.pro | 6 |
7 files changed, 201 insertions, 7 deletions
diff --git a/src/main.cpp b/src/main.cpp index d9e81f3..8ca1fa8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -41,6 +41,7 @@ #include <QQuickView> #include <QtWebEngine/qtwebengineglobal.h> +#include "navigationhistoryproxymodel.h" #include "touchmockingapplication.h" #include "engine.h" #include "touchtracker.h" @@ -73,6 +74,7 @@ int main(int argc, char **argv) app.setApplicationName("qtbrowser"); qmlRegisterType<TouchTracker>("io.qt.browser", 1, 0, "TouchTracker"); + qmlRegisterType<NavigationHistoryProxyModel>("io.qt.browser", 1, 0, "SearchProxyModel"); BrowserWindow window; QObject::connect(window.rootContext()->engine(), SIGNAL(quit()), &app, SLOT(quit())); diff --git a/src/navigationhistoryproxymodel.cpp b/src/navigationhistoryproxymodel.cpp new file mode 100644 index 0000000..d574ae5 --- /dev/null +++ b/src/navigationhistoryproxymodel.cpp @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtBrowser project. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPLv2 included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "navigationhistoryproxymodel.h" + +NavigationHistoryProxyModel::NavigationHistoryProxyModel(QObject *parent) + : QSortFilterProxyModel(parent) +{ + +} + +bool NavigationHistoryProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const +{ + QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); + + // Use UrlRole and TitleRole instead of DisplayRole + return (sourceModel()->data(index, Qt::UserRole + 1).toString().contains(filterRegExp()) + || sourceModel()->data(index, Qt::UserRole + 2).toString().contains(filterRegExp())); +} + +void NavigationHistoryProxyModel::setEnabled(bool enabled) +{ + if (dynamicSortFilter() == enabled) + return; + setDynamicSortFilter(enabled); + emit enabledChanged(); +} + +QString NavigationHistoryProxyModel::searchString() const +{ + return m_searchString; +} + +void NavigationHistoryProxyModel::setSearchString(const QString &pattern) +{ + if (m_searchString == pattern) + return; + + m_searchString = pattern; + setFilterRegExp(QRegExp(pattern, Qt::CaseInsensitive, QRegExp::FixedString)); + emit searchStringChanged(); +} diff --git a/src/navigationhistoryproxymodel.h b/src/navigationhistoryproxymodel.h new file mode 100644 index 0000000..c2e3886 --- /dev/null +++ b/src/navigationhistoryproxymodel.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtBrowser project. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPLv2 included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef NAVIGATIONHISTORYPROXYMODEL_H +#define NAVIGATIONHISTORYPROXYMODEL_H + +#include <QObject> +#include <QAbstractItemModel> +#include <QSortFilterProxyModel> + +class NavigationHistoryProxyModel : public QSortFilterProxyModel +{ + Q_OBJECT + + Q_PROPERTY(QAbstractItemModel * target READ sourceModel WRITE setSourceModel) + Q_PROPERTY(QString searchString READ searchString WRITE setSearchString NOTIFY searchStringChanged) + Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged) + +public: + explicit NavigationHistoryProxyModel(QObject *parent = 0); + + + bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override; + + bool enabled() const { return dynamicSortFilter(); } + void setEnabled(bool enabled); + + void setTarget(QAbstractItemModel *t); + + QString searchString() const ; + void setSearchString(const QString &pattern); + +signals: + void searchStringChanged(); + void enabledChanged(); + +private: + QString m_searchString; +}; + +#endif // NAVIGATIONHISTORYPROXYMODEL_H diff --git a/src/qml/BrowserWindow.qml b/src/qml/BrowserWindow.qml index 75dcb69..d819e15 100644 --- a/src/qml/BrowserWindow.qml +++ b/src/qml/BrowserWindow.qml @@ -48,6 +48,8 @@ import QtQuick.Dialogs 1.2 import QtQuick.Enterprise.VirtualKeyboard 1.2 import "assets" +import io.qt.browser 1.0 +import "Utils.js" as Utils Item { id: browserWindow @@ -257,6 +259,9 @@ Item { id: urlDropDown color: "white" visible: navigation.visible + + property string searchString: navigation.addressBar.text + anchors { left: parent.left right: parent.right @@ -287,10 +292,17 @@ Item { color: "#e4e4e4" } + SearchProxyModel { + id: proxy + target: navigation.webView.navigationHistory.items + searchString: urlDropDown.searchString + enabled: urlDropDown.state == "enabled" + } + ListView { id: historyList property int remainingHeight: Math.min((historyList.count + 1) * toolBarSize, inputPanel.y - toolBarSize - 3) - model: navigation.webView.navigationHistory.items + model: proxy clip: true boundsBehavior: Flickable.StopAtBounds footerPositioning: ListView.InlineFooter @@ -325,6 +337,7 @@ Item { horizontalCenter: parent.horizontalCenter } Text { + property string highlightTitle: title ? title : "" height: wrapper.height / 2 width: parent.width elide: Text.ElideRight @@ -337,9 +350,10 @@ Item { font.family: defaultFontFamily font.pixelSize: 23 color: "black" - text: title ? title : "" + text: Utils.highlight(highlightTitle, urlDropDown.searchString) } Text { + property string highlightUrl: url ? url : "" height: wrapper.height / 2 - 1 width: parent.width elide: Text.ElideRight @@ -347,7 +361,7 @@ Item { font.family: defaultFontFamily font.pixelSize: 23 color: uiColor - text: url ? url : "" + text: Utils.highlight(highlightUrl, urlDropDown.searchString) } Rectangle { anchors.horizontalCenter: parent.horizontalCenter @@ -364,7 +378,7 @@ Item { MouseArea { anchors.fill: parent onClicked: { - var string = searchText.text + var string = urlDropDown.searchString var constructedUrl = "" if (engine.isUrl(string)) { constructedUrl = engine.fromUserInput(string) @@ -392,7 +406,7 @@ Item { height: parent.height width: historyList.width - searchIcon.width - 30 elide: Text.ElideRight - text: navigation.addressBar.text + text: urlDropDown.searchString verticalAlignment: Text.AlignVCenter font.family: defaultFontFamily font.pixelSize: 23 diff --git a/src/qml/Utils.js b/src/qml/Utils.js new file mode 100644 index 0000000..d08c3db --- /dev/null +++ b/src/qml/Utils.js @@ -0,0 +1,24 @@ +. pragma library + +function quote(str, delimiter) { + // discuss at: http://phpjs.org/functions/preg_quote/ + // original by: booeyOH + // improved by: Ates Goral (http://magnetiq.com) + // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) + // improved by: Brett Zamir (http://brett-zamir.me) + // bugfixed by: Onno Marsman + // example 1: preg_quote("$40"); + // returns 1: '\\$40' + // example 2: preg_quote("*RRRING* Hello?"); + // returns 2: '\\*RRRING\\* Hello\\?' + // example 3: preg_quote("\\.+*?[^]$(){}=!<>|:"); + // returns 3: '\\\\\\.\\+\\*\\?\\[\\^\\]\\$\\(\\)\\{\\}\\=\\!\\<\\>\\|\\:' + + return String(str) + .replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&'); +} + +function highlight( text, search ) +{ + return text.replace( new RegExp( "(" + quote( search ) + ")" , 'gi' ), "<b>$1</b>" ); +} diff --git a/src/resources.qrc b/src/resources.qrc index 7eada49..7f33a7f 100644 --- a/src/resources.qrc +++ b/src/resources.qrc @@ -9,6 +9,7 @@ <file>qml/SettingsView.qml</file> <file>qml/assets/UIButton.qml</file> <file>qml/assets/UIToolBar.qml</file> + <file>qml/Utils.js</file> <file alias="home">qml/assets/icons/Btn_Home.png</file> <file alias="tabs">qml/assets/icons/Btn_Tabs.png</file> <file alias="forward">qml/assets/icons/Btn_Forward.png</file> diff --git a/src/src.pro b/src/src.pro index 22a341c..039b0c5 100644 --- a/src/src.pro +++ b/src/src.pro @@ -8,13 +8,15 @@ SOURCES = main.cpp \ touchmockingapplication.cpp \ browserwindow.cpp \ touchtracker.cpp \ - engine.cpp + engine.cpp \ + navigationhistoryproxymodel.cpp HEADERS = \ touchmockingapplication.h \ browserwindow.h \ touchtracker.h \ - engine.h + engine.h \ + navigationhistoryproxymodel.h OTHER_FILES = \ qml/assets/UIButton.qml \ |