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