KSyntaxHighlighting

kquicksyntaxhighlighter.cpp
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
18using namespace KSyntaxHighlighting;
19
20extern Repository *defaultRepository();
21
22KQuickSyntaxHighlighter::KQuickSyntaxHighlighter(QObject *parent)
23 : QObject(parent)
24 , m_textEdit(nullptr)
25 , m_highlighter(new KSyntaxHighlighting::SyntaxHighlighter(this))
26{
27}
28
29KQuickSyntaxHighlighter::~KQuickSyntaxHighlighter() = default;
30
31QObject *KQuickSyntaxHighlighter::textEdit() const
32{
33 return m_textEdit;
34}
35
36void KQuickSyntaxHighlighter::setTextEdit(QObject *textEdit)
37{
38 if (m_textEdit != textEdit) {
39 m_textEdit = textEdit;
40 m_highlighter->setDocument(m_textEdit->property("textDocument").value<QQuickTextDocument *>()->textDocument());
41 }
42}
43
44QVariant KQuickSyntaxHighlighter::definition() const
45{
46 return QVariant::fromValue(m_definition);
47}
48
49void KQuickSyntaxHighlighter::setDefinition(const QVariant &definition)
50{
51 Definition def;
52 if (definition.userType() == QMetaType::QString) {
53 def = unwrappedRepository()->definitionForName(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(QGuiApplication::palette()));
62 m_highlighter->setDefinition(def);
63
64 Q_EMIT definitionChanged();
65 }
66}
67
68QVariant KQuickSyntaxHighlighter::theme() const
69{
70 return QVariant::fromValue(m_theme);
71}
72
73void KQuickSyntaxHighlighter::setTheme(const QVariant &theme)
74{
75 Theme t;
76 if (theme.userType() == QMetaType::QString) {
77 t = unwrappedRepository()->theme(theme.toString());
78 } else if (theme.userType() == QMetaType::Int) {
79 t = unwrappedRepository()->defaultTheme(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
92Repository *KQuickSyntaxHighlighter::repository() const
93{
94 return m_repository;
95}
96
97void KQuickSyntaxHighlighter::setRepository(Repository *repository)
98{
99 if (m_repository == repository) {
100 return;
101 }
102 m_repository = repository;
103 Q_EMIT repositoryChanged();
104}
105
106Repository *KQuickSyntaxHighlighter::unwrappedRepository() const
107{
108 if (m_repository) {
109 return m_repository;
110 }
111 return defaultRepository();
112}
113
114#include "moc_kquicksyntaxhighlighter.cpp"
Represents a syntax definition.
Definition definition.h:83
DefaultTheme
Built-in default theme types.
Definition repository.h:213
Q_INVOKABLE KSyntaxHighlighting::Theme defaultTheme(DefaultTheme t=LightTheme) const
Returns a default theme instance of the given type.
Q_INVOKABLE KSyntaxHighlighting::Theme theme(const QString &themeName) const
Returns the theme called themeName.
Q_INVOKABLE KSyntaxHighlighting::Definition definitionForName(const QString &defName) const
Returns the Definition named defName.
A QSyntaxHighlighter implementation for use with QTextDocument.
void setTheme(const Theme &theme) override
Sets the theme used for highlighting.
void setDefinition(const Definition &def) override
Sets the syntax definition used for highlighting.
Color theme definition used for highlighting.
Definition theme.h:65
bool isValid() const
Returns true if this is a valid Theme.
Definition theme.cpp:37
Syntax highlighting engine for Kate syntax definitions.
QPalette palette()
Q_EMITQ_EMIT
QVariant property(const char *name) const const
QTextDocument * textDocument() const const
void setDocument(QTextDocument *doc)
QVariant fromValue(T &&value)
int toInt(bool *ok) const const
QString toString() const const
int userType() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:38:04 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.