From a56a58aea9dcb055388661cd2d3d1b58ff896832 Mon Sep 17 00:00:00 2001 From: Alfredo Ramos Date: Thu, 7 Jun 2018 07:23:41 -0500 Subject: [PATCH] Store settings colors in hexadecimal format (#243) This will make easier to see and edit colors in the settings file. The format is defined by QColor::HexRgb Fixes #142 https://doc.qt.io/qt-5/qcolor.html#NameFormat-enum * Add config colors validation If the user adds an invalid hexadecimal colors, defined default colors will be used instead --- src/utils/confighandler.cpp | 72 +++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 19 deletions(-) diff --git a/src/utils/confighandler.cpp b/src/utils/confighandler.cpp index d42a1a66..2c323e98 100644 --- a/src/utils/confighandler.cpp +++ b/src/utils/confighandler.cpp @@ -74,26 +74,43 @@ void ConfigHandler::setButtons(const QVector &buttons QVector ConfigHandler::getUserColors() { QVector colors; + const QVector &defaultColors = { + Qt::darkRed, + Qt::red, + Qt::yellow, + Qt::green, + Qt::darkGreen, + Qt::cyan, + Qt::blue, + Qt::magenta, + Qt::darkMagenta + }; + if (m_settings.contains("userColors")) { - colors = m_settings.value("userColors").value >(); + for (const QString &hex : m_settings.value("userColors").toStringList()) { + if (QColor::isValidColor(hex)) { + colors.append(QColor(hex)); + } + } + + if (colors.isEmpty()) { + colors = defaultColors; + } } else { - colors = { - Qt::darkRed, - Qt::red, - Qt::yellow, - Qt::green, - Qt::darkGreen, - Qt::cyan, - Qt::blue, - Qt::magenta, - Qt::darkMagenta, - }; + colors = defaultColors; } + return colors; } void ConfigHandler::setUserColors(const QVector &l) { - m_settings.setValue("userColors", QVariant::fromValue(l)); + QStringList hexColors; + + for (const QColor &color : l) { + hexColors.append(color.name()); + } + + m_settings.setValue("userColors", QVariant::fromValue(hexColors)); } QString ConfigHandler::savePathValue() { @@ -106,38 +123,55 @@ void ConfigHandler::setSavePath(const QString &savePath) { QColor ConfigHandler::uiMainColorValue() { QColor res = QColor(116, 0, 150); + if (m_settings.contains("uiColor")) { - res = m_settings.value("uiColor").value(); + QString hex = m_settings.value("uiColor").toString(); + + if (QColor::isValidColor(hex)) { + res = QColor(hex); + } } return res; } void ConfigHandler::setUIMainColor(const QColor &c) { - m_settings.setValue("uiColor", c); + m_settings.setValue("uiColor", c.name()); } QColor ConfigHandler::uiContrastColorValue() { QColor res = QColor(86, 0, 120); + if (m_settings.contains("contastUiColor")) { - res = m_settings.value("contastUiColor").value(); + QString hex = m_settings.value("contastUiColor").toString(); + + if (QColor::isValidColor(hex)) { + res = QColor(hex); + } } + return res; } void ConfigHandler::setUIContrastColor(const QColor &c) { - m_settings.setValue("contastUiColor", c); + m_settings.setValue("contastUiColor", c.name()); } QColor ConfigHandler::drawColorValue() { QColor res(Qt::red); + if (m_settings.contains("drawColor")) { - res = m_settings.value("drawColor").value(); + QString hex = m_settings.value("drawColor").toString(); + + if (QColor::isValidColor(hex)) { + res = QColor(hex); + } } + return res; } void ConfigHandler::setDrawColor(const QColor &c) { - m_settings.setValue("drawColor", c); + m_settings.setValue("drawColor", c.name()); } bool ConfigHandler::showHelpValue() {