Commit 689cbc06 authored by Lasse Holmstedt's avatar Lasse Holmstedt
Browse files

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.
parent fb681918
......@@ -401,8 +401,11 @@ void ClientProxy::objectTreeFetched(QDeclarativeDebugQuery::State state)
emit objectTreeUpdated(m_rootObject);
if (isDesignClientConnected() && !m_designClient->selectedItemIds().isEmpty()) {
onCurrentObjectsChanged(m_designClient->selectedItemIds());
if (isDesignClientConnected()) {
if (!m_designClient->selectedItemIds().isEmpty())
onCurrentObjectsChanged(m_designClient->selectedItemIds());
m_designClient->setObjectIdList(QList<QDeclarativeDebugObjectReference>() << m_rootObject);
}
}
......
......@@ -130,6 +130,40 @@ void QmlJSDesignDebugClient::setSelectedItemsByObjectId(const QList<QDeclarative
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()
{
if (!m_connection || !m_connection->isConnected())
......
......@@ -73,6 +73,9 @@ public:
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:
void currentObjectsChanged(const QList<int> &debugIds);
void selectedColorChanged(const QColor &color);
......
......@@ -155,17 +155,17 @@ QList<QObject*> AbstractFormEditorTool::toObjectList(const QList<QGraphicsItem*>
return objects;
}
QString AbstractFormEditorTool::titleForItem(const QGraphicsItem *item)
QString AbstractFormEditorTool::titleForItem(QGraphicsItem *item)
{
QString className("QGraphicsItem");
QString objectStringId;
const QGraphicsObject *gfxObject = item->toGraphicsObject();
QGraphicsObject *gfxObject = item->toGraphicsObject();
if (gfxObject) {
className = gfxObject->metaObject()->className();
className.replace(QRegExp("_QMLTYPE_\\d+"), "");
const QDeclarativeItem *declarativeItem = qobject_cast<const QDeclarativeItem*>(gfxObject);
QDeclarativeItem *declarativeItem = qobject_cast<QDeclarativeItem*>(gfxObject);
if (declarativeItem) {
//QDeclarativeData *ddata = QDeclarativeData::get(declarativeItem);
//ddata->context->findObjectId(declarativeItem);
......@@ -175,6 +175,7 @@ QString AbstractFormEditorTool::titleForItem(const QGraphicsItem *item)
// QDeclarativeContextData *cdata = QDeclarativeContextData::get(context);
// if (cdata)
// objectStringId = cdata->findObjectId(declarativeItem);
objectStringId = QDeclarativeDesignView::idStringForObject(declarativeItem);
}
}
......
......@@ -81,7 +81,7 @@ public:
bool topItemIsResizeHandle(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<QGraphicsObject*> toGraphicsObjectList(const QList<QGraphicsItem*> &itemList);
static QGraphicsItem* topMovableGraphicsItem(const QList<QGraphicsItem*> &itemList);
......
......@@ -67,6 +67,18 @@ void QDeclarativeDesignDebugServer::messageReceived(const QByteArray &message)
ds >> debugId;
if (QObject* obj = objectForId(debugId))
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);
}
QString QDeclarativeDesignDebugServer::idStringForObject(QObject *obj) const
{
int id = idForObject(obj);
QString idString = m_stringIdForObjectId.value(id, QString());
return idString;
}
......@@ -45,6 +45,8 @@
#include <private/qdeclarativedebugservice_p.h>
#include "qmlviewerconstants.h"
#include <QHash>
QT_BEGIN_NAMESPACE
class QColor;
......@@ -65,6 +67,8 @@ public:
void setCurrentTool(QmlViewer::Constants::DesignTool toolId);
void reloaded();
QString idStringForObject(QObject *obj) const;
public Q_SLOTS:
void selectedColorChanged(const QColor &color);
......@@ -89,6 +93,7 @@ protected:
virtual void messageReceived(const QByteArray &);
private:
QHash<int, QString> m_stringIdForObjectId;
};
......
......@@ -553,6 +553,11 @@ void QDeclarativeDesignView::onCurrentObjectsChanged(QList<QObject*> objects)
highlight(items, IgnoreContext);
}
QString QDeclarativeDesignView::idStringForObject(QObject *obj)
{
return qmlDesignDebugServer()->idStringForObject(obj);
}
QToolBar *QDeclarativeDesignView::toolbar() const
{
return m_toolbar;
......
......@@ -53,6 +53,7 @@ public:
QGraphicsItem *currentRootItem() const;
QToolBar *toolbar() const;
static QString idStringForObject(QObject *obj);
public Q_SLOTS:
void setDesignModeBehavior(bool value);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment