Files
slide/src/imageswitcher.cpp
Nathan Coad a9c5139d55
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
add mqtt control
2026-01-31 16:50:34 +11:00

126 lines
2.7 KiB
C++

#include "imageswitcher.h"
#include "imageselector.h"
#include "mainwindow.h"
#include <QDirIterator>
#include <QTimer>
#include <QApplication>
#include <iostream>
#include <memory>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
ImageSwitcher::ImageSwitcher(MainWindow& w, unsigned int timeoutMsec, std::unique_ptr<ImageSelector>& selector):
QObject::QObject(),
window(w),
timeout(timeoutMsec),
selector(std::move(selector)),
timer(this),
timerNoContent(this)
{
}
void ImageSwitcher::updateImage()
{
if (paused)
return;
if(reloadConfigIfNeeded)
{
reloadConfigIfNeeded(window, this);
}
ImageDetails imageDetails = selector->getNextImage(window.getBaseOptions());
if (imageDetails.filename == "")
{
window.warn("No image found.");
timerNoContent.start(timeoutNoContent);
}
else
{
window.setImage(imageDetails);
timerNoContent.stop(); // we have loaded content so stop the fast polling
}
}
void ImageSwitcher::start()
{
updateImage();
connect(&timer, SIGNAL(timeout()), this, SLOT(updateImage()));
connect(&timerNoContent, SIGNAL(timeout()), this, SLOT(updateImage()));
timer.start(timeout);
}
void ImageSwitcher::scheduleImageUpdate()
{
// update our image in 100msec, to let the system settle
QTimer::singleShot(100, this, SLOT(updateImage()));
}
void ImageSwitcher::setConfigFileReloader(std::function<void(MainWindow &w, ImageSwitcher *switcher)> reloadConfigIfNeededIn)
{
reloadConfigIfNeeded = reloadConfigIfNeededIn;
}
void ImageSwitcher::setRotationTime(unsigned int timeoutMsecIn)
{
timeout = timeoutMsecIn;
if (!paused)
timer.start(timeout);
}
void ImageSwitcher::setImageSelector(std::unique_ptr<ImageSelector>& selectorIn)
{
selector = std::move(selectorIn);
}
void ImageSwitcher::pause()
{
paused = true;
timer.stop();
timerNoContent.stop();
}
void ImageSwitcher::resume()
{
if (!paused)
return;
paused = false;
timer.start(timeout);
scheduleImageUpdate();
}
void ImageSwitcher::stepOnce()
{
bool wasPaused = paused;
if (wasPaused)
paused = false;
updateImage();
if (wasPaused)
{
paused = true;
timer.stop();
timerNoContent.stop();
}
}
void ImageSwitcher::restart(std::unique_ptr<ImageSelector>& selectorIn)
{
paused = false;
timerNoContent.stop();
setImageSelector(selectorIn);
timer.start(timeout);
scheduleImageUpdate();
}
bool ImageSwitcher::skipToNextFolder()
{
auto *listSelector = dynamic_cast<ListImageSelector*>(selector.get());
if (!listSelector)
return false;
stepOnce();
return true;
}
bool ImageSwitcher::isPaused() const
{
return paused;
}