diff --git a/src/imageselector.cpp b/src/imageselector.cpp index 961c1e6..7cdaf2d 100644 --- a/src/imageselector.cpp +++ b/src/imageselector.cpp @@ -12,8 +12,8 @@ #include // std::shuffle #include // std::default_random_engine -ImageSelector::ImageSelector(std::unique_ptr& pathTraverser): - pathTraverser(pathTraverser) +ImageSelector::ImageSelector(std::unique_ptr& pathTraverserIn): + pathTraverser(std::move(pathTraverserIn)) { } diff --git a/src/imageselector.h b/src/imageselector.h index 37182f5..128f93b 100644 --- a/src/imageselector.h +++ b/src/imageselector.h @@ -21,7 +21,7 @@ protected: ImageDetails populateImageDetails(const std::string&filename, const ImageDisplayOptions &baseOptions); bool imageValidForAspect(const ImageDetails& imageDetails); bool imageMatchesFilter(const ImageDetails& imageDetails); - std::unique_ptr& pathTraverser; + std::unique_ptr pathTraverser; bool debugMode = false; }; diff --git a/src/imageswitcher.cpp b/src/imageswitcher.cpp index 603ae36..4347af2 100644 --- a/src/imageswitcher.cpp +++ b/src/imageswitcher.cpp @@ -9,11 +9,11 @@ #include /* srand, rand */ #include /* time */ -ImageSwitcher::ImageSwitcher(MainWindow& w, unsigned int timeoutMsec, std::shared_ptr& selector): +ImageSwitcher::ImageSwitcher(MainWindow& w, unsigned int timeoutMsec, std::unique_ptr& selector): QObject::QObject(), window(w), timeout(timeoutMsec), - selector(selector), + selector(std::move(selector)), timer(this), timerNoContent(this) { @@ -23,7 +23,7 @@ void ImageSwitcher::updateImage() { if(reloadConfigIfNeeded) { - reloadConfigIfNeeded(); + reloadConfigIfNeeded(window, this, selector.get()); } ImageDetails imageDetails = selector->getNextImage(window.getBaseOptions()); if (imageDetails.filename == "") @@ -52,7 +52,7 @@ void ImageSwitcher::scheduleImageUpdate() QTimer::singleShot(100, this, SLOT(updateImage())); } -void ImageSwitcher::setConfigFileReloader(std::function reloadConfigIfNeededIn) +void ImageSwitcher::setConfigFileReloader(std::function reloadConfigIfNeededIn) { reloadConfigIfNeeded = reloadConfigIfNeededIn; } @@ -62,3 +62,8 @@ void ImageSwitcher::setRotationTime(unsigned int timeoutMsecIn) timeout = timeoutMsecIn; timer.start(timeout); } + +void ImageSwitcher::setImageSelector(std::unique_ptr& selectorIn) +{ + selector = std::move(selectorIn); +} diff --git a/src/imageswitcher.h b/src/imageswitcher.h index 553ee74..ea00e49 100644 --- a/src/imageswitcher.h +++ b/src/imageswitcher.h @@ -6,29 +6,30 @@ #include #include #include +#include "imageselector.h" class MainWindow; -class ImageSelector; class ImageSwitcher : public QObject { Q_OBJECT public: - ImageSwitcher(MainWindow& w, unsigned int timeoutMsec, std::shared_ptr& selector); + ImageSwitcher(MainWindow& w, unsigned int timeoutMsec, std::unique_ptr& selector); void start(); void scheduleImageUpdate(); - void setConfigFileReloader(std::function reloadConfigIfNeededIn); + void setConfigFileReloader(std::function reloadConfigIfNeededIn); void setRotationTime(unsigned int timeoutMsec); + void setImageSelector(std::unique_ptr& selector); public slots: void updateImage(); private: MainWindow& window; unsigned int timeout; - std::shared_ptr& selector; + std::unique_ptr selector; QTimer timer; const unsigned int timeoutNoContent = 5 * 1000; // 5 sec QTimer timerNoContent; - std::function reloadConfigIfNeeded; + std::function reloadConfigIfNeeded; }; #endif // IMAGESWITCHER_H diff --git a/src/main.cpp b/src/main.cpp index d0d3e63..91c4a9f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,22 +24,34 @@ void usage(std::string programName) { } struct Config { - std::string path = ""; - std::string configPath = ""; - unsigned int rotationSeconds = 30; - int blurRadius = -1; - int backgroundOpacity = -1; - bool recursive = false; - bool shuffle = false; - bool sorted = false; - bool debugMode = false; - ImageDisplayOptions baseDisplayOptions; - std::string valid_aspects = "alpm"; // all, landscape, portait - std::string overlay = ""; - std::string imageList = ""; // comma delimited list of images to show - QDateTime loadTime; + public: + std::string path = ""; + std::string configPath = ""; + unsigned int rotationSeconds = 30; + int blurRadius = -1; + int backgroundOpacity = -1; + bool recursive = false; + bool shuffle = false; + bool sorted = false; + bool debugMode = false; + ImageDisplayOptions baseDisplayOptions; + static const std::string valid_aspects; + std::string overlay = ""; + std::string imageList = ""; // comma delimited list of images to show + QDateTime loadTime; + public: + bool PathOptionsChanged(Config &other) { + if ( other.recursive != recursive || other.shuffle != shuffle + || other.sorted != sorted) + return true; + if ( other.path != path || other.imageList != imageList ) + return true; + return false; + } }; +const std::string Config::valid_aspects = "alpm"; // all, landscape, portait, monitor + ImageAspect parseAspectFromString(char aspect) { switch(aspect) { @@ -243,7 +255,58 @@ bool parseCommandLine(Config &appConfig, int argc, char *argv[]) { return true; } -void ReloadConfigIfNeeded(Config &appConfig, MainWindow &w, ImageSwitcher &switcher, std::shared_ptr &selector) +void ConfigureWindowFromSettings(MainWindow &w, const Config &appConfig) +{ + if (appConfig.blurRadius>= 0) + { + w.setBlurRadius(appConfig.blurRadius); + } + + if (appConfig.backgroundOpacity>= 0) + { + w.setBackgroundOpacity(appConfig.backgroundOpacity); + } + std::unique_ptr o = std::unique_ptr(new Overlay(appConfig.overlay)); + o->setDebugMode(appConfig.debugMode); + w.setDebugMode(appConfig.debugMode); + w.setOverlay(o); + w.setBaseOptions(appConfig.baseDisplayOptions); +} + +std::unique_ptr GetSelectorForConfig(const Config &appConfig) +{ + std::unique_ptr pathTraverser; + if (!appConfig.imageList.empty()) + { + pathTraverser = std::unique_ptr(new ImageListPathTraverser(appConfig.imageList, appConfig.debugMode)); + } + else if (appConfig.recursive) + { + pathTraverser = std::unique_ptr(new RecursivePathTraverser(appConfig.path, appConfig.debugMode)); + } + else + { + pathTraverser = std::unique_ptr(new DefaultPathTraverser(appConfig.path, appConfig.debugMode)); + } + + std::unique_ptr selector; + if (appConfig.sorted) + { + selector = std::unique_ptr(new SortedImageSelector(pathTraverser)); + } + else if (appConfig.shuffle) + { + selector = std::unique_ptr(new ShuffleImageSelector(pathTraverser)); + } + else + { + selector = std::unique_ptr(new RandomImageSelector(pathTraverser)); + } + + return selector; +} + +void ReloadConfigIfNeeded(Config &appConfig, MainWindow &w, ImageSwitcher *switcher, ImageSelector *selector) { QString jsonFile = getConfigFilePath(appConfig.configPath); QDir directory; @@ -254,13 +317,21 @@ void ReloadConfigIfNeeded(Config &appConfig, MainWindow &w, ImageSwitcher &switc if(appConfig.loadTime < QFileInfo(jsonFile).lastModified()) { + const std::string oldPath = appConfig.path; + const std::string oldImageList = appConfig.imageList; + + Config oldConfig = appConfig; appConfig = loadConfiguration(appConfig); - w.setBaseOptions(appConfig.baseDisplayOptions); - w.setDebugMode(appConfig.debugMode); + + ConfigureWindowFromSettings(w, appConfig); + if(appConfig.PathOptionsChanged(oldConfig)) + { + std::unique_ptr selector = GetSelectorForConfig(appConfig); + switcher->setImageSelector(selector); + } + selector->setDebugMode(appConfig.debugMode); - //Overlay o(appConfig.overlay); - //w.setOverlay(&o); - switcher.setRotationTime(appConfig.rotationSeconds * 1000); + switcher->setRotationTime(appConfig.rotationSeconds * 1000); } } @@ -268,7 +339,6 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); - MainWindow w; Config commandLineAppConfig; if (!parseCommandLine(commandLineAppConfig, argc, argv)) { @@ -285,58 +355,22 @@ int main(int argc, char *argv[]) return 1; } - if (appConfig.blurRadius>= 0) - { - w.setBlurRadius(appConfig.blurRadius); - } - - if (appConfig.backgroundOpacity>= 0) - { - w.setBackgroundOpacity(appConfig.backgroundOpacity); - } - - std::unique_ptr pathTraverser; - if (!appConfig.imageList.empty()) - { - pathTraverser = std::unique_ptr(new ImageListPathTraverser(appConfig.imageList, appConfig.debugMode)); - } - else if (appConfig.recursive) - { - pathTraverser = std::unique_ptr(new RecursivePathTraverser(appConfig.path, appConfig.debugMode)); - } - else - { - pathTraverser = std::unique_ptr(new DefaultPathTraverser(appConfig.path, appConfig.debugMode)); - } - - std::shared_ptr selector; - if (appConfig.sorted) - { - selector = std::shared_ptr(new SortedImageSelector(pathTraverser)); - } - else if (appConfig.shuffle) - { - selector = std::shared_ptr(new ShuffleImageSelector(pathTraverser)); - } - else - { - selector = std::shared_ptr(new RandomImageSelector(pathTraverser)); - } - selector->setDebugMode(appConfig.debugMode); if(appConfig.debugMode) { std::cout << "Rotation Time: " << appConfig.rotationSeconds << std::endl; std::cout << "Overlay input: " << appConfig.overlay << std::endl; } - Overlay o(appConfig.overlay); - o.setDebugMode(appConfig.debugMode); - w.setOverlay(&o); - w.setBaseOptions(appConfig.baseDisplayOptions); + + MainWindow w; + ConfigureWindowFromSettings(w, appConfig); w.show(); + std::unique_ptr selector = GetSelectorForConfig(appConfig); + selector->setDebugMode(appConfig.debugMode); + ImageSwitcher switcher(w, appConfig.rotationSeconds * 1000, selector); w.setImageSwitcher(&switcher); - std::function reloader = [&appConfig, &w, &switcher, &selector]() { ReloadConfigIfNeeded(appConfig, w, switcher, selector); }; + std::function reloader = [&appConfig](MainWindow &w, ImageSwitcher *switcher, ImageSelector *selector) { ReloadConfigIfNeeded(appConfig, w, switcher, selector); }; switcher.setConfigFileReloader(reloader); switcher.start(); return a.exec(); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 3516333..d6478f0 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -194,7 +194,7 @@ void MainWindow::updateImage(bool immediately) QPixmap background = getBlurredBackground(rotated, scaled); drawForeground(background, scaled); - if (overlay != NULL) + if (overlay != nullptr) { drawText(background, overlay->getMarginTopLeft(), overlay->getFontsizeTopLeft(), overlay->getRenderedTopLeft(currentImage.filename).c_str(), Qt::AlignTop|Qt::AlignLeft); drawText(background, overlay->getMarginTopRight(), overlay->getFontsizeTopRight(), overlay->getRenderedTopRight(currentImage.filename).c_str(), Qt::AlignTop|Qt::AlignRight); @@ -254,9 +254,9 @@ void MainWindow::drawForeground(QPixmap& background, const QPixmap& foreground) pt.drawPixmap((background.width()-foreground.width())/2, (background.height()-foreground.height())/2, foreground); } -void MainWindow::setOverlay(Overlay* o) +void MainWindow::setOverlay(std::unique_ptr &o) { - overlay = o; + overlay = std::move(o); } QPixmap MainWindow::getBlurredBackground(const QPixmap& originalSize, const QPixmap& scaled) diff --git a/src/mainwindow.h b/src/mainwindow.h index b73fe47..1a3c8f5 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -28,7 +28,7 @@ public: void setBlurRadius(unsigned int blurRadius); void setBackgroundOpacity(unsigned int opacity); void warn(std::string text); - void setOverlay(Overlay* overlay); + void setOverlay(std::unique_ptr &overlay); void setDebugMode(bool debugModeIn); void setBaseOptions(const ImageDisplayOptions &baseOptionsIn); const ImageDisplayOptions &getBaseOptions(); @@ -46,7 +46,7 @@ private: bool debugMode = false; QSize lastScreenSize = {0,0}; - Overlay* overlay = nullptr; + std::unique_ptr overlay; ImageSwitcher *switcher = nullptr; void drawText(QPixmap& image, int margin, int fontsize, QString text, int alignment);