qmljsinspectorclient.cpp 12.6 KB
Newer Older
hjk's avatar
hjk committed
1
/**************************************************************************
2
**
hjk's avatar
hjk committed
3
** This file is part of Qt Creator
4
**
hjk's avatar
hjk committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
8
9
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
10
11
12
13
14
15
16
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
18
**
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
21
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
22
** Other Usage
23
**
hjk's avatar
hjk committed
24
25
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
26
**
hjk's avatar
hjk committed
27
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
28
** Nokia at info@qt.nokia.com.
29
**
hjk's avatar
hjk committed
30
**************************************************************************/
31

32
#include "qmljsinspectorclient.h"
33
34
35
#include "qmljsclientproxy.h"
#include "qmljsinspectorconstants.h"

36
#include <QtGui/QColor>
37

38
39
using namespace QmlJSDebugger;

40
41
42
namespace QmlJSInspector {
namespace Internal {

43
QmlJSInspectorClient::QmlJSInspectorClient(QDeclarativeDebugConnection *client,
44
                                         QObject * /*parent*/)
45
    : QDeclarativeDebugClient(QLatin1String("QDeclarativeObserverMode"), client) ,
46
47
    m_connection(client)
{
48
    setObjectName(name());
49
50
}

51
void QmlJSInspectorClient::statusChanged(Status status)
52
53
54
55
{
    emit connectedStatusChanged(status);
}

56
void QmlJSInspectorClient::messageReceived(const QByteArray &message)
57
58
59
{
    QDataStream ds(message);

60
    InspectorProtocol::Message type;
61
62
    ds >> type;

63
    switch (type) {
64
    case InspectorProtocol::CurrentObjectsChanged: {
65
66
        int objectCount;
        ds >> objectCount;
Kai Koehne's avatar
Kai Koehne committed
67

68
        log(LogReceive, type, QString("%1 [list of debug ids]").arg(objectCount));
69

Kai Koehne's avatar
Kai Koehne committed
70
        m_currentDebugIds.clear();
71

72
        for (int i = 0; i < objectCount; ++i) {
73
74
            int debugId;
            ds >> debugId;
75
            if (debugId != -1)
Kai Koehne's avatar
Kai Koehne committed
76
                m_currentDebugIds << debugId;
77
        }
78

Kai Koehne's avatar
Kai Koehne committed
79
        emit currentObjectsChanged(m_currentDebugIds);
80
81
        break;
    }
82
    case InspectorProtocol::ToolChanged: {
83
84
85
        int toolId;
        ds >> toolId;

86
        log(LogReceive, type, QString::number(toolId));
87

88
89
90
91
92
93
94
95
96
        if (toolId == Constants::ColorPickerMode) {
            emit colorPickerActivated();
        } else if (toolId == Constants::ZoomMode) {
            emit zoomToolActivated();
        } else if (toolId == Constants::SelectionToolMode) {
            emit selectToolActivated();
        } else if (toolId == Constants::MarqueeSelectionToolMode) {
            emit selectMarqueeToolActivated();
        }
97
98
        break;
    }
99
    case InspectorProtocol::AnimationSpeedChanged: {
100
101
        qreal slowDownFactor;
        ds >> slowDownFactor;
102

103
        log(LogReceive, type, QString::number(slowDownFactor));
104

105
106
107
        emit animationSpeedChanged(slowDownFactor);
        break;
    }
108
    case InspectorProtocol::AnimationPausedChanged: {
109
110
111
112
113
        bool paused;
        ds >> paused;

        log(LogReceive, type, paused ? QLatin1String("true") : QLatin1String("false"));

114
        emit animationPausedChanged(paused);
115
116
        break;
    }
117
    case InspectorProtocol::SetDesignMode: {
118
119
        bool inDesignMode;
        ds >> inDesignMode;
120

121
        log(LogReceive, type, QLatin1String(inDesignMode ? "true" : "false"));
122

123
        emit designModeBehaviorChanged(inDesignMode);
124
125
        break;
    }
126
    case InspectorProtocol::ShowAppOnTop: {
127
128
        bool showAppOnTop;
        ds >> showAppOnTop;
129

130
        log(LogReceive, type, QLatin1String(showAppOnTop ? "true" : "false"));
131

132
        emit showAppOnTopChanged(showAppOnTop);
133
134
        break;
    }
135
    case InspectorProtocol::Reloaded: {
136
        log(LogReceive, type);
137
        emit reloaded();
138
139
        break;
    }
140
    case InspectorProtocol::ColorChanged: {
141
142
        QColor col;
        ds >> col;
143

144
        log(LogReceive, type, col.name());
145

146
        emit selectedColorChanged(col);
147
148
149
150
        break;
    }
    default:
        qWarning() << "Warning: Not handling message:" << type;
151
152
153
    }
}

154
QList<int> QmlJSInspectorClient::currentObjects() const
155
{
Kai Koehne's avatar
Kai Koehne committed
156
    return m_currentDebugIds;
157
158
}

159
void QmlJSInspectorClient::setCurrentObjects(const QList<int> &debugIds)
160
{
161
162
163
    if (!m_connection || !m_connection->isConnected())
        return;

164
165
166
167
168
    if (debugIds == m_currentDebugIds)
        return;

    m_currentDebugIds = debugIds;

169
170
171
    QByteArray message;
    QDataStream ds(&message, QIODevice::WriteOnly);

172
    InspectorProtocol::Message cmd = InspectorProtocol::SetCurrentObjects;
173
    ds << cmd
Kai Koehne's avatar
Kai Koehne committed
174
       << debugIds.length();
175

Kai Koehne's avatar
Kai Koehne committed
176
177
    foreach (int id, debugIds) {
        ds << id;
178
179
    }

180
    log(LogSend, cmd, QString("%1 [list of ids]").arg(debugIds.length()));
181

182
183
184
    sendMessage(message);
}

185
186
187
188
void recurseObjectIdList(const QDeclarativeDebugObjectReference &ref, QList<int> &debugIds, QList<QString> &objectIds)
{
    debugIds << ref.debugId();
    objectIds << ref.idString();
189
    foreach (const QDeclarativeDebugObjectReference &child, ref.children())
190
191
192
        recurseObjectIdList(child, debugIds, objectIds);
}

193
void QmlJSInspectorClient::setObjectIdList(const QList<QDeclarativeDebugObjectReference> &objectRoots)
194
195
196
197
198
199
200
{
    QByteArray message;
    QDataStream ds(&message, QIODevice::WriteOnly);

    QList<int> debugIds;
    QList<QString> objectIds;

201
    foreach (const QDeclarativeDebugObjectReference &ref, objectRoots)
202
203
        recurseObjectIdList(ref, debugIds, objectIds);

204
    InspectorProtocol::Message cmd = InspectorProtocol::ObjectIdList;
205
    ds << cmd
206
207
208
209
210
211
212
213
       << debugIds.length();

    Q_ASSERT(debugIds.length() == objectIds.length());

    for(int i = 0; i < debugIds.length(); ++i) {
        ds << debugIds[i] << objectIds[i];
    }

214
    log(LogSend, cmd, QString("%1 %2 [list of debug / object ids]").arg(debugIds.length()));
215

216
217
218
    sendMessage(message);
}

219
void QmlJSInspectorClient::clearComponentCache()
220
221
222
223
224
225
226
{
    if (!m_connection || !m_connection->isConnected())
        return;

    QByteArray message;
    QDataStream ds(&message, QIODevice::WriteOnly);

227
    InspectorProtocol::Message cmd = InspectorProtocol::ClearComponentCache;
228
    ds << cmd;
229

230
    log(LogSend, cmd);
231

232
233
234
    sendMessage(message);
}

235
void QmlJSInspectorClient::reloadViewer()
236
237
238
239
240
241
242
{
    if (!m_connection || !m_connection->isConnected())
        return;

    QByteArray message;
    QDataStream ds(&message, QIODevice::WriteOnly);

243
    InspectorProtocol::Message cmd = InspectorProtocol::Reload;
244
    ds << cmd;
245

246
    log(LogSend, cmd);
247

248
249
250
    sendMessage(message);
}

251
void QmlJSInspectorClient::setDesignModeBehavior(bool inDesignMode)
252
253
254
255
256
257
258
{
    if (!m_connection || !m_connection->isConnected())
        return;

    QByteArray message;
    QDataStream ds(&message, QIODevice::WriteOnly);

259
    InspectorProtocol::Message cmd = InspectorProtocol::SetDesignMode;
260
    ds << cmd
261
262
       << inDesignMode;

263
    log(LogSend, cmd, QLatin1String(inDesignMode ? "true" : "false"));
264

265
266
267
    sendMessage(message);
}

268
void QmlJSInspectorClient::setAnimationSpeed(qreal slowDownFactor)
269
270
271
272
273
274
275
{
    if (!m_connection || !m_connection->isConnected())
        return;

    QByteArray message;
    QDataStream ds(&message, QIODevice::WriteOnly);

276
    InspectorProtocol::Message cmd = InspectorProtocol::SetAnimationSpeed;
277
    ds << cmd
278
279
280
281
282
283
284
       << slowDownFactor;


    log(LogSend, cmd, QString::number(slowDownFactor));

    sendMessage(message);
}
285

286
void QmlJSInspectorClient::setAnimationPaused(bool paused)
287
288
289
290
291
292
293
{
    if (!m_connection || !m_connection->isConnected())
        return;

    QByteArray message;
    QDataStream ds(&message, QIODevice::WriteOnly);

294
    InspectorProtocol::Message cmd = InspectorProtocol::SetAnimationPaused;
295
296
    ds << cmd
       << paused;
297

298
    log(LogSend, cmd, paused ? QLatin1String("true") : QLatin1String("false"));
299

300
301
302
    sendMessage(message);
}

303
void QmlJSInspectorClient::changeToColorPickerTool()
304
305
306
307
308
309
310
{
    if (!m_connection || !m_connection->isConnected())
        return;

    QByteArray message;
    QDataStream ds(&message, QIODevice::WriteOnly);

311
312
    InspectorProtocol::Message cmd = InspectorProtocol::ChangeTool;
    InspectorProtocol::Tool tool = InspectorProtocol::ColorPickerTool;
313
314
    ds << cmd
       << tool;
315

316
    log(LogSend, cmd, InspectorProtocol::toString(tool));
317

318
319
320
    sendMessage(message);
}

321
void QmlJSInspectorClient::changeToSelectTool()
322
323
324
325
326
327
328
{
    if (!m_connection || !m_connection->isConnected())
        return;

    QByteArray message;
    QDataStream ds(&message, QIODevice::WriteOnly);

329
330
    InspectorProtocol::Message cmd = InspectorProtocol::ChangeTool;
    InspectorProtocol::Tool tool = InspectorProtocol::SelectTool;
331
332
    ds << cmd
       << tool;
333

334
    log(LogSend, cmd, InspectorProtocol::toString(tool));
335

336
337
338
    sendMessage(message);
}

339
void QmlJSInspectorClient::changeToSelectMarqueeTool()
340
341
342
343
344
345
346
{
    if (!m_connection || !m_connection->isConnected())
        return;

    QByteArray message;
    QDataStream ds(&message, QIODevice::WriteOnly);

347
348
    InspectorProtocol::Message cmd = InspectorProtocol::ChangeTool;
    InspectorProtocol::Tool tool = InspectorProtocol::SelectMarqueeTool;
349
350
    ds << cmd
       << tool;
351

352
    log(LogSend, cmd, InspectorProtocol::toString(tool));
353

354
355
356
    sendMessage(message);
}

357
void QmlJSInspectorClient::changeToZoomTool()
358
359
360
361
362
363
364
{
    if (!m_connection || !m_connection->isConnected())
        return;

    QByteArray message;
    QDataStream ds(&message, QIODevice::WriteOnly);

365
366
    InspectorProtocol::Message cmd = InspectorProtocol::ChangeTool;
    InspectorProtocol::Tool tool = InspectorProtocol::ZoomTool;
367
368
    ds << cmd
       << tool;
369

370
    log(LogSend, cmd, InspectorProtocol::toString(tool));
371

372
373
374
    sendMessage(message);
}

375
void QmlJSInspectorClient::showAppOnTop(bool showOnTop)
376
377
378
379
380
381
382
{
    if (!m_connection || !m_connection->isConnected())
        return;

    QByteArray message;
    QDataStream ds(&message, QIODevice::WriteOnly);

383
    InspectorProtocol::Message cmd = InspectorProtocol::ShowAppOnTop;
384
    ds << cmd << showOnTop;
385

386
    log(LogSend, cmd, QLatin1String(showOnTop ? "true" : "false"));
387
388
389
390

    sendMessage(message);
}

391
void QmlJSInspectorClient::createQmlObject(const QString &qmlText, int parentDebugId,
392
                                             const QStringList &imports, const QString &filename, int order)
393
394
395
396
397
398
399
{
    if (!m_connection || !m_connection->isConnected())
        return;

    QByteArray message;
    QDataStream ds(&message, QIODevice::WriteOnly);

400
    InspectorProtocol::Message cmd = InspectorProtocol::CreateObject;
401
    ds << cmd
402
       << qmlText
403
       << parentDebugId
404
       << imports
405
406
       << filename
       << order;
407

408
409
    log(LogSend, cmd, QString("%1 %2 [%3] %4").arg(qmlText, QString::number(parentDebugId),
                                                   imports.join(","), filename));
410

411
412
413
    sendMessage(message);
}

414
void QmlJSInspectorClient::destroyQmlObject(int debugId)
415
416
417
418
419
420
{
    if (!m_connection || !m_connection->isConnected())
        return;
    QByteArray message;
    QDataStream ds(&message, QIODevice::WriteOnly);

421
    InspectorProtocol::Message cmd = InspectorProtocol::DestroyObject;
422
    ds << cmd << debugId;
423

424
    log(LogSend, cmd, QString::number(debugId));
425

426
427
428
    sendMessage(message);
}

429
void QmlJSInspectorClient::reparentQmlObject(int debugId, int newParent)
430
431
432
433
434
435
{
    if (!m_connection || !m_connection->isConnected())
        return;
    QByteArray message;
    QDataStream ds(&message, QIODevice::WriteOnly);

436
    InspectorProtocol::Message cmd = InspectorProtocol::MoveObject;
437
    ds << cmd
438
439
440
       << debugId
       << newParent;

441
442
    log(LogSend, cmd, QString("%1 %2").arg(QString::number(debugId),
                                           QString::number(newParent)));
443

444
445
446
    sendMessage(message);
}

447

448
void QmlJSInspectorClient::applyChangesToQmlFile()
449
450
451
452
453
454
455
{
    if (!m_connection || !m_connection->isConnected())
        return;

    // TODO
}

456
void QmlJSInspectorClient::applyChangesFromQmlFile()
457
458
459
460
461
462
463
{
    if (!m_connection || !m_connection->isConnected())
        return;

    // TODO
}

464
void QmlJSInspectorClient::log(LogDirection direction, InspectorProtocol::Message message,
465
                              const QString &extra)
466
467
{
    QString msg;
468
469
470
471
472
    if (direction == LogSend)
        msg += QLatin1String(" sending ");
    else
        msg += QLatin1String(" receiving ");

473
    msg += InspectorProtocol::toString(message);
474
475
    msg += QLatin1Char(' ');
    msg += extra;
476
477
    emit logActivity(name(), msg);
}
478
479
480

} // namespace Internal
} // namespace QmlJSInspector