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:
luisangelsm
2026-03-22 12:13:07 +01:00
parent 2560174a84
commit 0affbded6e
11 changed files with 587 additions and 527 deletions

View File

@ -120,6 +120,8 @@ add_library(common_gui STATIC
themes/shared/whats_new_dialog_theme.h
themes/theme_editor_dialog.h
themes/theme_editor_dialog.cpp
themes/theme_json_utils.h
themes/theme_json_utils.cpp
themes/theme_meta.h
themes/theme_repository.h
themes/theme_repository.cpp

View File

@ -1,4 +1,5 @@
#include "theme_editor_dialog.h"
#include "theme_json_utils.h"
#include <QApplication>
#include <QColorDialog>
@ -51,17 +52,8 @@ static QString displayKey(const QString &key)
return result;
}
static bool isColorString(const QString &s)
{
// Accepts #RGB, #RRGGBB, #AARRGGBB
if (!s.startsWith('#'))
return false;
const int len = s.length();
return len == 4 || len == 7 || len == 9;
}
ThemeEditorDialog::ThemeEditorDialog(const QJsonObject &params, QWidget *parent)
: QDialog(parent), params(params)
: QDialog(parent), params(normalizeThemeJson(params))
{
setWindowTitle(tr("Theme Editor"));
resize(520, 700);
@ -206,7 +198,7 @@ void ThemeEditorDialog::populate(QTreeWidgetItem *parent, const QJsonObject &obj
item->setData(0, PathRole, childPath);
const QString strVal = val.toString();
if (val.isString() && isColorString(strVal)) {
if (val.isString() && isThemeHexColorString(strVal)) {
const QColor color(strVal);
item->setIcon(1, colorIcon(color));
item->setText(1, strVal);
@ -256,11 +248,12 @@ void ThemeEditorDialog::applyColorToItem(QTreeWidgetItem *item, const QColor &co
const QString hexStr = color.alpha() < 255
? color.name(QColor::HexArgb)
: color.name(QColor::HexRgb);
item->setText(1, hexStr);
const QString normalizedHexStr = hexStr.toUpper();
item->setText(1, normalizedHexStr);
item->setIcon(1, colorIcon(color));
const QStringList path = item->data(0, PathRole).toStringList();
setJsonPath(params, path, hexStr);
setJsonPath(params, path, normalizedHexStr);
}
void ThemeEditorDialog::toggleBoolItem(QTreeWidgetItem *item)
@ -409,7 +402,7 @@ void ThemeEditorDialog::saveToFile()
QMessageBox::warning(this, tr("Save failed"), tr("Could not open file for writing:\n%1").arg(path));
return;
}
file.write(QJsonDocument(params).toJson(QJsonDocument::Indented));
file.write(serializeNormalizedThemeJson(params));
}
void ThemeEditorDialog::loadFromFile()
@ -436,7 +429,7 @@ void ThemeEditorDialog::loadFromFile()
return;
}
params = doc.object();
params = normalizeThemeJson(doc.object());
syncMetaFromParams();
tree->clear();
populate(nullptr, params, { });

View 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);
}

View File

@ -0,0 +1,14 @@
#ifndef THEME_JSON_UTILS_H
#define THEME_JSON_UTILS_H
#include <QByteArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QString>
bool isThemeHexColorString(const QString &value);
QJsonObject normalizeThemeJson(const QJsonObject &json);
QByteArray serializeNormalizedThemeJson(const QJsonObject &json,
QJsonDocument::JsonFormat format = QJsonDocument::Indented);
#endif // THEME_JSON_UTILS_H

View File

@ -1,4 +1,5 @@
#include "theme_repository.h"
#include "theme_json_utils.h"
#include <QDir>
#include <QFile>
@ -78,7 +79,7 @@ QString ThemeRepository::saveUserTheme(QJsonObject themeJson)
QFile file(filePath);
if (file.open(QIODevice::WriteOnly)) {
file.write(QJsonDocument(themeJson).toJson(QJsonDocument::Indented));
file.write(serializeNormalizedThemeJson(themeJson));
file.close();
}