From 4a4fc8584f9c8592a6e3eb641a799c234245bdff Mon Sep 17 00:00:00 2001
From: Lasse Holmstedt <lasse.holmstedt@nokia.com>
Date: Wed, 14 Jul 2010 14:21:10 +0200
Subject: [PATCH] update object tree when unknown debug id is received

---
 .../qmljsinspector/qmljsclientproxy.cpp       | 39 +++++++++++++++++--
 src/plugins/qmljsinspector/qmljsclientproxy.h |  1 +
 .../qmljsinspector/qmljsdesigndebugclient.cpp | 11 +++---
 .../qmljsinspector/qmljsdesigndebugclient.h   |  2 +-
 .../qmljsinspector/qmljslivetextpreview.cpp   |  1 +
 .../qmlviewer/editor/selectionindicator.cpp   | 17 ++------
 .../qml/qmlviewer/qdeclarativedesignview.cpp  | 18 ++++-----
 7 files changed, 55 insertions(+), 34 deletions(-)

diff --git a/src/plugins/qmljsinspector/qmljsclientproxy.cpp b/src/plugins/qmljsinspector/qmljsclientproxy.cpp
index 705f6a73bfa..81576cae43f 100644
--- a/src/plugins/qmljsinspector/qmljsclientproxy.cpp
+++ b/src/plugins/qmljsinspector/qmljsclientproxy.cpp
@@ -66,8 +66,8 @@ bool ClientProxy::connectToViewer(const QString &host, quint16 port)
 
     if (m_designClient) {
 
-        disconnect(m_designClient, SIGNAL(currentObjectsChanged(QList<QDeclarativeDebugObjectReference>)),
-                            this, SIGNAL(selectedItemsChanged(QList<QDeclarativeDebugObjectReference>)));
+        disconnect(m_designClient, SIGNAL(currentObjectsChanged(QList<int>)),
+                            this, SLOT(onCurrentObjectsChanged(QList<int>)));
         disconnect(m_designClient,
                    SIGNAL(colorPickerActivated()), this, SIGNAL(colorPickerActivated()));
         disconnect(m_designClient,
@@ -113,6 +113,37 @@ bool ClientProxy::connectToViewer(const QString &host, quint16 port)
     return true;
 }
 
+void ClientProxy::onCurrentObjectsChanged(const QList<int> &debugIds)
+{
+    QList<QDeclarativeDebugObjectReference> selectedItems;
+
+    foreach(int debugId, debugIds) {
+        QDeclarativeDebugObjectReference ref = objectReferenceForId(debugId);
+        if (ref.debugId() != -1) {
+            selectedItems << ref;
+        } else {
+            // ### FIXME right now, there's no way in the protocol to
+            // a) get some item and know its parent (although that's possible by adding it to a separate plugin)
+            // b) add children to part of an existing tree.
+            // So the only choice that remains is to update the complete tree when we have an unknown debug id.
+            if (!m_objectTreeQuery) {
+                m_objectTreeQuery = m_client->queryObjectRecursive(m_rootObject, this);
+
+                if (!m_objectTreeQuery->isWaiting()) {
+                    objectTreeFetched();
+                } else {
+                    connect(m_objectTreeQuery,
+                            SIGNAL(stateChanged(QDeclarativeDebugQuery::State)),
+                            SLOT(objectTreeFetched(QDeclarativeDebugQuery::State)));
+                }
+            }
+
+        }
+    }
+
+    emit selectedItemsChanged(selectedItems);
+}
+
 void ClientProxy::disconnectFromViewer()
 {
     m_conn->disconnectFromHost();
@@ -162,8 +193,8 @@ void ClientProxy::connectionStateChanged()
                 emit connected(m_client);
 
                 connect(m_designClient,
-                        SIGNAL(currentObjectsChanged(QList<QDeclarativeDebugObjectReference>)),
-                        SIGNAL(selectedItemsChanged(QList<QDeclarativeDebugObjectReference>)));
+                        SIGNAL(currentObjectsChanged(QList<int>)),
+                        SLOT(onCurrentObjectsChanged(QList<int>)));
                 connect(m_designClient,
                         SIGNAL(colorPickerActivated()), SIGNAL(colorPickerActivated()));
                 connect(m_designClient,
diff --git a/src/plugins/qmljsinspector/qmljsclientproxy.h b/src/plugins/qmljsinspector/qmljsclientproxy.h
index f7cff5a8c06..b8a4bbe595b 100644
--- a/src/plugins/qmljsinspector/qmljsclientproxy.h
+++ b/src/plugins/qmljsinspector/qmljsclientproxy.h
@@ -112,6 +112,7 @@ private slots:
     void connectionStateChanged();
     void connectionError();
 
+    void onCurrentObjectsChanged(const QList<int> &debugIds);
     void updateEngineList();
     void objectTreeFetched(QDeclarativeDebugQuery::State state = QDeclarativeDebugQuery::Completed);
 
diff --git a/src/plugins/qmljsinspector/qmljsdesigndebugclient.cpp b/src/plugins/qmljsinspector/qmljsdesigndebugclient.cpp
index 7fb14f0359d..e422b8254a1 100644
--- a/src/plugins/qmljsinspector/qmljsdesigndebugclient.cpp
+++ b/src/plugins/qmljsinspector/qmljsdesigndebugclient.cpp
@@ -61,21 +61,20 @@ void QmlJSDesignDebugClient::messageReceived(const QByteArray &message)
     QByteArray type;
     ds >> type;
 
-    QList<QDeclarativeDebugObjectReference> references;
-
     if (type == "CURRENT_OBJECTS_CHANGED") {
         int objectCount;
         ds >> objectCount;
+        QList<int> debugIds;
+
         for(int i = 0; i < objectCount; ++i) {
             int debugId;
             ds >> debugId;
             if (debugId != -1) {
-                QDeclarativeDebugObjectReference ref = ClientProxy::instance()->objectReferenceForId(debugId);
-                if (ref.debugId() != -1)
-                    references << ref;
+                debugIds << debugId;
             }
         }
-        emit currentObjectsChanged(references);
+
+        emit currentObjectsChanged(debugIds);
     } else if (type == "TOOL_CHANGED") {
         int toolId;
         ds >> toolId;
diff --git a/src/plugins/qmljsinspector/qmljsdesigndebugclient.h b/src/plugins/qmljsinspector/qmljsdesigndebugclient.h
index 4d03abb5645..3488bad96ed 100644
--- a/src/plugins/qmljsinspector/qmljsdesigndebugclient.h
+++ b/src/plugins/qmljsinspector/qmljsdesigndebugclient.h
@@ -68,7 +68,7 @@ public:
     void applyChangesFromQmlFile();
 
 signals:
-    void currentObjectsChanged(const QList<QDeclarativeDebugObjectReference> &currentObjects);
+    void currentObjectsChanged(const QList<int> &debugIds);
     void colorPickerActivated();
     void selectToolActivated();
     void selectMarqueeToolActivated();
diff --git a/src/plugins/qmljsinspector/qmljslivetextpreview.cpp b/src/plugins/qmljsinspector/qmljslivetextpreview.cpp
index e627d7eb0a1..0858ed59254 100644
--- a/src/plugins/qmljsinspector/qmljslivetextpreview.cpp
+++ b/src/plugins/qmljsinspector/qmljslivetextpreview.cpp
@@ -143,6 +143,7 @@ void QmlJSLiveTextPreview::documentChanged(QmlJS::Document::Ptr doc)
     if (doc && m_previousDoc && doc->fileName() == m_previousDoc->fileName() && doc->qmlProgram() && m_previousDoc->qmlProgram()) {
         if (m_debugIds.isEmpty())
             m_debugIds = m_initialTable.value(doc->fileName());
+
         Delta delta;
         m_debugIds = delta(m_previousDoc, doc,  m_debugIds);
 
diff --git a/src/tools/qml/qmlviewer/editor/selectionindicator.cpp b/src/tools/qml/qmlviewer/editor/selectionindicator.cpp
index 0bed179e5f5..f20c751ba60 100644
--- a/src/tools/qml/qmlviewer/editor/selectionindicator.cpp
+++ b/src/tools/qml/qmlviewer/editor/selectionindicator.cpp
@@ -68,20 +68,12 @@ void SelectionIndicator::clear()
     m_indicatorShapeHash.clear();
 }
 
-//static void alignVertices(QPolygonF &polygon, double factor)
-//{
-//    QMutableVectorIterator<QPointF> iterator(polygon);
-//    while (iterator.hasNext()) {
-//        QPointF &vertex = iterator.next();
-//        vertex.setX(std::floor(vertex.x()) + factor);
-//        vertex.setY(std::floor(vertex.y()) + factor);
-//    }
-//
-//}
-
 QPolygonF SelectionIndicator::addBoundingRectToPolygon(QGraphicsItem *item, QPolygonF &polygon)
 {
-    polygon = polygon.united(item->mapToScene(item->boundingRect()));
+    // ### remove this if statement when QTBUG-12172 gets fixed
+    if (item->boundingRect() != QRectF(0,0,0,0))
+        polygon = polygon.united(item->mapToScene(item->boundingRect()));
+
     foreach(QGraphicsItem *child, item->childItems()) {
         if (!m_view->isEditorItem(child))
             addBoundingRectToPolygon(child, polygon);
@@ -104,7 +96,6 @@ void SelectionIndicator::setItems(const QList<QGraphicsObject*> &itemList)
 
         QPolygonF boundingRectInLayerItemSpace = m_layerItem->mapFromScene(boundingShapeInSceneSpace);
 
-
         QPen pen;
         pen.setColor(QColor(108, 141, 221));
         newSelectionIndicatorGraphicsItem->setData(Constants::EditorItemDataKey, QVariant(true));
diff --git a/src/tools/qml/qmlviewer/qdeclarativedesignview.cpp b/src/tools/qml/qmlviewer/qdeclarativedesignview.cpp
index e944d7807f7..fef2cff1b6c 100644
--- a/src/tools/qml/qmlviewer/qdeclarativedesignview.cpp
+++ b/src/tools/qml/qmlviewer/qdeclarativedesignview.cpp
@@ -120,10 +120,7 @@ void QDeclarativeDesignView::mouseReleaseEvent(QMouseEvent *event)
     m_cursorPos = event->pos();
     m_currentTool->mouseReleaseEvent(event);
 
-    if (event->buttons() & Qt::LeftButton) {
-        qDebug() << "setting current objects";
-        qmlDesignDebugServer()->setCurrentObjects(AbstractFormEditorTool::toObjectList(selectedItems()));
-    }
+    qmlDesignDebugServer()->setCurrentObjects(AbstractFormEditorTool::toObjectList(selectedItems()));
 }
 
 void QDeclarativeDesignView::keyPressEvent(QKeyEvent *event)
@@ -447,17 +444,18 @@ QList<QGraphicsItem*> QDeclarativeDesignView::filterForCurrentContext(QList<QGra
     foreach(QGraphicsItem *item, itemlist) {
 
         if (isEditorItem(item) || !m_subcomponentEditorTool->isDirectChildOfContext(item)) {
-            int index = itemlist.indexOf(item);
 
             // if we're a child, but not directly, replace with the parent that is directly in context.
             if (QGraphicsItem *contextParent = m_subcomponentEditorTool->firstChildOfContext(item)) {
-                if (index >= 0) {
-                    itemlist.replace(index, contextParent);
-                } else {
-                    itemlist.append(contextParent);
+                if (contextParent != item) {
+                    if (itemlist.contains(contextParent)) {
+                        itemlist.removeOne(item);
+                    } else {
+                        itemlist.replace(itemlist.indexOf(item), contextParent);
+                    }
                 }
             } else {
-                itemlist.removeAt(index);
+                itemlist.removeOne(item);
             }
         }
     }
-- 
GitLab