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 "mainwindow.h"
#include <QDirIterator>
#include <QTimer>
#include <QApplication>
#include <QDir>
@@ -7,11 +8,12 @@
#include <stdlib.h> /* srand, rand */
#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(),
window(w),
timeout(timeout),
path(path),
recursive(recursive),
timer(this)
{
srand (time(NULL));
@@ -20,15 +22,41 @@ ImageSelector::ImageSelector(MainWindow& w, unsigned int timeout, std::string pa
void ImageSelector::updateImage()
{
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();
std::string filename = directory.filePath(images.at(selectedImage)).toStdString();
std::cout << "updating image: " << filename << std::endl;
window.setImage(filename);
}
void ImageSelector::start(){
void ImageSelector::start()
{
updateImage();
connect(&timer, SIGNAL(timeout()), this, SLOT(updateImage()));
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
public:
ImageSelector(MainWindow& w, unsigned int timeout, std::string path);
ImageSelector(MainWindow& w, unsigned int timeout, std::string path, bool recursive);
void start();
public slots:
void updateImage();
private:
QStringList listImagesRecursive();
MainWindow& window;
unsigned int timeout;
std::string path;
bool recursive;
QTimer timer;
};

View File

@@ -10,7 +10,7 @@
#include <stdio.h>
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[])
@@ -22,7 +22,8 @@ int main(int argc, char *argv[])
MainWindow w;
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) {
case 'p':
path = optarg;
@@ -36,6 +37,9 @@ int main(int argc, char *argv[])
case 'o':
w.setBackgroundOpacity(atoi(optarg));
break;
case 'r':
recursive = true;
break;
default: /* '?' */
usage(argv[0]);
return 1;
@@ -49,7 +53,7 @@ int main(int argc, char *argv[])
}
w.show();
ImageSelector is(w, rotationSeconds * 1000, path);
ImageSelector is(w, rotationSeconds * 1000, path, recursive);
is.start();
return a.exec();
}