| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2019 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #ifndef HIGHDPI_H |
| 30 | #define HIGHDPI_H |
| 31 | |
| 32 | #include <QtCore/qbytearray.h> |
| 33 | #include <QtCore/qpoint.h> |
| 34 | #include <QtCore/qrect.h> |
| 35 | #include <QtCore/qsize.h> |
| 36 | |
| 37 | // Helpers for comparing geometries a that may go through scaling in the |
| 38 | // platform plugin with fuzz (pass rounded-down device pixel ratios or |
| 39 | // scaling factors). Error message for use with QVERIFY2() are also provided. |
| 40 | |
| 41 | class HighDpi |
| 42 | { |
| 43 | public: |
| 44 | HighDpi() = delete; |
| 45 | HighDpi(const HighDpi &) = delete; |
| 46 | HighDpi &operator=(const HighDpi &) = delete; |
| 47 | HighDpi(HighDpi &&) = delete; |
| 48 | HighDpi &operator=(HighDpi &&) = delete; |
| 49 | ~HighDpi() = delete; |
| 50 | |
| 51 | static int manhattanDelta(const QPoint &p1, const QPoint p2) |
| 52 | { |
| 53 | return (p1 - p2).manhattanLength(); |
| 54 | } |
| 55 | |
| 56 | static bool fuzzyCompare(const QPoint &p1, const QPoint p2, int fuzz) |
| 57 | { |
| 58 | return manhattanDelta(p1, p2) <= fuzz; |
| 59 | } |
| 60 | |
| 61 | static QByteArray msgPointMismatch(const QPoint &p1, const QPoint p2) |
| 62 | { |
| 63 | return QByteArray::number(p1.x()) + ',' + QByteArray::number(p1.y()) |
| 64 | + " != " + QByteArray::number(p2.x()) + ',' + QByteArray::number(p2.y()) |
| 65 | + ", manhattanLength=" + QByteArray::number(manhattanDelta(p1, p2)); |
| 66 | } |
| 67 | |
| 68 | // Compare a size that may go through scaling in the platform plugin with fuzz. |
| 69 | |
| 70 | static inline int manhattanDelta(const QSize &s1, const QSize &s2) |
| 71 | { |
| 72 | return qAbs(t: s1.width() - s2.width()) + qAbs(t: s1.height() - s2.height()); |
| 73 | } |
| 74 | |
| 75 | static inline bool fuzzyCompare(const QSize &s1, const QSize &s2, int fuzz) |
| 76 | { |
| 77 | return manhattanDelta(s1, s2) <= fuzz; |
| 78 | } |
| 79 | |
| 80 | static QByteArray msgSizeMismatch(const QSize &s1, const QSize &s2) |
| 81 | { |
| 82 | return QByteArray::number(s1.width()) + 'x' + QByteArray::number(s1.height()) |
| 83 | + " != " + QByteArray::number(s2.width()) + 'x' + QByteArray::number(s2.height()) |
| 84 | + ", manhattanLength=" + QByteArray::number(manhattanDelta(s1, s2)); |
| 85 | } |
| 86 | |
| 87 | // Compare a geometry that may go through scaling in the platform plugin with fuzz. |
| 88 | |
| 89 | static inline bool fuzzyCompare(const QRect &r1, const QRect &r2, int fuzz) |
| 90 | { |
| 91 | return manhattanDelta(p1: r1.topLeft(), p2: r2.topLeft()) <= fuzz |
| 92 | && manhattanDelta(s1: r1.size(), s2: r2.size()) <= fuzz; |
| 93 | } |
| 94 | |
| 95 | static QByteArray msgRectMismatch(const QRect &r1, const QRect &r2) |
| 96 | { |
| 97 | return formatRect(r: r1) + " != " + formatRect(r: r2); |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | static QByteArray formatRect(const QRect &r) |
| 102 | { |
| 103 | return QByteArray::number(r.width()) + 'x' + QByteArray::number(r.height()) |
| 104 | + (r.left() < 0 ? '-' : '+') + QByteArray::number(r.left()) |
| 105 | + (r.top() < 0 ? '-' : '+') + QByteArray::number(r.top()); |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | #endif // HIGHDPI_H |
| 110 | |