- 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
This commit is contained in:
Alfred Reynolds
2021-08-03 17:56:29 +12:00
parent 096a68636c
commit e8e4a2a1ce
5 changed files with 64 additions and 8 deletions

View File

@@ -4,7 +4,7 @@
#include <string> #include <string>
// possible aspect ratios of an image // 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 // options to consider when displaying an image
struct ImageDisplayOptions_t struct ImageDisplayOptions_t

View File

@@ -32,7 +32,7 @@ int main(int argc, char *argv[])
bool sorted = false; bool sorted = false;
bool debugMode = false; bool debugMode = false;
ImageDisplayOptions_t baseDisplayOptions; ImageDisplayOptions_t baseDisplayOptions;
std::string valid_aspects = "alp"; // all, landscape, portait std::string valid_aspects = "alpm"; // all, landscape, portait
std::string overlay = ""; std::string overlay = "";
std::string imageList = ""; // comma delimited list of images to show std::string imageList = ""; // comma delimited list of images to show
int debugInt = 0; int debugInt = 0;
@@ -71,6 +71,9 @@ int main(int argc, char *argv[])
case 'p': case 'p':
baseDisplayOptions.onlyAspect = EImageAspect_Portrait; baseDisplayOptions.onlyAspect = EImageAspect_Portrait;
break; break;
case 'm':
baseDisplayOptions.onlyAspect = EImageAspect_Monitor;
break;
default: default:
case 'a': case 'a':
baseDisplayOptions.onlyAspect = EImageAspect_Any; baseDisplayOptions.onlyAspect = EImageAspect_Any;
@@ -168,6 +171,7 @@ int main(int argc, char *argv[])
w.show(); w.show();
ImageSwitcher switcher(w, rotationSeconds * 1000, selector); ImageSwitcher switcher(w, rotationSeconds * 1000, selector);
w.setImageSwitcher(&switcher);
switcher.start(); switcher.start();
return a.exec(); return a.exec();
} }

View File

@@ -1,6 +1,7 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "overlay.h" #include "overlay.h"
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "imageswitcher.h"
#include <QLabel> #include <QLabel>
#include <QPixmap> #include <QPixmap>
#include <QBitmap> #include <QBitmap>
@@ -14,6 +15,7 @@
#include <QRect> #include <QRect>
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QGraphicsPixmapItem> #include <QGraphicsPixmapItem>
#include <QDesktopWidget>
#include <sstream> #include <sstream>
MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QWidget *parent) :
@@ -104,10 +106,43 @@ bool MainWindow::event(QEvent* event)
void MainWindow::resizeEvent(QResizeEvent* event) void MainWindow::resizeEvent(QResizeEvent* event)
{ {
QMainWindow::resizeEvent(event); QMainWindow::resizeEvent(event);
updateImage(true); // 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) void MainWindow::setImage(const ImageDetails_t &imageDetails)
{ {
currentImage = imageDetails; currentImage = imageDetails;
@@ -116,6 +151,7 @@ void MainWindow::setImage(const ImageDetails_t &imageDetails)
void MainWindow::updateImage(bool immediately) void MainWindow::updateImage(bool immediately)
{ {
checkWindowSize();
if (currentImage.filename == "") if (currentImage.filename == "")
return; return;
@@ -131,7 +167,7 @@ void MainWindow::updateImage(bool immediately)
QPixmap p( currentImage.filename.c_str() ); QPixmap p( currentImage.filename.c_str() );
if(debugMode) 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); QPixmap rotated = getRotatedPixmap(p);
@@ -302,3 +338,13 @@ void MainWindow::warn(std::string text)
QLabel *label = this->findChild<QLabel*>("image"); QLabel *label = this->findChild<QLabel*>("image");
label->setText(text.c_str()); 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;
}
}

View File

@@ -12,6 +12,7 @@ class MainWindow;
class QLabel; class QLabel;
class QKeyEvent; class QKeyEvent;
class Overlay; class Overlay;
class ImageSwitcher;
class MainWindow : public QMainWindow class MainWindow : public QMainWindow
{ {
@@ -29,18 +30,24 @@ public:
void warn(std::string text); void warn(std::string text);
void setOverlay(Overlay* overlay); void setOverlay(Overlay* overlay);
void setDebugMode(bool debugModeIn) {debugMode = debugModeIn;} 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; } const ImageDisplayOptions_t &getBaseOptions() { return baseImageOptions; }
void setImageSwitcher(ImageSwitcher *switcherIn) { switcher = switcherIn; }
public slots:
void checkWindowSize();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
unsigned int blurRadius = 20; unsigned int blurRadius = 20;
unsigned int backgroundOpacity = 150; unsigned int backgroundOpacity = 150;
ImageDisplayOptions_t baseImageOptions; ImageDisplayOptions_t baseImageOptions;
bool imageAspectMatchesMonitor = false;
ImageDetails_t currentImage; ImageDetails_t currentImage;
bool debugMode = false; 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); void drawText(QPixmap& image, int margin, int fontsize, QString text, int alignment);

View File

@@ -51,7 +51,6 @@ void PathTraverser::LoadOptionsForDirectory(const std::string &directoryPath, Im
std::cout << "Fit Aspect:" << options.fitAspectAxisToWindow << std::endl; std::cout << "Fit Aspect:" << options.fitAspectAxisToWindow << std::endl;
} }
} }
// read json
} }
} }