refactor: organize config manager data structure

This commit is contained in:
2025-08-07 23:02:27 +02:00
parent 94fb258f2f
commit 140db8d0d7
8 changed files with 116 additions and 114 deletions
+3 -4
View File
@@ -4,8 +4,7 @@
"~/Pictures/116327446_p0.jpg" "~/Pictures/116327446_p0.jpg"
], ],
"dirs": [ "dirs": [
"~/.config/backgrounds", "~/.config/backgrounds"
"/run/media/kolkas/移硬1T/逆流茶会/"
], ],
"excludes": [ "excludes": [
"~/.config/backgrounds/nao-stars-crop-adjust-flop.jpg", "~/.config/backgrounds/nao-stars-crop-adjust-flop.jpg",
@@ -13,7 +12,7 @@
"~/.config/backgrounds/README.md" "~/.config/backgrounds/README.md"
] ]
}, },
"actions": { "action": {
"confirm": "~/.scripts/change-wallpaper.sh \"%1\"" "confirm": "~/.scripts/change-wallpaper.sh \"%1\""
}, },
"style": { "style": {
@@ -22,7 +21,7 @@
"image_focus_width": 480, "image_focus_width": 480,
"window_width": 750, "window_width": 750,
"window_height": 500, "window_height": 500,
"no_loading_screen": true "no_loading_screen": false
}, },
"sort": { "sort": {
"type": "name", "type": "name",
+49 -48
View File
@@ -1,7 +1,7 @@
/* /*
* @Author: Uyanide pywang0608@foxmail.com * @Author: Uyanide pywang0608@foxmail.com
* @Date: 2025-08-05 01:34:52 * @Date: 2025-08-05 01:34:52
* @LastEditTime: 2025-08-07 22:02:55 * @LastEditTime: 2025-08-07 22:27:50
* @Description: Configuration manager. * @Description: Configuration manager.
*/ */
#include "config.h" #include "config.h"
@@ -19,6 +19,23 @@ using namespace GeneralLogger;
static QString expandPath(const QString &path); static QString expandPath(const QString &path);
const QString Config::s_DefaultConfigFileName = "config.json";
Config::Config(const QString &configDir, const QStringList &searchDirs, QObject *parent)
: QObject(parent), m_configDir(configDir) {
info(QString("Loading configuration from: %1").arg(configDir));
_loadConfig(configDir + QDir::separator() + s_DefaultConfigFileName);
info(QString("Additional search directories: %1").arg(searchDirs.join(", ")));
m_wallpaperConfig.dirs.append(searchDirs);
info("Loading wallpapers ...");
_loadWallpapers();
}
Config::~Config() {
}
void Config::_loadConfig(const QString &configPath) { void Config::_loadConfig(const QString &configPath) {
QFile configFile(configPath); QFile configFile(configPath);
if (!configFile.open(QIODevice::ReadOnly)) { if (!configFile.open(QIODevice::ReadOnly)) {
@@ -55,77 +72,77 @@ void Config::_loadConfig(const QString &configPath) {
std::vector<ConfigMapping> std::vector<ConfigMapping>
mappings = { mappings = {
{"wallpaper.paths", "paths", [this](const QJsonValue &val) { {"wallpaper.paths", "paths", [this](const QJsonValue &val) {
parseJsonArray(val, m_configItems.wallpaperPaths); parseJsonArray(val, m_wallpaperConfig.paths);
}}, }},
{"wallpaper.dirs", "dirs", [this](const QJsonValue &val) { {"wallpaper.dirs", "dirs", [this](const QJsonValue &val) {
parseJsonArray(val, m_configItems.wallpaperDirs); parseJsonArray(val, m_wallpaperConfig.dirs);
}}, }},
{"wallpaper.excludes", "excludes", [this](const QJsonValue &val) { {"wallpaper.excludes", "excludes", [this](const QJsonValue &val) {
parseJsonArray(val, m_configItems.wallpaperExcludes); parseJsonArray(val, m_wallpaperConfig.excludes);
}}, }},
{"actions.confirm", "confirm", [this](const QJsonValue &val) { {"action.confirm", "confirm", [this](const QJsonValue &val) {
if (val.isString()) { if (val.isString()) {
m_configItems.actionsConfirm = ::expandPath(val.toString()); m_actionConfig.confirm = ::expandPath(val.toString());
info(QString("Action confirm: %1").arg(m_configItems.actionsConfirm), GeneralLogger::STEP); info(QString("Action confirm: %1").arg(m_actionConfig.confirm), GeneralLogger::STEP);
} }
}}, }},
{"style.aspect_ratio", "aspect_ratio", [this](const QJsonValue &val) { {"style.aspect_ratio", "aspect_ratio", [this](const QJsonValue &val) {
if (val.isDouble() && val.toDouble() > 0) { if (val.isDouble() && val.toDouble() > 0) {
m_configItems.styleAspectRatio = val.toDouble(); m_styleConfig.aspectRatio = val.toDouble();
info(QString("Aspect ratio: %1").arg(m_configItems.styleAspectRatio), GeneralLogger::STEP); info(QString("Aspect ratio: %1").arg(m_styleConfig.aspectRatio), GeneralLogger::STEP);
} }
}}, }},
{"style.image_width", "image_width", [this](const QJsonValue &val) { {"style.image_width", "image_width", [this](const QJsonValue &val) {
if (val.isDouble() && val.toDouble() > 0) { if (val.isDouble() && val.toDouble() > 0) {
m_configItems.styleImageWidth = val.toInt(); m_styleConfig.imageWidth = val.toInt();
info(QString("Image width: %1").arg(m_configItems.styleImageWidth), GeneralLogger::STEP); info(QString("Image width: %1").arg(m_styleConfig.imageWidth), GeneralLogger::STEP);
} }
}}, }},
{"style.image_focus_width", "image_focus_width", [this](const QJsonValue &val) { {"style.image_focus_width", "image_focus_width", [this](const QJsonValue &val) {
if (val.isDouble() && val.toDouble() > 0) { if (val.isDouble() && val.toDouble() > 0) {
m_configItems.styleImageFocusWidth = val.toInt(); m_styleConfig.imageFocusWidth = val.toInt();
info(QString("Image focus width: %1").arg(m_configItems.styleImageFocusWidth), GeneralLogger::STEP); info(QString("Image focus width: %1").arg(m_styleConfig.imageFocusWidth), GeneralLogger::STEP);
} }
}}, }},
{"style.window_width", "window_width", [this](const QJsonValue &val) { {"style.window_width", "window_width", [this](const QJsonValue &val) {
if (val.isDouble() && val.toDouble() > 0) { if (val.isDouble() && val.toDouble() > 0) {
m_configItems.styleWindowWidth = val.toInt(); m_styleConfig.windowWidth = val.toInt();
info(QString("Window width: %1").arg(m_configItems.styleWindowWidth), GeneralLogger::STEP); info(QString("Window width: %1").arg(m_styleConfig.windowWidth), GeneralLogger::STEP);
} }
}}, }},
{"style.window_height", "window_height", [this](const QJsonValue &val) { {"style.window_height", "window_height", [this](const QJsonValue &val) {
if (val.isDouble() && val.toDouble() > 0) { if (val.isDouble() && val.toDouble() > 0) {
m_configItems.styleWindowHeight = val.toInt(); m_styleConfig.windowHeight = val.toInt();
info(QString("Window height: %1").arg(m_configItems.styleWindowHeight), GeneralLogger::STEP); info(QString("Window height: %1").arg(m_styleConfig.windowHeight), GeneralLogger::STEP);
} }
}}, }},
{"style.no_loading_screen", "no_loading_screen", [this](const QJsonValue &val) { {"style.no_loading_screen", "no_loading_screen", [this](const QJsonValue &val) {
if (val.isBool()) { if (val.isBool()) {
m_configItems.styleNoLoadingScreen = val.toBool(); m_styleConfig.noLoadingScreen = val.toBool();
info(QString("No loading screen: %1").arg(m_configItems.styleNoLoadingScreen), GeneralLogger::STEP); info(QString("No loading screen: %1").arg(m_styleConfig.noLoadingScreen), GeneralLogger::STEP);
} }
}}, }},
{"sort.type", "type", [this](const QJsonValue &val) { {"sort.type", "type", [this](const QJsonValue &val) {
if (val.isString()) { if (val.isString()) {
QString type = val.toString().toLower(); QString type = val.toString().toLower();
if (type == "none") { if (type == "none") {
m_configItems.sortType = SortType::None; m_sortConfig.type = SortType::None;
} else if (type == "name") { } else if (type == "name") {
m_configItems.sortType = SortType::Name; m_sortConfig.type = SortType::Name;
} else if (type == "date") { } else if (type == "date") {
m_configItems.sortType = SortType::Date; m_sortConfig.type = SortType::Date;
} else if (type == "size") { } else if (type == "size") {
m_configItems.sortType = SortType::Size; m_sortConfig.type = SortType::Size;
} else { } else {
warn(QString("Unknown sort type: %1").arg(type), GeneralLogger::STEP); warn(QString("Unknown sort type: %1").arg(type), GeneralLogger::STEP);
} }
} }
info(QString("Sort type: %1").arg(static_cast<int>(m_configItems.sortType)), GeneralLogger::STEP); info(QString("Sort type: %1").arg(static_cast<int>(m_sortConfig.type)), GeneralLogger::STEP);
}}, }},
{"sort.reverse", "reverse", [this](const QJsonValue &val) { {"sort.reverse", "reverse", [this](const QJsonValue &val) {
if (val.isBool()) { if (val.isBool()) {
m_configItems.sortReverse = val.toBool(); m_sortConfig.reverse = val.toBool();
info(QString("Sort reverse: %1").arg(m_configItems.sortReverse), GeneralLogger::STEP); info(QString("Sort reverse: %1").arg(m_sortConfig.reverse), GeneralLogger::STEP);
} }
}}, }},
}; };
@@ -158,34 +175,18 @@ void Config::_loadConfig(const QString &configPath) {
} }
} }
const QString Config::s_DefaultConfigFileName = "config.json";
Config::Config(const QString &configDir, const QStringList &searchDirs, QObject *parent) : QObject(parent) {
info(QString("Loading configuration from: %1").arg(configDir));
_loadConfig(configDir + QDir::separator() + s_DefaultConfigFileName);
info(QString("Additional search directories: %1").arg(searchDirs.join(", ")));
m_configItems.wallpaperDirs.append(searchDirs);
info("Loading wallpapers ...");
_loadWallpapers();
}
Config::~Config() {
}
void Config::_loadWallpapers() { void Config::_loadWallpapers() {
m_wallpapers.clear(); m_wallpapers.clear();
QSet<QString> paths; QSet<QString> paths;
info(QString("Loading wallpapers from %1 specified paths").arg(m_configItems.wallpaperPaths.size()), LogIndent::STEP); info(QString("Loading wallpapers from %1 specified paths").arg(m_wallpaperConfig.paths.size()), LogIndent::STEP);
for (const QString &path : m_configItems.wallpaperPaths) { for (const QString &path : m_wallpaperConfig.paths) {
paths.insert(path); paths.insert(path);
} }
info(QString("Loading wallpapers from %1 specified directories").arg(m_configItems.wallpaperDirs.size()), LogIndent::STEP); info(QString("Loading wallpapers from %1 specified directories").arg(m_wallpaperConfig.dirs.size()), LogIndent::STEP);
for (const QString &dirPath : m_configItems.wallpaperDirs) { for (const QString &dirPath : m_wallpaperConfig.dirs) {
QDir dir(dirPath); QDir dir(dirPath);
if (dir.exists()) { if (dir.exists()) {
QStringList files = dir.entryList(QDir::Files | QDir::NoDotAndDotDot); QStringList files = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
@@ -198,8 +199,8 @@ void Config::_loadWallpapers() {
} }
} }
info(QString("Excluding %1 specified paths").arg(m_configItems.wallpaperExcludes.size()), LogIndent::STEP); info(QString("Excluding %1 specified paths").arg(m_wallpaperConfig.excludes.size()), LogIndent::STEP);
for (const QString &exclude : m_configItems.wallpaperExcludes) { for (const QString &exclude : m_wallpaperConfig.excludes) {
paths.remove(exclude); paths.remove(exclude);
} }
+35 -31
View File
@@ -1,7 +1,7 @@
/* /*
* @Author: Uyanide pywang0608@foxmail.com * @Author: Uyanide pywang0608@foxmail.com
* @Date: 2025-08-05 01:34:52 * @Date: 2025-08-05 01:34:52
* @LastEditTime: 2025-08-07 22:03:05 * @LastEditTime: 2025-08-07 22:28:11
* @Description: Configuration manager. * @Description: Configuration manager.
*/ */
#ifndef CONFIG_H #ifndef CONFIG_H
@@ -22,6 +22,30 @@ class Config : public QObject {
Size, Size,
}; };
struct WallpaperConfigItems {
QStringList paths;
QStringList dirs;
QStringList excludes;
};
struct ActionConfigItems {
QString confirm;
};
struct StyleConfigItems {
double aspectRatio = 1.6;
int imageWidth = 320;
int imageFocusWidth = 480;
int windowWidth = 750;
int windowHeight = 500;
bool noLoadingScreen = false;
};
struct SortConfigItems {
SortType type = SortType::Name;
bool reverse = false;
};
Config(const QString& configDir, const QStringList& searchDirs = {}, QObject* parent = nullptr); Config(const QString& configDir, const QStringList& searchDirs = {}, QObject* parent = nullptr);
~Config(); ~Config();
@@ -32,46 +56,26 @@ class Config : public QObject {
[[nodiscard]] qint64 getWallpaperCount() const { return m_wallpapers.size(); } [[nodiscard]] qint64 getWallpaperCount() const { return m_wallpapers.size(); }
[[nodiscard]] const QString& getActionsConfirm() const { return m_configItems.actionsConfirm; } [[nodiscard]] const WallpaperConfigItems& getWallpaperConfig() const { return m_wallpaperConfig; }
[[nodiscard]] double getStyleAspectRatio() const { return m_configItems.styleAspectRatio; } [[nodiscard]] const ActionConfigItems& getActionConfig() const { return m_actionConfig; }
[[nodiscard]] int getStyleImageWidth() const { return m_configItems.styleImageWidth; } [[nodiscard]] const StyleConfigItems& getStyleConfig() const { return m_styleConfig; }
[[nodiscard]] int getStyleImageFocusWidth() const { return m_configItems.styleImageFocusWidth; } [[nodiscard]] const SortConfigItems& getSortConfig() const { return m_sortConfig; }
[[nodiscard]] int getStyleWindowWidth() const { return m_configItems.styleWindowWidth; }
[[nodiscard]] int getStyleWindowHeight() const { return m_configItems.styleWindowHeight; }
[[nodiscard]] bool isStyleNoLoadingScreen() const { return m_configItems.styleNoLoadingScreen; }
[[nodiscard]] SortType getSortType() const { return m_configItems.sortType; }
[[nodiscard]] bool isSortReverse() const { return m_configItems.sortReverse; }
static const QString s_DefaultConfigFileName; static const QString s_DefaultConfigFileName;
const QString m_configDir;
private: private:
void void _loadConfig(const QString& configPath);
_loadConfig(const QString& configPath);
void _loadWallpapers(); void _loadWallpapers();
private: private:
struct _ConfigItems { WallpaperConfigItems m_wallpaperConfig;
QStringList wallpaperPaths; ActionConfigItems m_actionConfig;
QStringList wallpaperDirs; StyleConfigItems m_styleConfig;
QStringList wallpaperExcludes; SortConfigItems m_sortConfig;
QString actionsConfirm;
double styleAspectRatio = 1.6;
int styleImageWidth = 320;
int styleImageFocusWidth = 480;
int styleWindowWidth = 720;
int styleWindowHeight = 500;
bool styleNoLoadingScreen = false;
SortType sortType = SortType::None;
bool sortReverse = false;
} m_configItems;
QStringList m_wallpapers; QStringList m_wallpapers;
}; };
+9 -12
View File
@@ -1,7 +1,7 @@
/* /*
* @Author: Uyanide pywang0608@foxmail.com * @Author: Uyanide pywang0608@foxmail.com
* @Date: 2025-08-05 01:22:53 * @Date: 2025-08-05 01:22:53
* @LastEditTime: 2025-08-07 22:06:44 * @LastEditTime: 2025-08-07 22:17:41
* @Description: Animated carousel widget for displaying and selecting images. * @Description: Animated carousel widget for displaying and selecting images.
*/ */
#include "images_carousel.h" #include "images_carousel.h"
@@ -25,20 +25,17 @@
using namespace GeneralLogger; using namespace GeneralLogger;
ImagesCarousel::ImagesCarousel(const double itemAspectRatio, ImagesCarousel::ImagesCarousel(const Config::StyleConfigItems& styleConfig,
const int itemWidth, const Config::SortConfigItems& sortConfig,
const int itemFocusWidth,
const Config::SortType sortType,
const bool sortReverse,
QWidget* parent) QWidget* parent)
: QWidget(parent), : QWidget(parent),
ui(new Ui::ImagesCarousel), ui(new Ui::ImagesCarousel),
m_itemWidth(itemWidth), m_itemWidth(styleConfig.imageWidth),
m_itemHeight(static_cast<int>(itemWidth / itemAspectRatio)), m_itemHeight(static_cast<int>(m_itemWidth / styleConfig.aspectRatio)),
m_itemFocusWidth(itemFocusWidth), m_itemFocusWidth(styleConfig.imageFocusWidth),
m_itemFocusHeight(static_cast<int>(itemFocusWidth / itemAspectRatio)), m_itemFocusHeight(static_cast<int>(styleConfig.imageFocusWidth / styleConfig.aspectRatio)),
m_sortType(sortType), m_sortType(sortConfig.type),
m_sortReverse(sortReverse) { m_sortReverse(sortConfig.reverse) {
ui->setupUi(this); ui->setupUi(this);
m_scrollArea = dynamic_cast<ImagesCarouselScrollArea*>(ui->scrollArea); m_scrollArea = dynamic_cast<ImagesCarouselScrollArea*>(ui->scrollArea);
+3 -6
View File
@@ -1,7 +1,7 @@
/* /*
* @Author: Uyanide pywang0608@foxmail.com * @Author: Uyanide pywang0608@foxmail.com
* @Date: 2025-08-05 01:22:53 * @Date: 2025-08-05 01:22:53
* @LastEditTime: 2025-08-07 00:52:14 * @LastEditTime: 2025-08-07 22:16:37
* @Description: Animated carousel widget for displaying and selecting images. * @Description: Animated carousel widget for displaying and selecting images.
*/ */
#ifndef IMAGES_CAROUSEL_H #ifndef IMAGES_CAROUSEL_H
@@ -111,11 +111,8 @@ class ImagesCarousel : public QWidget {
friend void ImageLoader::run(); friend void ImageLoader::run();
public: public:
explicit ImagesCarousel(const double itemAspectRatio, explicit ImagesCarousel(const Config::StyleConfigItems& styleConfig,
const int itemWidth, const Config::SortConfigItems& sortConfig,
const int itemFocusWidth,
const Config::SortType sortType,
const bool sortReverse,
QWidget* parent = nullptr); QWidget* parent = nullptr);
~ImagesCarousel(); ~ImagesCarousel();
+4 -2
View File
@@ -1,11 +1,13 @@
/* /*
* @Author: Uyanide pywang0608@foxmail.com * @Author: Uyanide pywang0608@foxmail.com
* @Date: 2025-08-07 01:12:37 * @Date: 2025-08-07 01:12:37
* @LastEditTime: 2025-08-07 21:21:45 * @LastEditTime: 2025-08-07 22:24:27
* @Description: Implementation of logger. * @Description: Implementation of logger.
*/ */
#include "logger.h" #include "logger.h"
#include <qnamespace.h>
#ifndef GENERAL_LOGGER_DISABLED #ifndef GENERAL_LOGGER_DISABLED
#include <unistd.h> #include <unistd.h>
@@ -40,7 +42,7 @@ Logger::Logger(FILE* stream,
&Logger::logSig, &Logger::logSig,
this, this,
&Logger::_log, &Logger::_log,
Qt::QueuedConnection); Qt::AutoConnection);
} }
void Logger::_log( void Logger::_log(
+6 -1
View File
@@ -1,7 +1,7 @@
/* /*
* @Author: Uyanide pywang0608@foxmail.com * @Author: Uyanide pywang0608@foxmail.com
* @Date: 2025-08-05 00:37:58 * @Date: 2025-08-05 00:37:58
* @LastEditTime: 2025-08-07 21:38:26 * @LastEditTime: 2025-08-07 22:21:29
* @Description: Entry point. * @Description: Entry point.
*/ */
#include <qapplication.h> #include <qapplication.h>
@@ -12,6 +12,7 @@
#include <QTextStream> #include <QTextStream>
#include "config.h" #include "config.h"
#include "logger.h"
#include "main_window.h" #include "main_window.h"
static QString getConfigDir() { static QString getConfigDir() {
@@ -26,6 +27,10 @@ static QString getConfigDir() {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
QApplication a(argc, argv); QApplication a(argc, argv);
#ifndef GENERAL_LOGGER_DISABLED
Logger::instance(stderr, GeneralLogger::LogIndent::DETAIL, &a);
#endif // GENERAL_LOGGER_DISABLED
Config config(getConfigDir()); Config config(getConfigDir());
MainWindow w(config); MainWindow w(config);
+7 -10
View File
@@ -1,7 +1,7 @@
/* /*
* @Author: Uyanide pywang0608@foxmail.com * @Author: Uyanide pywang0608@foxmail.com
* @Date: 2025-08-05 00:37:58 * @Date: 2025-08-05 00:37:58
* @LastEditTime: 2025-08-07 22:03:58 * @LastEditTime: 2025-08-07 22:15:47
* @Description: MainWindow implementation. * @Description: MainWindow implementation.
*/ */
#include "main_window.h" #include "main_window.h"
@@ -36,11 +36,8 @@ void MainWindow::_setupUI() {
// create images carousel // create images carousel
m_carousel = new ImagesCarousel( m_carousel = new ImagesCarousel(
m_config.getStyleAspectRatio(), m_config.getStyleConfig(),
m_config.getStyleImageWidth(), m_config.getSortConfig(),
m_config.getStyleImageFocusWidth(),
m_config.getSortType(),
m_config.isSortReverse(),
this); this);
ui->mainLayout->insertWidget(2, m_carousel); ui->mainLayout->insertWidget(2, m_carousel);
connect(m_carousel, connect(m_carousel,
@@ -66,8 +63,8 @@ void MainWindow::_setupUI() {
m_loadingIndicatorIndex = ui->stackedWidget->addWidget(m_loadingIndicator); m_loadingIndicatorIndex = ui->stackedWidget->addWidget(m_loadingIndicator);
// set window size // set window size
setMinimumSize(m_config.getStyleWindowWidth(), m_config.getStyleWindowHeight()); setMinimumSize(m_config.getStyleConfig().windowWidth, m_config.getStyleConfig().windowHeight);
setMaximumSize(m_config.getStyleWindowWidth(), m_config.getStyleWindowHeight()); setMaximumSize(m_config.getStyleConfig().windowWidth, m_config.getStyleConfig().windowHeight);
connect(ui->confirmButton, connect(ui->confirmButton,
&QPushButton::clicked, &QPushButton::clicked,
@@ -115,7 +112,7 @@ void MainWindow::onConfirm() {
return; return;
} }
info(QString("Selected image: %1").arg(path)); info(QString("Selected image: %1").arg(path));
const auto cmdOrig = m_config.getActionsConfirm(); const auto cmdOrig = m_config.getActionConfig().confirm;
if (cmdOrig.isEmpty()) { if (cmdOrig.isEmpty()) {
warn("No action defined for confirmation"); warn("No action defined for confirmation");
return; return;
@@ -140,7 +137,7 @@ void MainWindow::_onImageFocused(const QString &path, const int index, const int
} }
void MainWindow::_onLoadingStarted(const qsizetype amount) { void MainWindow::_onLoadingStarted(const qsizetype amount) {
if (m_config.isStyleNoLoadingScreen()) { if (m_config.getStyleConfig().noLoadingScreen) {
return; return;
} }
m_loadingIndicator->setMaximum(amount); m_loadingIndicator->setMaximum(amount);