From 7fbd3cbdadb5bfb509b9bc396d949fe38a067072 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 21 Feb 2018 13:35:11 +0100 Subject: Search in conversationview ...via syntax highligher or search api. --- framework/src/CMakeLists.txt | 4 +- framework/src/frameworkplugin.cpp | 2 + framework/src/viewhighlighter.cpp | 91 +++++++++++++++++++++++++++++++++++++++ framework/src/viewhighlighter.h | 40 +++++++++++++++++ 4 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 framework/src/viewhighlighter.cpp create mode 100644 framework/src/viewhighlighter.h (limited to 'framework/src') diff --git a/framework/src/CMakeLists.txt b/framework/src/CMakeLists.txt index 97cb453b..f11e6077 100644 --- a/framework/src/CMakeLists.txt +++ b/framework/src/CMakeLists.txt @@ -1,5 +1,5 @@ -find_package(Qt5 COMPONENTS REQUIRED Core Concurrent Quick Qml WebEngineWidgets Test WebEngine) +find_package(Qt5 COMPONENTS REQUIRED Core Concurrent Quick Qml WebEngineWidgets Test WebEngine Gui) find_package(KF5Mime "4.87.0" CONFIG REQUIRED) find_package(Sink CONFIG REQUIRED) find_package(KAsync CONFIG REQUIRED) @@ -48,6 +48,7 @@ add_library(kubeframework SHARED keyring.cpp domainobjectcontroller.cpp extensionmodel.cpp + viewhighlighter.cpp ) target_link_libraries(kubeframework sink @@ -58,6 +59,7 @@ target_link_libraries(kubeframework Qt5::WebEngineWidgets Qt5::Test Qt5::WebEngine + Qt5::Gui KF5::Codecs KF5::Contacts KAsync diff --git a/framework/src/frameworkplugin.cpp b/framework/src/frameworkplugin.cpp index b0e2f9c9..d512ce10 100644 --- a/framework/src/frameworkplugin.cpp +++ b/framework/src/frameworkplugin.cpp @@ -42,6 +42,7 @@ #include "controller.h" #include "domainobjectcontroller.h" #include "extensionmodel.h" +#include "viewhighlighter.h" #include #include @@ -142,6 +143,7 @@ void FrameworkPlugin::registerTypes (const char *uri) qmlRegisterType(uri, 1, 0, "KubeImage"); qmlRegisterType(uri, 1, 0, "Clipboard"); qmlRegisterType(uri, 1, 0, "StartupCheck"); + qmlRegisterType(uri, 1, 0, "ViewHighlighter"); qmlRegisterSingletonType(uri, 1, 0, "WebEngineProfile", webengineprofile_singletontype_provider); qmlRegisterSingletonType(uri, 1, 0, "Keyring", keyring_singletontype_provider); } diff --git a/framework/src/viewhighlighter.cpp b/framework/src/viewhighlighter.cpp new file mode 100644 index 00000000..553646f2 --- /dev/null +++ b/framework/src/viewhighlighter.cpp @@ -0,0 +1,91 @@ +/* + Copyright (c) 2018 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ +#include "viewhighlighter.h" + +#include +#include +#include +#include + +class SearchHighlighter : public QSyntaxHighlighter +{ + Q_OBJECT + +public: + SearchHighlighter(QTextDocument *parent = 0) + : QSyntaxHighlighter(parent) + { + } + + void setSearchString(const QString &s) + { + mSearchString = s; + rehighlight(); + } + +protected: + void highlightBlock(const QString &text) override + { + if (!mSearchString.isEmpty()) { + QTextCharFormat format; + format.setFontWeight(QFont::Bold); + format.setBackground(QColor{"#f67400"}); + + QRegularExpression expression(mSearchString, QRegularExpression::CaseInsensitiveOption); + auto i = expression.globalMatch(text); + while (i.hasNext()) { + auto match = i.next(); + setFormat(match.capturedStart(), match.capturedLength(), format); + } + } + + } + +private: + QString mSearchString; +}; + +struct ViewHighlighter::Private { + SearchHighlighter *searchHighligher; + +}; + + +ViewHighlighter::ViewHighlighter(QObject *parent) + : QObject(parent), + d{new Private} +{ + +} + +void ViewHighlighter::setTextDocument(QQuickTextDocument *document) +{ + if (document) { + d->searchHighligher = new SearchHighlighter{document->textDocument()}; + } +} + +void ViewHighlighter::setSearchString(const QString &s) +{ + if (d->searchHighligher) { + d->searchHighligher->setSearchString(s); + } +} + +#include "viewhighlighter.moc" diff --git a/framework/src/viewhighlighter.h b/framework/src/viewhighlighter.h new file mode 100644 index 00000000..00b4d4b6 --- /dev/null +++ b/framework/src/viewhighlighter.h @@ -0,0 +1,40 @@ +/* + Copyright (c) 2018 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include +#include + +class QQuickTextDocument; + +class ViewHighlighter : public QObject { + Q_OBJECT + + Q_PROPERTY(QString searchString WRITE setSearchString) + Q_PROPERTY(QQuickTextDocument *textDocument WRITE setTextDocument CONSTANT) + +public: + ViewHighlighter(QObject *parent = nullptr); + + void setTextDocument(QQuickTextDocument *); + void setSearchString(const QString& ); + +private: + struct Private; + QSharedPointer d; +}; -- cgit v1.2.3