File indexing completed on 2024-05-12 04:02:21

0001 /*
0002     SPDX-FileCopyrightText: 2018 Eike Hein <hein@kde.org>
0003     SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0004 
0005     SPDX-License-Identifier: MIT
0006 */
0007 
0008 #include "kquicksyntaxhighlighter.h"
0009 
0010 #include <KSyntaxHighlighting/Repository>
0011 #include <KSyntaxHighlighting/SyntaxHighlighter>
0012 
0013 #include <QGuiApplication>
0014 #include <QPalette>
0015 #include <QQuickTextDocument>
0016 #include <QTextDocument>
0017 
0018 using namespace KSyntaxHighlighting;
0019 
0020 extern Repository *defaultRepository();
0021 
0022 KQuickSyntaxHighlighter::KQuickSyntaxHighlighter(QObject *parent)
0023     : QObject(parent)
0024     , m_textEdit(nullptr)
0025     , m_highlighter(new KSyntaxHighlighting::SyntaxHighlighter(this))
0026 {
0027 }
0028 
0029 KQuickSyntaxHighlighter::~KQuickSyntaxHighlighter() = default;
0030 
0031 QObject *KQuickSyntaxHighlighter::textEdit() const
0032 {
0033     return m_textEdit;
0034 }
0035 
0036 void KQuickSyntaxHighlighter::setTextEdit(QObject *textEdit)
0037 {
0038     if (m_textEdit != textEdit) {
0039         m_textEdit = textEdit;
0040         m_highlighter->setDocument(m_textEdit->property("textDocument").value<QQuickTextDocument *>()->textDocument());
0041     }
0042 }
0043 
0044 QVariant KQuickSyntaxHighlighter::definition() const
0045 {
0046     return QVariant::fromValue(m_definition);
0047 }
0048 
0049 void KQuickSyntaxHighlighter::setDefinition(const QVariant &definition)
0050 {
0051     Definition def;
0052     if (definition.userType() == QMetaType::QString) {
0053         def = unwrappedRepository()->definitionForName(definition.toString());
0054     } else {
0055         def = definition.value<Definition>();
0056     }
0057 
0058     if (m_definition != def) {
0059         m_definition = def;
0060 
0061         m_highlighter->setTheme(m_theme.isValid() ? m_theme : unwrappedRepository()->themeForPalette(QGuiApplication::palette()));
0062         m_highlighter->setDefinition(def);
0063 
0064         Q_EMIT definitionChanged();
0065     }
0066 }
0067 
0068 QVariant KQuickSyntaxHighlighter::theme() const
0069 {
0070     return QVariant::fromValue(m_theme);
0071 }
0072 
0073 void KQuickSyntaxHighlighter::setTheme(const QVariant &theme)
0074 {
0075     Theme t;
0076     if (theme.userType() == QMetaType::QString) {
0077         t = unwrappedRepository()->theme(theme.toString());
0078     } else if (theme.userType() == QMetaType::Int) {
0079         t = unwrappedRepository()->defaultTheme(static_cast<Repository::DefaultTheme>(theme.toInt()));
0080     } else {
0081         t = theme.value<Theme>();
0082     }
0083 
0084     if (m_theme.name() != t.name()) {
0085         m_theme = t;
0086         m_highlighter->setTheme(m_theme);
0087         m_highlighter->rehighlight();
0088         Q_EMIT themeChanged();
0089     }
0090 }
0091 
0092 Repository *KQuickSyntaxHighlighter::repository() const
0093 {
0094     return m_repository;
0095 }
0096 
0097 void KQuickSyntaxHighlighter::setRepository(Repository *repository)
0098 {
0099     if (m_repository == repository) {
0100         return;
0101     }
0102     m_repository = repository;
0103     Q_EMIT repositoryChanged();
0104 }
0105 
0106 Repository *KQuickSyntaxHighlighter::unwrappedRepository() const
0107 {
0108     if (m_repository) {
0109         return m_repository;
0110     }
0111     return defaultRepository();
0112 }
0113 
0114 #include "moc_kquicksyntaxhighlighter.cpp"