This commit is contained in:
Manuel
2020-01-26 22:11:53 +01:00
parent bc9dd90acc
commit fa25ced107
2 changed files with 25 additions and 11 deletions

View File

@@ -18,25 +18,38 @@ ImageSelector::ImageSelector(std::string path, bool recursive):
std::string ImageSelector::getNextImage() const
{
QDir directory(path.c_str());
QStringList images;
std:: string filename;
try
{
if (recursive)
{
images = listImagesRecursive();
QStringList images = listImagesRecursive();
unsigned int selectedImage = selectRandom(images);
filename = images.at(selectedImage).toStdString();
}
else
{
images = directory.entryList(QStringList() << "*.jpg" << "*.JPG", QDir::Files);
QStringList images = directory.entryList(QStringList() << "*.jpg" << "*.JPG", QDir::Files);
unsigned int selectedImage = selectRandom(images);
filename = directory.filePath(images.at(selectedImage)).toStdString();
}
}
catch(const std::string& err)
{
std::cerr << "Error: " << err << std::endl;
}
std::cout << "updating image: " << filename << std::endl;
return filename;
}
unsigned int ImageSelector::selectRandom(const QStringList& images) const
{
std::cout << "images: " << images.size() << std::endl;
if (images.size() == 0)
{
std::cerr << "No jpg images found in folder " << path << std::endl;
return "";
throw std::string("No jpg images found in folder " + path);
}
unsigned int selectedImage = rand() % images.size();
std::string filename = directory.filePath(images.at(selectedImage)).toStdString();
std::cout << "updating image: " << filename << std::endl;
return filename;
return rand() % images.size();
}

View File

@@ -13,6 +13,7 @@ public:
private:
QStringList listImagesRecursive() const;
unsigned int selectRandom(const QStringList& images) const;
std::string path;
bool recursive;
};