QCursor: Add equality operators
[ChangeLog][QtGui][QCursor] Added equality operators. Change-Id: Iedeab4673b2a77ad2e51a0d50297b12bba3b9505 Reviewed-by: Marc Mutz <marc.mutz@kdab.com> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
parent
464c0a5824
commit
6445aa4b06
@ -471,6 +471,54 @@ QCursor::QCursor(Qt::CursorShape shape)
|
|||||||
setShape(shape);
|
setShape(shape);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\fn bool operator==(const QCursor &lhs, const QCursor &rhs)
|
||||||
|
\relates QCursor
|
||||||
|
\since 5.10
|
||||||
|
|
||||||
|
Equality operator. Returns \c true if \a lhs and \a rhs
|
||||||
|
have the same \l{QCursor::}{shape()} and, in the case of
|
||||||
|
\l{Qt::BitmapCursor}{bitmap cursors}, the same \l{QCursor::}{hotSpot()}
|
||||||
|
and either the same \l{QCursor::}{pixmap()} or the same
|
||||||
|
\l{QCursor::}{bitmap()} and \l{QCursor::}{mask()}.
|
||||||
|
|
||||||
|
\note When comparing bitmap cursors, this function only
|
||||||
|
compares the bitmaps' \l{QPixmap::cacheKey()}{cache keys},
|
||||||
|
not each pixel.
|
||||||
|
|
||||||
|
\sa operator!=(const QCursor &lhs, const QCursor &rhs)
|
||||||
|
*/
|
||||||
|
bool operator==(const QCursor &lhs, const QCursor &rhs) Q_DECL_NOTHROW
|
||||||
|
{
|
||||||
|
if (lhs.d == rhs.d)
|
||||||
|
return true; // Copy or same shape
|
||||||
|
|
||||||
|
// Check pixmaps or bitmaps cache keys. Notice that having BitmapCursor
|
||||||
|
// shape implies either non-null pixmap or non-null bitmap and mask
|
||||||
|
if (lhs.shape() == Qt::BitmapCursor && rhs.shape() == Qt::BitmapCursor
|
||||||
|
&& lhs.hotSpot() == rhs.hotSpot()) {
|
||||||
|
if (!lhs.d->pixmap.isNull())
|
||||||
|
return lhs.d->pixmap.cacheKey() == rhs.d->pixmap.cacheKey();
|
||||||
|
|
||||||
|
if (!rhs.d->pixmap.isNull())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return lhs.d->bm->cacheKey() == rhs.d->bm->cacheKey()
|
||||||
|
&& lhs.d->bmm->cacheKey() == rhs.d->bmm->cacheKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\fn bool operator!=(const QCursor &lhs, const QCursor &rhs)
|
||||||
|
\relates QCursor
|
||||||
|
\since 5.10
|
||||||
|
|
||||||
|
Inequality operator. Returns the equivalent of !(\a lhs == \a rhs).
|
||||||
|
|
||||||
|
\sa operator==(const QCursor &lhs, const QCursor &rhs)
|
||||||
|
*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Returns the cursor shape identifier. The return value is one of
|
Returns the cursor shape identifier. The return value is one of
|
||||||
|
@ -112,10 +112,14 @@ public:
|
|||||||
inline static void setPos(QScreen *screen, const QPoint &p) { setPos(screen, p.x(), p.y()); }
|
inline static void setPos(QScreen *screen, const QPoint &p) { setPos(screen, p.x(), p.y()); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
friend Q_GUI_EXPORT bool operator==(const QCursor &lhs, const QCursor &rhs) Q_DECL_NOTHROW;
|
||||||
QCursorData *d;
|
QCursorData *d;
|
||||||
};
|
};
|
||||||
Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QCursor)
|
Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QCursor)
|
||||||
|
|
||||||
|
Q_GUI_EXPORT bool operator==(const QCursor &lhs, const QCursor &rhs) Q_DECL_NOTHROW;
|
||||||
|
inline bool operator!=(const QCursor &lhs, const QCursor &rhs) Q_DECL_NOTHROW { return !(lhs == rhs); }
|
||||||
|
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
QCursor stream functions
|
QCursor stream functions
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
@ -2,6 +2,7 @@ TEMPLATE=subdirs
|
|||||||
SUBDIRS=\
|
SUBDIRS=\
|
||||||
qbackingstore \
|
qbackingstore \
|
||||||
qclipboard \
|
qclipboard \
|
||||||
|
qcursor \
|
||||||
qdrag \
|
qdrag \
|
||||||
qevent \
|
qevent \
|
||||||
qfileopenevent \
|
qfileopenevent \
|
||||||
|
6
tests/auto/gui/kernel/qcursor/qcursor.pro
Normal file
6
tests/auto/gui/kernel/qcursor/qcursor.pro
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
QT += testlib
|
||||||
|
TARGET = tst_qcursor
|
||||||
|
CONFIG += testcase
|
||||||
|
CONFIG -= app_bundle
|
||||||
|
|
||||||
|
SOURCES += tst_qcursor.cpp
|
123
tests/auto/gui/kernel/qcursor/tst_qcursor.cpp
Normal file
123
tests/auto/gui/kernel/qcursor/tst_qcursor.cpp
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2017 The Qt Company Ltd.
|
||||||
|
** Contact: https://www.qt.io/licensing/
|
||||||
|
**
|
||||||
|
** This file is part of the test suite of the Qt Toolkit.
|
||||||
|
**
|
||||||
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
||||||
|
** Commercial License Usage
|
||||||
|
** Licensees holding valid commercial Qt licenses may use this file in
|
||||||
|
** accordance with the commercial license agreement provided with the
|
||||||
|
** Software or, alternatively, in accordance with the terms contained in
|
||||||
|
** a written agreement between you and The Qt Company. For licensing terms
|
||||||
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||||
|
** information use the contact form at https://www.qt.io/contact-us.
|
||||||
|
**
|
||||||
|
** GNU General Public License Usage
|
||||||
|
** Alternatively, this file may be used under the terms of the GNU
|
||||||
|
** General Public License version 3 as published by the Free Software
|
||||||
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||||
|
** included in the packaging of this file. Please review the following
|
||||||
|
** information to ensure the GNU General Public License requirements will
|
||||||
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||||
|
**
|
||||||
|
** $QT_END_LICENSE$
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <QtTest/QTest>
|
||||||
|
#include <qcursor.h>
|
||||||
|
#include <qpixmap.h>
|
||||||
|
#include <qbitmap.h>
|
||||||
|
|
||||||
|
class tst_QCursor : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void equality();
|
||||||
|
};
|
||||||
|
|
||||||
|
#define VERIFY_EQUAL(lhs, rhs) \
|
||||||
|
QVERIFY(lhs == rhs); \
|
||||||
|
QVERIFY(rhs == lhs); \
|
||||||
|
QVERIFY(!(rhs != lhs)); \
|
||||||
|
QVERIFY(!(lhs != rhs))
|
||||||
|
|
||||||
|
#define VERIFY_DIFFERENT(lhs, rhs) \
|
||||||
|
QVERIFY(lhs != rhs); \
|
||||||
|
QVERIFY(rhs != lhs); \
|
||||||
|
QVERIFY(!(rhs == lhs)); \
|
||||||
|
QVERIFY(!(lhs == rhs))
|
||||||
|
|
||||||
|
void tst_QCursor::equality()
|
||||||
|
{
|
||||||
|
VERIFY_EQUAL(QCursor(), QCursor());
|
||||||
|
VERIFY_EQUAL(QCursor(Qt::CrossCursor), QCursor(Qt::CrossCursor));
|
||||||
|
VERIFY_DIFFERENT(QCursor(Qt::CrossCursor), QCursor());
|
||||||
|
|
||||||
|
// Shape
|
||||||
|
QCursor shapeCursor(Qt::WaitCursor);
|
||||||
|
VERIFY_EQUAL(shapeCursor, shapeCursor);
|
||||||
|
QCursor shapeCursorCopy(shapeCursor);
|
||||||
|
VERIFY_EQUAL(shapeCursor, shapeCursorCopy);
|
||||||
|
shapeCursorCopy.setShape(Qt::DragMoveCursor);
|
||||||
|
VERIFY_DIFFERENT(shapeCursor, shapeCursorCopy);
|
||||||
|
shapeCursorCopy.setShape(shapeCursor.shape());
|
||||||
|
VERIFY_EQUAL(shapeCursor, shapeCursorCopy);
|
||||||
|
|
||||||
|
// Pixmap
|
||||||
|
QPixmap pixmap(16, 16);
|
||||||
|
QCursor pixmapCursor(pixmap);
|
||||||
|
VERIFY_EQUAL(pixmapCursor, pixmapCursor);
|
||||||
|
VERIFY_EQUAL(pixmapCursor, QCursor(pixmapCursor));
|
||||||
|
VERIFY_EQUAL(pixmapCursor, QCursor(pixmap));
|
||||||
|
VERIFY_DIFFERENT(pixmapCursor, QCursor());
|
||||||
|
VERIFY_DIFFERENT(pixmapCursor, QCursor(pixmap, 5, 5));
|
||||||
|
VERIFY_DIFFERENT(pixmapCursor, QCursor(QPixmap(16, 16)));
|
||||||
|
VERIFY_DIFFERENT(pixmapCursor, shapeCursor);
|
||||||
|
|
||||||
|
// Bitmap & mask
|
||||||
|
QBitmap bitmap(16, 16);
|
||||||
|
QBitmap mask(16, 16);
|
||||||
|
QCursor bitmapCursor(bitmap, mask);
|
||||||
|
VERIFY_EQUAL(bitmapCursor, bitmapCursor);
|
||||||
|
VERIFY_EQUAL(bitmapCursor, QCursor(bitmapCursor));
|
||||||
|
VERIFY_EQUAL(bitmapCursor, QCursor(bitmap, mask));
|
||||||
|
VERIFY_DIFFERENT(bitmapCursor, QCursor());
|
||||||
|
VERIFY_DIFFERENT(bitmapCursor, QCursor(bitmap, mask, 5, 5));
|
||||||
|
VERIFY_DIFFERENT(bitmapCursor, QCursor(bitmap, QBitmap(16, 16)));
|
||||||
|
VERIFY_DIFFERENT(bitmapCursor, QCursor(QBitmap(16, 16), mask));
|
||||||
|
VERIFY_DIFFERENT(bitmapCursor, shapeCursor);
|
||||||
|
VERIFY_DIFFERENT(bitmapCursor, pixmapCursor);
|
||||||
|
|
||||||
|
// Empty pixmap
|
||||||
|
QPixmap emptyPixmap;
|
||||||
|
QCursor emptyPixmapCursor(emptyPixmap);
|
||||||
|
QCOMPARE(emptyPixmapCursor.shape(), Qt::ArrowCursor);
|
||||||
|
VERIFY_EQUAL(emptyPixmapCursor, QCursor());
|
||||||
|
VERIFY_EQUAL(emptyPixmapCursor, QCursor(emptyPixmap, 5, 5));
|
||||||
|
VERIFY_DIFFERENT(emptyPixmapCursor, shapeCursor);
|
||||||
|
VERIFY_DIFFERENT(emptyPixmapCursor, pixmapCursor);
|
||||||
|
VERIFY_DIFFERENT(emptyPixmapCursor, bitmapCursor);
|
||||||
|
|
||||||
|
// Empty bitmap & mask
|
||||||
|
QBitmap emptyBitmap;
|
||||||
|
QCursor emptyBitmapCursor(emptyBitmap, emptyBitmap);
|
||||||
|
QCOMPARE(emptyBitmapCursor.shape(), Qt::ArrowCursor);
|
||||||
|
VERIFY_EQUAL(emptyBitmapCursor, QCursor());
|
||||||
|
VERIFY_EQUAL(emptyBitmapCursor, QCursor(emptyBitmap, emptyBitmap, 5, 5));
|
||||||
|
VERIFY_EQUAL(emptyBitmapCursor, QCursor(emptyBitmap, mask));
|
||||||
|
VERIFY_EQUAL(emptyBitmapCursor, QCursor(bitmap, emptyBitmap));
|
||||||
|
VERIFY_EQUAL(emptyBitmapCursor, emptyPixmapCursor);
|
||||||
|
VERIFY_DIFFERENT(emptyBitmapCursor, shapeCursor);
|
||||||
|
VERIFY_DIFFERENT(emptyBitmapCursor, pixmapCursor);
|
||||||
|
VERIFY_DIFFERENT(emptyBitmapCursor, bitmapCursor);
|
||||||
|
}
|
||||||
|
|
||||||
|
#undef VERIFY_EQUAL
|
||||||
|
#undef VERIFY_DIFFERENT
|
||||||
|
|
||||||
|
QTEST_MAIN(tst_QCursor)
|
||||||
|
#include "tst_qcursor.moc"
|
Loading…
Reference in New Issue
Block a user