summaryrefslogtreecommitdiffstats
path: root/framework/src/viewhighlighter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/viewhighlighter.cpp')
-rw-r--r--framework/src/viewhighlighter.cpp91
1 files changed, 91 insertions, 0 deletions
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 @@
1/*
2 Copyright (c) 2018 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19#include "viewhighlighter.h"
20
21#include <QQuickTextDocument>
22#include <QSyntaxHighlighter>
23#include <QRegularExpression>
24#include <QString>
25
26class SearchHighlighter : public QSyntaxHighlighter
27{
28 Q_OBJECT
29
30public:
31 SearchHighlighter(QTextDocument *parent = 0)
32 : QSyntaxHighlighter(parent)
33 {
34 }
35
36 void setSearchString(const QString &s)
37 {
38 mSearchString = s;
39 rehighlight();
40 }
41
42protected:
43 void highlightBlock(const QString &text) override
44 {
45 if (!mSearchString.isEmpty()) {
46 QTextCharFormat format;
47 format.setFontWeight(QFont::Bold);
48 format.setBackground(QColor{"#f67400"});
49
50 QRegularExpression expression(mSearchString, QRegularExpression::CaseInsensitiveOption);
51 auto i = expression.globalMatch(text);
52 while (i.hasNext()) {
53 auto match = i.next();
54 setFormat(match.capturedStart(), match.capturedLength(), format);
55 }
56 }
57
58 }
59
60private:
61 QString mSearchString;
62};
63
64struct ViewHighlighter::Private {
65 SearchHighlighter *searchHighligher;
66
67};
68
69
70ViewHighlighter::ViewHighlighter(QObject *parent)
71 : QObject(parent),
72 d{new Private}
73{
74
75}
76
77void ViewHighlighter::setTextDocument(QQuickTextDocument *document)
78{
79 if (document) {
80 d->searchHighligher = new SearchHighlighter{document->textDocument()};
81 }
82}
83
84void ViewHighlighter::setSearchString(const QString &s)
85{
86 if (d->searchHighligher) {
87 d->searchHighligher->setSearchString(s);
88 }
89}
90
91#include "viewhighlighter.moc"