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:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user