From 05e5bb946017a16f562139187a749c6af283c406 Mon Sep 17 00:00:00 2001 From: Manuel Dewald Date: Thu, 13 Aug 2020 17:09:17 +0200 Subject: [PATCH] sorted mode --- README.md | 1 + src/imageselector.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++ src/imageselector.h | 10 +++++++++ src/main.cpp | 12 +++++++++-- 4 files changed, 70 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1bedc3c..27309b4 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ slide [-t rotation_seconds] [-o background_opacity(0..255)] [-b blur_radius] -p * `image_folder`: where to search for images (.jpg files) * `-r` for recursive traversal of `image_folder` * `-s` for shuffle instead of random image rotation +* `-S` for sorted rotation (files ordered by name, first images then subfolders) * `rotation_seconds(default=30)`: time until next random image is chosen from the given folder * `background_opacity(default=150)`: opacity of the background filling image between 0 (black background) and 255 * `blur_radius(default=20)`: blur radius of the background filling image diff --git a/src/imageselector.cpp b/src/imageselector.cpp index 12f68a0..3d082d6 100644 --- a/src/imageselector.cpp +++ b/src/imageselector.cpp @@ -91,3 +91,52 @@ std::string ShuffleImageSelector::getNextImage() current_image_shuffle = current_image_shuffle + 1; return filename; } + +SortedImageSelector::SortedImageSelector(std::unique_ptr& pathTraverser): + ImageSelector(pathTraverser), + images() +{ + srand (time(NULL)); +} + +SortedImageSelector::~SortedImageSelector() +{ +} + +bool operator<(const QString& lhs, const QString& rhs) noexcept{ + if (lhs.count(QLatin1Char('/')) < rhs.count(QLatin1Char('/'))) { + return true; + } + if (lhs.count(QLatin1Char('/')) > rhs.count(QLatin1Char('/'))) { + return false; + } + + return lhs.toStdString() < rhs.toStdString(); + +} + +std::string SortedImageSelector::getNextImage() +{ + if (images.size() == 0) + { + images = pathTraverser->getImages(); + std::sort(images.begin(), images.end()); + std::cout << "read " << images.size() << " images." << std::endl; + for (int i = 0;i getImagePath(images.takeFirst().toStdString()); + if(!QFileInfo::exists(QString(filename.c_str()))) + { + std::cout << "file not found: " << filename << std::endl; + return getNextImage(); + } + std::cout << "updating image: " << filename << std::endl; + return filename; +} diff --git a/src/imageselector.h b/src/imageselector.h index b01d662..dc73b83 100644 --- a/src/imageselector.h +++ b/src/imageselector.h @@ -42,4 +42,14 @@ private: QStringList images; }; +class SortedImageSelector : public ImageSelector +{ +public: + SortedImageSelector(std::unique_ptr& pathTraverser); + virtual ~SortedImageSelector(); + virtual std::string getNextImage(); + +private: + QStringList images; +}; #endif // IMAGESELECTOR_H diff --git a/src/main.cpp b/src/main.cpp index 6bb3cfd..a176639 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,7 +27,8 @@ int main(int argc, char *argv[]) int opt; bool recursive = false; bool shuffle = false; - while ((opt = getopt(argc, argv, "b:p:t:o:rs")) != -1) { + bool sorted = false; + while ((opt = getopt(argc, argv, "b:p:t:o:rsS")) != -1) { switch (opt) { case 'p': path = optarg; @@ -48,6 +49,9 @@ int main(int argc, char *argv[]) shuffle = true; std::cout << "Shuffle mode is on." << std::endl; break; + case 'S': + sorted = true; + break; default: /* '?' */ usage(argv[0]); return 1; @@ -72,7 +76,11 @@ int main(int argc, char *argv[]) } std::unique_ptr selector; - if (shuffle) + if (sorted) + { + selector = std::unique_ptr(new SortedImageSelector(pathTraverser)); + } + else if (shuffle) { selector = std::unique_ptr(new ShuffleImageSelector(pathTraverser)); }