KSyntaxHighlighting

format.cpp
1/*
2 SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
3 SPDX-FileCopyrightText: 2020 Jonathan Poelen <jonathan.poelen@gmail.com>
4
5 SPDX-License-Identifier: MIT
6*/
7
8#include "format.h"
9#include "definition.h"
10#include "format_p.h"
11#include "textstyledata_p.h"
12#include "themedata_p.h"
13#include "xml_p.h"
14
15#include <QColor>
16#include <QMetaEnum>
17#include <QXmlStreamReader>
18
19using namespace KSyntaxHighlighting;
20
21static Theme::TextStyle stringToDefaultFormat(QStringView str)
22{
23 if (!str.startsWith(QLatin1String("ds"))) {
24 return Theme::Normal;
25 }
26
27 const auto metaEnum = QMetaEnum::fromType<Theme::TextStyle>();
28
29 bool ok = false;
30 const auto value = metaEnum.keyToValue(str.sliced(2).toLatin1().constData(), &ok);
31 if (!ok || value < 0) {
32 return Theme::Normal;
33 }
34 return static_cast<Theme::TextStyle>(value);
35}
36
37FormatPrivate *FormatPrivate::detachAndGet(Format &format)
38{
39 format.d.detach();
40 return format.d.data();
41}
42
43TextStyleData FormatPrivate::styleOverride(const Theme &theme) const
44{
45 return ThemeData::get(theme)->textStyleOverride(definitionName, name);
46}
47
48QColor FormatPrivate::color(const Theme &theme, StyleColor styleColor, ThemeColor themeColor) const
49{
50 const auto overrideStyle = styleOverride(theme);
51 if (overrideStyle.*styleColor) {
52 return QColor::fromRgb(overrideStyle.*styleColor);
53 }
54 // use QColor::fromRgba for QRgb => QColor conversion to avoid unset colors == black!
55 return QColor::fromRgba(style.*styleColor ? style.*styleColor : (theme.*themeColor)(defaultStyle));
56}
57
58bool FormatPrivate::hasColor(const Theme &theme, StyleColor styleColor, ThemeColor themeColor) const
59{
60 // use QColor::fromRgba for background QRgb => QColor conversion to avoid unset colors == black!
61 return color(theme, styleColor, themeColor) != QColor::fromRgba((theme.*themeColor)(Theme::Normal)) && (style.*styleColor || (theme.*themeColor)(defaultStyle) || styleOverride(theme).*styleColor);
62}
63
64static QExplicitlySharedDataPointer<FormatPrivate> &sharedDefaultPrivate()
65{
66 static QExplicitlySharedDataPointer<FormatPrivate> def(new FormatPrivate);
67 return def;
68}
69
70Format::Format()
71 : d(sharedDefaultPrivate())
72{
73}
74
75Format::Format(const Format &other)
76 : d(other.d)
77{
78}
79
80Format::~Format()
81{
82}
83
84Format &Format::operator=(const Format &other)
85{
86 d = other.d;
87 return *this;
88}
89
90bool Format::isValid() const
91{
92 return !d->name.isEmpty();
93}
94
96{
97 return d->name;
98}
99
100int Format::id() const
101{
102 return d->id;
103}
104
106{
107 return d->defaultStyle;
108}
109
110bool Format::isDefaultTextStyle(const Theme &theme) const
111{
112 // use QColor::fromRgba for background QRgb => QColor conversion to avoid unset colors == black!
113 return (!hasTextColor(theme)) && (!hasBackgroundColor(theme)) && (selectedTextColor(theme).rgba() == theme.selectedTextColor(Theme::Normal))
114 && (selectedBackgroundColor(theme).rgba() == (theme.selectedBackgroundColor(Theme::Normal))) && (isBold(theme) == theme.isBold(Theme::Normal))
115 && (isItalic(theme) == theme.isItalic(Theme::Normal)) && (isUnderline(theme) == theme.isUnderline(Theme::Normal))
116 && (isStrikeThrough(theme) == theme.isStrikeThrough(Theme::Normal));
117}
118
119bool Format::hasTextColor(const Theme &theme) const
120{
121 return d->hasColor(theme, &TextStyleData::textColor, &Theme::textColor);
122}
123
124QColor Format::textColor(const Theme &theme) const
125{
126 return d->color(theme, &TextStyleData::textColor, &Theme::textColor);
127}
128
130{
131 return d->color(theme, &TextStyleData::selectedTextColor, &Theme::selectedTextColor);
132}
133
134bool Format::hasBackgroundColor(const Theme &theme) const
135{
136 return d->hasColor(theme, &TextStyleData::backgroundColor, &Theme::backgroundColor);
137}
138
140{
141 return d->color(theme, &TextStyleData::backgroundColor, &Theme::backgroundColor);
142}
143
145{
146 return d->color(theme, &TextStyleData::selectedBackgroundColor, &Theme::selectedBackgroundColor);
147}
148
149bool Format::isBold(const Theme &theme) const
150{
151 const auto overrideStyle = d->styleOverride(theme);
152 if (overrideStyle.hasBold) {
153 return overrideStyle.bold;
154 }
155 return d->style.hasBold ? d->style.bold : theme.isBold(d->defaultStyle);
156}
157
158bool Format::isItalic(const Theme &theme) const
159{
160 const auto overrideStyle = d->styleOverride(theme);
161 if (overrideStyle.hasItalic) {
162 return overrideStyle.italic;
163 }
164 return d->style.hasItalic ? d->style.italic : theme.isItalic(d->defaultStyle);
165}
166
167bool Format::isUnderline(const Theme &theme) const
168{
169 const auto overrideStyle = d->styleOverride(theme);
170 if (overrideStyle.hasUnderline) {
171 return overrideStyle.underline;
172 }
173 return d->style.hasUnderline ? d->style.underline : theme.isUnderline(d->defaultStyle);
174}
175
176bool Format::isStrikeThrough(const Theme &theme) const
177{
178 const auto overrideStyle = d->styleOverride(theme);
179 if (overrideStyle.hasStrikeThrough) {
180 return overrideStyle.strikeThrough;
181 }
182 return d->style.hasStrikeThrough ? d->style.strikeThrough : theme.isStrikeThrough(d->defaultStyle);
183}
184
186{
187 return d->spellCheck;
188}
189
191{
192 return d->style.hasBold;
193}
194
196{
197 return d->style.hasItalic;
198}
199
201{
202 return d->style.hasUnderline;
203}
204
206{
207 return d->style.hasStrikeThrough;
208}
209
211{
212 return d->style.textColor;
213}
214
216{
217 return d->style.backgroundColor;
218}
219
221{
222 return d->style.selectedTextColor;
223}
224
226{
227 return d->style.selectedBackgroundColor;
228}
229
230void FormatPrivate::load(QXmlStreamReader &reader)
231{
232 name = reader.attributes().value(QLatin1String("name")).toString();
233 defaultStyle = stringToDefaultFormat(reader.attributes().value(QLatin1String("defStyleNum")));
234
235 QStringView attribute = reader.attributes().value(QLatin1String("color"));
236 if (!attribute.isEmpty()) {
237 style.textColor = QColor(attribute).rgba();
238 }
239
240 attribute = reader.attributes().value(QLatin1String("selColor"));
241 if (!attribute.isEmpty()) {
242 style.selectedTextColor = QColor(attribute).rgba();
243 }
244
245 attribute = reader.attributes().value(QLatin1String("backgroundColor"));
246 if (!attribute.isEmpty()) {
247 style.backgroundColor = QColor(attribute).rgba();
248 }
249
250 attribute = reader.attributes().value(QLatin1String("selBackgroundColor"));
251 if (!attribute.isEmpty()) {
252 style.selectedBackgroundColor = QColor(attribute).rgba();
253 }
254
255 attribute = reader.attributes().value(QLatin1String("italic"));
256 if (!attribute.isEmpty()) {
257 style.hasItalic = true;
258 style.italic = Xml::attrToBool(attribute);
259 }
260
261 attribute = reader.attributes().value(QLatin1String("bold"));
262 if (!attribute.isEmpty()) {
263 style.hasBold = true;
264 style.bold = Xml::attrToBool(attribute);
265 }
266
267 attribute = reader.attributes().value(QLatin1String("underline"));
268 if (!attribute.isEmpty()) {
269 style.hasUnderline = true;
270 style.underline = Xml::attrToBool(attribute);
271 }
272
273 attribute = reader.attributes().value(QLatin1String("strikeOut"));
274 if (!attribute.isEmpty()) {
275 style.hasStrikeThrough = true;
276 style.strikeThrough = Xml::attrToBool(attribute);
277 }
278
279 attribute = reader.attributes().value(QLatin1String("spellChecking"));
280 if (!attribute.isEmpty()) {
281 spellCheck = Xml::attrToBool(attribute);
282 }
283}
Describes the format to be used for a specific text fragment.
Definition format.h:28
bool hasSelectedTextColorOverride() const
Returns true if the syntax definition file specifies a value for the selected text color attribute.
Definition format.cpp:220
bool hasBackgroundColorOverride() const
Returns true if the syntax definition file sets a value for the background color attribute and,...
Definition format.cpp:215
bool hasBoldOverride() const
Returns true if the syntax definition file sets a value for the bold text attribute and,...
Definition format.cpp:190
bool isUnderline(const Theme &theme) const
Returns true if the combination of this format and the given theme results in underlined text.
Definition format.cpp:167
QString name() const
The name of this format as used in the syntax definition file.
Definition format.cpp:95
bool hasTextColorOverride() const
Returns true if the syntax definition file sets a value for the foreground text color attribute and,...
Definition format.cpp:210
bool isDefaultTextStyle(const Theme &theme) const
Returns true if the combination of this format and the theme theme do not change the default text for...
Definition format.cpp:110
QColor textColor(const Theme &theme) const
Returns the foreground color of the combination of this format and the given theme.
Definition format.cpp:124
QColor selectedBackgroundColor(const Theme &theme) const
Returns the background color of selected text of the combination of this format and the given theme.
Definition format.cpp:144
bool hasBackgroundColor(const Theme &theme) const
Returns true if the combination of this format and the theme theme change the background color compar...
Definition format.cpp:134
bool isValid() const
Returns true if this is a valid format, ie.
Definition format.cpp:90
Theme::TextStyle textStyle() const
Returns the underlying TextStyle of this Format.
Definition format.cpp:105
bool spellCheck() const
Returns whether characters with this format should be spell checked.
Definition format.cpp:185
bool isBold(const Theme &theme) const
Returns true if the combination of this format and the given theme results in bold text formatting.
Definition format.cpp:149
bool hasSelectedBackgroundColorOverride() const
Returns true if the syntax definition file specifies a value for the selected background color attrib...
Definition format.cpp:225
Format()
Creates an empty/invalid format.
Definition format.cpp:70
QColor backgroundColor(const Theme &theme) const
Returns the background color of the combination of this format and the given theme.
Definition format.cpp:139
bool hasItalicOverride() const
Returns true if the syntax definition file sets a value for the italic text attribute and,...
Definition format.cpp:195
bool hasStrikeThroughOverride() const
Returns true if the syntax definition file specifies a value for the struck through text attribute.
Definition format.cpp:205
QColor selectedTextColor(const Theme &theme) const
Returns the foreground color for selected text of the combination of this format and the given theme.
Definition format.cpp:129
int id() const
Returns a unique identifier of this format.
Definition format.cpp:100
bool isItalic(const Theme &theme) const
Returns true if the combination of this format and the given theme results in italic text formatting.
Definition format.cpp:158
bool hasUnderlineOverride() const
Returns true if the syntax definition file sets a value for the underlined text attribute and,...
Definition format.cpp:200
bool isStrikeThrough(const Theme &theme) const
Returns true if the combination of this format and the given theme results in struck through text.
Definition format.cpp:176
bool hasTextColor(const Theme &theme) const
Returns true if the combination of this format and the theme theme change the foreground color compar...
Definition format.cpp:119
Color theme definition used for highlighting.
Definition theme.h:65
bool isStrikeThrough(TextStyle style) const
Returns whether the given style should be shown struck through.
Definition theme.cpp:97
QRgb textColor(TextStyle style) const
Returns the text color to be used for style.
Definition theme.cpp:62
bool isBold(TextStyle style) const
Returns whether the given style should be shown in bold.
Definition theme.cpp:82
QRgb selectedTextColor(TextStyle style) const
Returns the selected text color to be used for style.
Definition theme.cpp:67
QRgb selectedBackgroundColor(TextStyle style) const
Returns the background color to be used for selected text for style.
Definition theme.cpp:77
bool isUnderline(TextStyle style) const
Returns whether the given style should be shown underlined.
Definition theme.cpp:92
TextStyle
Default styles that can be referenced from syntax definition XML files.
Definition theme.h:75
@ Normal
Default text style for normal text and source code without special highlighting.
Definition theme.h:78
bool isItalic(TextStyle style) const
Returns whether the given style should be shown in italic.
Definition theme.cpp:87
QRgb backgroundColor(TextStyle style) const
Returns the background color to be used for style.
Definition theme.cpp:72
Syntax highlighting engine for Kate syntax definitions.
const char * constData() const const
QColor fromRgb(QRgb rgb)
QColor fromRgba(QRgb rgba)
QRgb rgba() const const
QMetaEnum fromType()
bool isEmpty() const const
QStringView sliced(qsizetype pos) const const
bool startsWith(QChar ch) const const
QByteArray toLatin1() const const
QString toString() const const
QStringView value(QAnyStringView namespaceUri, QAnyStringView name) const const
QXmlStreamAttributes attributes() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:33 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.