Recursive folders

* Allow recursive traversing of folders with -r flag
* Print useful error in case of no image found
* Fixes issues 4 & 6
This commit is contained in:
Manuel
2019-12-23 11:28:12 +01:00
parent ee7e88128a
commit 09fc431034
3 changed files with 41 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
#include "imageselector.h" #include "imageselector.h"
#include "mainwindow.h" #include "mainwindow.h"
#include <QDirIterator>
#include <QTimer> #include <QTimer>
#include <QApplication> #include <QApplication>
#include <QDir> #include <QDir>
@@ -7,11 +8,12 @@
#include <stdlib.h> /* srand, rand */ #include <stdlib.h> /* srand, rand */
#include <time.h> /* time */ #include <time.h> /* time */
ImageSelector::ImageSelector(MainWindow& w, unsigned int timeout, std::string path): ImageSelector::ImageSelector(MainWindow& w, unsigned int timeout, std::string path, bool recursive):
QObject::QObject(), QObject::QObject(),
window(w), window(w),
timeout(timeout), timeout(timeout),
path(path), path(path),
recursive(recursive),
timer(this) timer(this)
{ {
srand (time(NULL)); srand (time(NULL));
@@ -20,15 +22,41 @@ ImageSelector::ImageSelector(MainWindow& w, unsigned int timeout, std::string pa
void ImageSelector::updateImage() void ImageSelector::updateImage()
{ {
QDir directory(path.c_str()); QDir directory(path.c_str());
QStringList images = directory.entryList(QStringList() << "*.jpg" << "*.JPG",QDir::Files); QStringList images;
if (recursive)
{
images = listImagesRecursive();
}
else
{
images = directory.entryList(QStringList() << "*.jpg" << "*.JPG", QDir::Files);
}
std::cout << "images: " << images.size() << std::endl;
if (images.size() == 0)
{
std::cerr << "No jpg images found in folder " << path << std::endl;
return;
}
unsigned int selectedImage = rand() % images.size(); unsigned int selectedImage = rand() % images.size();
std::string filename = directory.filePath(images.at(selectedImage)).toStdString(); std::string filename = directory.filePath(images.at(selectedImage)).toStdString();
std::cout << "updating image: " << filename << std::endl; std::cout << "updating image: " << filename << std::endl;
window.setImage(filename); window.setImage(filename);
} }
void ImageSelector::start(){ void ImageSelector::start()
{
updateImage(); updateImage();
connect(&timer, SIGNAL(timeout()), this, SLOT(updateImage())); connect(&timer, SIGNAL(timeout()), this, SLOT(updateImage()));
timer.start(timeout); timer.start(timeout);
} }
QStringList ImageSelector::listImagesRecursive()
{
QDirIterator it(QString(path.c_str()), QStringList() << "*.jpg" << "*.JPG", QDir::Files, QDirIterator::Subdirectories);
QStringList files;
while (it.hasNext())
{
files.append(it.next());
}
return files;
}

View File

@@ -9,15 +9,17 @@ class ImageSelector : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
ImageSelector(MainWindow& w, unsigned int timeout, std::string path); ImageSelector(MainWindow& w, unsigned int timeout, std::string path, bool recursive);
void start(); void start();
public slots: public slots:
void updateImage(); void updateImage();
private: private:
QStringList listImagesRecursive();
MainWindow& window; MainWindow& window;
unsigned int timeout; unsigned int timeout;
std::string path; std::string path;
bool recursive;
QTimer timer; QTimer timer;
}; };

View File

@@ -10,7 +10,7 @@
#include <stdio.h> #include <stdio.h>
void usage(std::string programName) { void usage(std::string programName) {
std::cerr << "Usage: " << programName << " [-t rotation_seconds] [-o background_opacity(0..255)] [-b blur_radius] -p image_folder" << std::endl; std::cerr << "Usage: " << programName << " [-t rotation_seconds] [-o background_opacity(0..255)] [-b blur_radius] -p image_folder -r" << std::endl;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@@ -22,7 +22,8 @@ int main(int argc, char *argv[])
MainWindow w; MainWindow w;
int opt; int opt;
while ((opt = getopt(argc, argv, "b:p:t:o:")) != -1) { bool recursive = false;
while ((opt = getopt(argc, argv, "b:p:t:o:r")) != -1) {
switch (opt) { switch (opt) {
case 'p': case 'p':
path = optarg; path = optarg;
@@ -36,6 +37,9 @@ int main(int argc, char *argv[])
case 'o': case 'o':
w.setBackgroundOpacity(atoi(optarg)); w.setBackgroundOpacity(atoi(optarg));
break; break;
case 'r':
recursive = true;
break;
default: /* '?' */ default: /* '?' */
usage(argv[0]); usage(argv[0]);
return 1; return 1;
@@ -49,7 +53,7 @@ int main(int argc, char *argv[])
} }
w.show(); w.show();
ImageSelector is(w, rotationSeconds * 1000, path); ImageSelector is(w, rotationSeconds * 1000, path, recursive);
is.start(); is.start();
return a.exec(); return a.exec();
} }