From 6d8f353602e29b9f0d1bfa6182b39c11e6b7c63e Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sun, 10 Jan 2021 14:57:01 +0800 Subject: [PATCH] fix: some clazy checks --- app/aboutdialog.cpp | 2 +- app/framelesswindow.cpp | 87 ++++++++++++++++++++++++++++++++++++ app/framelesswindow.h | 3 +- app/main.cpp | 2 +- app/mainwindow.cpp | 97 +++-------------------------------------- app/mainwindow.h | 7 ++- 6 files changed, 99 insertions(+), 99 deletions(-) diff --git a/app/aboutdialog.cpp b/app/aboutdialog.cpp index 64b2e03..48c8298 100644 --- a/app/aboutdialog.cpp +++ b/app/aboutdialog.cpp @@ -131,7 +131,7 @@ SOFTWARE. m_licenseTextEdit->setText(licenseStr.join('\n').arg(qApp->applicationDisplayName(), mitLicense)); - m_3rdPartyLibsTextEdit->setText(thirdPartyLibsStr.join('\n').arg(QStringLiteral("%1")).arg(qApp->applicationDisplayName())); + m_3rdPartyLibsTextEdit->setText(thirdPartyLibsStr.join('\n').arg(QStringLiteral("%1").arg(qApp->applicationDisplayName()))); m_3rdPartyLibsTextEdit->setOpenExternalLinks(true); m_tabWidget->addTab(m_helpTextEdit, tr("&Help")); diff --git a/app/framelesswindow.cpp b/app/framelesswindow.cpp index d2fdee9..8c20782 100644 --- a/app/framelesswindow.cpp +++ b/app/framelesswindow.cpp @@ -21,3 +21,90 @@ void FramelessWindow::setCentralWidget(QWidget *widget) m_centralLayout->addWidget(widget); m_centralWidget = widget; } + +bool FramelessWindow::nativeEvent(const QByteArray &eventType, void *message, long *result) +{ +#ifdef _WIN32 + // https://stackoverflow.com/questions/43505580/qt-windows-resizable-frameless-window + // Too lazy to do this now.. just stackoverflow it and did a copy and paste.. + Q_UNUSED(eventType) + MSG* msg = static_cast(message); + + if (msg->message == WM_NCHITTEST) { + if (isMaximized()) { + return false; + } + + *result = 0; + const LONG borderWidth = 8; + RECT winrect; + GetWindowRect(reinterpret_cast(winId()), &winrect); + + // must be short to correctly work with multiple monitors (negative coordinates) + short x = msg->lParam & 0x0000FFFF; + short y = (msg->lParam & 0xFFFF0000) >> 16; + + bool resizeWidth = minimumWidth() != maximumWidth(); + bool resizeHeight = minimumHeight() != maximumHeight(); + if (resizeWidth) { + //left border + if (x >= winrect.left && x < winrect.left + borderWidth) { + *result = HTLEFT; + } + //right border + if (x < winrect.right && x >= winrect.right - borderWidth) { + *result = HTRIGHT; + } + } + if (resizeHeight) { + //bottom border + if (y < winrect.bottom && y >= winrect.bottom - borderWidth) { + *result = HTBOTTOM; + } + //top border + if (y >= winrect.top && y < winrect.top + borderWidth) { + *result = HTTOP; + } + } + if (resizeWidth && resizeHeight) { + //bottom left corner + if (x >= winrect.left && x < winrect.left + borderWidth && + y < winrect.bottom && y >= winrect.bottom - borderWidth) + { + *result = HTBOTTOMLEFT; + } + //bottom right corner + if (x < winrect.right && x >= winrect.right - borderWidth && + y < winrect.bottom && y >= winrect.bottom - borderWidth) + { + *result = HTBOTTOMRIGHT; + } + //top left corner + if (x >= winrect.left && x < winrect.left + borderWidth && + y >= winrect.top && y < winrect.top + borderWidth) + { + *result = HTTOPLEFT; + } + //top right corner + if (x < winrect.right && x >= winrect.right - borderWidth && + y >= winrect.top && y < winrect.top + borderWidth) + { + *result = HTTOPRIGHT; + } + } + + if (*result != 0) + return true; + + QWidget *action = QApplication::widgetAt(QCursor::pos()); + if (action == this) { + *result = HTCAPTION; + return true; + } + } + + return false; +#else + return QWidget::nativeEvent(eventType, message, result); +#endif // _WIN32 +} diff --git a/app/framelesswindow.h b/app/framelesswindow.h index 28ab8cc..15ea473 100644 --- a/app/framelesswindow.h +++ b/app/framelesswindow.h @@ -15,7 +15,8 @@ public: void setCentralWidget(QWidget * widget); -signals: +protected: + bool nativeEvent(const QByteArray& eventType, void* message, long* result) override; private: QVBoxLayout * m_centralLayout = nullptr; diff --git a/app/main.cpp b/app/main.cpp index 0655b4a..73c19dc 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -36,7 +36,7 @@ int main(int argc, char *argv[]) QStringList urlStrList = parser.positionalArguments(); QList urlList; - for (const QString & str : urlStrList) { + for (const QString & str : qAsConst(urlStrList)) { QUrl url = QUrl::fromLocalFile(str); if (url.isValid()) { urlList.append(url); diff --git a/app/mainwindow.cpp b/app/mainwindow.cpp index ef87b3c..de18c87 100644 --- a/app/mainwindow.cpp +++ b/app/mainwindow.cpp @@ -515,98 +515,6 @@ void MainWindow::contextMenuEvent(QContextMenuEvent *event) return FramelessWindow::contextMenuEvent(event); } -bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result) -{ -#ifdef _WIN32 - // https://stackoverflow.com/questions/43505580/qt-windows-resizable-frameless-window - // Too lazy to do this now.. just stackoverflow it and did a copy and paste.. - Q_UNUSED(eventType) - MSG* msg = static_cast(message); - - if (msg->message == WM_NCHITTEST) { - if (isMaximized()) { - return false; - } - - *result = 0; - const LONG borderWidth = 8; - RECT winrect; - GetWindowRect(reinterpret_cast(winId()), &winrect); - - // must be short to correctly work with multiple monitors (negative coordinates) - short x = msg->lParam & 0x0000FFFF; - short y = (msg->lParam & 0xFFFF0000) >> 16; - - bool resizeWidth = minimumWidth() != maximumWidth(); - bool resizeHeight = minimumHeight() != maximumHeight(); - if (resizeWidth) { - //left border - if (x >= winrect.left && x < winrect.left + borderWidth) { - *result = HTLEFT; - } - //right border - if (x < winrect.right && x >= winrect.right - borderWidth) { - *result = HTRIGHT; - } - } - if (resizeHeight) { - //bottom border - if (y < winrect.bottom && y >= winrect.bottom - borderWidth) { - *result = HTBOTTOM; - } - //top border - if (y >= winrect.top && y < winrect.top + borderWidth) { - *result = HTTOP; - } - } - if (resizeWidth && resizeHeight) { - //bottom left corner - if (x >= winrect.left && x < winrect.left + borderWidth && - y < winrect.bottom && y >= winrect.bottom - borderWidth) - { - *result = HTBOTTOMLEFT; - } - //bottom right corner - if (x < winrect.right && x >= winrect.right - borderWidth && - y < winrect.bottom && y >= winrect.bottom - borderWidth) - { - *result = HTBOTTOMRIGHT; - } - //top left corner - if (x >= winrect.left && x < winrect.left + borderWidth && - y >= winrect.top && y < winrect.top + borderWidth) - { - *result = HTTOPLEFT; - } - //top right corner - if (x < winrect.right && x >= winrect.right - borderWidth && - y >= winrect.top && y < winrect.top + borderWidth) - { - *result = HTTOPRIGHT; - } - } - - if (*result != 0) - return true; - - QWidget *action = QApplication::widgetAt(QCursor::pos()); - if (action == this) { - *result = HTCAPTION; - return true; - } - } - - return false; -#else - return FramelessWindow::nativeEvent(eventType, message, result); -#endif // _WIN32 -} - -QSize MainWindow::sizeHint() const -{ - return QSize(710, 530); -} - void MainWindow::centerWindow() { this->setGeometry( @@ -683,3 +591,8 @@ void MainWindow::toggleMaximize() showMaximized(); } } + +QSize MainWindow::sizeHint() const +{ + return QSize(710, 530); +} diff --git a/app/mainwindow.h b/app/mainwindow.h index a62c587..635e4ef 100644 --- a/app/mainwindow.h +++ b/app/mainwindow.h @@ -49,10 +49,6 @@ protected slots: void resizeEvent(QResizeEvent *event) override; void contextMenuEvent(QContextMenuEvent *event) override; - bool nativeEvent(const QByteArray& eventType, void* message, long* result) override; - - QSize sizeHint() const override; - void centerWindow(); void closeWindow(); void updateWidgetsPosition(); @@ -63,6 +59,9 @@ protected slots: void toggleFullscreen(); void toggleMaximize(); +protected: + QSize sizeHint() const override; + private: QPoint m_oldMousePos; QPropertyAnimation *m_fadeOutAnimation;