JS8Call-Improved master
Loading...
Searching...
No Matches
qt_helpers.h
1#ifndef QT_HELPERS_HPP_
2#define QT_HELPERS_HPP_
3
4#include <QDataStream>
5#include <QHostAddress>
6#include <QMetaEnum>
7#include <QMetaObject>
8#include <QMetaType>
9#include <QString>
10
11#include <stdexcept>
12
13#define ENUM_QDATASTREAM_OPS_DECL(CLASS, ENUM) \
14 QDataStream &operator<<(QDataStream &, CLASS::ENUM const &); \
15 QDataStream &operator>>(QDataStream &, CLASS::ENUM &);
16
17#define ENUM_QDATASTREAM_OPS_IMPL(CLASS, ENUM) \
18 QDataStream &operator<<(QDataStream &os, CLASS::ENUM const &v) { \
19 auto const &mo = CLASS::staticMetaObject; \
20 return os << mo.enumerator(mo.indexOfEnumerator(#ENUM)).valueToKey(v); \
21 } \
22 \
23 QDataStream &operator>>(QDataStream &is, CLASS::ENUM &v) { \
24 char *buffer; \
25 is >> buffer; \
26 bool ok{false}; \
27 auto const &mo = CLASS::staticMetaObject; \
28 auto const &me = mo.enumerator(mo.indexOfEnumerator(#ENUM)); \
29 if (buffer) { \
30 v = static_cast<CLASS::ENUM>(me.keyToValue(buffer, &ok)); \
31 delete[] buffer; \
32 } \
33 if (!ok) { \
34 v = static_cast<CLASS::ENUM>(me.value(0)); \
35 } \
36 return is; \
37 }
38
39#define ENUM_CONVERSION_OPS_DECL(CLASS, ENUM) \
40 QString enum_to_qstring(CLASS::ENUM const &);
41
42#define ENUM_CONVERSION_OPS_IMPL(CLASS, ENUM) \
43 QString enum_to_qstring(CLASS::ENUM const &m) { \
44 auto const &mo = CLASS::staticMetaObject; \
45 return QString{ \
46 mo.enumerator(mo.indexOfEnumerator(#ENUM)).valueToKey(m)}; \
47 }
48
49#if QT_VERSION >= 0x050500
50
51// Qt 5.5 now has Q_ENUM which registers enumns better
52#define ENUM_QDEBUG_OPS_DECL(CLASS, ENUM)
53#define ENUM_QDEBUG_OPS_IMPL(CLASS, ENUM)
54
55#else
56
57#define Q_ENUM(E)
58
59#include <QDebug>
60
61class QVariant;
62
63#define ENUM_QDEBUG_OPS_DECL(CLASS, ENUM) \
64 QDebug operator<<(QDebug, CLASS::ENUM const &);
65
66#define ENUM_QDEBUG_OPS_IMPL(CLASS, ENUM) \
67 QDebug operator<<(QDebug d, CLASS::ENUM const &m) { \
68 auto const &mo = CLASS::staticMetaObject; \
69 return d << mo.enumerator(mo.indexOfEnumerator(#ENUM)).valueToKey(m); \
70 }
71
72#endif
73
74inline void throw_qstring(QString const &qs) {
75 throw std::runtime_error{qs.toLocal8Bit().constData()};
76}
77
78QString font_as_stylesheet(QFont const &);
79
80// do what is necessary to change a dynamic property and trigger any
81// conditional style sheet updates
82void update_dynamic_property(QWidget *, char const *property,
83 QVariant const &value);
84
85template <class T> class VPtr {
86 public:
87 static T *asPtr(QVariant v) {
88 return reinterpret_cast<T *>(v.value<void *>());
89 }
90
91 static QVariant asQVariant(T *ptr) {
92 return QVariant::fromValue(reinterpret_cast<void *>(ptr));
93 }
94};
95
96// Register some useful Qt types with QMetaType
97Q_DECLARE_METATYPE(QHostAddress);
98
99inline bool is_broadcast_address(QHostAddress const &host_addr) {
100#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
101 return host_addr.isBroadcast();
102#else
103 bool ok;
104 return host_addr.toIPv4Address(&ok) == 0xffffffffu && ok;
105#endif
106}
107
108inline bool is_multicast_address(QHostAddress const &host_addr) {
109#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
110 return host_addr.isMulticast();
111#else
112 bool ok;
113 return (((host_addr.toIPv4Address(&ok) & 0xf0000000u) == 0xe0000000u) &&
114 ok) ||
115 host_addr.toIPv6Address()[0] == 0xff;
116#endif
117}
118
119#endif
Definition qt_helpers.h:85