From bc9dd90acc90d57c83b1c88ef75a62b253f1ab5e Mon Sep 17 00:00:00 2001 From: Manuel Date: Thu, 2 Jan 2020 23:33:39 +0100 Subject: [PATCH] split imageselector * image updating in seperate class ImageSwitcher * image selection in class ImageSelector --- src/imageselector.cpp | 22 ++++++---------------- src/imageselector.h | 24 +++++++++--------------- src/imageswitcher.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/imageswitcher.h | 26 ++++++++++++++++++++++++++ src/main.cpp | 6 ++++-- src/mainwindow.cpp | 6 ++++++ src/mainwindow.h | 1 + src/slide.pro | 4 +++- 8 files changed, 94 insertions(+), 34 deletions(-) create mode 100644 src/imageswitcher.cpp create mode 100644 src/imageswitcher.h diff --git a/src/imageselector.cpp b/src/imageselector.cpp index 7d5cb13..4d99215 100644 --- a/src/imageselector.cpp +++ b/src/imageselector.cpp @@ -8,18 +8,14 @@ #include /* srand, rand */ #include /* time */ -ImageSelector::ImageSelector(MainWindow& w, unsigned int timeout, std::string path, bool recursive): - QObject::QObject(), - window(w), - timeout(timeout), +ImageSelector::ImageSelector(std::string path, bool recursive): path(path), - recursive(recursive), - timer(this) + recursive(recursive) { srand (time(NULL)); } -void ImageSelector::updateImage() +std::string ImageSelector::getNextImage() const { QDir directory(path.c_str()); QStringList images; @@ -35,22 +31,16 @@ void ImageSelector::updateImage() if (images.size() == 0) { std::cerr << "No jpg images found in folder " << path << std::endl; - return; + return ""; } unsigned int selectedImage = rand() % images.size(); std::string filename = directory.filePath(images.at(selectedImage)).toStdString(); std::cout << "updating image: " << filename << std::endl; - window.setImage(filename); + return filename; } -void ImageSelector::start() -{ - updateImage(); - connect(&timer, SIGNAL(timeout()), this, SLOT(updateImage())); - timer.start(timeout); -} -QStringList ImageSelector::listImagesRecursive() +QStringList ImageSelector::listImagesRecursive() const { QDirIterator it(QString(path.c_str()), QStringList() << "*.jpg" << "*.JPG", QDir::Files, QDirIterator::Subdirectories); QStringList files; diff --git a/src/imageselector.h b/src/imageselector.h index c3b498b..788aeea 100644 --- a/src/imageselector.h +++ b/src/imageselector.h @@ -1,26 +1,20 @@ #ifndef IMAGESELECTOR_H #define IMAGESELECTOR_H -#include -#include #include -class MainWindow; -class ImageSelector : public QObject -{ - Q_OBJECT -public: - ImageSelector(MainWindow& w, unsigned int timeout, std::string path, bool recursive); - void start(); +#include + +class MainWindow; +class ImageSelector +{ +public: + ImageSelector(std::string path, bool recursive); + std::string getNextImage() const; -public slots: - void updateImage(); private: - QStringList listImagesRecursive(); - MainWindow& window; - unsigned int timeout; + QStringList listImagesRecursive() const; std::string path; bool recursive; - QTimer timer; }; #endif // IMAGESELECTOR_H diff --git a/src/imageswitcher.cpp b/src/imageswitcher.cpp new file mode 100644 index 0000000..ea9753a --- /dev/null +++ b/src/imageswitcher.cpp @@ -0,0 +1,39 @@ +#include "imageswitcher.h" +#include "imageselector.h" +#include "mainwindow.h" +#include +#include +#include +#include +#include +#include /* srand, rand */ +#include /* time */ + +ImageSwitcher::ImageSwitcher(MainWindow& w, unsigned int timeout, const ImageSelector& selector): + QObject::QObject(), + window(w), + timeout(timeout), + selector(selector), + timer(this) +{ +} + +void ImageSwitcher::updateImage() +{ + std::string filename(selector.getNextImage()); + if (filename == "") + { + window.warn("No image found."); + } + else + { + window.setImage(filename); + } +} + +void ImageSwitcher::start() +{ + updateImage(); + connect(&timer, SIGNAL(timeout()), this, SLOT(updateImage())); + timer.start(timeout); +} diff --git a/src/imageswitcher.h b/src/imageswitcher.h new file mode 100644 index 0000000..8c6b027 --- /dev/null +++ b/src/imageswitcher.h @@ -0,0 +1,26 @@ +#ifndef IMAGESWITCHER_H +#define IMAGESWITCHER_H + +#include +#include +#include + +class MainWindow; +class ImageSelector; +class ImageSwitcher : public QObject +{ + Q_OBJECT +public: + ImageSwitcher(MainWindow& w, unsigned int timeout, const ImageSelector& selector); + void start(); + +public slots: + void updateImage(); +private: + MainWindow& window; + unsigned int timeout; + const ImageSelector& selector; + QTimer timer; +}; + +#endif // IMAGESWITCHER_H diff --git a/src/main.cpp b/src/main.cpp index 6b2e342..a922eab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,6 @@ #include "mainwindow.h" #include "imageselector.h" +#include "imageswitcher.h" #include #include #include @@ -53,7 +54,8 @@ int main(int argc, char *argv[]) } w.show(); - ImageSelector is(w, rotationSeconds * 1000, path, recursive); - is.start(); + ImageSelector selector(path, recursive); + ImageSwitcher switcher(w, rotationSeconds * 1000, selector); + switcher.start(); return a.exec(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index a7c24fb..f050002 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -168,3 +168,9 @@ void MainWindow::setBackgroundOpacity(unsigned int backgroundOpacity) { this->backgroundOpacity = backgroundOpacity; } + +void MainWindow::warn(std::string text) +{ + QLabel *label = this->findChild("image"); + label->setText(text.c_str()); +} diff --git a/src/mainwindow.h b/src/mainwindow.h index cb8fc31..636ebb4 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -22,6 +22,7 @@ public: void setImage(std::string path); void setBlurRadius(unsigned int blurRadius); void setBackgroundOpacity(unsigned int opacity); + void warn(std::string text); private: Ui::MainWindow *ui; diff --git a/src/slide.pro b/src/slide.pro index e0d3bdc..906ba94 100644 --- a/src/slide.pro +++ b/src/slide.pro @@ -26,11 +26,13 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp \ mainwindow.cpp \ + imageswitcher.cpp \ imageselector.cpp HEADERS += \ mainwindow.h \ - imageselector.h + imageselector.h \ + imageswitcher.h FORMS += \ mainwindow.ui