aboutsummaryrefslogtreecommitdiffstats
path: root/tests/manual/syntaxhighlighter/documenthighlighter.h
blob: 20c6000801c919cb636735ae085a1278066a1e18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef DOCUMENTHIGHLIGHTER_H
#define DOCUMENTHIGHLIGHTER_H

#include <QSyntaxHighlighter>
#include <QTextCharFormat>
#include <QRegularExpression>
#include <QQuickTextDocument>
#include <QtQml>

class Highlighter : public QSyntaxHighlighter
{
public:
    Highlighter(QTextDocument *parent = nullptr, int underlineTest = 0);

    void highlightBlock(const QString &text) override;

    QRegularExpression m_testRegex;
    int m_style = 0;
};

class DocumentHighlighter : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QQuickTextDocument *document READ document WRITE setDocument NOTIFY documentChanged)
    Q_PROPERTY(int style READ style WRITE setStyle NOTIFY styleChanged)
    QML_ELEMENT

public:
    DocumentHighlighter(QObject *parent = nullptr);

    QQuickTextDocument *document() const { return m_document; }
    void setDocument(QQuickTextDocument * document);

    int style() const { return m_highlighter.m_style; }
    void setStyle(int style);

signals:
    void documentChanged();
    void styleChanged();

private:
    QQuickTextDocument *m_document = nullptr;
    Highlighter m_highlighter;
};

#endif // DOCUMENTHIGHLIGHTER_H