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

Last change on this file since 1054 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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