split imageselector

* image updating in seperate class ImageSwitcher
 * image selection in class ImageSelector
This commit is contained in:
Manuel
2020-01-02 23:33:39 +01:00
parent eec0eb8998
commit bc9dd90acc
8 changed files with 94 additions and 34 deletions

View File

@@ -8,18 +8,14 @@
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
ImageSelector::ImageSelector(MainWindow& w, unsigned int timeout, std::string path, bool recursive):
QObject::QObject(),
window(w),
timeout(timeout),
ImageSelector::ImageSelector(std::string path, bool recursive):
path(path),
recursive(recursive),
timer(this)
recursive(recursive)
{
srand (time(NULL));
}
void ImageSelector::updateImage()
std::string ImageSelector::getNextImage() const
{
QDir directory(path.c_str());
QStringList images;
@@ -35,22 +31,16 @@ void ImageSelector::updateImage()
if (images.size() == 0)
{
std::cerr << "No jpg images found in folder " << path << std::endl;
return;
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);
return filename;
}
void ImageSelector::start()
{
updateImage();
connect(&timer, SIGNAL(timeout()), this, SLOT(updateImage()));
timer.start(timeout);
}
QStringList ImageSelector::listImagesRecursive()
QStringList ImageSelector::listImagesRecursive() const
{
QDirIterator it(QString(path.c_str()), QStringList() << "*.jpg" << "*.JPG", QDir::Files, QDirIterator::Subdirectories);
QStringList files;

View File

@@ -1,26 +1,20 @@
#ifndef IMAGESELECTOR_H
#define IMAGESELECTOR_H
#include <QObject>
#include <QTimer>
#include <iostream>
class MainWindow;
class ImageSelector : public QObject
{
Q_OBJECT
public:
ImageSelector(MainWindow& w, unsigned int timeout, std::string path, bool recursive);
void start();
#include <QStringList>
class MainWindow;
class ImageSelector
{
public:
ImageSelector(std::string path, bool recursive);
std::string getNextImage() const;
public slots:
void updateImage();
private:
QStringList listImagesRecursive();
MainWindow& window;
unsigned int timeout;
QStringList listImagesRecursive() const;
std::string path;
bool recursive;
QTimer timer;
};
#endif // IMAGESELECTOR_H

39
src/imageswitcher.cpp Normal file
View File

@@ -0,0 +1,39 @@
#include "imageswitcher.h"
#include "imageselector.h"
#include "mainwindow.h"
#include <QDirIterator>
#include <QTimer>
#include <QApplication>
#include <QDir>
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
ImageSwitcher::ImageSwitcher(MainWindow& w, unsigned int timeout, const ImageSelector& selector):
QObject::QObject(),
window(w),
timeout(timeout),
selector(selector),
timer(this)
{
}
void ImageSwitcher::updateImage()
{
std::string filename(selector.getNextImage());
if (filename == "")
{
window.warn("No image found.");
}
else
{
window.setImage(filename);
}
}
void ImageSwitcher::start()
{
updateImage();
connect(&timer, SIGNAL(timeout()), this, SLOT(updateImage()));
timer.start(timeout);
}

26
src/imageswitcher.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef IMAGESWITCHER_H
#define IMAGESWITCHER_H
#include <QObject>
#include <QTimer>
#include <iostream>
class MainWindow;
class ImageSelector;
class ImageSwitcher : public QObject
{
Q_OBJECT
public:
ImageSwitcher(MainWindow& w, unsigned int timeout, const ImageSelector& selector);
void start();
public slots:
void updateImage();
private:
MainWindow& window;
unsigned int timeout;
const ImageSelector& selector;
QTimer timer;
};
#endif // IMAGESWITCHER_H

View File

@@ -1,5 +1,6 @@
#include "mainwindow.h"
#include "imageselector.h"
#include "imageswitcher.h"
#include <QApplication>
#include <iostream>
#include <sys/file.h>
@@ -53,7 +54,8 @@ int main(int argc, char *argv[])
}
w.show();
ImageSelector is(w, rotationSeconds * 1000, path, recursive);
is.start();
ImageSelector selector(path, recursive);
ImageSwitcher switcher(w, rotationSeconds * 1000, selector);
switcher.start();
return a.exec();
}

View File

@@ -168,3 +168,9 @@ void MainWindow::setBackgroundOpacity(unsigned int backgroundOpacity)
{
this->backgroundOpacity = backgroundOpacity;
}
void MainWindow::warn(std::string text)
{
QLabel *label = this->findChild<QLabel*>("image");
label->setText(text.c_str());
}

View File

@@ -22,6 +22,7 @@ public:
void setImage(std::string path);
void setBlurRadius(unsigned int blurRadius);
void setBackgroundOpacity(unsigned int opacity);
void warn(std::string text);
private:
Ui::MainWindow *ui;

View File

@@ -26,11 +26,13 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
mainwindow.cpp \
imageswitcher.cpp \
imageselector.cpp
HEADERS += \
mainwindow.h \
imageselector.h
imageselector.h \
imageswitcher.h
FORMS += \
mainwindow.ui