aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/3rdparty/syntax-highlighting/src/lib/format.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libs/3rdparty/syntax-highlighting/src/lib/format.cpp')
-rw-r--r--src/libs/3rdparty/syntax-highlighting/src/lib/format.cpp196
1 files changed, 108 insertions, 88 deletions
diff --git a/src/libs/3rdparty/syntax-highlighting/src/lib/format.cpp b/src/libs/3rdparty/syntax-highlighting/src/lib/format.cpp
index d1808cafef..2259cd3411 100644
--- a/src/libs/3rdparty/syntax-highlighting/src/lib/format.cpp
+++ b/src/libs/3rdparty/syntax-highlighting/src/lib/format.cpp
@@ -1,58 +1,41 @@
/*
- Copyright (C) 2016 Volker Krause <vkrause@kde.org>
-
- Permission is hereby granted, free of charge, to any person obtaining
- a copy of this software and associated documentation files (the
- "Software"), to deal in the Software without restriction, including
- without limitation the rights to use, copy, modify, merge, publish,
- distribute, sublicense, and/or sell copies of the Software, and to
- permit persons to whom the Software is furnished to do so, subject to
- the following conditions:
-
- The above copyright notice and this permission notice shall be included
- in all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
+ SPDX-FileCopyrightText: 2020 Jonathan Poelen <jonathan.poelen@gmail.com>
+
+ SPDX-License-Identifier: MIT
*/
#include "format.h"
-#include "format_p.h"
#include "definition.h"
#include "definitionref_p.h"
+#include "format_p.h"
#include "textstyledata_p.h"
#include "themedata_p.h"
#include "xml_p.h"
#include <QColor>
-#include <QDebug>
#include <QMetaEnum>
#include <QXmlStreamReader>
using namespace KSyntaxHighlighting;
-static Theme::TextStyle stringToDefaultFormat(const QStringRef &str)
+static Theme::TextStyle stringToDefaultFormat(QStringView str)
{
- if (!str.startsWith(QLatin1String("ds")))
+ if (!str.startsWith(QLatin1String("ds"))) {
return Theme::Normal;
+ }
- static const auto idx = Theme::staticMetaObject.indexOfEnumerator("TextStyle");
- Q_ASSERT(idx >= 0);
- const auto metaEnum = Theme::staticMetaObject.enumerator(idx);
+ const auto metaEnum = QMetaEnum::fromType<Theme::TextStyle>();
bool ok = false;
const auto value = metaEnum.keyToValue(str.mid(2).toLatin1().constData(), &ok);
- if (!ok || value < 0)
+ if (!ok || value < 0) {
return Theme::Normal;
+ }
return static_cast<Theme::TextStyle>(value);
}
-FormatPrivate* FormatPrivate::detachAndGet(Format &format)
+FormatPrivate *FormatPrivate::detachAndGet(Format &format)
{
format.d.detach();
return format.d.data();
@@ -60,10 +43,23 @@ FormatPrivate* FormatPrivate::detachAndGet(Format &format)
TextStyleData FormatPrivate::styleOverride(const Theme &theme) const
{
- const auto themeData = ThemeData::get(theme);
- if (themeData)
- return themeData->textStyleOverride(definition.definition().name(), name);
- return TextStyleData();
+ return ThemeData::get(theme)->textStyleOverride(definitionName, name);
+}
+
+QColor FormatPrivate::color(const Theme &theme, StyleColor styleColor, ThemeColor themeColor) const
+{
+ const auto overrideStyle = styleOverride(theme);
+ if (overrideStyle.*styleColor) {
+ return QColor::fromRgb(overrideStyle.*styleColor);
+ }
+ // use QColor::fromRgba for QRgb => QColor conversion to avoid unset colors == black!
+ return QColor::fromRgba(style.*styleColor ? style.*styleColor : (theme.*themeColor)(defaultStyle));
+}
+
+bool FormatPrivate::hasColor(const Theme &theme, StyleColor styleColor, ThemeColor themeColor) const
+{
+ // use QColor::fromRgba for background QRgb => QColor conversion to avoid unset colors == black!
+ return color(theme, styleColor, themeColor) != QColor::fromRgba((theme.*themeColor)(Theme::Normal)) && (style.*styleColor || (theme.*themeColor)(defaultStyle) || styleOverride(theme).*styleColor);
}
static QExplicitlySharedDataPointer<FormatPrivate> &sharedDefaultPrivate()
@@ -72,12 +68,13 @@ static QExplicitlySharedDataPointer<FormatPrivate> &sharedDefaultPrivate()
return def;
}
-Format::Format() : d(sharedDefaultPrivate())
+Format::Format()
+ : d(sharedDefaultPrivate())
{
}
-Format::Format(const Format &other) :
- d(other.d)
+Format::Format(const Format &other)
+ : d(other.d)
{
}
@@ -85,7 +82,7 @@ Format::~Format()
{
}
-Format& Format::operator=(const Format& other)
+Format &Format::operator=(const Format &other)
{
d = other.d;
return *this;
@@ -101,7 +98,7 @@ QString Format::name() const
return d->name;
}
-quint16 Format::id() const
+int Format::id() const
{
return d->id;
}
@@ -113,92 +110,76 @@ Theme::TextStyle Format::textStyle() const
bool Format::isDefaultTextStyle(const Theme &theme) const
{
- return (!hasTextColor(theme))
- && (!hasBackgroundColor(theme))
- && (selectedTextColor(theme) == theme.selectedTextColor(Theme::Normal))
- && (selectedBackgroundColor(theme) == theme.selectedBackgroundColor(Theme::Normal))
- && (isBold(theme) == theme.isBold(Theme::Normal))
- && (isItalic(theme) == theme.isItalic(Theme::Normal))
- && (isUnderline(theme) == theme.isUnderline(Theme::Normal))
+ // use QColor::fromRgba for background QRgb => QColor conversion to avoid unset colors == black!
+ return (!hasTextColor(theme)) && (!hasBackgroundColor(theme)) && (selectedTextColor(theme).rgba() == theme.selectedTextColor(Theme::Normal))
+ && (selectedBackgroundColor(theme).rgba() == (theme.selectedBackgroundColor(Theme::Normal))) && (isBold(theme) == theme.isBold(Theme::Normal))
+ && (isItalic(theme) == theme.isItalic(Theme::Normal)) && (isUnderline(theme) == theme.isUnderline(Theme::Normal))
&& (isStrikeThrough(theme) == theme.isStrikeThrough(Theme::Normal));
}
bool Format::hasTextColor(const Theme &theme) const
{
- const auto overrideStyle = d->styleOverride(theme);
- return textColor(theme) != theme.textColor(Theme::Normal)
- && (d->style.textColor || theme.textColor(d->defaultStyle) || overrideStyle.textColor);
+ return d->hasColor(theme, &TextStyleData::textColor, &Theme::textColor);
}
QColor Format::textColor(const Theme &theme) const
{
- const auto overrideStyle = d->styleOverride(theme);
- if (overrideStyle.textColor)
- return overrideStyle.textColor;
- return d->style.textColor ? d->style.textColor : theme.textColor(d->defaultStyle);
+ return d->color(theme, &TextStyleData::textColor, &Theme::textColor);
}
QColor Format::selectedTextColor(const Theme &theme) const
{
- const auto overrideStyle = d->styleOverride(theme);
- if (overrideStyle.selectedTextColor)
- return overrideStyle.selectedTextColor;
- return d->style.selectedTextColor ? d->style.selectedTextColor : theme.selectedTextColor(d->defaultStyle);
+ return d->color(theme, &TextStyleData::selectedTextColor, &Theme::selectedTextColor);
}
bool Format::hasBackgroundColor(const Theme &theme) const
{
- const auto overrideStyle = d->styleOverride(theme);
- return backgroundColor(theme) != theme.backgroundColor(Theme::Normal)
- && (d->style.backgroundColor || theme.backgroundColor(d->defaultStyle) || overrideStyle.backgroundColor);
+ return d->hasColor(theme, &TextStyleData::backgroundColor, &Theme::backgroundColor);
}
QColor Format::backgroundColor(const Theme &theme) const
{
- const auto overrideStyle = d->styleOverride(theme);
- if (overrideStyle.backgroundColor)
- return overrideStyle.backgroundColor;
- return d->style.backgroundColor ? d->style.backgroundColor : theme.backgroundColor(d->defaultStyle);
+ return d->color(theme, &TextStyleData::backgroundColor, &Theme::backgroundColor);
}
QColor Format::selectedBackgroundColor(const Theme &theme) const
{
- const auto overrideStyle = d->styleOverride(theme);
- if (overrideStyle.selectedBackgroundColor)
- return overrideStyle.selectedBackgroundColor;
- return d->style.selectedBackgroundColor ? d->style.selectedBackgroundColor
- : theme.selectedBackgroundColor(d->defaultStyle);
+ return d->color(theme, &TextStyleData::selectedBackgroundColor, &Theme::selectedBackgroundColor);
}
bool Format::isBold(const Theme &theme) const
{
const auto overrideStyle = d->styleOverride(theme);
- if (overrideStyle.hasBold)
+ if (overrideStyle.hasBold) {
return overrideStyle.bold;
+ }
return d->style.hasBold ? d->style.bold : theme.isBold(d->defaultStyle);
}
bool Format::isItalic(const Theme &theme) const
{
const auto overrideStyle = d->styleOverride(theme);
- if (overrideStyle.hasItalic)
+ if (overrideStyle.hasItalic) {
return overrideStyle.italic;
+ }
return d->style.hasItalic ? d->style.italic : theme.isItalic(d->defaultStyle);
}
bool Format::isUnderline(const Theme &theme) const
{
const auto overrideStyle = d->styleOverride(theme);
- if (overrideStyle.hasUnderline)
+ if (overrideStyle.hasUnderline) {
return overrideStyle.underline;
+ }
return d->style.hasUnderline ? d->style.underline : theme.isUnderline(d->defaultStyle);
}
bool Format::isStrikeThrough(const Theme &theme) const
{
const auto overrideStyle = d->styleOverride(theme);
- if (overrideStyle.hasStrikeThrough)
+ if (overrideStyle.hasStrikeThrough) {
return overrideStyle.strikeThrough;
+ }
return d->style.hasStrikeThrough ? d->style.strikeThrough : theme.isStrikeThrough(d->defaultStyle);
}
@@ -207,57 +188,96 @@ bool Format::spellCheck() const
return d->spellCheck;
}
+bool Format::hasBoldOverride() const
+{
+ return d->style.hasBold;
+}
+
+bool Format::hasItalicOverride() const
+{
+ return d->style.hasItalic;
+}
+
+bool Format::hasUnderlineOverride() const
+{
+ return d->style.hasUnderline;
+}
+
+bool Format::hasStrikeThroughOverride() const
+{
+ return d->style.hasStrikeThrough;
+}
+
+bool Format::hasTextColorOverride() const
+{
+ return d->style.textColor;
+}
+
+bool Format::hasBackgroundColorOverride() const
+{
+ return d->style.backgroundColor;
+}
+
+bool Format::hasSelectedTextColorOverride() const
+{
+ return d->style.selectedTextColor;
+}
+
+bool Format::hasSelectedBackgroundColorOverride() const
+{
+ return d->style.selectedBackgroundColor;
+}
-void FormatPrivate::load(QXmlStreamReader& reader)
+void FormatPrivate::load(QXmlStreamReader &reader)
{
- name = reader.attributes().value(QStringLiteral("name")).toString();
- defaultStyle = stringToDefaultFormat(reader.attributes().value(QStringLiteral("defStyleNum")));
+ name = reader.attributes().value(QLatin1String("name")).toString();
+ defaultStyle = stringToDefaultFormat(reader.attributes().value(QLatin1String("defStyleNum")));
- QStringRef attribute = reader.attributes().value(QStringLiteral("color"));
+ QStringView attribute = reader.attributes().value(QLatin1String("color"));
if (!attribute.isEmpty()) {
- style.textColor = QColor(attribute.toString()).rgba();
+ style.textColor = QColor(attribute).rgba();
}
- attribute = reader.attributes().value(QStringLiteral("selColor"));
+ attribute = reader.attributes().value(QLatin1String("selColor"));
if (!attribute.isEmpty()) {
- style.selectedTextColor = QColor(attribute.toString()).rgba();
+ style.selectedTextColor = QColor(attribute).rgba();
}
- attribute = reader.attributes().value(QStringLiteral("backgroundColor"));
+ attribute = reader.attributes().value(QLatin1String("backgroundColor"));
if (!attribute.isEmpty()) {
- style.backgroundColor = QColor(attribute.toString()).rgba();
+ style.backgroundColor = QColor(attribute).rgba();
}
- attribute = reader.attributes().value(QStringLiteral("selBackgroundColor"));
+ attribute = reader.attributes().value(QLatin1String("selBackgroundColor"));
if (!attribute.isEmpty()) {
- style.selectedBackgroundColor = QColor(attribute.toString()).rgba();
+ style.selectedBackgroundColor = QColor(attribute).rgba();
}
- attribute = reader.attributes().value(QStringLiteral("italic"));
+ attribute = reader.attributes().value(QLatin1String("italic"));
if (!attribute.isEmpty()) {
style.hasItalic = true;
style.italic = Xml::attrToBool(attribute);
}
- attribute = reader.attributes().value(QStringLiteral("bold"));
+ attribute = reader.attributes().value(QLatin1String("bold"));
if (!attribute.isEmpty()) {
style.hasBold = true;
style.bold = Xml::attrToBool(attribute);
}
- attribute = reader.attributes().value(QStringLiteral("underline"));
+ attribute = reader.attributes().value(QLatin1String("underline"));
if (!attribute.isEmpty()) {
style.hasUnderline = true;
style.underline = Xml::attrToBool(attribute);
}
- attribute = reader.attributes().value(QStringLiteral("strikeOut"));
+ attribute = reader.attributes().value(QLatin1String("strikeOut"));
if (!attribute.isEmpty()) {
style.hasStrikeThrough = true;
style.strikeThrough = Xml::attrToBool(attribute);
}
- attribute = reader.attributes().value(QStringLiteral("spellChecking"));
+ attribute = reader.attributes().value(QLatin1String("spellChecking"));
if (!attribute.isEmpty()) {
spellCheck = Xml::attrToBool(attribute);
}