From e8e4a2a1ce42bc45781a26d366f82ab2a5d0bdc5 Mon Sep 17 00:00:00 2001 From: Alfred Reynolds Date: Tue, 3 Aug 2021 17:56:29 +1200 Subject: [PATCH] - Add new aspect mode EImageAspect_Monitor , match the aspect of the monitor - Added code to make sure the QMainWindow matches the screen size, dynamic rotation of the monitor caused incorrect screen sizes --- src/imagestructs.h | 2 +- src/main.cpp | 6 ++++- src/mainwindow.cpp | 52 ++++++++++++++++++++++++++++++++++++++++--- src/mainwindow.h | 11 +++++++-- src/pathtraverser.cpp | 1 - 5 files changed, 64 insertions(+), 8 deletions(-) diff --git a/src/imagestructs.h b/src/imagestructs.h index 8356bf2..1338dfd 100644 --- a/src/imagestructs.h +++ b/src/imagestructs.h @@ -4,7 +4,7 @@ #include // possible aspect ratios of an image -enum EImageAspect { EImageAspect_Landscape = 0, EImageAspect_Portrait, EImageAspect_Any }; +enum EImageAspect { EImageAspect_Landscape = 0, EImageAspect_Portrait, EImageAspect_Any, EImageAspect_Monitor /* match monitors aspect */ }; // options to consider when displaying an image struct ImageDisplayOptions_t diff --git a/src/main.cpp b/src/main.cpp index ae116ff..d636a4a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,7 +32,7 @@ int main(int argc, char *argv[]) bool sorted = false; bool debugMode = false; ImageDisplayOptions_t baseDisplayOptions; - std::string valid_aspects = "alp"; // all, landscape, portait + std::string valid_aspects = "alpm"; // all, landscape, portait std::string overlay = ""; std::string imageList = ""; // comma delimited list of images to show int debugInt = 0; @@ -71,6 +71,9 @@ int main(int argc, char *argv[]) case 'p': baseDisplayOptions.onlyAspect = EImageAspect_Portrait; break; + case 'm': + baseDisplayOptions.onlyAspect = EImageAspect_Monitor; + break; default: case 'a': baseDisplayOptions.onlyAspect = EImageAspect_Any; @@ -168,6 +171,7 @@ int main(int argc, char *argv[]) w.show(); ImageSwitcher switcher(w, rotationSeconds * 1000, selector); + w.setImageSwitcher(&switcher); switcher.start(); return a.exec(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index b4dbd27..5094443 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1,6 +1,7 @@ #include "mainwindow.h" #include "overlay.h" #include "ui_mainwindow.h" +#include "imageswitcher.h" #include #include #include @@ -14,6 +15,7 @@ #include #include #include +#include #include MainWindow::MainWindow(QWidget *parent) : @@ -104,10 +106,43 @@ bool MainWindow::event(QEvent* event) void MainWindow::resizeEvent(QResizeEvent* event) { - QMainWindow::resizeEvent(event); - updateImage(true); + QMainWindow::resizeEvent(event); + // the window size in this event may not match the monitor size, so use QDesktopWidget to find the true monitor size + QDesktopWidget desktop; + QSize screenSize = desktop.screenGeometry(this).size(); + bool isLandscape = screenSize.width() > screenSize.height(); + if (imageAspectMatchesMonitor) + { + baseImageOptions.onlyAspect = isLandscape ? EImageAspect_Landscape : EImageAspect_Portrait; + } + if(debugMode) + { + std::cout << "Got resize:" << this << " " << screenSize.width() << "," << screenSize.height() << " , is landscape? " << (isLandscape ? "y" : "n") << std::endl; + } + updateImage(true); + QTimer::singleShot(5, this, SLOT(checkWindowSize())); + if(lastScreenSize != screenSize && switcher != nullptr) + { + lastScreenSize = screenSize; + switcher->updateImage(); + } } +void MainWindow::checkWindowSize() +{ + QDesktopWidget desktop; + QRect screenSize = desktop.screenGeometry(this); + if(size() != screenSize.size()) + { + if(debugMode) + { + std::cout << "Resizing Window" << screenSize.width() << "," << screenSize.height() << std::endl; + } + setFixedSize(screenSize.size()); + } +} + + void MainWindow::setImage(const ImageDetails_t &imageDetails) { currentImage = imageDetails; @@ -116,6 +151,7 @@ void MainWindow::setImage(const ImageDetails_t &imageDetails) void MainWindow::updateImage(bool immediately) { + checkWindowSize(); if (currentImage.filename == "") return; @@ -131,7 +167,7 @@ void MainWindow::updateImage(bool immediately) QPixmap p( currentImage.filename.c_str() ); if(debugMode) { - std::cout << "size:" << p.width() << "x" << p.height() << std::endl; + std::cout << "size:" << p.width() << "x" << p.height() << "(window:" << width() << "," << height() << ")" << std::endl; } QPixmap rotated = getRotatedPixmap(p); @@ -302,3 +338,13 @@ void MainWindow::warn(std::string text) QLabel *label = this->findChild("image"); label->setText(text.c_str()); } + +void MainWindow::setBaseOptions(const ImageDisplayOptions_t &baseOptionsIn) +{ + baseImageOptions = baseOptionsIn; + if(baseImageOptions.onlyAspect == EImageAspect_Monitor) + { + imageAspectMatchesMonitor = true; + baseImageOptions.onlyAspect = width() >= height() ? EImageAspect_Landscape : EImageAspect_Portrait; + } +} diff --git a/src/mainwindow.h b/src/mainwindow.h index 6c7642b..79fb59b 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -12,6 +12,7 @@ class MainWindow; class QLabel; class QKeyEvent; class Overlay; +class ImageSwitcher; class MainWindow : public QMainWindow { @@ -29,18 +30,24 @@ public: void warn(std::string text); void setOverlay(Overlay* overlay); void setDebugMode(bool debugModeIn) {debugMode = debugModeIn;} - void setBaseOptions(const ImageDisplayOptions_t &baseOptionsIn) { baseImageOptions = baseOptionsIn; } + void setBaseOptions(const ImageDisplayOptions_t &baseOptionsIn); const ImageDisplayOptions_t &getBaseOptions() { return baseImageOptions; } + void setImageSwitcher(ImageSwitcher *switcherIn) { switcher = switcherIn; } +public slots: + void checkWindowSize(); private: Ui::MainWindow *ui; unsigned int blurRadius = 20; unsigned int backgroundOpacity = 150; ImageDisplayOptions_t baseImageOptions; + bool imageAspectMatchesMonitor = false; ImageDetails_t currentImage; bool debugMode = false; + QSize lastScreenSize = {0,0}; - Overlay* overlay; + Overlay* overlay = nullptr; + ImageSwitcher *switcher = nullptr; void drawText(QPixmap& image, int margin, int fontsize, QString text, int alignment); diff --git a/src/pathtraverser.cpp b/src/pathtraverser.cpp index b8c3b82..c901f21 100644 --- a/src/pathtraverser.cpp +++ b/src/pathtraverser.cpp @@ -51,7 +51,6 @@ void PathTraverser::LoadOptionsForDirectory(const std::string &directoryPath, Im std::cout << "Fit Aspect:" << options.fitAspectAxisToWindow << std::endl; } } - // read json } }