mirror of
https://github.com/YACReader/yacreader
synced 2026-04-12 15:49:53 -04:00
Normalize color strings so they are always upper case for better readability of the theme files and the values in the editor
This commit is contained in:
50
common/themes/theme_json_utils.cpp
Normal file
50
common/themes/theme_json_utils.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
#include "theme_json_utils.h"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonValue>
|
||||
#include <QRegularExpression>
|
||||
|
||||
namespace {
|
||||
|
||||
QJsonValue normalizeThemeJsonValue(const QJsonValue &value)
|
||||
{
|
||||
if (value.isObject())
|
||||
return normalizeThemeJson(value.toObject());
|
||||
|
||||
if (value.isArray()) {
|
||||
QJsonArray normalizedArray = value.toArray();
|
||||
for (int i = 0; i < normalizedArray.size(); ++i)
|
||||
normalizedArray[i] = normalizeThemeJsonValue(normalizedArray.at(i));
|
||||
return normalizedArray;
|
||||
}
|
||||
|
||||
if (value.isString()) {
|
||||
const QString stringValue = value.toString();
|
||||
if (isThemeHexColorString(stringValue))
|
||||
return stringValue.toUpper();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool isThemeHexColorString(const QString &value)
|
||||
{
|
||||
static const QRegularExpression colorRegex(
|
||||
"^#(?:[0-9A-Fa-f]{3}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$");
|
||||
return colorRegex.match(value).hasMatch();
|
||||
}
|
||||
|
||||
QJsonObject normalizeThemeJson(const QJsonObject &json)
|
||||
{
|
||||
QJsonObject normalized;
|
||||
for (auto it = json.constBegin(); it != json.constEnd(); ++it)
|
||||
normalized.insert(it.key(), normalizeThemeJsonValue(it.value()));
|
||||
return normalized;
|
||||
}
|
||||
|
||||
QByteArray serializeNormalizedThemeJson(const QJsonObject &json, QJsonDocument::JsonFormat format)
|
||||
{
|
||||
return QJsonDocument(normalizeThemeJson(json)).toJson(format);
|
||||
}
|
||||
Reference in New Issue
Block a user