From db09dc43f24bbc87e2e05e1a790e7bc4abf2087c Mon Sep 17 00:00:00 2001 From: Manuel Date: Thu, 13 Feb 2020 21:52:17 +0100 Subject: [PATCH] split switcher and traverser to prepare for shuffle mode --- src/imageselector.cpp | 50 ++++++++++--------------- src/imageselector.h | 22 ++++++++--- src/imageswitcher.cpp | 6 +-- src/imageswitcher.h | 5 ++- src/main.cpp | 85 +++++++++++++++++++++++++------------------ src/pathtraverser.cpp | 55 ++++++++++++++++++++++++++++ src/pathtraverser.h | 41 +++++++++++++++++++++ src/slide.pro | 2 + 8 files changed, 189 insertions(+), 77 deletions(-) create mode 100644 src/pathtraverser.cpp create mode 100644 src/pathtraverser.h diff --git a/src/imageselector.cpp b/src/imageselector.cpp index 5f9261b..c9e1b9d 100644 --- a/src/imageselector.cpp +++ b/src/imageselector.cpp @@ -1,4 +1,5 @@ #include "imageselector.h" +#include "pathtraverser.h" #include "mainwindow.h" #include #include @@ -8,31 +9,30 @@ #include /* srand, rand */ #include /* time */ -ImageSelector::ImageSelector(std::string path, bool recursive): - path(path), - recursive(recursive) + +ImageSelector::ImageSelector(std::unique_ptr& pathTraverser): + pathTraverser(pathTraverser) +{ +} + +ImageSelector::~ImageSelector(){} + +DefaultImageSelector::DefaultImageSelector(std::unique_ptr& pathTraverser): + ImageSelector(pathTraverser) { srand (time(NULL)); } -std::string ImageSelector::getNextImage() const +DefaultImageSelector::~DefaultImageSelector(){} + +std::string DefaultImageSelector::getNextImage() const { - QDir directory(path.c_str()); std:: string filename; try { - if (recursive) - { - QStringList images = listImagesRecursive(); - unsigned int selectedImage = selectRandom(images); - filename = images.at(selectedImage).toStdString(); - } - else - { - QStringList images = directory.entryList(QStringList() << "*.jpg" << "*.JPG", QDir::Files); - unsigned int selectedImage = selectRandom(images); - filename = directory.filePath(images.at(selectedImage)).toStdString(); - } + QStringList images = pathTraverser->getImages(); + unsigned int selectedImage = selectRandom(images); + filename = pathTraverser->getImagePath(images.at(selectedImage).toStdString()); } catch(const std::string& err) { @@ -42,24 +42,12 @@ std::string ImageSelector::getNextImage() const return filename; } -unsigned int ImageSelector::selectRandom(const QStringList& images) const +unsigned int DefaultImageSelector::selectRandom(const QStringList& images) const { std::cout << "images: " << images.size() << std::endl; if (images.size() == 0) { - throw std::string("No jpg images found in folder " + path); + throw std::string("No jpg images found in given folder"); } return rand() % images.size(); } - - -QStringList ImageSelector::listImagesRecursive() const -{ - QDirIterator it(QString(path.c_str()), QStringList() << "*.jpg" << "*.JPG", QDir::Files, QDirIterator::Subdirectories); - QStringList files; - while (it.hasNext()) - { - files.append(it.next()); - } - return files; -} diff --git a/src/imageselector.h b/src/imageselector.h index 2a5b2c7..beea8c9 100644 --- a/src/imageselector.h +++ b/src/imageselector.h @@ -2,20 +2,32 @@ #define IMAGESELECTOR_H #include +#include #include class MainWindow; +class PathTraverser; + class ImageSelector { public: - ImageSelector(std::string path, bool recursive); - std::string getNextImage() const; + ImageSelector(std::unique_ptr& pathTraverser); + virtual ~ImageSelector(); + virtual std::string getNextImage() const = 0; + +protected: + std::unique_ptr& pathTraverser; +}; + +class DefaultImageSelector : public ImageSelector +{ +public: + DefaultImageSelector(std::unique_ptr& pathTraverser); + virtual ~DefaultImageSelector(); + virtual std::string getNextImage() const; private: - QStringList listImagesRecursive() const; unsigned int selectRandom(const QStringList& images) const; - std::string path; - bool recursive; }; #endif // IMAGESELECTOR_H diff --git a/src/imageswitcher.cpp b/src/imageswitcher.cpp index ea9753a..e64979d 100644 --- a/src/imageswitcher.cpp +++ b/src/imageswitcher.cpp @@ -4,12 +4,12 @@ #include #include #include -#include #include +#include #include /* srand, rand */ #include /* time */ -ImageSwitcher::ImageSwitcher(MainWindow& w, unsigned int timeout, const ImageSelector& selector): +ImageSwitcher::ImageSwitcher(MainWindow& w, unsigned int timeout, std::unique_ptr& selector): QObject::QObject(), window(w), timeout(timeout), @@ -20,7 +20,7 @@ ImageSwitcher::ImageSwitcher(MainWindow& w, unsigned int timeout, const ImageSel void ImageSwitcher::updateImage() { - std::string filename(selector.getNextImage()); + std::string filename(selector->getNextImage()); if (filename == "") { window.warn("No image found."); diff --git a/src/imageswitcher.h b/src/imageswitcher.h index 8c6b027..d49d3ad 100644 --- a/src/imageswitcher.h +++ b/src/imageswitcher.h @@ -4,6 +4,7 @@ #include #include #include +#include class MainWindow; class ImageSelector; @@ -11,7 +12,7 @@ class ImageSwitcher : public QObject { Q_OBJECT public: - ImageSwitcher(MainWindow& w, unsigned int timeout, const ImageSelector& selector); + ImageSwitcher(MainWindow& w, unsigned int timeout, std::unique_ptr& selector); void start(); public slots: @@ -19,7 +20,7 @@ public slots: private: MainWindow& window; unsigned int timeout; - const ImageSelector& selector; + std::unique_ptr& selector; QTimer timer; }; diff --git a/src/main.cpp b/src/main.cpp index a922eab..8757bc4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,7 @@ #include "mainwindow.h" #include "imageselector.h" #include "imageswitcher.h" +#include "pathtraverser.h" #include #include #include @@ -9,6 +10,7 @@ #include #include #include +#include void usage(std::string programName) { std::cerr << "Usage: " << programName << " [-t rotation_seconds] [-o background_opacity(0..255)] [-b blur_radius] -p image_folder -r" << std::endl; @@ -16,46 +18,57 @@ void usage(std::string programName) { int main(int argc, char *argv[]) { - unsigned int rotationSeconds = 30; - std::string path = ""; + unsigned int rotationSeconds = 30; + std::string path = ""; - QApplication a(argc, argv); + QApplication a(argc, argv); - MainWindow w; - int opt; - bool recursive = false; - while ((opt = getopt(argc, argv, "b:p:t:o:r")) != -1) { - switch (opt) { - case 'p': - path = optarg; - break; - case 't': - rotationSeconds = atoi(optarg); - break; - case 'b': - w.setBlurRadius(atoi(optarg)); - break; - case 'o': - w.setBackgroundOpacity(atoi(optarg)); - break; - case 'r': - recursive = true; - break; - default: /* '?' */ - usage(argv[0]); - return 1; - } - } - - if (path.empty()) { - std::cout << "Error: Path expected." << std::endl; + MainWindow w; + int opt; + bool recursive = false; + while ((opt = getopt(argc, argv, "b:p:t:o:r")) != -1) { + switch (opt) { + case 'p': + path = optarg; + break; + case 't': + rotationSeconds = atoi(optarg); + break; + case 'b': + w.setBlurRadius(atoi(optarg)); + break; + case 'o': + w.setBackgroundOpacity(atoi(optarg)); + break; + case 'r': + recursive = true; + break; + default: /* '?' */ usage(argv[0]); return 1; } - w.show(); + } - ImageSelector selector(path, recursive); - ImageSwitcher switcher(w, rotationSeconds * 1000, selector); - switcher.start(); - return a.exec(); + if (path.empty()) + { + std::cout << "Error: Path expected." << std::endl; + usage(argv[0]); + return 1; + } + + std::unique_ptr pathTraverser; + if (recursive) + { + pathTraverser = std::unique_ptr(new RecursivePathTraverser(path)); + } else { + pathTraverser = std::unique_ptr(new DefaultPathTraverser(path)); + } + + std::unique_ptr selector = std::unique_ptr(new DefaultImageSelector(pathTraverser)); + + w.show(); + + ImageSwitcher switcher(w, rotationSeconds * 1000, selector); + switcher.start(); + return a.exec(); } diff --git a/src/pathtraverser.cpp b/src/pathtraverser.cpp new file mode 100644 index 0000000..5686e25 --- /dev/null +++ b/src/pathtraverser.cpp @@ -0,0 +1,55 @@ +#include "pathtraverser.h" +#include "mainwindow.h" +#include +#include +#include +#include +#include +#include /* srand, rand */ + +PathTraverser::PathTraverser(const std::string path): + path(path) +{} + +PathTraverser::~PathTraverser() {} + +RecursivePathTraverser::RecursivePathTraverser(const std::string path): + PathTraverser(path) +{} + +RecursivePathTraverser::~RecursivePathTraverser() {} + + +QStringList RecursivePathTraverser::getImages() const +{ + QDirIterator it(QString(path.c_str()), QStringList() << "*.jpg" << "*.JPG", QDir::Files, QDirIterator::Subdirectories); + QStringList files; + while (it.hasNext()) + { + files.append(it.next()); + } + return files; +} + +const std::string RecursivePathTraverser::getImagePath(const std::string image) const +{ + return image; +} + +DefaultPathTraverser::DefaultPathTraverser(const std::string path): + PathTraverser(path), + directory(path.c_str()) +{} + +DefaultPathTraverser::~DefaultPathTraverser() {} + + +QStringList DefaultPathTraverser::getImages() const +{ + return directory.entryList(QStringList() << "*.jpg" << "*.JPG", QDir::Files); +} + +const std::string DefaultPathTraverser::getImagePath(const std::string image) const +{ + return directory.filePath(QString(image.c_str())).toStdString(); +} diff --git a/src/pathtraverser.h b/src/pathtraverser.h new file mode 100644 index 0000000..d400ce8 --- /dev/null +++ b/src/pathtraverser.h @@ -0,0 +1,41 @@ +#ifndef PATHTRAVERSER_H +#define PATHTRAVERSER_H + +#include +#include +#include + +class MainWindow; +class PathTraverser +{ + public: + PathTraverser(const std::string path); + virtual ~PathTraverser(); + virtual QStringList getImages() const = 0; + virtual const std::string getImagePath(const std::string image) const = 0; + + protected: + const std::string path; +}; + +class RecursivePathTraverser : public PathTraverser +{ + public: + RecursivePathTraverser(const std::string path); + virtual ~RecursivePathTraverser(); + QStringList getImages() const; + virtual const std::string getImagePath(const std::string image) const; +}; + +class DefaultPathTraverser : public PathTraverser +{ + public: + DefaultPathTraverser(const std::string path); + virtual ~DefaultPathTraverser(); + QStringList getImages() const; + virtual const std::string getImagePath(const std::string image) const; + private: + QDir directory; +}; + +#endif // PATHTRAVERSER_H diff --git a/src/slide.pro b/src/slide.pro index 906ba94..8597bdf 100644 --- a/src/slide.pro +++ b/src/slide.pro @@ -27,11 +27,13 @@ SOURCES += \ main.cpp \ mainwindow.cpp \ imageswitcher.cpp \ + pathtraverser.cpp \ imageselector.cpp HEADERS += \ mainwindow.h \ imageselector.h \ + pathtraverser.h \ imageswitcher.h FORMS += \