~autopilot/autopilot-qt/1.4

« back to all changes in this revision

Viewing changes to driver/qtnode.cpp

  • Committer: Tarmac
  • Author(s): Thomi Richards
  • Date: 2013-09-16 19:29:53 UTC
  • mfrom: (70.1.15 experimental)
  • Revision ID: tarmac-20130916192953-gfi8t8ot60cvm4ea
1.4 wire protocol changes. Fixes: https://bugs.launchpad.net/bugs/1195141, https://bugs.launchpad.net/bugs/1214128.

Approved by PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
  #include <QGraphicsScene>
16
16
  #include <QGraphicsObject>
17
17
#endif
 
18
#include <QDBusArgument>
 
19
 
 
20
// Marshall the NodeIntrospectionData data into a D-Bus argument
 
21
 QDBusArgument &operator<<(QDBusArgument &argument, const NodeIntrospectionData &node_data)
 
22
 {
 
23
     argument.beginStructure();
 
24
     argument << node_data.object_path << node_data.state;
 
25
     argument.endStructure();
 
26
     return argument;
 
27
 }
 
28
 
 
29
 // Retrieve the NodeIntrospectionData data from the D-Bus argument
 
30
 const QDBusArgument &operator>>(const QDBusArgument &argument, NodeIntrospectionData &node_data)
 
31
 {
 
32
     argument.beginStructure();
 
33
     argument >> node_data.object_path >> node_data.state;
 
34
     argument.endStructure();
 
35
     return argument;
 
36
 }
18
37
 
19
38
const QByteArray AP_ID_NAME("_autopilot_id");
20
39
 
21
 
QtNode::QtNode(QObject *obj, std::string const& parent_path)
22
 
    : object_(obj)
 
40
QtNode::QtNode(QObject *obj, QtNode::Ptr parent)
 
41
: object_(obj)
 
42
, parent_(parent)
23
43
{
 
44
    std::string parent_path = parent ? parent->GetPath() : "";
24
45
    full_path_ = parent_path + "/" + GetName();
25
46
}
26
47
 
 
48
QtNode::QtNode(QObject* obj)
 
49
: object_(obj)
 
50
{
 
51
    full_path_ = "/" + GetName();
 
52
}
 
53
 
27
54
QObject* QtNode::getWrappedObject() const
28
55
{
29
56
    return object_;
30
57
}
31
58
 
32
 
QVariant QtNode::IntrospectNode() const
33
 
{
34
 
    // return must be (name, state_map)
35
 
    QString object_name = QString::fromStdString(GetPath());
36
 
    QVariantMap object_properties = GetNodeProperties(object_);
37
 
    object_properties["id"] = GetObjectId();
38
 
    QList<QVariant> object_tuple = { QVariant(object_name), QVariant(object_properties) };
39
 
    return QVariant(object_tuple);
40
 
}
41
 
 
42
 
qint64 QtNode::GetObjectId() const
43
 
{
44
 
    // Note: This starts at 1 for a reason - 1 is reserved for the pseudo root node, and
45
 
    // so must never be allocated to a regular object.
46
 
    static qint64 next_id=1;
47
 
 
48
 
    QList<QByteArray> property_names = object_->dynamicPropertyNames();
49
 
    if (!property_names.contains(AP_ID_NAME))
50
 
        object_->setProperty(AP_ID_NAME, QVariant(++next_id));
51
 
    return object_->property(AP_ID_NAME).toLongLong();
52
 
 
 
59
NodeIntrospectionData QtNode::GetIntrospectionData() const
 
60
{
 
61
    NodeIntrospectionData data;
 
62
    data.object_path = QString::fromStdString(GetPath());
 
63
    data.state = GetNodeProperties(object_);
 
64
    data.state["id"] = PackProperty(GetId());
 
65
    return data;
53
66
}
54
67
 
55
68
std::string QtNode::GetName() const
68
81
    return full_path_;
69
82
}
70
83
 
71
 
bool QtNode::MatchProperty(const std::string& name, const std::string& value) const
 
84
int32_t QtNode::GetId() const
 
85
{
 
86
    // Note: This method is used to assign ids to both the root node (with a QApplication object) and
 
87
    // child nodes. This used to be separate code, but now that we export QApplication properties,
 
88
    // we can use this one method everywhere.
 
89
    static int32_t next_id=0;
 
90
 
 
91
    QList<QByteArray> property_names = object_->dynamicPropertyNames();
 
92
    if (!property_names.contains(AP_ID_NAME))
 
93
    {
 
94
        int32_t new_id = ++next_id;
 
95
        object_->setProperty(AP_ID_NAME, QVariant(new_id));
 
96
    }
 
97
    return qvariant_cast<int32_t>(object_->property(AP_ID_NAME));
 
98
}
 
99
 
 
100
bool QtNode::MatchStringProperty(const std::string& name, const std::string& value) const
 
101
{
 
102
    QVariantMap properties = GetNodeProperties(object_);
 
103
 
 
104
    QString qname = QString::fromStdString(name);
 
105
    if (! properties.contains(qname))
 
106
        return false;
 
107
 
 
108
    QVariant object_value = qvariant_cast<QVariantList>(properties[qname]).at(1);
 
109
    QVariant check_value(QString::fromStdString(value));
 
110
    if (check_value.canConvert(object_value.type()))
 
111
    {
 
112
        check_value.convert(object_value.type());
 
113
        return check_value == object_value;
 
114
    }
 
115
 
 
116
    return false;
 
117
}
 
118
 
 
119
bool QtNode::MatchIntegerProperty(const std::string& name, int32_t value) const
72
120
{
73
121
    if (name == "id")
74
 
        return QString::fromStdString(value).toLongLong() == GetObjectId();
75
 
    QVariantMap properties = GetNodeProperties(object_);
76
 
 
77
 
    QString qname = QString::fromStdString(name);
78
 
    if (! properties.contains(qname))
79
 
        return false;
80
 
 
81
 
    QVariant object_value = properties[qname];
82
 
    QVariant check_value(QString::fromStdString(value));
83
 
    if (check_value.canConvert(object_value.type()))
84
 
    {
85
 
        check_value.convert(object_value.type());
86
 
        return check_value == object_value;
87
 
    }
88
 
 
89
 
    return false;
90
 
}
91
 
 
92
 
xpathselect::NodeList QtNode::Children() const
93
 
{
94
 
    xpathselect::NodeList children;
 
122
        return value == GetId();
 
123
 
 
124
    QVariantMap properties = GetNodeProperties(object_);
 
125
 
 
126
    QString qname = QString::fromStdString(name);
 
127
    if (! properties.contains(qname))
 
128
        return false;
 
129
 
 
130
    QVariant object_value = qvariant_cast<QVariantList>(properties[qname]).at(1);
 
131
    QVariant check_value(value);
 
132
    if (check_value.canConvert(object_value.type()))
 
133
    {
 
134
        check_value.convert(object_value.type());
 
135
        return check_value == object_value;
 
136
    }
 
137
 
 
138
    return false;
 
139
}
 
140
 
 
141
bool QtNode::MatchBooleanProperty(const std::string& name, bool value) const
 
142
{
 
143
    QVariantMap properties = GetNodeProperties(object_);
 
144
 
 
145
    QString qname = QString::fromStdString(name);
 
146
    if (! properties.contains(qname))
 
147
        return false;
 
148
 
 
149
    QVariant object_value = qvariant_cast<QVariantList>(properties[qname]).at(1);
 
150
    QVariant check_value(value);
 
151
 
 
152
    if (check_value.canConvert(object_value.type()))
 
153
    {
 
154
        check_value.convert(object_value.type());
 
155
        return check_value == object_value;
 
156
    }
 
157
 
 
158
    return false;
 
159
}
 
160
 
 
161
xpathselect::NodeVector QtNode::Children() const
 
162
{
 
163
    xpathselect::NodeVector children;
95
164
 
96
165
#ifdef QT5_SUPPORT
97
166
    // Qt5's hierarchy for QML has changed a bit:
101
170
 
102
171
    QQuickView *view = qobject_cast<QQuickView*>(object_);
103
172
    if (view && view->rootObject() != 0) {
104
 
        children.push_back(std::make_shared<QtNode>(view->rootObject(), GetPath()));
 
173
        children.push_back(std::make_shared<QtNode>(view->rootObject(), shared_from_this()));
105
174
    }
106
175
 
107
176
    QQuickItem* item = qobject_cast<QQuickItem*>(object_);
108
177
    if (item) {
109
178
        foreach (QQuickItem *childItem, item->childItems()) {
110
179
            if (childItem->parentItem() == item) {
111
 
                children.push_back(std::make_shared<QtNode>(childItem, GetPath()));
 
180
                children.push_back(std::make_shared<QtNode>(childItem, shared_from_this()));
112
181
            }
113
182
        }
114
183
    } else {
115
184
        foreach (QObject *child, object_->children())
116
185
        {
117
186
            if (child->parent() == object_)
118
 
                children.push_back(std::make_shared<QtNode>(child, GetPath()));
 
187
                children.push_back(std::make_shared<QtNode>(child, shared_from_this()));
119
188
        }
120
189
    }
121
190
 
123
192
    foreach (QObject *child, object_->children())
124
193
    {
125
194
        if (child->parent() == object_)
126
 
            children.push_back(std::make_shared<QtNode>(child, GetPath()));
 
195
            children.push_back(std::make_shared<QtNode>(child, shared_from_this()));
127
196
    }
128
197
 
129
198
    // If our wrapped object is a QGraphicsScene, we need to explicitly grab any child graphics
137
206
        {
138
207
            QGraphicsObject *obj = item->toGraphicsObject();
139
208
            if (obj && ! obj->parent())
140
 
                children.push_back(std::make_shared<QtNode>(obj, GetPath()));
 
209
                children.push_back(std::make_shared<QtNode>(obj, shared_from_this()));
141
210
        }
142
211
    }
143
212
#endif
144
213
 
145
214
    return children;
146
215
}
 
216
 
 
217
 
 
218
xpathselect::Node::Ptr QtNode::GetParent() const
 
219
{
 
220
    return parent_;
 
221
}