aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/3rdparty/syntax-highlighting/src/quick/kquicksyntaxhighlighter.cpp
blob: 19cfbacf58c91441e476ba4736ec32bc2045aec8 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
    SPDX-FileCopyrightText: 2018 Eike Hein <hein@kde.org>
    SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>

    SPDX-License-Identifier: MIT
*/

#include "kquicksyntaxhighlighter.h"

#include <KSyntaxHighlighting/Repository>
#include <KSyntaxHighlighting/SyntaxHighlighter>

#include <QGuiApplication>
#include <QPalette>
#include <QQuickTextDocument>
#include <QTextDocument>

using namespace KSyntaxHighlighting;

extern Repository *defaultRepository();

KQuickSyntaxHighlighter::KQuickSyntaxHighlighter(QObject *parent)
    : QObject(parent)
    , m_textEdit(nullptr)
    , m_highlighter(new KSyntaxHighlighting::SyntaxHighlighter(this))
{
}

KQuickSyntaxHighlighter::~KQuickSyntaxHighlighter() = default;

QObject *KQuickSyntaxHighlighter::textEdit() const
{
    return m_textEdit;
}

void KQuickSyntaxHighlighter::setTextEdit(QObject *textEdit)
{
    if (m_textEdit != textEdit) {
        m_textEdit = textEdit;
        m_highlighter->setDocument(m_textEdit->property("textDocument").value<QQuickTextDocument *>()->textDocument());
    }
}

QVariant KQuickSyntaxHighlighter::definition() const
{
    return QVariant::fromValue(m_definition);
}

void KQuickSyntaxHighlighter::setDefinition(const QVariant &definition)
{
    Definition def;
    if (definition.userType() == QMetaType::QString) {
        def = unwrappedRepository()->definitionForName(definition.toString());
    } else {
        def = definition.value<Definition>();
    }

    if (m_definition != def) {
        m_definition = def;

        m_highlighter->setTheme(m_theme.isValid() ? m_theme : unwrappedRepository()->themeForPalette(QGuiApplication::palette()));
        m_highlighter->setDefinition(def);

        Q_EMIT definitionChanged();
    }
}

QVariant KQuickSyntaxHighlighter::theme() const
{
    return QVariant::fromValue(m_theme);
}

void KQuickSyntaxHighlighter::setTheme(const QVariant &theme)
{
    Theme t;
    if (theme.userType() == QMetaType::QString) {
        t = unwrappedRepository()->theme(theme.toString());
    } else if (theme.userType() == QMetaType::Int) {
        t = unwrappedRepository()->defaultTheme(static_cast<Repository::DefaultTheme>(theme.toInt()));
    } else {
        t = theme.value<Theme>();
    }

    if (m_theme.name() != t.name()) {
        m_theme = t;
        m_highlighter->setTheme(m_theme);
        m_highlighter->rehighlight();
        Q_EMIT themeChanged();
    }
}

Repository *KQuickSyntaxHighlighter::repository() const
{
    return m_repository;
}

void KQuickSyntaxHighlighter::setRepository(Repository *repository)
{
    if (m_repository == repository) {
        return;
    }
    m_repository = repository;
    Q_EMIT repositoryChanged();
}

Repository *KQuickSyntaxHighlighter::unwrappedRepository() const
{
    if (m_repository) {
        return m_repository;
    }
    return defaultRepository();
}

#include "moc_kquicksyntaxhighlighter.cpp"