15
15
#include <QGraphicsScene>
16
16
#include <QGraphicsObject>
18
#include <QDBusArgument>
20
// Marshall the NodeIntrospectionData data into a D-Bus argument
21
QDBusArgument &operator<<(QDBusArgument &argument, const NodeIntrospectionData &node_data)
23
argument.beginStructure();
24
argument << node_data.object_path << node_data.state;
25
argument.endStructure();
29
// Retrieve the NodeIntrospectionData data from the D-Bus argument
30
const QDBusArgument &operator>>(const QDBusArgument &argument, NodeIntrospectionData &node_data)
32
argument.beginStructure();
33
argument >> node_data.object_path >> node_data.state;
34
argument.endStructure();
19
38
const QByteArray AP_ID_NAME("_autopilot_id");
21
QtNode::QtNode(QObject *obj, std::string const& parent_path)
40
QtNode::QtNode(QObject *obj, QtNode::Ptr parent)
44
std::string parent_path = parent ? parent->GetPath() : "";
24
45
full_path_ = parent_path + "/" + GetName();
48
QtNode::QtNode(QObject* obj)
51
full_path_ = "/" + GetName();
27
54
QObject* QtNode::getWrappedObject() const
32
QVariant QtNode::IntrospectNode() const
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);
42
qint64 QtNode::GetObjectId() const
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;
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();
59
NodeIntrospectionData QtNode::GetIntrospectionData() const
61
NodeIntrospectionData data;
62
data.object_path = QString::fromStdString(GetPath());
63
data.state = GetNodeProperties(object_);
64
data.state["id"] = PackProperty(GetId());
55
68
std::string QtNode::GetName() const
71
bool QtNode::MatchProperty(const std::string& name, const std::string& value) const
84
int32_t QtNode::GetId() const
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;
91
QList<QByteArray> property_names = object_->dynamicPropertyNames();
92
if (!property_names.contains(AP_ID_NAME))
94
int32_t new_id = ++next_id;
95
object_->setProperty(AP_ID_NAME, QVariant(new_id));
97
return qvariant_cast<int32_t>(object_->property(AP_ID_NAME));
100
bool QtNode::MatchStringProperty(const std::string& name, const std::string& value) const
102
QVariantMap properties = GetNodeProperties(object_);
104
QString qname = QString::fromStdString(name);
105
if (! properties.contains(qname))
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()))
112
check_value.convert(object_value.type());
113
return check_value == object_value;
119
bool QtNode::MatchIntegerProperty(const std::string& name, int32_t value) const
74
return QString::fromStdString(value).toLongLong() == GetObjectId();
75
QVariantMap properties = GetNodeProperties(object_);
77
QString qname = QString::fromStdString(name);
78
if (! properties.contains(qname))
81
QVariant object_value = properties[qname];
82
QVariant check_value(QString::fromStdString(value));
83
if (check_value.canConvert(object_value.type()))
85
check_value.convert(object_value.type());
86
return check_value == object_value;
92
xpathselect::NodeList QtNode::Children() const
94
xpathselect::NodeList children;
122
return value == GetId();
124
QVariantMap properties = GetNodeProperties(object_);
126
QString qname = QString::fromStdString(name);
127
if (! properties.contains(qname))
130
QVariant object_value = qvariant_cast<QVariantList>(properties[qname]).at(1);
131
QVariant check_value(value);
132
if (check_value.canConvert(object_value.type()))
134
check_value.convert(object_value.type());
135
return check_value == object_value;
141
bool QtNode::MatchBooleanProperty(const std::string& name, bool value) const
143
QVariantMap properties = GetNodeProperties(object_);
145
QString qname = QString::fromStdString(name);
146
if (! properties.contains(qname))
149
QVariant object_value = qvariant_cast<QVariantList>(properties[qname]).at(1);
150
QVariant check_value(value);
152
if (check_value.canConvert(object_value.type()))
154
check_value.convert(object_value.type());
155
return check_value == object_value;
161
xpathselect::NodeVector QtNode::Children() const
163
xpathselect::NodeVector children;
96
165
#ifdef QT5_SUPPORT
97
166
// Qt5's hierarchy for QML has changed a bit:
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()));
107
176
QQuickItem* item = qobject_cast<QQuickItem*>(object_);
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()));
115
184
foreach (QObject *child, object_->children())
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()));