From 1563c38a4b46ad427907b544508e888efbac9772 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sat, 3 Aug 2019 09:27:49 +0300 Subject: [PATCH] QPoint/F: add transposed() For symmetry with QSize and QRect and because there were some users in Qt. Port those users. [ChangeLog][QtCore][QPoint/QPointF] Added transposed(). Change-Id: If4f23dbcf7d67983a6b1885e0d1d538115b49e2b Reviewed-by: Edward Welbourne Reviewed-by: Lars Knoll --- src/corelib/tools/qpoint.cpp | 23 +++++++++++++++++++ src/corelib/tools/qpoint.h | 4 ++++ .../platforms/xcb/qxcbconnection_xi2.cpp | 6 ++--- src/plugins/platforms/xcb/qxcbwindow.cpp | 2 +- .../auto/corelib/tools/qpoint/tst_qpoint.cpp | 7 ++++++ .../corelib/tools/qpointf/tst_qpointf.cpp | 7 ++++++ 6 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/corelib/tools/qpoint.cpp b/src/corelib/tools/qpoint.cpp index e98eaac4fc2..432fb332974 100644 --- a/src/corelib/tools/qpoint.cpp +++ b/src/corelib/tools/qpoint.cpp @@ -138,6 +138,17 @@ QT_BEGIN_NAMESPACE \sa y(), setX() */ +/*! + \fn QPoint::transposed() const + \since 5.14 + + Returns a point with x and y coordinates exchanged: + \code + QPoint{1, 2}.transposed() // {2, 1} + \endcode + + \sa x(), y(), setX(), setY() +*/ /*! \fn int &QPoint::rx() @@ -582,6 +593,18 @@ QDebug operator<<(QDebug dbg, const QPointF &p) \sa y(), setX() */ +/*! + \fn QPointF::transposed() const + \since 5.14 + + Returns a point with x and y coordinates exchanged: + \code + QPointF{1.0, 2.0}.transposed() // {2.0, 1.0} + \endcode + + \sa x(), y(), setX(), setY() +*/ + /*! \fn qreal& QPointF::rx() diff --git a/src/corelib/tools/qpoint.h b/src/corelib/tools/qpoint.h index 34df673b937..fe952f95da4 100644 --- a/src/corelib/tools/qpoint.h +++ b/src/corelib/tools/qpoint.h @@ -64,6 +64,8 @@ public: Q_DECL_CONSTEXPR inline int manhattanLength() const; + Q_DECL_CONSTEXPR QPoint transposed() const noexcept { return {yp, xp}; } + Q_DECL_RELAXED_CONSTEXPR inline int &rx(); Q_DECL_RELAXED_CONSTEXPR inline int &ry(); @@ -232,6 +234,8 @@ public: Q_DECL_RELAXED_CONSTEXPR inline void setX(qreal x); Q_DECL_RELAXED_CONSTEXPR inline void setY(qreal y); + Q_DECL_CONSTEXPR QPointF transposed() const noexcept { return {yp, xp}; } + Q_DECL_RELAXED_CONSTEXPR inline qreal &rx(); Q_DECL_RELAXED_CONSTEXPR inline qreal &ry(); diff --git a/src/plugins/platforms/xcb/qxcbconnection_xi2.cpp b/src/plugins/platforms/xcb/qxcbconnection_xi2.cpp index bc09fe2f918..46391854165 100644 --- a/src/plugins/platforms/xcb/qxcbconnection_xi2.cpp +++ b/src/plugins/platforms/xcb/qxcbconnection_xi2.cpp @@ -997,8 +997,8 @@ void QXcbConnection::xi2HandleScrollEvent(void *event, ScrollingDevice &scrollin QPoint global(fixed1616ToReal(xiDeviceEvent->root_x), fixed1616ToReal(xiDeviceEvent->root_y)); Qt::KeyboardModifiers modifiers = keyboard()->translateModifiers(xiDeviceEvent->mods.effective); if (modifiers & Qt::AltModifier) { - std::swap(angleDelta.rx(), angleDelta.ry()); - std::swap(rawDelta.rx(), rawDelta.ry()); + angleDelta = angleDelta.transposed(); + rawDelta = rawDelta.transposed(); } qCDebug(lcQpaXInputEvents) << "scroll wheel @ window pos" << local << "delta px" << rawDelta << "angle" << angleDelta; QWindowSystemInterface::handleWheelEvent(platformWindow->window(), xiDeviceEvent->time, local, global, rawDelta, angleDelta, modifiers); @@ -1024,7 +1024,7 @@ void QXcbConnection::xi2HandleScrollEvent(void *event, ScrollingDevice &scrollin QPoint global(fixed1616ToReal(xiDeviceEvent->root_x), fixed1616ToReal(xiDeviceEvent->root_y)); Qt::KeyboardModifiers modifiers = keyboard()->translateModifiers(xiDeviceEvent->mods.effective); if (modifiers & Qt::AltModifier) - std::swap(angleDelta.rx(), angleDelta.ry()); + angleDelta = angleDelta.transposed(); qCDebug(lcQpaXInputEvents) << "scroll wheel (button" << xiDeviceEvent->detail << ") @ window pos" << local << "delta angle" << angleDelta; QWindowSystemInterface::handleWheelEvent(platformWindow->window(), xiDeviceEvent->time, local, global, QPoint(), angleDelta, modifiers); } diff --git a/src/plugins/platforms/xcb/qxcbwindow.cpp b/src/plugins/platforms/xcb/qxcbwindow.cpp index 1d1a4eea1ce..97da4207983 100644 --- a/src/plugins/platforms/xcb/qxcbwindow.cpp +++ b/src/plugins/platforms/xcb/qxcbwindow.cpp @@ -1908,7 +1908,7 @@ void QXcbWindow::handleButtonPressEvent(int event_x, int event_y, int root_x, in else if (detail == 7) angleDelta.setX(-120); if (modifiers & Qt::AltModifier) - std::swap(angleDelta.rx(), angleDelta.ry()); + angleDelta = angleDelta.transposed(); QWindowSystemInterface::handleWheelEvent(window(), timestamp, local, global, QPoint(), angleDelta, modifiers); #if QT_CONFIG(xcb_xinput) } diff --git a/tests/auto/corelib/tools/qpoint/tst_qpoint.cpp b/tests/auto/corelib/tools/qpoint/tst_qpoint.cpp index 8e184f3ef36..f25492d2db1 100644 --- a/tests/auto/corelib/tools/qpoint/tst_qpoint.cpp +++ b/tests/auto/corelib/tools/qpoint/tst_qpoint.cpp @@ -42,6 +42,8 @@ private slots: void getSet_data(); void getSet(); + void transposed(); + void rx(); void ry(); @@ -126,6 +128,11 @@ void tst_QPoint::getSet() QCOMPARE(point.y(), i); } +void tst_QPoint::transposed() +{ + QCOMPARE(QPoint(1, 2).transposed(), QPoint(2, 1)); +} + void tst_QPoint::rx() { const QPoint originalPoint(-1, 0); diff --git a/tests/auto/corelib/tools/qpointf/tst_qpointf.cpp b/tests/auto/corelib/tools/qpointf/tst_qpointf.cpp index d4ccdf7ba60..e78a8e30823 100644 --- a/tests/auto/corelib/tools/qpointf/tst_qpointf.cpp +++ b/tests/auto/corelib/tools/qpointf/tst_qpointf.cpp @@ -46,6 +46,8 @@ private slots: void getSet_data(); void getSet(); + void transposed(); + void rx(); void ry(); @@ -154,6 +156,11 @@ void tst_QPointF::getSet() QCOMPARE(point.y(), r); } +void tst_QPointF::transposed() +{ + QCOMPARE(QPointF(1, 2).transposed(), QPointF(2, 1)); +} + void tst_QPointF::rx() { const QPointF originalPoint(-1, 0);