~autopilot/autopilot-qt/trunk

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
Copyright 2012 Canonical

This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3, as published
by the Free Software Foundation.
*/


#include <xpathselect/node.h>
#include <xpathselect/xpathselect.h>

#include <QDebug>

#ifdef QT5_SUPPORT
  #include <QtWidgets/QApplication>
  #include <QtWidgets/QGraphicsItem>
  #include <QtWidgets/QGraphicsScene>
  #include <QtWidgets/QGraphicsView>
  #include <QtWidgets/QWidget>
  #include <QtQuick/QQuickItem>
  #include <QtQuick/QQuickWindow>
#else
  #include <QGraphicsItem>
  #include <QGraphicsScene>
  #include <QGraphicsView>
  #include <QApplication>
  #include <QWidget>
#endif

#include <QMap>
#include <QMetaProperty>
#include <QObject>
#include <QStringList>
#include <QVariant>
#include <QRect>
#include <QUrl>
#include <QDateTime>

#include "autopilot_types.h"
#include "introspection.h"
#include "qtnode.h"
#include "rootnode.h"


QVariant IntrospectNode(QObject* obj);
QString GetNodeName(QObject* obj);
QStringList GetNodeChildNames(QObject* obj);
void AddCustomProperties(QObject* obj, QVariantMap& properties);

QList<NodeIntrospectionData> Introspect(QString const& query_string)
{
    QList<NodeIntrospectionData> state;
    QList<DBusNode::Ptr> node_list = GetNodesThatMatchQuery(query_string);
    foreach (DBusNode::Ptr obj, node_list)
    {
        state.append(obj->GetIntrospectionData());
    }

    return state;

}


QList<DBusNode::Ptr> GetNodesThatMatchQuery(QString const& query_string)
{
#ifdef QT5_SUPPORT
    std::shared_ptr<RootNode> root = std::make_shared<RootNode>(QApplication::instance());

    // Add all QWidget top level widgets
    foreach (const QWidget *widget, QApplication::topLevelWidgets())
    {
        root->AddChild((QObject*) widget);
    }
    // Add all QML top level Windows
    foreach (QWindow *widget, QGuiApplication::allWindows())
    {
        root->AddChild((QObject*) widget);
    }
#else
    std::shared_ptr<RootNode> root = std::make_shared<RootNode>(QApplication::instance());
    foreach (QWidget *widget, QApplication::topLevelWidgets())
    {
        root->AddChild((QObject*) widget);
    }
#endif
    QList<DBusNode::Ptr> node_list;

    xpathselect::NodeVector list = xpathselect::SelectNodes(root, query_string.toStdString());
    for (auto node : list)
    {
        // node may be our root node wrapper *or* an ordinary qobject wrapper
        auto object_ptr = std::static_pointer_cast<const DBusNode>(node);
        if (object_ptr)
        {
            node_list.append(object_ptr);
        }

    }
    return node_list;
}


QVariant IntrospectNode(QObject* obj)
{
    // return must be (name, state_map)
    QString object_name = GetNodeName(obj);
    QVariantMap object_properties = GetNodeProperties(obj);
    QList<QVariant> object_tuple = { QVariant(object_name), QVariant(object_properties) };
    return QVariant(object_tuple);
}


QString GetNodeName(QObject* obj)
{
    return obj->metaObject()->className();
}


QVariantMap GetNodeProperties(QObject* obj)
{
    QVariantMap object_properties;
    const QMetaObject* meta = obj->metaObject();
    do
    {
        for(int i = meta->propertyOffset(); i < meta->propertyCount(); ++i)
        {
            QMetaProperty prop = meta->property(i);
            if (!prop.isValid())
            {
                qDebug() << "Property at index" << i << "Is not valid!";
                continue;
            }
            QVariant object_property = PackProperty(prop.read(obj));
            if (! object_property.isValid())
                continue;
            if (!object_properties.contains(prop.name())) {
                object_properties[prop.name()] = object_property;
            }
        }
        foreach(const QByteArray &dynamicPropertyName, obj->dynamicPropertyNames()) {
            QVariant dynamicPropertyValue = obj->property(dynamicPropertyName);

            QVariant object_property = PackProperty(dynamicPropertyValue);
            if (! object_property.isValid())
                continue;
            object_properties[dynamicPropertyName] = object_property;
        }

        meta = meta->superClass();
    } while(meta);

    AddCustomProperties(obj, object_properties);

    // add the 'Children' pseudo-property:
    QStringList children = GetNodeChildNames(obj);
    if (!children.empty())
        object_properties["Children"] = PackProperty(children);

    return object_properties;
}


void AddCustomProperties(QObject* obj, QVariantMap &properties)
{
    // Add any custom properties we need to the given QObject.
    // Add GlobalRect support for QWidget-derived classes
    QWidget *w = qobject_cast<QWidget*>(obj);
    if (w)
    {
        QRect r = w->rect();
        r = QRect(w->mapToGlobal(r.topLeft()), r.size());
        properties["globalRect"] = PackProperty(r);
    }
    // ...and support for QGraphicsItem-derived classes.
    else if (QGraphicsItem *i = qobject_cast<QGraphicsItem*>(obj))
    {
        // need to get the view that this item is in. Should only be one. If there's
        // more than one, we're in trouble.
        QGraphicsView *view = i->scene()->views().last();
        QRectF bounding_rect = i->boundingRect();
        bounding_rect = i->mapRectToScene(bounding_rect);
        QRect scene_rect = view->mapFromScene(bounding_rect).boundingRect();
        QRect global_rect = QRect(
                    view->mapToGlobal(scene_rect.topLeft()),
                    scene_rect.size());
        properties["globalRect"] = PackProperty(global_rect);
    }
#ifdef QT5_SUPPORT
    // ... and support for QQuickItems (aka. Qt5 Declarative items)
    else if (QQuickItem *i = qobject_cast<QQuickItem*>(obj))
    {
        QQuickWindow *view = i->window();
        QRectF bounding_rect = i->boundingRect();
        bounding_rect = i->mapRectToScene(bounding_rect);
        QRect global_rect = QRect(view->mapToGlobal(bounding_rect.toRect().topLeft()), bounding_rect.size().toSize());
        properties["globalRect"] = PackProperty(global_rect);
    }
#endif
}

QVariant PackProperty(QVariant const& prop)
{
    switch (prop.type())
    {
    case QVariant::Int:
    case QVariant::Bool:
    case QVariant::String:
    case QVariant::UInt:
    case QVariant::LongLong:
    case QVariant::ULongLong:
    case QVariant::StringList:
    case QVariant::Double:
    {
        return QList<QVariant> {
            QVariant(TYPE_PLAIN),
            prop
        };
    }

    case QVariant::ByteArray:
    {
        return QList<QVariant> {
            QVariant(TYPE_PLAIN),
            QVariant(QString(qvariant_cast<QByteArray>(prop)))
        };
    }

    case QVariant::Point:
    {
        QPoint p = qvariant_cast<QPoint>(prop);
        return QList<QVariant> {
            QVariant(TYPE_POINT),
            QVariant(p.x()),
            QVariant(p.y())
        };
    }

    case QVariant::Rect:
    {
        QRect r = qvariant_cast<QRect>(prop);
        return QList<QVariant> {
            QVariant(TYPE_RECT),
            QVariant(r.x()),
            QVariant(r.y()),
            QVariant(r.width()),
            QVariant(r.height()) };
    }

    case QVariant::Size:
    {
        QSize s = qvariant_cast<QSize>(prop);
        return QList<QVariant> {
            QVariant(TYPE_SIZE),
            QVariant(s.width()),
            QVariant(s.height())
        };
    }

    case QVariant::Color:
    {
        QColor color = qvariant_cast<QColor>(prop).toRgb();
        return QList<QVariant> {
            QVariant(TYPE_COLOR),
            QVariant(color.red()),
            QVariant(color.green()),
            QVariant(color.blue()),
            QVariant(color.alpha())
        };
    }

    case QVariant::Url:
    {
        return QList<QVariant> {
            QVariant(TYPE_PLAIN),
            QVariant(prop.toUrl().toString())
        };
    }

    // Depending on the architecture, floating points might be of type QMetaType::Float instead of QVariant::Double
    // QDBus however, can only carry QVariant types, so lets convert it to QVariant::Double
    case QMetaType::Float:
    {
        return QList<QVariant> {
            QVariant(TYPE_PLAIN),
            QVariant(prop.toDouble())
        };
    }

    case QVariant::Date:
    case QVariant::DateTime:
    {
        return QList<QVariant> {
            QVariant(TYPE_DATETIME),
            QVariant(prop.toDateTime().toTime_t())
        };
    }

    case QVariant::Time:
    {
        QTime t = qvariant_cast<QTime>(prop);
        return QList<QVariant> {
            QVariant(TYPE_TIME),
            QVariant(t.hour()),
            QVariant(t.minute()),
            QVariant(t.second()),
            QVariant(t.msec())
        };
    }

    default:
    {
        return QVariant(); // unsupported type, will not be sent to the client.
    }
    }
}


QStringList GetNodeChildNames(QObject* obj)
{
    QStringList child_names;
    foreach (QObject *child, obj->children())
    {
        if (child->parent() == obj) {
            child_names.append(GetNodeName(child));
        }
    }
#ifdef QT5_SUPPORT
    // In case of a QQuickWindow, add the main contentItem()
    if (QQuickWindow *window = qobject_cast<QQuickWindow*>(obj)) {
        child_names.append(GetNodeName(window->contentItem()));
    }
    // In case of QQuickItems include also childItems(), not only children().
    if (QQuickItem *item = qobject_cast<QQuickItem*>(obj)) {
        foreach (QObject *child, item->childItems()) {
            child_names.append(GetNodeName(child));
        }
    }
#endif
    return child_names;
}