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

View File

@@ -1,26 +1,20 @@
#ifndef IMAGESELECTOR_H #ifndef IMAGESELECTOR_H
#define IMAGESELECTOR_H #define IMAGESELECTOR_H
#include <QObject>
#include <QTimer>
#include <iostream> #include <iostream>
class MainWindow; #include <QStringList>
class ImageSelector : public QObject
{ class MainWindow;
Q_OBJECT class ImageSelector
public: {
ImageSelector(MainWindow& w, unsigned int timeout, std::string path, bool recursive); public:
void start(); ImageSelector(std::string path, bool recursive);
std::string getNextImage() const;
public slots:
void updateImage();
private: private:
QStringList listImagesRecursive(); QStringList listImagesRecursive() const;
MainWindow& window;
unsigned int timeout;
std::string path; std::string path;
bool recursive; bool recursive;
QTimer timer;
}; };
#endif // IMAGESELECTOR_H #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 "mainwindow.h"
#include "imageselector.h" #include "imageselector.h"
#include "imageswitcher.h"
#include <QApplication> #include <QApplication>
#include <iostream> #include <iostream>
#include <sys/file.h> #include <sys/file.h>
@@ -53,7 +54,8 @@ int main(int argc, char *argv[])
} }
w.show(); w.show();
ImageSelector is(w, rotationSeconds * 1000, path, recursive); ImageSelector selector(path, recursive);
is.start(); ImageSwitcher switcher(w, rotationSeconds * 1000, selector);
switcher.start();
return a.exec(); return a.exec();
} }

View File

@@ -168,3 +168,9 @@ void MainWindow::setBackgroundOpacity(unsigned int backgroundOpacity)
{ {
this->backgroundOpacity = 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 setImage(std::string path);
void setBlurRadius(unsigned int blurRadius); void setBlurRadius(unsigned int blurRadius);
void setBackgroundOpacity(unsigned int opacity); void setBackgroundOpacity(unsigned int opacity);
void warn(std::string text);
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;

View File

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