QmlJS Live Preview: Showing object id's in context menu

To show the object string id's, the debug protocol is used to get them.
The problem is that the required methods in QDeclarativeContext(Data)
are not exposed, and it's too late to do it for 4.7.0. Hence this change
should be reverted in 4.8 when more efficient way of getting the id's
comes available.
This commit is contained in:
Lasse Holmstedt
2010-07-26 15:31:59 +02:00
parent fb681918c6
commit 689cbc06f1
9 changed files with 77 additions and 6 deletions

View File

@@ -401,8 +401,11 @@ void ClientProxy::objectTreeFetched(QDeclarativeDebugQuery::State state)
emit objectTreeUpdated(m_rootObject); emit objectTreeUpdated(m_rootObject);
if (isDesignClientConnected() && !m_designClient->selectedItemIds().isEmpty()) { if (isDesignClientConnected()) {
onCurrentObjectsChanged(m_designClient->selectedItemIds()); if (!m_designClient->selectedItemIds().isEmpty())
onCurrentObjectsChanged(m_designClient->selectedItemIds());
m_designClient->setObjectIdList(QList<QDeclarativeDebugObjectReference>() << m_rootObject);
} }
} }

View File

@@ -130,6 +130,40 @@ void QmlJSDesignDebugClient::setSelectedItemsByObjectId(const QList<QDeclarative
sendMessage(message); sendMessage(message);
} }
void recurseObjectIdList(const QDeclarativeDebugObjectReference &ref, QList<int> &debugIds, QList<QString> &objectIds)
{
debugIds << ref.debugId();
objectIds << ref.idString();
foreach(const QDeclarativeDebugObjectReference &child, ref.children()) {
recurseObjectIdList(child, debugIds, objectIds);
}
}
void QmlJSDesignDebugClient::setObjectIdList(const QList<QDeclarativeDebugObjectReference> &objectRoots)
{
QByteArray message;
QDataStream ds(&message, QIODevice::WriteOnly);
QList<int> debugIds;
QList<QString> objectIds;
foreach(const QDeclarativeDebugObjectReference &ref, objectRoots) {
recurseObjectIdList(ref, debugIds, objectIds);
}
ds << QByteArray("OBJECT_ID_LIST")
<< debugIds.length();
Q_ASSERT(debugIds.length() == objectIds.length());
for(int i = 0; i < debugIds.length(); ++i) {
ds << debugIds[i] << objectIds[i];
}
sendMessage(message);
}
void QmlJSDesignDebugClient::reloadViewer() void QmlJSDesignDebugClient::reloadViewer()
{ {
if (!m_connection || !m_connection->isConnected()) if (!m_connection || !m_connection->isConnected())

View File

@@ -73,6 +73,9 @@ public:
QList<int> selectedItemIds() const; QList<int> selectedItemIds() const;
// ### Qt 4.8: remove if we can have access to qdeclarativecontextdata or id's
void setObjectIdList(const QList<QDeclarativeDebugObjectReference> &objectRoots);
signals: signals:
void currentObjectsChanged(const QList<int> &debugIds); void currentObjectsChanged(const QList<int> &debugIds);
void selectedColorChanged(const QColor &color); void selectedColorChanged(const QColor &color);

View File

@@ -155,17 +155,17 @@ QList<QObject*> AbstractFormEditorTool::toObjectList(const QList<QGraphicsItem*>
return objects; return objects;
} }
QString AbstractFormEditorTool::titleForItem(const QGraphicsItem *item) QString AbstractFormEditorTool::titleForItem(QGraphicsItem *item)
{ {
QString className("QGraphicsItem"); QString className("QGraphicsItem");
QString objectStringId; QString objectStringId;
const QGraphicsObject *gfxObject = item->toGraphicsObject(); QGraphicsObject *gfxObject = item->toGraphicsObject();
if (gfxObject) { if (gfxObject) {
className = gfxObject->metaObject()->className(); className = gfxObject->metaObject()->className();
className.replace(QRegExp("_QMLTYPE_\\d+"), ""); className.replace(QRegExp("_QMLTYPE_\\d+"), "");
const QDeclarativeItem *declarativeItem = qobject_cast<const QDeclarativeItem*>(gfxObject); QDeclarativeItem *declarativeItem = qobject_cast<QDeclarativeItem*>(gfxObject);
if (declarativeItem) { if (declarativeItem) {
//QDeclarativeData *ddata = QDeclarativeData::get(declarativeItem); //QDeclarativeData *ddata = QDeclarativeData::get(declarativeItem);
//ddata->context->findObjectId(declarativeItem); //ddata->context->findObjectId(declarativeItem);
@@ -175,6 +175,7 @@ QString AbstractFormEditorTool::titleForItem(const QGraphicsItem *item)
// QDeclarativeContextData *cdata = QDeclarativeContextData::get(context); // QDeclarativeContextData *cdata = QDeclarativeContextData::get(context);
// if (cdata) // if (cdata)
// objectStringId = cdata->findObjectId(declarativeItem); // objectStringId = cdata->findObjectId(declarativeItem);
objectStringId = QDeclarativeDesignView::idStringForObject(declarativeItem);
} }
} }

View File

@@ -81,7 +81,7 @@ public:
bool topItemIsResizeHandle(const QList<QGraphicsItem*> &itemList); bool topItemIsResizeHandle(const QList<QGraphicsItem*> &itemList);
bool topSelectedItemIsMovable(const QList<QGraphicsItem*> &itemList); bool topSelectedItemIsMovable(const QList<QGraphicsItem*> &itemList);
static QString titleForItem(const QGraphicsItem *item); static QString titleForItem(QGraphicsItem *item);
static QList<QObject*> toObjectList(const QList<QGraphicsItem*> &itemList); static QList<QObject*> toObjectList(const QList<QGraphicsItem*> &itemList);
static QList<QGraphicsObject*> toGraphicsObjectList(const QList<QGraphicsItem*> &itemList); static QList<QGraphicsObject*> toGraphicsObjectList(const QList<QGraphicsItem*> &itemList);
static QGraphicsItem* topMovableGraphicsItem(const QList<QGraphicsItem*> &itemList); static QGraphicsItem* topMovableGraphicsItem(const QList<QGraphicsItem*> &itemList);

View File

@@ -67,6 +67,18 @@ void QDeclarativeDesignDebugServer::messageReceived(const QByteArray &message)
ds >> debugId; ds >> debugId;
if (QObject* obj = objectForId(debugId)) if (QObject* obj = objectForId(debugId))
obj->deleteLater(); obj->deleteLater();
} else if (type == "OBJECT_ID_LIST") {
int itemCount;
ds >> itemCount;
m_stringIdForObjectId.clear();
for(int i = 0; i < itemCount; ++i) {
int itemDebugId;
QString itemIdString;
ds >> itemDebugId
>> itemIdString;
m_stringIdForObjectId.insert(itemDebugId, itemIdString);
}
} }
} }
@@ -140,3 +152,10 @@ void QDeclarativeDesignDebugServer::selectedColorChanged(const QColor &color)
sendMessage(message); sendMessage(message);
} }
QString QDeclarativeDesignDebugServer::idStringForObject(QObject *obj) const
{
int id = idForObject(obj);
QString idString = m_stringIdForObjectId.value(id, QString());
return idString;
}

View File

@@ -45,6 +45,8 @@
#include <private/qdeclarativedebugservice_p.h> #include <private/qdeclarativedebugservice_p.h>
#include "qmlviewerconstants.h" #include "qmlviewerconstants.h"
#include <QHash>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QColor; class QColor;
@@ -65,6 +67,8 @@ public:
void setCurrentTool(QmlViewer::Constants::DesignTool toolId); void setCurrentTool(QmlViewer::Constants::DesignTool toolId);
void reloaded(); void reloaded();
QString idStringForObject(QObject *obj) const;
public Q_SLOTS: public Q_SLOTS:
void selectedColorChanged(const QColor &color); void selectedColorChanged(const QColor &color);
@@ -89,6 +93,7 @@ protected:
virtual void messageReceived(const QByteArray &); virtual void messageReceived(const QByteArray &);
private: private:
QHash<int, QString> m_stringIdForObjectId;
}; };

View File

@@ -553,6 +553,11 @@ void QDeclarativeDesignView::onCurrentObjectsChanged(QList<QObject*> objects)
highlight(items, IgnoreContext); highlight(items, IgnoreContext);
} }
QString QDeclarativeDesignView::idStringForObject(QObject *obj)
{
return qmlDesignDebugServer()->idStringForObject(obj);
}
QToolBar *QDeclarativeDesignView::toolbar() const QToolBar *QDeclarativeDesignView::toolbar() const
{ {
return m_toolbar; return m_toolbar;

View File

@@ -53,6 +53,7 @@ public:
QGraphicsItem *currentRootItem() const; QGraphicsItem *currentRootItem() const;
QToolBar *toolbar() const; QToolBar *toolbar() const;
static QString idStringForObject(QObject *obj);
public Q_SLOTS: public Q_SLOTS:
void setDesignModeBehavior(bool value); void setDesignModeBehavior(bool value);