1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QDEBUG_P_H |
5 | #define QDEBUG_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists for the convenience |
12 | // of other Qt classes. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtCore/private/qglobal_p.h> |
19 | #include "QtCore/qdebug.h" |
20 | #include "QtCore/qmetaobject.h" |
21 | #include "QtCore/qflags.h" |
22 | #include "QtCore/qbytearray.h" |
23 | |
24 | QT_BEGIN_NAMESPACE |
25 | |
26 | class QRect; |
27 | |
28 | namespace QtDebugUtils { |
29 | |
30 | Q_CORE_EXPORT QByteArray toPrintable(const char *data, qint64 len, qsizetype maxSize); |
31 | |
32 | // inline helpers for formatting basic classes. |
33 | |
34 | template <class Point> |
35 | static inline void formatQPoint(QDebug &debug, const Point &point) |
36 | { |
37 | debug << point.x() << ',' << point.y(); |
38 | } |
39 | |
40 | template <class Size> |
41 | static inline void formatQSize(QDebug &debug, const Size &size) |
42 | { |
43 | debug << size.width() << ", " << size.height(); |
44 | } |
45 | |
46 | template <class Rect> |
47 | static inline void formatQRect(QDebug &debug, const Rect &rect) |
48 | { |
49 | debug << rect.x() << ',' << rect.y() << ' '; |
50 | if constexpr (std::is_same_v<Rect, QRect>) { |
51 | // QRect may overflow. Calculate width and height in higher precision. |
52 | const qint64 w = qint64(rect.right()) - rect.left() + 1; |
53 | const qint64 h = qint64(rect.bottom()) - rect.top() + 1; |
54 | debug << w << 'x' << h; |
55 | } else { |
56 | debug << rect.width() << 'x' << rect.height(); |
57 | } |
58 | } |
59 | |
60 | template <class Margins> |
61 | static inline void formatQMargins(QDebug &debug, const Margins &margins) |
62 | { |
63 | debug << margins.left() << ", " << margins.top() << ", " << margins.right() |
64 | << ", " << margins.bottom(); |
65 | } |
66 | |
67 | #ifndef QT_NO_QOBJECT |
68 | template <class QEnum> |
69 | static inline void formatQEnum(QDebug &debug, QEnum value) |
70 | { |
71 | const QMetaObject *metaObject = qt_getEnumMetaObject(value); |
72 | const QMetaEnum me = metaObject->enumerator(index: metaObject->indexOfEnumerator(name: qt_getEnumName(value))); |
73 | if (const char *key = me.valueToKey(value: int(value))) |
74 | debug << key; |
75 | else |
76 | debug << int(value); |
77 | } |
78 | |
79 | template <class QEnum> |
80 | static inline void formatNonNullQEnum(QDebug &debug, const char *prefix, QEnum value) |
81 | { |
82 | if (value) { |
83 | debug << prefix; |
84 | formatQEnum(debug, value); |
85 | } |
86 | } |
87 | |
88 | template <class Enum> |
89 | static inline void formatQFlags(QDebug &debug, const QFlags<Enum> &value) |
90 | { |
91 | const QMetaEnum me = QMetaEnum::fromType<QFlags<Enum>>(); |
92 | const QDebugStateSaver saver(debug); |
93 | debug.noquote(); |
94 | debug << me.valueToKeys(value: value.toInt()); |
95 | } |
96 | |
97 | template <class Enum> |
98 | static inline void formatNonNullQFlags(QDebug &debug, const char *prefix, const QFlags<Enum> &value) |
99 | { |
100 | if (value) { |
101 | debug << prefix; |
102 | formatQFlags(debug, value); |
103 | } |
104 | } |
105 | |
106 | #endif // !QT_NO_QOBJECT |
107 | |
108 | } // namespace QtDebugUtils |
109 | |
110 | QT_END_NAMESPACE |
111 | |
112 | #endif // QDEBUG_P_H |
113 | |