diff --git a/WallReel/Core/Config/data.hpp b/WallReel/Core/Config/data.hpp index 31f59c0..b93d26d 100644 --- a/WallReel/Core/Config/data.hpp +++ b/WallReel/Core/Config/data.hpp @@ -19,11 +19,12 @@ // wallpaper.dirs[].recursive boolean false Whether to search the directory recursively. // wallpaper.excludes array [] Exclude patterns (regex) // -// palettes array [] -// palettes[].name string "" Name of the palette -// palettes[].colors array [] List of colors in the palette -// palettes[].colors[].name string "" Name of the color -// palettes[].colors[].value string "" Color value in hex format, e.g. "#ff0000" for red +// theme.defaultPalette string "" Name of the default palette to use +// theme.palettes array [] +// theme.palettes[].name string "" Name of the palette +// theme.palettes[].colors array [] List of colors in the palette +// theme.palettes[].colors[].name string "" Name of the color +// theme.palettes[].colors[].value string "" Color value in hex format, e.g. "#ff0000" for red // // action.previewDebounceTime number 300 Debounce time for preview action in milliseconds // action.printSelected boolean true Whether to print the selected wallpaper path to stdout on confirm @@ -73,7 +74,7 @@ struct WallpaperConfigItems { QList excludes; }; -struct PaletteConfigItems { +struct ThemeConfigItems { struct PaletteColorConfigItem { QString name; QColor value; @@ -85,6 +86,7 @@ struct PaletteConfigItems { }; QList palettes; + QString defaultPalette; }; struct ActionConfigItems { diff --git a/WallReel/Core/Config/manager.cpp b/WallReel/Core/Config/manager.cpp index df5b57c..42583b9 100644 --- a/WallReel/Core/Config/manager.cpp +++ b/WallReel/Core/Config/manager.cpp @@ -60,7 +60,7 @@ void WallReel::Core::Config::Manager::_loadConfig(const QString& configPath) { const auto jsonObj = jsonDoc.object(); _loadWallpaperConfig(jsonObj); - _loadPaletteConfig(jsonObj); + _loadThemeConfig(jsonObj); _loadActionConfig(jsonObj); _loadStyleConfig(jsonObj); _loadSortConfig(jsonObj); @@ -112,22 +112,30 @@ void WallReel::Core::Config::Manager::_loadWallpaperConfig(const QJsonObject& ro } } -void WallReel::Core::Config::Manager::_loadPaletteConfig(const QJsonObject& root) { - if (!root.contains("palettes") || !root["palettes"].isArray()) { +void WallReel::Core::Config::Manager::_loadThemeConfig(const QJsonObject& root) { + if (!root.contains("theme") || !root["theme"].isObject()) { return; } - const QJsonArray& palettes = root["palettes"].toArray(); + const QJsonObject& theme = root["theme"].toObject(); + if (theme.contains("defaultPalette") && theme["defaultPalette"].isString()) { + m_themeConfig.defaultPalette = theme["defaultPalette"].toString(); + } + + if (!theme.contains("palettes") || !theme["palettes"].isArray()) { + return; + } + const QJsonArray& palettes = theme["palettes"].toArray(); for (const auto& palItem : palettes) { if (palItem.isObject()) { QJsonObject palObj = palItem.toObject(); - PaletteConfigItems::PaletteConfigItem palette; + ThemeConfigItems::PaletteConfigItem palette; if (palObj.contains("name") && palObj["name"].isString()) { palette.name = palObj["name"].toString(); } if (palObj.contains("colors") && palObj["colors"].isArray()) { for (const auto& colorItem : palObj["colors"].toArray()) { - PaletteConfigItems::PaletteColorConfigItem colorConfig; + ThemeConfigItems::PaletteColorConfigItem colorConfig; if (colorItem.isObject()) { QJsonObject colorObj = colorItem.toObject(); if (colorObj.contains("name") && colorObj["name"].isString()) { @@ -154,7 +162,7 @@ void WallReel::Core::Config::Manager::_loadPaletteConfig(const QJsonObject& root } } } - m_paletteConfig.palettes.append(palette); + m_themeConfig.palettes.append(palette); } } } diff --git a/WallReel/Core/Config/manager.hpp b/WallReel/Core/Config/manager.hpp index e86c340..a7628c3 100644 --- a/WallReel/Core/Config/manager.hpp +++ b/WallReel/Core/Config/manager.hpp @@ -55,7 +55,7 @@ class Manager : public QObject { const WallpaperConfigItems& getWallpaperConfig() const { return m_wallpaperConfig; } - const PaletteConfigItems& getPaletteConfig() const { return m_paletteConfig; } + const ThemeConfigItems& getThemeConfig() const { return m_themeConfig; } const ActionConfigItems& getActionConfig() const { return m_actionConfig; } @@ -96,7 +96,7 @@ class Manager : public QObject { // Parse config void _loadConfig(const QString& configPath); void _loadWallpaperConfig(const QJsonObject& config); - void _loadPaletteConfig(const QJsonObject& config); + void _loadThemeConfig(const QJsonObject& config); void _loadActionConfig(const QJsonObject& config); void _loadStyleConfig(const QJsonObject& config); void _loadSortConfig(const QJsonObject& config); @@ -108,7 +108,7 @@ class Manager : public QObject { private: const QDir m_configDir; WallpaperConfigItems m_wallpaperConfig; - PaletteConfigItems m_paletteConfig; + ThemeConfigItems m_themeConfig; ActionConfigItems m_actionConfig; StyleConfigItems m_styleConfig; SortConfigItems m_sortConfig; diff --git a/WallReel/Core/Palette/manager.cpp b/WallReel/Core/Palette/manager.cpp index 902dded..40f99a1 100644 --- a/WallReel/Core/Palette/manager.cpp +++ b/WallReel/Core/Palette/manager.cpp @@ -6,7 +6,7 @@ #include "predefined.hpp" WallReel::Core::Palette::Manager::Manager( - const Config::PaletteConfigItems& config, + const Config::ThemeConfigItems& config, Image::Model& imageModel, QObject* parent) : QObject(parent), m_imageModel(imageModel) { connect(&m_imageModel, &Image::Model::focusedImageChanged, this, &Manager::updateColor); @@ -45,6 +45,19 @@ WallReel::Core::Palette::Manager::Manager( m_palettes.append(newP); } } + + // Set default palette if specified + if (!config.defaultPalette.isEmpty()) { + for (const auto& p : m_palettes) { + if (p.name == config.defaultPalette) { + m_selectedColor = std::nullopt; + m_selectedPalette = p; + emit selectedColorChanged(); + emit selectedPaletteChanged(); + break; + } + } + } } void WallReel::Core::Palette::Manager::updateColor() { diff --git a/WallReel/Core/Palette/manager.hpp b/WallReel/Core/Palette/manager.hpp index deabe36..29ccc3a 100644 --- a/WallReel/Core/Palette/manager.hpp +++ b/WallReel/Core/Palette/manager.hpp @@ -12,11 +12,13 @@ namespace WallReel::Core::Palette { class Manager : public QObject { Q_OBJECT Q_PROPERTY(QList availablePalettes READ availablePalettes CONSTANT) + Q_PROPERTY(QVariant selectedPalette READ selectedPalette WRITE setSelectedPalette NOTIFY selectedPaletteChanged) + Q_PROPERTY(QVariant selectedColor READ selectedColor WRITE setSelectedColor NOTIFY selectedColorChanged) Q_PROPERTY(QColor color READ color NOTIFY colorChanged) Q_PROPERTY(QString colorName READ colorName NOTIFY colorNameChanged) public: - Manager(const Config::PaletteConfigItems& config, + Manager(const Config::ThemeConfigItems& config, Image::Model& imageModel, QObject* parent = nullptr); @@ -28,7 +30,19 @@ class Manager : public QObject { const QString& colorName() const { return m_displayColorName; } - // Setters + QVariant selectedPalette() const { + if (m_selectedPalette) { + return QVariant::fromValue(*m_selectedPalette); + } + return QVariant(); + } + + QVariant selectedColor() const { + if (m_selectedColor) { + return QVariant::fromValue(*m_selectedColor); + } + return QVariant(); + } Q_INVOKABLE void setSelectedPalette(const QVariant& paletteVar) { if (paletteVar.isNull() || !paletteVar.isValid()) { @@ -36,6 +50,9 @@ class Manager : public QObject { } else { m_selectedPalette = paletteVar.value(); } + m_selectedColor = std::nullopt; + emit selectedPaletteChanged(); + emit selectedColorChanged(); updateColor(); } @@ -45,6 +62,7 @@ class Manager : public QObject { } else { m_selectedColor = colorVar.value(); } + emit selectedColorChanged(); updateColor(); } @@ -84,6 +102,8 @@ class Manager : public QObject { void updateColor(); // <- Image::Model::focusedImageChanged signals: + void selectedPaletteChanged(); + void selectedColorChanged(); void colorChanged(); void colorNameChanged(); diff --git a/WallReel/UI/Modules/ColorControl.qml b/WallReel/UI/Modules/ColorControl.qml index ac6e71c..5ffda49 100644 --- a/WallReel/UI/Modules/ColorControl.qml +++ b/WallReel/UI/Modules/ColorControl.qml @@ -18,6 +18,14 @@ Item { implicitWidth: row.implicitWidth implicitHeight: row.implicitHeight + Component.onCompleted: { + paletteCombo.currentIndex = selectedPalette ? availablePalettes.findIndex((p) => { + return p.name === selectedPalette.name; + }) : -1; + colorCombo.currentIndex = selectedColor ? availableColors.findIndex((c) => { + return c.name === selectedColor.name; + }) + 1 : 0; + } RowLayout { id: row diff --git a/WallReel/UI/Providers/CarouselProvider.qml b/WallReel/UI/Providers/CarouselProvider.qml index 7290a26..a116dc9 100644 --- a/WallReel/UI/Providers/CarouselProvider.qml +++ b/WallReel/UI/Providers/CarouselProvider.qml @@ -23,9 +23,9 @@ QtObject { property bool isSortReverse: ImageModel.currentSortReverse //// Palette / Color readonly property var availablePalettes: PaletteManager.availablePalettes - property var selectedPalette: null // PaletteItem | null + property var selectedPalette: PaletteManager.selectedPalette // PaletteItem | null readonly property var availableColors: selectedPalette ? selectedPalette.colors : [] - property var selectedColor: null // ColorItem | null (null means "auto") + property var selectedColor: PaletteManager.selectedColor // ColorItem | null (null means "auto") readonly property string colorName: PaletteManager.colorName readonly property string colorHex: PaletteManager.color readonly property color colorValue: PaletteManager.color @@ -58,12 +58,11 @@ QtObject { } function selectPalette(palette) { - selectedPalette = palette; - selectedColor = null; // reset color when palette changes + PaletteManager.selectedPalette = palette; } function selectColor(colorItem) { - selectedColor = colorItem; + PaletteManager.selectedColor = colorItem; } function setSearchText(text) { @@ -83,10 +82,4 @@ QtObject { ImageModel.focusOnIndex(currentIndex); } - onSelectedPaletteChanged: () => { - PaletteManager.setSelectedPalette(selectedPalette); - } - onSelectedColorChanged: () => { - PaletteManager.setSelectedColor(selectedColor); - } } diff --git a/WallReel/main.cpp b/WallReel/main.cpp index 2b9363b..df35fd0 100644 --- a/WallReel/main.cpp +++ b/WallReel/main.cpp @@ -57,7 +57,7 @@ int main(int argc, char* argv[]) { imageModel); auto paletteMgr = new Palette::Manager( - config->getPaletteConfig(), + config->getThemeConfig(), *imageModel, imageModel); engine.rootContext()->setContextProperty("PaletteManager", paletteMgr);