1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
33 #include "qmljsobjecttree.h"
35 #include <QContextMenuEvent>
37 #include <QtGui/QMenu>
38 #include <QtGui/QAction>
39 #include <QApplication>
40 #include <QInputDialog>
44 namespace QmlJSInspector {
47 // *************************************************************************
49 // *************************************************************************
51 class ObjectTreeItem : public QTreeWidgetItem
54 explicit ObjectTreeItem(QTreeWidget *widget, int type = 0);
55 ObjectTreeItem(QTreeWidgetItem *parentItem, int type = 0);
56 QVariant data (int column, int role) const;
57 void setData (int column, int role, const QVariant & value);
59 void setHasValidDebugId(bool value);
63 bool m_hasValidDebugId;
66 ObjectTreeItem::ObjectTreeItem(QTreeWidget *widget, int type) :
67 QTreeWidgetItem(widget, type), m_hasValidDebugId(true)
72 ObjectTreeItem::ObjectTreeItem(QTreeWidgetItem *parentItem, int type) :
73 QTreeWidgetItem(parentItem, type), m_hasValidDebugId(true)
78 QVariant ObjectTreeItem::data (int column, int role) const
80 if (role == Qt::ForegroundRole)
81 return m_hasValidDebugId ? qApp->palette().color(QPalette::Foreground) : qApp->palette().color(QPalette::Disabled, QPalette::Foreground);
83 return QTreeWidgetItem::data(column, role);
86 void ObjectTreeItem::setData (int column, int role, const QVariant & value)
88 QTreeWidgetItem::setData(column, role, value);
91 void ObjectTreeItem::setHasValidDebugId(bool value)
93 m_hasValidDebugId = value;
96 // *************************************************************************
98 // *************************************************************************
100 QmlJSObjectTree::QmlJSObjectTree(QWidget *parent)
101 : QTreeWidget(parent)
103 , m_currentObjectDebugId(0)
105 setAttribute(Qt::WA_MacShowFocusRect, false);
106 setFrameStyle(QFrame::NoFrame);
107 setHeaderHidden(true);
108 setExpandsOnDoubleClick(false);
110 m_goToFileAction = new QAction(tr("Go to file"), this);
111 connect(m_goToFileAction, SIGNAL(triggered()), SLOT(goToFile()));
113 connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
114 SLOT(currentItemChanged(QTreeWidgetItem *)));
115 connect(this, SIGNAL(itemActivated(QTreeWidgetItem *, int)),
116 SLOT(activated(QTreeWidgetItem *)));
117 connect(this, SIGNAL(itemSelectionChanged()), SLOT(selectionChanged()));
120 void QmlJSObjectTree::selectionChanged()
122 if (selectedItems().isEmpty())
128 void QmlJSObjectTree::setCurrentObject(int debugId)
130 QTreeWidgetItem *item = findItemByObjectId(debugId);
132 setCurrentItem(item);
134 item->setExpanded(true);
138 void QmlJSObjectTree::currentItemChanged(QTreeWidgetItem *item)
143 QDeclarativeDebugObjectReference obj = item->data(0, Qt::UserRole).value<QDeclarativeDebugObjectReference>();
144 if (obj.debugId() >= 0)
145 emit currentObjectChanged(obj);
148 void QmlJSObjectTree::activated(QTreeWidgetItem *item)
153 QDeclarativeDebugObjectReference obj = item->data(0, Qt::UserRole).value<QDeclarativeDebugObjectReference>();
154 if (obj.debugId() >= 0)
158 void QmlJSObjectTree::buildTree(const QDeclarativeDebugObjectReference &obj, QTreeWidgetItem *parent)
163 if (obj.contextDebugId() < 0)
166 ObjectTreeItem *item = parent ? new ObjectTreeItem(parent) : new ObjectTreeItem(this);
167 if (obj.idString().isEmpty())
168 item->setText(0, QString("<%1>").arg(obj.className()));
170 item->setText(0, obj.idString());
171 item->setData(0, Qt::UserRole, qVariantFromValue(obj));
173 if (parent && obj.contextDebugId() >= 0
174 && obj.contextDebugId() != parent->data(0, Qt::UserRole
175 ).value<QDeclarativeDebugObjectReference>().contextDebugId())
178 QDeclarativeDebugFileReference source = obj.source();
179 if (!source.url().isEmpty()) {
180 QString toolTipString = tr("Url: ") + source.url().toString();
181 item->setToolTip(0, toolTipString);
184 item->setExpanded(true);
187 if (obj.contextDebugId() < 0)
188 item->setHasValidDebugId(false);
190 for (int ii = 0; ii < obj.children().count(); ++ii)
191 buildTree(obj.children().at(ii), item);
194 QTreeWidgetItem *QmlJSObjectTree::findItemByObjectId(int debugId) const
196 for (int i=0; i<topLevelItemCount(); ++i) {
197 QTreeWidgetItem *item = findItem(topLevelItem(i), debugId);
205 QTreeWidgetItem *QmlJSObjectTree::findItem(QTreeWidgetItem *item, int debugId) const
207 if (item->data(0, Qt::UserRole).value<QDeclarativeDebugObjectReference>().debugId() == debugId)
210 QTreeWidgetItem *child;
211 for (int i=0; i<item->childCount(); ++i) {
212 child = findItem(item->child(i), debugId);
220 void QmlJSObjectTree::goToFile()
222 QDeclarativeDebugObjectReference obj =
223 currentItem()->data(0, Qt::UserRole).value<QDeclarativeDebugObjectReference>();
225 if (obj.debugId() >= 0)
229 void QmlJSObjectTree::contextMenuEvent(QContextMenuEvent *event)
231 m_clickedItem = itemAt(QPoint(event->pos().x(),
237 menu.addAction(m_goToFileAction);
238 menu.exec(event->globalPos());