From ed372e3d88f1f81464b9d25cc8cf77fa4b59e34d Mon Sep 17 00:00:00 2001 From: delia Date: Sat, 23 Aug 2025 17:52:22 +0200 Subject: [PATCH] Added seperat WallpaperSettingsWidget Class --- .clangd | 3 ++ CMakeLists.txt | 2 + src/Qt/UIWindow.cpp | 64 +++++------------------------- src/Qt/UIWindow.h | 6 +-- src/Qt/WallpaperSettingsWidget.cpp | 56 ++++++++++++++++++++++++++ src/Qt/WallpaperSettingsWidget.h | 23 +++++++++++ 6 files changed, 95 insertions(+), 59 deletions(-) create mode 100644 .clangd create mode 100644 src/Qt/WallpaperSettingsWidget.cpp create mode 100644 src/Qt/WallpaperSettingsWidget.h diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..f9f1e05 --- /dev/null +++ b/.clangd @@ -0,0 +1,3 @@ +CompileFlags: + Remove: [-mono-direct-extern-access] + Add: [-std=c++20] diff --git a/CMakeLists.txt b/CMakeLists.txt index 723c299..f4079e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -263,6 +263,8 @@ add_executable( src/Qt/SingleInstanceManager.cpp src/Qt/WallpaperButton.h src/Qt/WallpaperButton.cpp + src/Qt/WallpaperSettingsWidget.h + src/Qt/WallpaperSettingsWidget.cpp src/External/MimeTypes/MimeTypes.cpp src/External/MimeTypes/MimeTypes.h diff --git a/src/Qt/UIWindow.cpp b/src/Qt/UIWindow.cpp index e091ae1..2ecfe5d 100644 --- a/src/Qt/UIWindow.cpp +++ b/src/Qt/UIWindow.cpp @@ -3,10 +3,7 @@ #include "Qt/WallpaperButton.h" #include #include -#include #include -#include -#include #include #include #include @@ -35,6 +32,7 @@ #include #include #include +#include "Qt/WallpaperSettingsWidget.h" #include "WallpaperButton.h" #define PICTURE_SIZE 128 @@ -46,6 +44,8 @@ UIWindow::UIWindow(QWidget* parent, QApplication* qapp, SingleInstanceManager* i this->wallpaperEngine = new QProcess(this); this->instanceGuard = ig; this->buttonLayout = new QGridLayout(this); + + this->wallpaperSettingsWidget = nullptr; } void UIWindow::setupUIWindow(std::vector wallpaperPaths) { @@ -77,10 +77,10 @@ void UIWindow::setupUIWindow(std::vector wallpaperPaths) { startNewWallpaperEngine(); - QObject::connect(wallpaperEngine, &QProcess::started, button, [=]() { + QObject::connect(wallpaperEngine, &QProcess::started, button, [this, button]() { button->setEnabled(true); updateSelectedButton(); - updateConfigLayout(); + this->wallpaperSettingsWidget->update(this->selectedWallpapers[this->screenSelector->currentText().toStdString()]); }); }); @@ -122,7 +122,7 @@ void UIWindow::setupUIWindow(std::vector wallpaperPaths) { QObject::connect(this->screenSelector, QOverload::of(&QComboBox::currentIndexChanged), [this](int index) { updateSelectedButton(); - updateConfigLayout(); + this->wallpaperSettingsWidget->update(this->selectedWallpapers[this->screenSelector->currentText().toStdString()]); }); auto* screenSelectorLayout = new QVBoxLayout(); @@ -154,19 +154,10 @@ void UIWindow::setupUIWindow(std::vector wallpaperPaths) { leftWidget->setLayout(leftLayout); // right side - auto* rightWidget = new QWidget(splitWidget); - auto* rightLayout = new QVBoxLayout(rightWidget); - this->previewTitleLabel = new QLabel("...", rightWidget); - this->previewTitleLabel->setAlignment(Qt::AlignTop); - this->previewImageLabel = new QLabel(rightWidget); - this->previewImageLabel->setFixedSize(256, 256); - this->previewImageLabel->setAlignment(Qt::AlignCenter); - rightLayout->addWidget(previewImageLabel); - rightLayout->addWidget(previewTitleLabel); - rightWidget->setLayout(rightLayout); + this->wallpaperSettingsWidget = new WallpaperSettingsWidget(splitWidget); splitLayout->addWidget(leftWidget, 2); - splitLayout->addWidget(rightWidget, 1); + splitLayout->addWidget(this->wallpaperSettingsWidget, 1); mainlayout->addWidget(splitWidget); mainlayout->addWidget(extraFlagsInput); @@ -236,49 +227,12 @@ void UIWindow::startNewWallpaperEngine() { wallpaperEngine->start(QCoreApplication::applicationFilePath(), args); } -void UIWindow::updateConfigLayout() { - std::string selected = this->selectedWallpapers[this->screenSelector->currentText().toStdString()]; - if (selected.empty()) return; - - std::ifstream file(selected + "/project.json"); - nlohmann::json wallpaperJSON = nlohmann::json::parse(file); - - if (wallpaperJSON.empty()) { - return; - } - - std::string title = wallpaperJSON.at("title"); - if (title.size() > 25) { - title = title.substr(0, 24) + ".."; - } - - QPixmap pixmap(QString::fromStdString(selected + "/preview.jpg")); - - - if (pixmap.isNull()) { - pixmap = QPixmap(256, 256); - pixmap.fill(Qt::black); - - auto* movie = new QMovie(QString::fromStdString(selected + "/preview.gif")); - if (movie->isValid()) { - movie->jumpToFrame(0); - pixmap = movie->currentPixmap().scaled(256, 256, Qt::KeepAspectRatio, Qt::SmoothTransformation); - } - delete movie; - } else pixmap = pixmap.scaled(256, 256, Qt::KeepAspectRatio, Qt::SmoothTransformation); - - // edit previewLabel - this->previewImageLabel->setPixmap(pixmap); - // edit Title - this->previewTitleLabel->setText(QString::fromStdString(title)); -} - void UIWindow::updateSelectedButton() { for (int i = 0; i < this->buttonLayout->rowCount(); i++) { for (int j = 0; j < this->buttonLayout->columnCount(); j++) { auto* item = this->buttonLayout->itemAtPosition(i, j); if (!item) continue; - + auto* widget = item->widget(); if (!widget) continue; diff --git a/src/Qt/UIWindow.h b/src/Qt/UIWindow.h index 8bd9ced..57a242a 100644 --- a/src/Qt/UIWindow.h +++ b/src/Qt/UIWindow.h @@ -29,9 +29,9 @@ #include #include #include -#include #include #include +#include "Qt/WallpaperSettingsWidget.h" class UIWindow : public QWidget { Q_OBJECT @@ -47,8 +47,7 @@ class UIWindow : public QWidget { QComboBox* screenSelector; QLineEdit* extraFlagsInput; QGridLayout* buttonLayout; - QLabel* previewImageLabel; - QLabel* previewTitleLabel; + WallpaperSettingsWidget* wallpaperSettingsWidget; // Important Fields std::map selectedWallpapers; @@ -57,7 +56,6 @@ class UIWindow : public QWidget { void startNewWallpaperEngine(); void updateSelectedButton(); - void updateConfigLayout(); static std::vector split(const std::string &str, char r); protected: diff --git a/src/Qt/WallpaperSettingsWidget.cpp b/src/Qt/WallpaperSettingsWidget.cpp new file mode 100644 index 0000000..c68317e --- /dev/null +++ b/src/Qt/WallpaperSettingsWidget.cpp @@ -0,0 +1,56 @@ +#include "WallpaperSettingsWidget.h" +#include +#include +#include +#include +#include + +WallpaperSettingsWidget::WallpaperSettingsWidget(QWidget* parent) + : QWidget(parent) { + + preview.title = new QLabel("...", this); + preview.title->setAlignment(Qt::AlignTop); + preview.image = new QLabel(this); + preview.image->setFixedSize(256, 256); + preview.image->setAlignment(Qt::AlignCenter); + + layout->addWidget(preview.image); + layout->addWidget(preview.title); + + setLayout(layout); +} + +void WallpaperSettingsWidget::update(const std::string& selected) { + if (selected.empty()) return; + + std::ifstream file(selected + "/project.json"); + nlohmann::json wallpaperJSON = nlohmann::json::parse(file); + + if (wallpaperJSON.empty()) { + return; + } + + std::string title = wallpaperJSON.at("title"); + if (title.size() > 25) { + title = title.substr(0, 24) + ".."; + } + + QPixmap pixmap(QString::fromStdString(selected + "/preview.jpg")); + + if (pixmap.isNull()) { + pixmap = QPixmap(256, 256); + pixmap.fill(Qt::black); + + auto* movie = new QMovie(QString::fromStdString(selected + "/preview.gif")); + if (movie->isValid()) { + movie->jumpToFrame(0); + pixmap = movie->currentPixmap().scaled(256, 256, Qt::KeepAspectRatio, Qt::SmoothTransformation); + } + delete movie; + } else pixmap = pixmap.scaled(256, 256, Qt::KeepAspectRatio, Qt::SmoothTransformation); + + // edit previewLabel + preview.image->setPixmap(pixmap); + // edit Title + preview.title->setText(QString::fromStdString(title)); +} diff --git a/src/Qt/WallpaperSettingsWidget.h b/src/Qt/WallpaperSettingsWidget.h new file mode 100644 index 0000000..0edfaba --- /dev/null +++ b/src/Qt/WallpaperSettingsWidget.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include +#include +#include + +struct Preview { + QLabel* image; + QLabel* title; +}; + +class WallpaperSettingsWidget : public QWidget { + public: + explicit WallpaperSettingsWidget(QWidget* parent = nullptr); + void update(const std::string& currentWallpaperPath); + + private: + QVBoxLayout* layout = new QVBoxLayout(this); + Preview preview = {nullptr, nullptr}; + + protected: +};