qqmlhelpers.h 6.38 KB
Newer Older
Renaud Guezennec's avatar
Renaud Guezennec committed
1
2
3
4
5
#ifndef QQMLHELPERS_H
#define QQMLHELPERS_H

#include <QObject>

Renaud Guezennec's avatar
Renaud Guezennec committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#define QML_WRITABLE_PROPERTY(type, name)                                       \
protected:                                                                      \
    Q_PROPERTY(type name READ get_##name WRITE set_##name NOTIFY name##Changed) \
private:                                                                        \
    type m_##name;                                                              \
                                                                                \
public:                                                                         \
    type get_##name() const { return m_##name; }                                \
public Q_SLOTS:                                                                 \
    bool set_##name(type name)                                                  \
    {                                                                           \
        bool ret= false;                                                        \
        if((ret= m_##name != name))                                             \
        {                                                                       \
            m_##name= name;                                                     \
            emit name##Changed(m_##name);                                       \
        }                                                                       \
        return ret;                                                             \
    }                                                                           \
Q_SIGNALS:                                                                      \
    void name##Changed(type name);                                              \
                                                                                \
private:
Renaud Guezennec's avatar
Renaud Guezennec committed
29

Renaud Guezennec's avatar
Renaud Guezennec committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#define QML_READONLY_PROPERTY(type, name)                      \
protected:                                                     \
    Q_PROPERTY(type name READ get_##name NOTIFY name##Changed) \
private:                                                       \
    type m_##name;                                             \
                                                               \
public:                                                        \
    type get_##name() const { return m_##name; }               \
    bool update_##name(type name)                              \
    {                                                          \
        bool ret= false;                                       \
        if((ret= m_##name != name))                            \
        {                                                      \
            m_##name= name;                                    \
            emit name##Changed(m_##name);                      \
        }                                                      \
        return ret;                                            \
    }                                                          \
Q_SIGNALS:                                                     \
    void name##Changed(type name);                             \
                                                               \
private:
Renaud Guezennec's avatar
Renaud Guezennec committed
52

Renaud Guezennec's avatar
Renaud Guezennec committed
53
54
55
56
57
58
59
60
61
62
#define QML_CONSTANT_PROPERTY(type, name)          \
protected:                                         \
    Q_PROPERTY(type name READ get_##name CONSTANT) \
private:                                           \
    type m_##name;                                 \
                                                   \
public:                                            \
    type get_##name() const { return m_##name; }   \
                                                   \
private:
Renaud Guezennec's avatar
Renaud Guezennec committed
63

Renaud Guezennec's avatar
Renaud Guezennec committed
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
#define QML_LIST_PROPERTY(CLASS, NAME, TYPE)                           \
public:                                                                \
    static int NAME##_count(QQmlListProperty<TYPE>* prop)              \
    {                                                                  \
        CLASS* instance= qobject_cast<CLASS*>(prop->object);           \
        return (instance != NULL ? instance->m_##NAME.count() : 0);    \
    }                                                                  \
    static void NAME##_clear(QQmlListProperty<TYPE>* prop)             \
    {                                                                  \
        CLASS* instance= qobject_cast<CLASS*>(prop->object);           \
        if(instance != NULL)                                           \
        {                                                              \
            instance->m_##NAME.clear();                                \
        }                                                              \
    }                                                                  \
    static void NAME##_append(QQmlListProperty<TYPE>* prop, TYPE* obj) \
    {                                                                  \
        CLASS* instance= qobject_cast<CLASS*>(prop->object);           \
        if(instance != NULL && obj != NULL)                            \
        {                                                              \
            instance->m_##NAME.append(obj);                            \
        }                                                              \
    }                                                                  \
    static TYPE* NAME##_at(QQmlListProperty<TYPE>* prop, int idx)      \
    {                                                                  \
        CLASS* instance= qobject_cast<CLASS*>(prop->object);           \
        return (instance != NULL ? instance->m_##NAME.at(idx) : NULL); \
    }                                                                  \
    QList<TYPE*> get_##NAME##s(void) const { return m_##NAME; }        \
                                                                       \
private:                                                               \
    QList<TYPE*> m_##NAME;
Renaud Guezennec's avatar
Renaud Guezennec committed
96
97

#define QML_ENUM_CLASS(name, ...) \
Renaud Guezennec's avatar
Renaud Guezennec committed
98
99
100
101
102
103
104
105
106
    class name : public QObject   \
    {                             \
        Q_GADGET                  \
    public:                       \
        enum Type                 \
        {                         \
            __VA_ARGS__           \
        };                        \
        Q_ENUMS(Type)             \
Renaud Guezennec's avatar
Renaud Guezennec committed
107
108
    };

Renaud Guezennec's avatar
Renaud Guezennec committed
109
110
111
112
class QmlProperty : public QObject
{
    Q_OBJECT
}; // NOTE : to avoid "no suitable class found" MOC note
Renaud Guezennec's avatar
Renaud Guezennec committed
113
114

#endif // QQMLHELPERS_H