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// Q_ENUM registers enums with the meta-object system
50#define ENUM_QDEBUG_OPS_DECL(CLASS, ENUM)
51#define ENUM_QDEBUG_OPS_IMPL(CLASS, ENUM)
52
53inline void throw_qstring(QString const &qs) {
54 throw std::runtime_error{qs.toLocal8Bit().constData()};
55}
56
57QString font_as_stylesheet(QFont const &);
58
59// do what is necessary to change a dynamic property and trigger any
60// conditional style sheet updates
61void update_dynamic_property(QWidget *, char const *property,
62 QVariant const &value);
63
64template <class T> class VPtr {
65 public:
66 static T *asPtr(QVariant v) {
67 return reinterpret_cast<T *>(v.value<void *>());
68 }
69
70 static QVariant asQVariant(T *ptr) {
71 return QVariant::fromValue(reinterpret_cast<void *>(ptr));
72 }
73};
74
75// Register some useful Qt types with QMetaType
76Q_DECLARE_METATYPE(QHostAddress);
77
78inline bool is_broadcast_address(QHostAddress const &host_addr) {
79 return host_addr.isBroadcast();
80}
81
82inline bool is_multicast_address(QHostAddress const &host_addr) {
83 return host_addr.isMulticast();
84}
85
86#endif
Definition qt_helpers.h:64