source: trunk/src/scripttools/debugging/qscriptdebuggerlocalsmodel.cpp@ 324

Last change on this file since 324 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 31.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtSCriptTools module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qscriptdebuggerlocalsmodel_p.h"
43#include "qscriptdebuggercommandschedulerjob_p.h"
44#include "qscriptdebuggervalue_p.h"
45#include "qscriptdebuggerresponse_p.h"
46#include "qscriptdebuggerevent_p.h"
47#include "qscriptdebuggervalueproperty_p.h"
48#include "qscriptdebuggercommandschedulerinterface_p.h"
49#include "qscriptdebuggercommandschedulerfrontend_p.h"
50#include "qscriptdebuggerjobschedulerinterface_p.h"
51#include "qscriptdebuggerobjectsnapshotdelta_p.h"
52
53#include "private/qabstractitemmodel_p.h"
54
55#include <QtCore/qdebug.h>
56#include <QtGui/qbrush.h>
57#include <QtGui/qfont.h>
58
59Q_DECLARE_METATYPE(QScriptDebuggerObjectSnapshotDelta)
60
61QT_BEGIN_NAMESPACE
62
63struct QScriptDebuggerLocalsModelNode
64{
65 enum PopulationState {
66 NotPopulated,
67 Populating,
68 Populated
69 };
70
71 QScriptDebuggerLocalsModelNode()
72 : parent(0), populationState(NotPopulated), snapshotId(-1), changed(false) {}
73
74 QScriptDebuggerLocalsModelNode(
75 const QScriptDebuggerValueProperty &prop,
76 QScriptDebuggerLocalsModelNode *par)
77 : property(prop), parent(par),
78 populationState(NotPopulated), snapshotId(-1), changed(false)
79 {
80 parent->children.append(this);
81 }
82
83 ~QScriptDebuggerLocalsModelNode() { qDeleteAll(children); }
84
85 QScriptDebuggerLocalsModelNode *findChild(const QString &name)
86 {
87 for (int i = 0; i < children.size(); ++i) {
88 QScriptDebuggerLocalsModelNode *child = children.at(i);
89 if (child->property.name() == name)
90 return child;
91 }
92 return 0;
93 }
94
95 QScriptDebuggerValueProperty property;
96 QScriptDebuggerLocalsModelNode *parent;
97 QList<QScriptDebuggerLocalsModelNode*> children;
98 PopulationState populationState;
99 int snapshotId;
100 bool changed;
101};
102
103class QScriptDebuggerLocalsModelPrivate
104 : public QAbstractItemModelPrivate
105{
106 Q_DECLARE_PUBLIC(QScriptDebuggerLocalsModel)
107public:
108 QScriptDebuggerLocalsModelPrivate();
109 ~QScriptDebuggerLocalsModelPrivate();
110
111 static QScriptDebuggerLocalsModelPrivate *get(QScriptDebuggerLocalsModel *q);
112
113 QModelIndex addTopLevelObject(const QString &name, const QScriptDebuggerValue &object);
114
115 QScriptDebuggerLocalsModelNode *nodeFromIndex(const QModelIndex &index) const;
116 QModelIndex indexFromNode(QScriptDebuggerLocalsModelNode *node) const;
117 bool isTopLevelNode(QScriptDebuggerLocalsModelNode *node) const;
118
119 void populateIndex(const QModelIndex &index);
120 void reallyPopulateIndex(const QModelIndex &index,
121 const QScriptDebuggerValuePropertyList &props);
122 void syncIndex(const QModelIndex &index);
123 void reallySyncIndex(const QModelIndex &index,
124 const QScriptDebuggerObjectSnapshotDelta &delta);
125 void syncTopLevelNodes();
126 void removeTopLevelNodes();
127 void emitScopeObjectAvailable(const QModelIndex &index);
128
129 void emitDataChanged(const QModelIndex &tl, const QModelIndex &br);
130 void removeChild(const QModelIndex &parentIndex,
131 QScriptDebuggerLocalsModelNode *parentNode, int row);
132 void depopulate(QScriptDebuggerLocalsModelNode *node);
133 void repopulate(QScriptDebuggerLocalsModelNode *node);
134 void addChildren(const QModelIndex &parentIndex,
135 QScriptDebuggerLocalsModelNode *parentNode,
136 const QScriptDebuggerValuePropertyList &props);
137
138 void deleteObjectSnapshots(const QList<qint64> &snapshotIds);
139
140 QScriptDebuggerJobSchedulerInterface *jobScheduler;
141 QScriptDebuggerCommandSchedulerInterface *commandScheduler;
142 QScriptDebuggerLocalsModelNode *invisibleRootNode;
143 int frameIndex;
144};
145
146QScriptDebuggerLocalsModelPrivate::QScriptDebuggerLocalsModelPrivate()
147{
148 invisibleRootNode = new QScriptDebuggerLocalsModelNode();
149 frameIndex = -1;
150}
151
152QScriptDebuggerLocalsModelPrivate::~QScriptDebuggerLocalsModelPrivate()
153{
154 delete invisibleRootNode;
155}
156
157void QScriptDebuggerLocalsModelPrivate::emitDataChanged(const QModelIndex &tl, const QModelIndex &br)
158{
159 q_func()->dataChanged(tl, br);
160}
161
162static QList<qint64> findSnapshotIdsRecursively(QScriptDebuggerLocalsModelNode *root)
163{
164 QList<qint64> result;
165 if (root->snapshotId == -1) {
166 Q_ASSERT(root->children.isEmpty());
167 return result;
168 }
169 QList<QScriptDebuggerLocalsModelNode*> nodeStack;
170 nodeStack.append(root);
171 while (!nodeStack.isEmpty()) {
172 QScriptDebuggerLocalsModelNode *node = nodeStack.takeFirst();
173 result.append(node->snapshotId);
174 for (int i = 0; i < node->children.count(); ++i) {
175 QScriptDebuggerLocalsModelNode *child = node->children.at(i);
176 if (child->snapshotId != -1)
177 nodeStack.prepend(child);
178 }
179 }
180 return result;
181}
182
183void QScriptDebuggerLocalsModelPrivate::removeChild(const QModelIndex &parentIndex,
184 QScriptDebuggerLocalsModelNode *parentNode,
185 int row)
186{
187 Q_Q(QScriptDebuggerLocalsModel);
188 q->beginRemoveRows(parentIndex, row, row);
189 QScriptDebuggerLocalsModelNode *child = parentNode->children.takeAt(row);
190 QList<qint64> snapshotIds = findSnapshotIdsRecursively(child);
191 delete child;
192 q->endRemoveRows();
193 deleteObjectSnapshots(snapshotIds);
194}
195
196void QScriptDebuggerLocalsModelPrivate::depopulate(QScriptDebuggerLocalsModelNode *node)
197{
198 Q_Q(QScriptDebuggerLocalsModel);
199 bool hasChildren = !node->children.isEmpty();
200 if (hasChildren)
201 q->beginRemoveRows(indexFromNode(node), 0, node->children.count() - 1);
202 QList<qint64> snapshotIds = findSnapshotIdsRecursively(node);
203 qDeleteAll(node->children);
204 node->children.clear();
205 node->snapshotId = -1;
206 node->populationState = QScriptDebuggerLocalsModelNode::NotPopulated;
207 if (hasChildren)
208 q->endRemoveRows();
209 deleteObjectSnapshots(snapshotIds);
210}
211
212void QScriptDebuggerLocalsModelPrivate::repopulate(QScriptDebuggerLocalsModelNode *node)
213{
214 if (node->populationState != QScriptDebuggerLocalsModelNode::Populated)
215 return;
216 depopulate(node);
217 if (node->property.value().type() == QScriptDebuggerValue::ObjectValue)
218 populateIndex(indexFromNode(node));
219}
220
221void QScriptDebuggerLocalsModelPrivate::addChildren(const QModelIndex &parentIndex,
222 QScriptDebuggerLocalsModelNode *parentNode,
223 const QScriptDebuggerValuePropertyList &props)
224{
225 Q_Q(QScriptDebuggerLocalsModel);
226 if (props.isEmpty())
227 return;
228 int first = parentNode->children.size();
229 int last = first + props.size() - 1;
230 q->beginInsertRows(parentIndex, first, last);
231 for (int i = 0; i < props.size(); ++i)
232 new QScriptDebuggerLocalsModelNode(props.at(i), parentNode);
233 q->endInsertRows();
234}
235
236void QScriptDebuggerLocalsModelPrivate::deleteObjectSnapshots(const QList<qint64> &snapshotIds)
237{
238 QScriptDebuggerCommandSchedulerFrontend frontend(commandScheduler, 0);
239 for (int i = 0; i < snapshotIds.size(); ++i)
240 frontend.scheduleDeleteScriptObjectSnapshot(snapshotIds.at(i));
241}
242
243QScriptDebuggerLocalsModelPrivate *QScriptDebuggerLocalsModelPrivate::get(QScriptDebuggerLocalsModel *q)
244{
245 return q->d_func();
246}
247
248namespace {
249
250class SetPropertyJob : public QScriptDebuggerCommandSchedulerJob
251{
252public:
253 SetPropertyJob(const QPersistentModelIndex &index,
254 const QString &expression,
255 QScriptDebuggerCommandSchedulerInterface *scheduler)
256 : QScriptDebuggerCommandSchedulerJob(scheduler),
257 m_index(index), m_expression(expression), m_state(0) {}
258
259 QScriptDebuggerLocalsModelPrivate *model() const
260 {
261 if (!m_index.isValid())
262 return 0;
263 QAbstractItemModel *m = const_cast<QAbstractItemModel*>(m_index.model());
264 QScriptDebuggerLocalsModel *lm = qobject_cast<QScriptDebuggerLocalsModel*>(m);
265 return QScriptDebuggerLocalsModelPrivate::get(lm);
266 }
267
268 void start()
269 {
270 if (!m_index.isValid()) {
271 // nothing to do, the node has been removed
272 return;
273 }
274 QScriptDebuggerLocalsModelNode *node = model()->nodeFromIndex(m_index);
275 QScriptDebuggerCommandSchedulerFrontend frontend(commandScheduler(), this);
276 frontend.scheduleEvaluate(model()->frameIndex, m_expression,
277 QString::fromLatin1("set property '%0' (%1)")
278 .arg(node->property.name())
279 .arg(QDateTime::currentDateTime().toString()));
280 }
281
282 void handleResponse(const QScriptDebuggerResponse &, int)
283 {
284 switch (m_state) {
285 case 0:
286 hibernateUntilEvaluateFinished();
287 ++m_state;
288 break;
289 case 1:
290 finish();
291 break;
292 }
293 }
294
295 void evaluateFinished(const QScriptDebuggerValue &result)
296 {
297 if (!m_index.isValid()) {
298 // nothing to do, the node has been removed
299 return;
300 }
301 QScriptDebuggerLocalsModelNode *node = model()->nodeFromIndex(m_index);
302 Q_ASSERT(node->parent != 0);
303 QScriptDebuggerValue object = node->parent->property.value();
304 QScriptDebuggerCommandSchedulerFrontend frontend(commandScheduler(), this);
305 frontend.scheduleSetScriptValueProperty(object, node->property.name(), result);
306 }
307
308private:
309 QPersistentModelIndex m_index;
310 QString m_expression;
311 int m_state;
312};
313
314} // namespace
315
316QScriptDebuggerLocalsModelNode *QScriptDebuggerLocalsModelPrivate::nodeFromIndex(
317 const QModelIndex &index) const
318{
319 if (!index.isValid())
320 return invisibleRootNode;
321 return static_cast<QScriptDebuggerLocalsModelNode*>(index.internalPointer());
322}
323
324QModelIndex QScriptDebuggerLocalsModelPrivate::indexFromNode(
325 QScriptDebuggerLocalsModelNode *node) const
326{
327 if (!node || (node == invisibleRootNode))
328 return QModelIndex();
329 QScriptDebuggerLocalsModelNode *par = node->parent;
330 int row = par ? par->children.indexOf(node) : 0;
331 return createIndex(row, 0, node);
332}
333
334bool QScriptDebuggerLocalsModelPrivate::isTopLevelNode(QScriptDebuggerLocalsModelNode *node) const
335{
336 return node && (node->parent == invisibleRootNode);
337}
338
339namespace {
340
341class PopulateModelIndexJob : public QScriptDebuggerCommandSchedulerJob
342{
343public:
344 PopulateModelIndexJob(const QPersistentModelIndex &index,
345 QScriptDebuggerCommandSchedulerInterface *scheduler)
346 : QScriptDebuggerCommandSchedulerJob(scheduler),
347 m_index(index), m_state(0)
348 { }
349
350 QScriptDebuggerLocalsModelPrivate *model() const
351 {
352 if (!m_index.isValid())
353 return 0;
354 QAbstractItemModel *m = const_cast<QAbstractItemModel*>(m_index.model());
355 QScriptDebuggerLocalsModel *lm = qobject_cast<QScriptDebuggerLocalsModel*>(m);
356 return QScriptDebuggerLocalsModelPrivate::get(lm);
357 }
358
359 void start()
360 {
361 if (!m_index.isValid()) {
362 // nothing to do, the node has been removed
363 return;
364 }
365 QScriptDebuggerCommandSchedulerFrontend frontend(commandScheduler(), this);
366 frontend.scheduleNewScriptObjectSnapshot();
367 }
368
369 void handleResponse(const QScriptDebuggerResponse &response,
370 int)
371 {
372 if (!m_index.isValid()) {
373 // the node has been removed
374 finish();
375 return;
376 }
377 switch (m_state) {
378 case 0: {
379 QScriptDebuggerLocalsModelNode *node = model()->nodeFromIndex(m_index);
380 Q_ASSERT(node->populationState == QScriptDebuggerLocalsModelNode::Populating);
381 node->snapshotId = response.resultAsInt();
382 QScriptDebuggerCommandSchedulerFrontend frontend(commandScheduler(), this);
383 frontend.scheduleScriptObjectSnapshotCapture(node->snapshotId, node->property.value());
384 ++m_state;
385 } break;
386 case 1: {
387 QScriptDebuggerObjectSnapshotDelta delta;
388 delta = qvariant_cast<QScriptDebuggerObjectSnapshotDelta>(response.result());
389 Q_ASSERT(delta.removedProperties.isEmpty());
390 Q_ASSERT(delta.changedProperties.isEmpty());
391 QScriptDebuggerValuePropertyList props = delta.addedProperties;
392 model()->reallyPopulateIndex(m_index, props);
393 finish();
394 } break;
395 }
396 }
397
398private:
399 QPersistentModelIndex m_index;
400 int m_state;
401};
402
403} // namespace
404
405void QScriptDebuggerLocalsModelPrivate::populateIndex(
406 const QModelIndex &index)
407{
408 if (!index.isValid())
409 return;
410 QScriptDebuggerLocalsModelNode *node = nodeFromIndex(index);
411 if (node->populationState != QScriptDebuggerLocalsModelNode::NotPopulated)
412 return;
413 if (node->property.value().type() != QScriptDebuggerValue::ObjectValue)
414 return;
415 node->populationState = QScriptDebuggerLocalsModelNode::Populating;
416 QScriptDebuggerJob *job = new PopulateModelIndexJob(index, commandScheduler);
417 jobScheduler->scheduleJob(job);
418}
419
420void QScriptDebuggerLocalsModelPrivate::reallyPopulateIndex(
421 const QModelIndex &index,
422 const QScriptDebuggerValuePropertyList &props)
423{
424 if (!index.isValid())
425 return;
426 QScriptDebuggerLocalsModelNode *node = nodeFromIndex(index);
427 Q_ASSERT(node->populationState == QScriptDebuggerLocalsModelNode::Populating);
428 node->populationState = QScriptDebuggerLocalsModelNode::Populated;
429 addChildren(index, node, props);
430}
431
432QScriptDebuggerLocalsModel::QScriptDebuggerLocalsModel(
433 QScriptDebuggerJobSchedulerInterface *jobScheduler,
434 QScriptDebuggerCommandSchedulerInterface *commandScheduler,
435 QObject *parent)
436 : QAbstractItemModel(*new QScriptDebuggerLocalsModelPrivate, parent)
437{
438 Q_D(QScriptDebuggerLocalsModel);
439 d->jobScheduler = jobScheduler;
440 d->commandScheduler = commandScheduler;
441}
442
443QScriptDebuggerLocalsModel::~QScriptDebuggerLocalsModel()
444{
445 Q_D(QScriptDebuggerLocalsModel);
446 QList<qint64> snapshotIds;
447 for (int i = 0; i < d->invisibleRootNode->children.count(); ++i)
448 snapshotIds += findSnapshotIdsRecursively(d->invisibleRootNode->children.at(i));
449 QScriptDebuggerCommandSchedulerFrontend frontend(d->commandScheduler, 0);
450 for (int j = 0; j < snapshotIds.size(); ++j)
451 frontend.scheduleDeleteScriptObjectSnapshot(snapshotIds.at(j));
452}
453
454QModelIndex QScriptDebuggerLocalsModelPrivate::addTopLevelObject(const QString &name, const QScriptDebuggerValue &object)
455{
456 Q_Q(QScriptDebuggerLocalsModel);
457 QScriptDebuggerLocalsModelNode *node = invisibleRootNode->findChild(name);
458 if (node)
459 return indexFromNode(node);
460 QScriptDebuggerValueProperty prop(name, object,
461 QString::fromLatin1(""), // ### string representation of object
462 /*flags=*/0);
463 int rowIndex = invisibleRootNode->children.size();
464 q->beginInsertRows(QModelIndex(), rowIndex, rowIndex);
465 node = new QScriptDebuggerLocalsModelNode(prop, invisibleRootNode);
466 q->endInsertRows();
467 return indexFromNode(node);
468}
469
470namespace {
471
472class InitModelJob : public QScriptDebuggerCommandSchedulerJob
473{
474public:
475 InitModelJob(QScriptDebuggerLocalsModelPrivate *model,
476 int frameIndex,
477 QScriptDebuggerCommandSchedulerInterface *scheduler)
478 : QScriptDebuggerCommandSchedulerJob(scheduler),
479 m_model(model), m_frameIndex(frameIndex), m_state(0)
480 { }
481
482 void start()
483 {
484 QScriptDebuggerCommandSchedulerFrontend frontend(commandScheduler(), this);
485 frontend.scheduleGetScopeChain(m_frameIndex);
486 }
487
488 void handleResponse(const QScriptDebuggerResponse &response,
489 int)
490 {
491 QScriptDebuggerCommandSchedulerFrontend frontend(commandScheduler(), this);
492 switch (m_state) {
493 case 0: {
494 QScriptDebuggerValueList scopeChain = response.resultAsScriptValueList();
495 for (int i = 0; i < scopeChain.size(); ++i) {
496 const QScriptDebuggerValue &scopeObject = scopeChain.at(i);
497 QString name = QString::fromLatin1("Scope");
498 if (i > 0)
499 name.append(QString::fromLatin1(" (%0)").arg(i));
500 QModelIndex index = m_model->addTopLevelObject(name, scopeObject);
501 if (i == 0)
502 m_model->emitScopeObjectAvailable(index);
503 }
504 frontend.scheduleGetThisObject(m_frameIndex);
505 ++m_state;
506 } break;
507 case 1: {
508 QScriptDebuggerValue thisObject = response.resultAsScriptValue();
509 m_model->addTopLevelObject(QLatin1String("this"), thisObject);
510 finish();
511 } break;
512 }
513 }
514
515private:
516 QScriptDebuggerLocalsModelPrivate *m_model;
517 int m_frameIndex;
518 int m_state;
519};
520
521} // namespace
522
523void QScriptDebuggerLocalsModel::init(int frameIndex)
524{
525 Q_D(QScriptDebuggerLocalsModel);
526 d->frameIndex = frameIndex;
527 QScriptDebuggerJob *job = new InitModelJob(d, frameIndex, d->commandScheduler);
528 d->jobScheduler->scheduleJob(job);
529}
530
531namespace {
532
533class SyncModelJob : public QScriptDebuggerCommandSchedulerJob
534{
535public:
536 SyncModelJob(QScriptDebuggerLocalsModelPrivate *model,
537 int frameIndex,
538 QScriptDebuggerCommandSchedulerInterface *scheduler)
539 : QScriptDebuggerCommandSchedulerJob(scheduler),
540 m_model(model), m_frameIndex(frameIndex), m_state(0)
541 { }
542
543 void start()
544 {
545 QScriptDebuggerCommandSchedulerFrontend frontend(commandScheduler(), this);
546 frontend.scheduleGetScopeChain(m_frameIndex);
547 }
548
549 void handleResponse(const QScriptDebuggerResponse &response,
550 int)
551 {
552 QScriptDebuggerCommandSchedulerFrontend frontend(commandScheduler(), this);
553 switch (m_state) {
554 case 0: {
555 QScriptDebuggerValueList scopeChain = response.resultAsScriptValueList();
556 m_topLevelObjects << scopeChain;
557 frontend.scheduleGetThisObject(m_frameIndex);
558 ++m_state;
559 } break;
560 case 1: {
561 QScriptDebuggerValue thisObject = response.resultAsScriptValue();
562 m_topLevelObjects.append(thisObject);
563 bool equal = (m_topLevelObjects.size() == m_model->invisibleRootNode->children.size());
564 for (int i = 0; equal && (i < m_topLevelObjects.size()); ++i) {
565 const QScriptDebuggerValue &object = m_topLevelObjects.at(i);
566 equal = (object == m_model->invisibleRootNode->children.at(i)->property.value());
567 }
568 if (!equal) {
569 // the scope chain and/or this-object changed, so invalidate the model.
570 // we could try to be more clever, i.e. figure out
571 // exactly which objects were popped/pushed
572 m_model->removeTopLevelNodes();
573 for (int j = 0; j < m_topLevelObjects.size(); ++j) {
574 const QScriptDebuggerValue &object = m_topLevelObjects.at(j);
575 QString name;
576 if (j == m_topLevelObjects.size()-1) {
577 name = QString::fromLatin1("this");
578 } else {
579 name = QString::fromLatin1("Scope");
580 if (j > 0)
581 name.append(QString::fromLatin1(" (%0)").arg(j));
582 }
583 QModelIndex index = m_model->addTopLevelObject(name, object);
584 if (j == 0)
585 m_model->emitScopeObjectAvailable(index);
586 }
587 } else {
588 m_model->syncTopLevelNodes();
589 }
590 finish();
591 } break;
592 }
593 }
594
595private:
596 QScriptDebuggerLocalsModelPrivate *m_model;
597 int m_frameIndex;
598 int m_state;
599 QScriptDebuggerValueList m_topLevelObjects;
600};
601
602} // namespace
603
604void QScriptDebuggerLocalsModel::sync(int frameIndex)
605{
606 Q_D(QScriptDebuggerLocalsModel);
607 d->frameIndex = frameIndex;
608 QScriptDebuggerJob *job = new SyncModelJob(d, frameIndex, d->commandScheduler);
609 d->jobScheduler->scheduleJob(job);
610}
611
612namespace {
613
614class SyncModelIndexJob : public QScriptDebuggerCommandSchedulerJob
615{
616public:
617 SyncModelIndexJob(const QPersistentModelIndex &index,
618 QScriptDebuggerCommandSchedulerInterface *scheduler)
619 : QScriptDebuggerCommandSchedulerJob(scheduler),
620 m_index(index)
621 { }
622
623 QScriptDebuggerLocalsModelPrivate *model() const
624 {
625 if (!m_index.isValid())
626 return 0;
627 QAbstractItemModel *m = const_cast<QAbstractItemModel*>(m_index.model());
628 QScriptDebuggerLocalsModel *lm = qobject_cast<QScriptDebuggerLocalsModel*>(m);
629 return QScriptDebuggerLocalsModelPrivate::get(lm);
630 }
631
632 void start()
633 {
634 if (!m_index.isValid()) {
635 // nothing to do, the node has been removed
636 return;
637 }
638 QScriptDebuggerCommandSchedulerFrontend frontend(commandScheduler(), this);
639 QScriptDebuggerLocalsModelNode *node = model()->nodeFromIndex(m_index);
640 frontend.scheduleScriptObjectSnapshotCapture(node->snapshotId, node->property.value());
641 }
642
643 void handleResponse(const QScriptDebuggerResponse &response,
644 int)
645 {
646 QScriptDebuggerObjectSnapshotDelta delta;
647 delta = qvariant_cast<QScriptDebuggerObjectSnapshotDelta>(response.result());
648 model()->reallySyncIndex(m_index, delta);
649 finish();
650 }
651
652private:
653 QPersistentModelIndex m_index;
654};
655
656} // namespace
657
658void QScriptDebuggerLocalsModelPrivate::syncIndex(const QModelIndex &index)
659{
660 if (!index.isValid())
661 return;
662 QScriptDebuggerLocalsModelNode *node = nodeFromIndex(index);
663 if (node->populationState != QScriptDebuggerLocalsModelNode::Populated)
664 return;
665 QScriptDebuggerJob *job = new SyncModelIndexJob(index, commandScheduler);
666 jobScheduler->scheduleJob(job);
667}
668
669void QScriptDebuggerLocalsModelPrivate::reallySyncIndex(const QModelIndex &index,
670 const QScriptDebuggerObjectSnapshotDelta &delta)
671{
672 if (!index.isValid())
673 return;
674 QScriptDebuggerLocalsModelNode *node = nodeFromIndex(index);
675 // update or remove existing children
676 for (int i = 0; i < node->children.count(); ++i) {
677 QScriptDebuggerLocalsModelNode *child = node->children.at(i);
678 int j;
679 for (j = 0; j < delta.changedProperties.count(); ++j) {
680 if (child->property.name() == delta.changedProperties.at(j).name()) {
681 child->property = delta.changedProperties.at(j);
682 child->changed = true;
683 emitDataChanged(index, index.sibling(0, 1));
684 repopulate(child);
685 break;
686 }
687 }
688 if (j != delta.changedProperties.count())
689 continue; // was changed
690 for (j = 0; j < delta.removedProperties.count(); ++j) {
691 if (child->property.name() == delta.removedProperties.at(j)) {
692 removeChild(index, node, i);
693 --i;
694 break;
695 }
696 }
697 if (j != delta.removedProperties.count())
698 continue; // was removed
699 // neither changed nor removed, but its children might be
700 if (child->populationState == QScriptDebuggerLocalsModelNode::Populated) {
701 QScriptDebuggerJob *job = new SyncModelIndexJob(indexFromNode(child), commandScheduler);
702 jobScheduler->scheduleJob(job);
703 }
704 }
705 addChildren(index, node, delta.addedProperties);
706}
707
708void QScriptDebuggerLocalsModelPrivate::syncTopLevelNodes()
709{
710 Q_Q(QScriptDebuggerLocalsModel);
711 for (int i = 0; i < invisibleRootNode->children.count(); ++i) {
712 QModelIndex index = q->index(i, 0, QModelIndex());
713 syncIndex(index);
714 if (i == 0)
715 emit q->scopeObjectAvailable(index);
716 }
717}
718
719void QScriptDebuggerLocalsModelPrivate::removeTopLevelNodes()
720{
721 while (!invisibleRootNode->children.isEmpty())
722 removeChild(QModelIndex(), invisibleRootNode, 0);
723}
724
725void QScriptDebuggerLocalsModelPrivate::emitScopeObjectAvailable(const QModelIndex &index)
726{
727 emit q_func()->scopeObjectAvailable(index);
728}
729
730int QScriptDebuggerLocalsModel::frameIndex() const
731{
732 Q_D(const QScriptDebuggerLocalsModel);
733 return d->frameIndex;
734}
735
736/*!
737 \reimp
738*/
739QModelIndex QScriptDebuggerLocalsModel::index(int row, int column, const QModelIndex &parent) const
740{
741 Q_D(const QScriptDebuggerLocalsModel);
742 QScriptDebuggerLocalsModelNode *node = d->nodeFromIndex(parent);
743 if ((row < 0) || (row >= node->children.count()))
744 return QModelIndex();
745 return createIndex(row, column, node->children.at(row));
746}
747
748/*!
749 \reimp
750*/
751QModelIndex QScriptDebuggerLocalsModel::parent(const QModelIndex &index) const
752{
753 Q_D(const QScriptDebuggerLocalsModel);
754 if (!index.isValid())
755 return QModelIndex();
756 QScriptDebuggerLocalsModelNode *node = d->nodeFromIndex(index);
757 return d->indexFromNode(node->parent);
758}
759
760/*!
761 \reimp
762*/
763int QScriptDebuggerLocalsModel::columnCount(const QModelIndex &) const
764{
765 return 2;
766}
767
768/*!
769 \reimp
770*/
771int QScriptDebuggerLocalsModel::rowCount(const QModelIndex &parent) const
772{
773 Q_D(const QScriptDebuggerLocalsModel);
774 // ### need this to make it work with a sortfilterproxymodel (QSFPM is too eager)
775 const_cast<QScriptDebuggerLocalsModel*>(this)->fetchMore(parent);
776 QScriptDebuggerLocalsModelNode *node = d->nodeFromIndex(parent);
777 return node ? node->children.count() : 0;
778}
779
780/*!
781 \reimp
782*/
783QVariant QScriptDebuggerLocalsModel::data(const QModelIndex &index, int role) const
784{
785 Q_D(const QScriptDebuggerLocalsModel);
786 if (!index.isValid())
787 return QVariant();
788 QScriptDebuggerLocalsModelNode *node = d->nodeFromIndex(index);
789 if (role == Qt::DisplayRole) {
790 if (index.column() == 0)
791 return node->property.name();
792 else if (index.column() == 1) {
793 QString str = node->property.valueAsString();
794 if (str.indexOf(QLatin1Char('\n')) != -1) {
795 QStringList lines = str.split(QLatin1Char('\n'));
796 int lineCount = lines.size();
797 if (lineCount > 1) {
798 lines = lines.mid(0, 1);
799 lines.append(QString::fromLatin1("(... %0 more lines ...)").arg(lineCount - 1));
800 }
801 str = lines.join(QLatin1String("\n"));
802 }
803 return str;
804 }
805 } else if (role == Qt::EditRole) {
806 if ((index.column() == 1) && !d->isTopLevelNode(node)) {
807 QString str = node->property.valueAsString();
808 if (node->property.value().type() == QScriptDebuggerValue::StringValue) {
809 // escape
810 str.replace(QLatin1String("\""), QLatin1String("\\\""));
811 str.prepend(QLatin1Char('\"'));
812 str.append(QLatin1Char('\"'));
813 }
814 return str;
815 }
816 } else if (role == Qt::ToolTipRole) {
817 if (index.column() == 1) {
818 QString str = node->property.valueAsString();
819 if (str.indexOf(QLatin1Char('\n')) != -1)
820 return str;
821 }
822 }
823 // ### do this in the delegate
824 else if (role == Qt::BackgroundRole) {
825 if (d->isTopLevelNode(node))
826 return QBrush(Qt::darkGray);
827 } else if (role == Qt::TextColorRole) {
828 if (d->isTopLevelNode(node))
829 return QColor(Qt::white);
830 } else if (role == Qt::FontRole) {
831 if (d->isTopLevelNode(node) || node->changed) {
832 QFont fnt;
833 fnt.setBold(true);
834 return fnt;
835 }
836 }
837 return QVariant();
838}
839
840/*!
841 \reimp
842*/
843bool QScriptDebuggerLocalsModel::setData(const QModelIndex &index, const QVariant &value, int role)
844{
845 Q_D(QScriptDebuggerLocalsModel);
846 if (!index.isValid())
847 return false;
848 if (role != Qt::EditRole)
849 return false;
850 QScriptDebuggerLocalsModelNode *node = d->nodeFromIndex(index);
851 if (!node)
852 return false;
853 QString expr = value.toString().trimmed();
854 if (expr.isEmpty())
855 return false;
856 QScriptDebuggerJob *job = new SetPropertyJob(index, expr, d->commandScheduler);
857 d->jobScheduler->scheduleJob(job);
858 return true;
859}
860
861/*!
862 \reimp
863*/
864QVariant QScriptDebuggerLocalsModel::headerData(int section, Qt::Orientation orient, int role) const
865{
866 if (orient == Qt::Horizontal) {
867 if (role == Qt::DisplayRole) {
868 if (section == 0)
869 return QObject::tr("Name");
870 else if (section == 1)
871 return QObject::tr("Value");
872 }
873 }
874 return QVariant();
875}
876
877/*!
878 \reimp
879*/
880Qt::ItemFlags QScriptDebuggerLocalsModel::flags(const QModelIndex &index) const
881{
882 Q_D(const QScriptDebuggerLocalsModel);
883 if (!index.isValid())
884 return 0;
885 Qt::ItemFlags ret = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
886 if ((index.column() == 1) && index.parent().isValid()) {
887 QScriptDebuggerLocalsModelNode *node = d->nodeFromIndex(index);
888 if (!(node->property.flags() & QScriptValue::ReadOnly))
889 ret |= Qt::ItemIsEditable;
890 }
891 return ret;
892}
893
894/*!
895 \reimp
896*/
897bool QScriptDebuggerLocalsModel::hasChildren(const QModelIndex &parent) const
898{
899 Q_D(const QScriptDebuggerLocalsModel);
900 QScriptDebuggerLocalsModelNode *node = d->nodeFromIndex(parent);
901 if (!node)
902 return false;
903 return !node->children.isEmpty()
904 || ((node->property.value().type() == QScriptDebuggerValue::ObjectValue)
905 && (node->populationState == QScriptDebuggerLocalsModelNode::NotPopulated));
906}
907
908/*!
909 \reimp
910*/
911bool QScriptDebuggerLocalsModel::canFetchMore(const QModelIndex &parent) const
912{
913 Q_D(const QScriptDebuggerLocalsModel);
914 if (!parent.isValid())
915 return false;
916 QScriptDebuggerLocalsModelNode *node = d->nodeFromIndex(parent);
917 return node
918 && (node->property.value().type() == QScriptDebuggerValue::ObjectValue)
919 && (node->populationState == QScriptDebuggerLocalsModelNode::NotPopulated);
920}
921
922/*!
923 \reimp
924*/
925void QScriptDebuggerLocalsModel::fetchMore(const QModelIndex &parent)
926{
927 Q_D(QScriptDebuggerLocalsModel);
928 d->populateIndex(parent);
929}
930
931QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.