From 7a7dcad9c65951a9b6e1120f83c5fcc812a24674 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Tue, 1 Oct 2019 11:44:35 +0800 Subject: [PATCH] auto adjust window size --- icons/app-icon.svg | 102 +++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 1 + mainwindow.cpp | 44 +++++++++++++++---- mainwindow.h | 2 + resources.qrc | 1 + 5 files changed, 142 insertions(+), 8 deletions(-) create mode 100644 icons/app-icon.svg diff --git a/icons/app-icon.svg b/icons/app-icon.svg new file mode 100644 index 0000000..a4dbd1f --- /dev/null +++ b/icons/app-icon.svg @@ -0,0 +1,102 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + diff --git a/main.cpp b/main.cpp index aae8572..ba68d8b 100644 --- a/main.cpp +++ b/main.cpp @@ -27,6 +27,7 @@ int main(int argc, char *argv[]) if (!urlList.isEmpty()) { w.showUrls(urlList); + w.adjustWindowSizeBySceneRect(); } return a.exec(); diff --git a/mainwindow.cpp b/mainwindow.cpp index e1b7597..85a9fc1 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -22,6 +22,7 @@ MainWindow::MainWindow(QWidget *parent) : this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint); this->setAttribute(Qt::WA_TranslucentBackground, true); this->setMinimumSize(710, 530); + this->setWindowIcon(QIcon(":/icons/app-icon.svg")); m_fadeOutAnimation = new QPropertyAnimation(this, "windowOpacity"); m_fadeOutAnimation->setDuration(300); @@ -66,14 +67,7 @@ MainWindow::MainWindow(QWidget *parent) : connect(m_bottomButtonGroup, &BottomButtonGroup::toggleCheckerboardBtnClicked, this, [ = ](){ m_graphicsView->toggleCheckerboard(); }); - this->setGeometry( - QStyle::alignedRect( - Qt::LeftToRight, - Qt::AlignCenter, - this->size(), - qApp->screenAt(QCursor::pos())->geometry() - ) - ); + centerWindow(); } MainWindow::~MainWindow() @@ -86,6 +80,28 @@ void MainWindow::showUrls(const QList &urls) m_graphicsView->showFromUrlList(urls); } +void MainWindow::adjustWindowSizeBySceneRect() +{ + if (m_graphicsView->transform().m11() < 1) { + // if it scaled down by the resize policy: + QSize screenSize = qApp->screenAt(QCursor::pos())->availableSize(); + QSize sceneSize = m_graphicsView->sceneRect().toRect().size(); + QSize sceneSizeWithMargins = sceneSize + QSize(20, 20); + if (screenSize.expandedTo(sceneSize) == screenSize) { + // we can show the picture by increase the window size. + if (screenSize.expandedTo(sceneSizeWithMargins) == screenSize) { + this->resize(sceneSizeWithMargins); + } else { + this->resize(screenSize); + } + centerWindow(); + } else { + // toggle maximum + showMaximized(); + } + } +} + void MainWindow::showEvent(QShowEvent *event) { updateWidgetsPosition(); @@ -223,6 +239,18 @@ bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *r #endif // _WIN32 } +void MainWindow::centerWindow() +{ + this->setGeometry( + QStyle::alignedRect( + Qt::LeftToRight, + Qt::AlignCenter, + this->size(), + qApp->screenAt(QCursor::pos())->geometry() + ) + ); +} + void MainWindow::closeWindow() { QRect windowRect(this->geometry()); diff --git a/mainwindow.h b/mainwindow.h index a6c39e7..a90fd63 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -17,6 +17,7 @@ public: ~MainWindow() override; void showUrls(const QList &urls); + void adjustWindowSizeBySceneRect(); protected slots: void showEvent(QShowEvent *event) override; @@ -28,6 +29,7 @@ protected slots: bool nativeEvent(const QByteArray& eventType, void* message, long* result) override; + void centerWindow(); void closeWindow(); void updateWidgetsPosition(); diff --git a/resources.qrc b/resources.qrc index 4c2ddc8..608eea7 100644 --- a/resources.qrc +++ b/resources.qrc @@ -6,5 +6,6 @@ icons/zoom-original.svg icons/object-rorate-right.svg icons/view-background-checkerboard.svg + icons/app-icon.svg