From ed3771c949eae017cc86dc31ee918203122b6cb4 Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Mon, 12 Dec 2022 09:34:46 +0800 Subject: [PATCH] replace qRound with std::round It's better to do so. Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- .../Core/framelesshelpercore_global.h | 1 + src/core/framelesshelper_win.cpp | 6 +++--- src/core/micamaterial.cpp | 2 +- src/core/utils.cpp | 14 +++++++------- src/core/utils_win.cpp | 16 ++++++++-------- src/quick/quickimageitem.cpp | 2 +- src/quick/quickstandardtitlebar.cpp | 2 +- src/quick/quickwindowborder.cpp | 2 +- src/widgets/standardsystembutton.cpp | 4 ++-- src/widgets/standardtitlebar.cpp | 2 +- 10 files changed, 26 insertions(+), 25 deletions(-) diff --git a/include/FramelessHelper/Core/framelesshelpercore_global.h b/include/FramelessHelper/Core/framelesshelpercore_global.h index 3ce0270..3f2d0a5 100644 --- a/include/FramelessHelper/Core/framelesshelpercore_global.h +++ b/include/FramelessHelper/Core/framelesshelpercore_global.h @@ -207,6 +207,7 @@ Q_NAMESPACE_EXPORT(FRAMELESSHELPER_CORE_API) [[maybe_unused]] inline constexpr const int kDefaultTitleBarFontPointSize = 11; [[maybe_unused]] inline constexpr const int kDefaultTitleBarContentsMargin = 10; [[maybe_unused]] inline constexpr const QSize kDefaultWindowIconSize = {16, 16}; +// We have to use "qRound()" here because "std::round()" is not constexpr, yet. [[maybe_unused]] inline constexpr const QSize kDefaultSystemButtonSize = {qRound(qreal(kDefaultTitleBarHeight) * 1.5), kDefaultTitleBarHeight}; [[maybe_unused]] inline constexpr const QSize kDefaultSystemButtonIconSize = kDefaultWindowIconSize; [[maybe_unused]] inline constexpr const QSize kDefaultWindowSize = {160, 160}; // Value taken from QPA. diff --git a/src/core/framelesshelper_win.cpp b/src/core/framelesshelper_win.cpp index 8ba95bd..606a4bc 100644 --- a/src/core/framelesshelper_win.cpp +++ b/src/core/framelesshelper_win.cpp @@ -1066,7 +1066,7 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me // Make the border a little wider to let the user easy to resize on corners. const qreal scaleFactor = ((isTop || isBottom) ? 2.0 : 1.0); const int frameSizeX = Utils::getResizeBorderThickness(windowId, true, true); - const int scaledFrameSizeX = qRound(qreal(frameSizeX) * scaleFactor); + const int scaledFrameSizeX = std::round(qreal(frameSizeX) * scaleFactor); const bool isLeft = (nativeLocalPos.x < scaledFrameSizeX); const bool isRight = (nativeLocalPos.x >= (width - scaledFrameSizeX)); if (dontOverrideCursor && (isTop || isBottom || isLeft || isRight)) { @@ -1145,8 +1145,8 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me const qreal newDpr = Utils::roundScaleFactor(qreal(newDpi) / defaultDpi); const QSizeF newSize = (oldSize / oldDpr * newDpr); const auto suggestedSize = reinterpret_cast(lParam); - suggestedSize->cx = qRound(newSize.width()); - suggestedSize->cy = qRound(newSize.height()); + suggestedSize->cx = std::round(newSize.width()); + suggestedSize->cy = std::round(newSize.height()); // If the window frame is visible, we need to expand the suggested size, currently // it's pure client size, we need to add the frame size to it. Windows expects a // full window size, including the window frame. diff --git a/src/core/micamaterial.cpp b/src/core/micamaterial.cpp index 3d4af8a..f5b6cc8 100644 --- a/src/core/micamaterial.cpp +++ b/src/core/micamaterial.cpp @@ -230,7 +230,7 @@ static inline void expblur(QImage &img, qreal radius, const bool improvedQuality // the cutOffIntensity static constexpr const qreal cutOffIntensity = 2.0; const int alpha = ((radius <= qreal(1e-5)) ? ((1 << aprec) - 1) : - qRound((1 << aprec) * (1 - qPow(cutOffIntensity / qreal(255), qreal(1) / radius)))); + std::round((1 << aprec) * (1 - qPow(cutOffIntensity / qreal(255), qreal(1) / radius)))); int img_height = img.height(); for (int row = 0; row != img_height; ++row) { diff --git a/src/core/utils.cpp b/src/core/utils.cpp index 457d3bf..bb20802 100644 --- a/src/core/utils.cpp +++ b/src/core/utils.cpp @@ -214,8 +214,8 @@ void Utils::moveWindowToDesktopCenter(const GetWindowScreenCallback &getWindowSc } const QSize screenSize = (considerTaskBar ? screen->availableVirtualSize() : screen->virtualSize()); const QPoint offset = (considerTaskBar ? screen->availableVirtualGeometry().topLeft() : QPoint(0, 0)); - const int newX = qRound(qreal(screenSize.width() - windowSize.width()) / 2.0); - const int newY = qRound(qreal(screenSize.height() - windowSize.height()) / 2.0); + const int newX = std::round(qreal(screenSize.width() - windowSize.width()) / 2.0); + const int newY = std::round(qreal(screenSize.height() - windowSize.height()) / 2.0); setWindowPosition(QPoint(newX + offset.x(), newY + offset.y())); } @@ -315,20 +315,20 @@ qreal Utils::roundScaleFactor(const qreal factor) static const auto policy = QGuiApplication::highDpiScaleFactorRoundingPolicy(); switch (policy) { case Qt::HighDpiScaleFactorRoundingPolicy::Round: - return qRound(factor); + return std::round(factor); case Qt::HighDpiScaleFactorRoundingPolicy::Ceil: return qCeil(factor); case Qt::HighDpiScaleFactorRoundingPolicy::Floor: return qFloor(factor); case Qt::HighDpiScaleFactorRoundingPolicy::RoundPreferFloor: - return (((factor - qreal(int(factor))) >= qreal(0.75)) ? qRound(factor) : qFloor(factor)); + return (((factor - qreal(int(factor))) >= qreal(0.75)) ? std::round(factor) : qFloor(factor)); case Qt::HighDpiScaleFactorRoundingPolicy::PassThrough: case Qt::HighDpiScaleFactorRoundingPolicy::Unset: // According to Qt source code, this enum value is the same with PassThrough. return factor; } return 1; # else // (QT_VERSION < QT_VERSION_CHECK(5, 14, 0)) - return qRound(factor); + return std::round(factor); # endif // (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) #else // (!FRAMELESSHELPER_CORE_NO_PRIVATE && (QT_VERSION >= QT_VERSION_CHECK(6, 2, 1))) return QHighDpiScaling::roundScaleFactor(factor); @@ -342,7 +342,7 @@ int Utils::toNativePixels(const QWindow *window, const int value) return 0; } #ifdef FRAMELESSHELPER_CORE_NO_PRIVATE - return qRound(qreal(value) * window->devicePixelRatio()); + return std::round(qreal(value) * window->devicePixelRatio()); #else // !FRAMELESSHELPER_CORE_NO_PRIVATE return QHighDpi::toNativePixels(value, window); #endif // FRAMELESSHELPER_CORE_NO_PRIVATE @@ -394,7 +394,7 @@ int Utils::fromNativePixels(const QWindow *window, const int value) return 0; } #ifdef FRAMELESSHELPER_CORE_NO_PRIVATE - return qRound(qreal(value) / window->devicePixelRatio()); + return std::round(qreal(value) / window->devicePixelRatio()); #else // !FRAMELESSHELPER_CORE_NO_PRIVATE return QHighDpi::fromNativePixels(value, window); #endif // FRAMELESSHELPER_CORE_NO_PRIVATE diff --git a/src/core/utils_win.cpp b/src/core/utils_win.cpp index 2dadd09..3dd6c12 100644 --- a/src/core/utils_win.cpp +++ b/src/core/utils_win.cpp @@ -1033,7 +1033,7 @@ static inline void moveWindowToMonitor(const HWND hwnd, const MONITORINFOEXW &ac static constexpr const auto defaultDpi = qreal(USER_DEFAULT_SCREEN_DPI); const qreal currentDpr = (qreal(Utils::getPrimaryScreenDpi(horizontal)) / defaultDpi); const qreal requestedDpr = (qreal(dpi) / defaultDpi); - return qRound(qreal(GetSystemMetrics(index)) / currentDpr * requestedDpr); + return std::round(qreal(GetSystemMetrics(index)) / currentDpr * requestedDpr); } [[nodiscard]] static inline int getSystemMetrics2(const WId windowId, const int index, @@ -1053,7 +1053,7 @@ static inline void moveWindowToMonitor(const HWND hwnd, const MONITORINFOEXW &ac // GetSystemMetrics() will always return a scaled value, so if we want to get an unscaled // one, we have to calculate it ourself. const qreal dpr = (scaled ? qreal(1) : (qreal(realDpi) / qreal(USER_DEFAULT_SCREEN_DPI))); - return qRound(qreal(GetSystemMetrics(index)) / dpr); + return std::round(qreal(GetSystemMetrics(index)) / dpr); } [[maybe_unused]] [[nodiscard]] static inline @@ -1547,7 +1547,7 @@ void Utils::syncWmPaintWithDwm() Q_ASSERT(m >= 0); Q_ASSERT(m < period); const qreal m_ms = (1000.0 * qreal(m) / qreal(freq.QuadPart)); - Sleep(static_cast(qRound(m_ms))); + Sleep(static_cast(std::round(m_ms))); if (API_CALL_FUNCTION4(timeEndPeriod, ms_granularity) != TIMERR_NOERROR) { WARNING << "timeEndPeriod() failed."; } @@ -1584,7 +1584,7 @@ quint32 Utils::getPrimaryScreenDpi(const bool horizontal) _DEVICE_SCALE_FACTOR factor = _DEVICE_SCALE_FACTOR_INVALID; const HRESULT hr = API_CALL_FUNCTION4(GetScaleFactorForMonitor, hMonitor, &factor); if (SUCCEEDED(hr) && (factor != _DEVICE_SCALE_FACTOR_INVALID)) { - return quint32(qRound(qreal(USER_DEFAULT_SCREEN_DPI) * qreal(factor) / qreal(100))); + return quint32(std::round(qreal(USER_DEFAULT_SCREEN_DPI) * qreal(factor) / qreal(100))); } else { WARNING << __getSystemErrorMessage(kGetScaleFactorForMonitor, hr); } @@ -1638,7 +1638,7 @@ quint32 Utils::getPrimaryScreenDpi(const bool horizontal) d2dFactory->GetDesktopDpi(&dpiX, &dpiY); QT_WARNING_POP if ((dpiX > 0.0f) && (dpiY > 0.0f)) { - return (horizontal ? quint32(qRound(dpiX)) : quint32(qRound(dpiY))); + return (horizontal ? quint32(std::round(dpiX)) : quint32(std::round(dpiY))); } else { WARNING << "GetDesktopDpi() failed."; } @@ -1816,7 +1816,7 @@ quint32 Utils::getFrameBorderThicknessForDpi(const quint32 dpi) return 0; } const qreal dpr = (qreal(dpi) / qreal(USER_DEFAULT_SCREEN_DPI)); - return qRound(qreal(kDefaultWindowFrameBorderThickness) * dpr); + return std::round(qreal(kDefaultWindowFrameBorderThickness) * dpr); } quint32 Utils::getFrameBorderThickness(const WId windowId, const bool scaled) @@ -1840,10 +1840,10 @@ quint32 Utils::getFrameBorderThickness(const WId windowId, const bool scaled) _DWMWA_VISIBLE_FRAME_BORDER_THICKNESS, &value, sizeof(value)); if (SUCCEEDED(hr)) { const qreal dpr = (scaled ? 1.0 : scaleFactor); - return qRound(qreal(value) / dpr); + return std::round(qreal(value) / dpr); } else { const qreal dpr = (scaled ? scaleFactor : 1.0); - return qRound(qreal(kDefaultWindowFrameBorderThickness) * dpr); + return std::round(qreal(kDefaultWindowFrameBorderThickness) * dpr); } } diff --git a/src/quick/quickimageitem.cpp b/src/quick/quickimageitem.cpp index c2e5c1a..395a8e0 100644 --- a/src/quick/quickimageitem.cpp +++ b/src/quick/quickimageitem.cpp @@ -214,7 +214,7 @@ QRect QuickImageItemPrivate::paintArea() const #if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) const QSize size = q->size().toSize(); #else - const QSize size = {qRound(q->width()), qRound(q->height())}; + const QSize size = {int(std::round(q->width())), int(std::round(q->height()))}; #endif return {QPoint(0, 0), size}; } diff --git a/src/quick/quickstandardtitlebar.cpp b/src/quick/quickstandardtitlebar.cpp index 3d99eb7..bdf8cba 100644 --- a/src/quick/quickstandardtitlebar.cpp +++ b/src/quick/quickstandardtitlebar.cpp @@ -407,7 +407,7 @@ bool QuickStandardTitleBar::mouseEventHandler(QMouseEvent *event) } FramelessQuickHelper::get(this)->showSystemMenu([this, button, &scenePos]() -> QPoint { if (button == Qt::LeftButton) { - return {0, qRound(height())}; + return {0, int(std::round(height()))}; } return scenePos; }()); diff --git a/src/quick/quickwindowborder.cpp b/src/quick/quickwindowborder.cpp index d4c986b..a7923c1 100644 --- a/src/quick/quickwindowborder.cpp +++ b/src/quick/quickwindowborder.cpp @@ -263,7 +263,7 @@ void QuickWindowBorder::setThickness(const qreal value) if (qFuzzyCompare(thickness(), value)) { return; } - d->m_borderPainter->setThickness(qRound(value)); + d->m_borderPainter->setThickness(std::round(value)); } void QuickWindowBorder::setEdges(const QuickGlobal::WindowEdges value) diff --git a/src/widgets/standardsystembutton.cpp b/src/widgets/standardsystembutton.cpp index 7adda32..5537195 100644 --- a/src/widgets/standardsystembutton.cpp +++ b/src/widgets/standardsystembutton.cpp @@ -183,10 +183,10 @@ void StandardSystemButtonPrivate::setHovered(const bool value) static const int h = kDefaultSystemButtonSize.height(); if (const QWidget * const window = q->window()) { if (Utils::windowStatesToWindowState(window->windowState()) == Qt::WindowMaximized) { - return qRound(h * 0.5); + return std::round(h * 0.5); } } - return -qRound(h * 1.3); + return -std::round(h * 1.3); }(); QToolTip::showText(q->mapToGlobal(QPoint(-2, yPos)), toolTip, q, q->geometry()); } diff --git a/src/widgets/standardtitlebar.cpp b/src/widgets/standardtitlebar.cpp index 7441fc3..2e8ac71 100644 --- a/src/widgets/standardtitlebar.cpp +++ b/src/widgets/standardtitlebar.cpp @@ -342,7 +342,7 @@ QRect StandardTitleBarPrivate::windowIconRect() const { Q_Q(const StandardTitleBar); const QSize size = windowIconSize(); - const int y = qRound(qreal(q->height() - size.height()) / qreal(2)); + const int y = std::round(qreal(q->height() - size.height()) / qreal(2)); return {QPoint(kDefaultTitleBarContentsMargin, y), size}; }