1 | /* |
---|---|
2 | SPDX-FileCopyrightText: 2018 Eike Hein <hein@kde.org> |
3 | SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org> |
4 | |
5 | SPDX-License-Identifier: MIT |
6 | */ |
7 | |
8 | #include "kquicksyntaxhighlighter.h" |
9 | |
10 | #include <KSyntaxHighlighting/Repository> |
11 | #include <KSyntaxHighlighting/SyntaxHighlighter> |
12 | |
13 | #include <QGuiApplication> |
14 | #include <QPalette> |
15 | #include <QQuickTextDocument> |
16 | #include <QTextDocument> |
17 | |
18 | using namespace KSyntaxHighlighting; |
19 | |
20 | extern Repository *defaultRepository(); |
21 | |
22 | KQuickSyntaxHighlighter::KQuickSyntaxHighlighter(QObject *parent) |
23 | : QObject(parent) |
24 | , m_textEdit(nullptr) |
25 | , m_highlighter(new KSyntaxHighlighting::SyntaxHighlighter(this)) |
26 | { |
27 | } |
28 | |
29 | KQuickSyntaxHighlighter::~KQuickSyntaxHighlighter() = default; |
30 | |
31 | QObject *KQuickSyntaxHighlighter::textEdit() const |
32 | { |
33 | return m_textEdit; |
34 | } |
35 | |
36 | void KQuickSyntaxHighlighter::setTextEdit(QObject *textEdit) |
37 | { |
38 | if (m_textEdit != textEdit) { |
39 | m_textEdit = textEdit; |
40 | m_highlighter->setDocument(m_textEdit->property(name: "textDocument").value<QQuickTextDocument *>()->textDocument()); |
41 | } |
42 | } |
43 | |
44 | QVariant KQuickSyntaxHighlighter::definition() const |
45 | { |
46 | return QVariant::fromValue(value: m_definition); |
47 | } |
48 | |
49 | void KQuickSyntaxHighlighter::setDefinition(const QVariant &definition) |
50 | { |
51 | Definition def; |
52 | if (definition.userType() == QMetaType::QString) { |
53 | def = unwrappedRepository()->definitionForName(defName: definition.toString()); |
54 | } else { |
55 | def = definition.value<Definition>(); |
56 | } |
57 | |
58 | if (m_definition != def) { |
59 | m_definition = def; |
60 | |
61 | m_highlighter->setTheme(m_theme.isValid() ? m_theme : unwrappedRepository()->themeForPalette(palette: QGuiApplication::palette())); |
62 | m_highlighter->setDefinition(def); |
63 | |
64 | Q_EMIT definitionChanged(); |
65 | } |
66 | } |
67 | |
68 | QVariant KQuickSyntaxHighlighter::theme() const |
69 | { |
70 | return QVariant::fromValue(value: m_theme); |
71 | } |
72 | |
73 | void KQuickSyntaxHighlighter::setTheme(const QVariant &theme) |
74 | { |
75 | Theme t; |
76 | if (theme.userType() == QMetaType::QString) { |
77 | t = unwrappedRepository()->theme(themeName: theme.toString()); |
78 | } else if (theme.userType() == QMetaType::Int) { |
79 | t = unwrappedRepository()->defaultTheme(t: static_cast<Repository::DefaultTheme>(theme.toInt())); |
80 | } else { |
81 | t = theme.value<Theme>(); |
82 | } |
83 | |
84 | if (m_theme.name() != t.name()) { |
85 | m_theme = t; |
86 | m_highlighter->setTheme(m_theme); |
87 | m_highlighter->rehighlight(); |
88 | Q_EMIT themeChanged(); |
89 | } |
90 | } |
91 | |
92 | Repository *KQuickSyntaxHighlighter::repository() const |
93 | { |
94 | return m_repository; |
95 | } |
96 | |
97 | void KQuickSyntaxHighlighter::setRepository(Repository *repository) |
98 | { |
99 | if (m_repository == repository) { |
100 | return; |
101 | } |
102 | m_repository = repository; |
103 | Q_EMIT repositoryChanged(); |
104 | } |
105 | |
106 | Repository *KQuickSyntaxHighlighter::unwrappedRepository() const |
107 | { |
108 | if (m_repository) { |
109 | return m_repository; |
110 | } |
111 | return defaultRepository(); |
112 | } |
113 | |
114 | #include "moc_kquicksyntaxhighlighter.cpp" |
115 |