split switcher and traverser to prepare for shuffle mode
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "imageselector.h"
|
||||
#include "pathtraverser.h"
|
||||
#include "mainwindow.h"
|
||||
#include <QDirIterator>
|
||||
#include <QTimer>
|
||||
@@ -8,31 +9,30 @@
|
||||
#include <stdlib.h> /* srand, rand */
|
||||
#include <time.h> /* time */
|
||||
|
||||
ImageSelector::ImageSelector(std::string path, bool recursive):
|
||||
path(path),
|
||||
recursive(recursive)
|
||||
|
||||
ImageSelector::ImageSelector(std::unique_ptr<PathTraverser>& pathTraverser):
|
||||
pathTraverser(pathTraverser)
|
||||
{
|
||||
}
|
||||
|
||||
ImageSelector::~ImageSelector(){}
|
||||
|
||||
DefaultImageSelector::DefaultImageSelector(std::unique_ptr<PathTraverser>& pathTraverser):
|
||||
ImageSelector(pathTraverser)
|
||||
{
|
||||
srand (time(NULL));
|
||||
}
|
||||
|
||||
std::string ImageSelector::getNextImage() const
|
||||
DefaultImageSelector::~DefaultImageSelector(){}
|
||||
|
||||
std::string DefaultImageSelector::getNextImage() const
|
||||
{
|
||||
QDir directory(path.c_str());
|
||||
std:: string filename;
|
||||
try
|
||||
{
|
||||
if (recursive)
|
||||
{
|
||||
QStringList images = listImagesRecursive();
|
||||
QStringList images = pathTraverser->getImages();
|
||||
unsigned int selectedImage = selectRandom(images);
|
||||
filename = images.at(selectedImage).toStdString();
|
||||
}
|
||||
else
|
||||
{
|
||||
QStringList images = directory.entryList(QStringList() << "*.jpg" << "*.JPG", QDir::Files);
|
||||
unsigned int selectedImage = selectRandom(images);
|
||||
filename = directory.filePath(images.at(selectedImage)).toStdString();
|
||||
}
|
||||
filename = pathTraverser->getImagePath(images.at(selectedImage).toStdString());
|
||||
}
|
||||
catch(const std::string& err)
|
||||
{
|
||||
@@ -42,24 +42,12 @@ std::string ImageSelector::getNextImage() const
|
||||
return filename;
|
||||
}
|
||||
|
||||
unsigned int ImageSelector::selectRandom(const QStringList& images) const
|
||||
unsigned int DefaultImageSelector::selectRandom(const QStringList& images) const
|
||||
{
|
||||
std::cout << "images: " << images.size() << std::endl;
|
||||
if (images.size() == 0)
|
||||
{
|
||||
throw std::string("No jpg images found in folder " + path);
|
||||
throw std::string("No jpg images found in given folder");
|
||||
}
|
||||
return rand() % images.size();
|
||||
}
|
||||
|
||||
|
||||
QStringList ImageSelector::listImagesRecursive() const
|
||||
{
|
||||
QDirIterator it(QString(path.c_str()), QStringList() << "*.jpg" << "*.JPG", QDir::Files, QDirIterator::Subdirectories);
|
||||
QStringList files;
|
||||
while (it.hasNext())
|
||||
{
|
||||
files.append(it.next());
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
@@ -2,20 +2,32 @@
|
||||
#define IMAGESELECTOR_H
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <QStringList>
|
||||
|
||||
class MainWindow;
|
||||
class PathTraverser;
|
||||
|
||||
class ImageSelector
|
||||
{
|
||||
public:
|
||||
ImageSelector(std::string path, bool recursive);
|
||||
std::string getNextImage() const;
|
||||
ImageSelector(std::unique_ptr<PathTraverser>& pathTraverser);
|
||||
virtual ~ImageSelector();
|
||||
virtual std::string getNextImage() const = 0;
|
||||
|
||||
protected:
|
||||
std::unique_ptr<PathTraverser>& pathTraverser;
|
||||
};
|
||||
|
||||
class DefaultImageSelector : public ImageSelector
|
||||
{
|
||||
public:
|
||||
DefaultImageSelector(std::unique_ptr<PathTraverser>& pathTraverser);
|
||||
virtual ~DefaultImageSelector();
|
||||
virtual std::string getNextImage() const;
|
||||
|
||||
private:
|
||||
QStringList listImagesRecursive() const;
|
||||
unsigned int selectRandom(const QStringList& images) const;
|
||||
std::string path;
|
||||
bool recursive;
|
||||
};
|
||||
|
||||
#endif // IMAGESELECTOR_H
|
||||
|
||||
@@ -4,12 +4,12 @@
|
||||
#include <QDirIterator>
|
||||
#include <QTimer>
|
||||
#include <QApplication>
|
||||
#include <QDir>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <stdlib.h> /* srand, rand */
|
||||
#include <time.h> /* time */
|
||||
|
||||
ImageSwitcher::ImageSwitcher(MainWindow& w, unsigned int timeout, const ImageSelector& selector):
|
||||
ImageSwitcher::ImageSwitcher(MainWindow& w, unsigned int timeout, std::unique_ptr<ImageSelector>& selector):
|
||||
QObject::QObject(),
|
||||
window(w),
|
||||
timeout(timeout),
|
||||
@@ -20,7 +20,7 @@ ImageSwitcher::ImageSwitcher(MainWindow& w, unsigned int timeout, const ImageSel
|
||||
|
||||
void ImageSwitcher::updateImage()
|
||||
{
|
||||
std::string filename(selector.getNextImage());
|
||||
std::string filename(selector->getNextImage());
|
||||
if (filename == "")
|
||||
{
|
||||
window.warn("No image found.");
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <QObject>
|
||||
#include <QTimer>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
class MainWindow;
|
||||
class ImageSelector;
|
||||
@@ -11,7 +12,7 @@ class ImageSwitcher : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ImageSwitcher(MainWindow& w, unsigned int timeout, const ImageSelector& selector);
|
||||
ImageSwitcher(MainWindow& w, unsigned int timeout, std::unique_ptr<ImageSelector>& selector);
|
||||
void start();
|
||||
|
||||
public slots:
|
||||
@@ -19,7 +20,7 @@ public slots:
|
||||
private:
|
||||
MainWindow& window;
|
||||
unsigned int timeout;
|
||||
const ImageSelector& selector;
|
||||
std::unique_ptr<ImageSelector>& selector;
|
||||
QTimer timer;
|
||||
};
|
||||
|
||||
|
||||
17
src/main.cpp
17
src/main.cpp
@@ -1,6 +1,7 @@
|
||||
#include "mainwindow.h"
|
||||
#include "imageselector.h"
|
||||
#include "imageswitcher.h"
|
||||
#include "pathtraverser.h"
|
||||
#include <QApplication>
|
||||
#include <iostream>
|
||||
#include <sys/file.h>
|
||||
@@ -9,6 +10,7 @@
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <memory>
|
||||
|
||||
void usage(std::string programName) {
|
||||
std::cerr << "Usage: " << programName << " [-t rotation_seconds] [-o background_opacity(0..255)] [-b blur_radius] -p image_folder -r" << std::endl;
|
||||
@@ -47,14 +49,25 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
}
|
||||
|
||||
if (path.empty()) {
|
||||
if (path.empty())
|
||||
{
|
||||
std::cout << "Error: Path expected." << std::endl;
|
||||
usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::unique_ptr<PathTraverser> pathTraverser;
|
||||
if (recursive)
|
||||
{
|
||||
pathTraverser = std::unique_ptr<PathTraverser>(new RecursivePathTraverser(path));
|
||||
} else {
|
||||
pathTraverser = std::unique_ptr<PathTraverser>(new DefaultPathTraverser(path));
|
||||
}
|
||||
|
||||
std::unique_ptr<ImageSelector> selector = std::unique_ptr<ImageSelector>(new DefaultImageSelector(pathTraverser));
|
||||
|
||||
w.show();
|
||||
|
||||
ImageSelector selector(path, recursive);
|
||||
ImageSwitcher switcher(w, rotationSeconds * 1000, selector);
|
||||
switcher.start();
|
||||
return a.exec();
|
||||
|
||||
55
src/pathtraverser.cpp
Normal file
55
src/pathtraverser.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "pathtraverser.h"
|
||||
#include "mainwindow.h"
|
||||
#include <QDirIterator>
|
||||
#include <QTimer>
|
||||
#include <QApplication>
|
||||
#include <QDir>
|
||||
#include <iostream>
|
||||
#include <stdlib.h> /* srand, rand */
|
||||
|
||||
PathTraverser::PathTraverser(const std::string path):
|
||||
path(path)
|
||||
{}
|
||||
|
||||
PathTraverser::~PathTraverser() {}
|
||||
|
||||
RecursivePathTraverser::RecursivePathTraverser(const std::string path):
|
||||
PathTraverser(path)
|
||||
{}
|
||||
|
||||
RecursivePathTraverser::~RecursivePathTraverser() {}
|
||||
|
||||
|
||||
QStringList RecursivePathTraverser::getImages() const
|
||||
{
|
||||
QDirIterator it(QString(path.c_str()), QStringList() << "*.jpg" << "*.JPG", QDir::Files, QDirIterator::Subdirectories);
|
||||
QStringList files;
|
||||
while (it.hasNext())
|
||||
{
|
||||
files.append(it.next());
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
const std::string RecursivePathTraverser::getImagePath(const std::string image) const
|
||||
{
|
||||
return image;
|
||||
}
|
||||
|
||||
DefaultPathTraverser::DefaultPathTraverser(const std::string path):
|
||||
PathTraverser(path),
|
||||
directory(path.c_str())
|
||||
{}
|
||||
|
||||
DefaultPathTraverser::~DefaultPathTraverser() {}
|
||||
|
||||
|
||||
QStringList DefaultPathTraverser::getImages() const
|
||||
{
|
||||
return directory.entryList(QStringList() << "*.jpg" << "*.JPG", QDir::Files);
|
||||
}
|
||||
|
||||
const std::string DefaultPathTraverser::getImagePath(const std::string image) const
|
||||
{
|
||||
return directory.filePath(QString(image.c_str())).toStdString();
|
||||
}
|
||||
41
src/pathtraverser.h
Normal file
41
src/pathtraverser.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef PATHTRAVERSER_H
|
||||
#define PATHTRAVERSER_H
|
||||
|
||||
#include <iostream>
|
||||
#include <QDir>
|
||||
#include <QStringList>
|
||||
|
||||
class MainWindow;
|
||||
class PathTraverser
|
||||
{
|
||||
public:
|
||||
PathTraverser(const std::string path);
|
||||
virtual ~PathTraverser();
|
||||
virtual QStringList getImages() const = 0;
|
||||
virtual const std::string getImagePath(const std::string image) const = 0;
|
||||
|
||||
protected:
|
||||
const std::string path;
|
||||
};
|
||||
|
||||
class RecursivePathTraverser : public PathTraverser
|
||||
{
|
||||
public:
|
||||
RecursivePathTraverser(const std::string path);
|
||||
virtual ~RecursivePathTraverser();
|
||||
QStringList getImages() const;
|
||||
virtual const std::string getImagePath(const std::string image) const;
|
||||
};
|
||||
|
||||
class DefaultPathTraverser : public PathTraverser
|
||||
{
|
||||
public:
|
||||
DefaultPathTraverser(const std::string path);
|
||||
virtual ~DefaultPathTraverser();
|
||||
QStringList getImages() const;
|
||||
virtual const std::string getImagePath(const std::string image) const;
|
||||
private:
|
||||
QDir directory;
|
||||
};
|
||||
|
||||
#endif // PATHTRAVERSER_H
|
||||
@@ -27,11 +27,13 @@ SOURCES += \
|
||||
main.cpp \
|
||||
mainwindow.cpp \
|
||||
imageswitcher.cpp \
|
||||
pathtraverser.cpp \
|
||||
imageselector.cpp
|
||||
|
||||
HEADERS += \
|
||||
mainwindow.h \
|
||||
imageselector.h \
|
||||
pathtraverser.h \
|
||||
imageswitcher.h
|
||||
|
||||
FORMS += \
|
||||
|
||||
Reference in New Issue
Block a user