KSyntaxHighlighting

kquicksyntaxhighlighter.cpp
1 /*
2  SPDX-FileCopyrightText: 2018 Eike Hein <[email protected]>
3  SPDX-FileCopyrightText: 2021 Volker Krause <[email protected]>
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("textDocument").value<QQuickTextDocument *>()->textDocument());
41  }
42 }
43 
44 QVariant KQuickSyntaxHighlighter::definition() const
45 {
46  return QVariant::fromValue(m_definition);
47 }
48 
49 void KQuickSyntaxHighlighter::setDefinition(const QVariant &definition)
50 {
51  Definition def;
52  if (definition.type() == QVariant::String) {
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 
68 QVariant KQuickSyntaxHighlighter::theme() const
69 {
70  return QVariant::fromValue(m_theme);
71 }
72 
73 void KQuickSyntaxHighlighter::setTheme(const QVariant &theme)
74 {
75  Theme t;
76  if (theme.type() == QVariant::String) {
77  t = unwrappedRepository()->theme(theme.toString());
78  } else if (theme.type() == QVariant::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 
92 RepositoryWrapper *KQuickSyntaxHighlighter::repository() const
93 {
94  return m_repository;
95 }
96 
97 void KQuickSyntaxHighlighter::setRepository(RepositoryWrapper *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->m_repository;
110  }
111  return defaultRepository();
112 }
QVariant fromValue(const T &value)
T value() const const
Syntax highlighting repository.
Definition: repository.h:127
QVariant::Type type() const const
QTextDocument * textDocument() const const
int toInt(bool *ok) const const
A QSyntaxHighlighter implementation for use with QTextDocument.
QPalette palette()
Represents a syntax definition.
Definition: definition.h:86
QString toString() const const
Color theme definition used for highlighting.
Definition: theme.h:64
DefaultTheme
Built-in default theme types.
Definition: repository.h:214
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Mar 26 2023 04:09:17 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.