From f8fc9084601679dc4f8734200fd787d0b6e9fc34 Mon Sep 17 00:00:00 2001
From: Lasse Holmstedt <lasse.holmstedt@nokia.com>
Date: Wed, 14 Jul 2010 17:22:22 +0200
Subject: [PATCH] Updating selection after new tree is fetched & code cleanup

---
 .../qmljsinspector/qmljsclientproxy.cpp       | 43 ++++++++++++-------
 src/plugins/qmljsinspector/qmljsclientproxy.h |  1 +
 .../qmljsinspector/qmljsdesigndebugclient.cpp | 11 +++--
 .../qmljsinspector/qmljsdesigndebugclient.h   |  3 ++
 4 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/src/plugins/qmljsinspector/qmljsclientproxy.cpp b/src/plugins/qmljsinspector/qmljsclientproxy.cpp
index 81576cae43f..9b01e49e3cd 100644
--- a/src/plugins/qmljsinspector/qmljsclientproxy.cpp
+++ b/src/plugins/qmljsinspector/qmljsclientproxy.cpp
@@ -128,20 +128,22 @@ void ClientProxy::onCurrentObjectsChanged(const QList<int> &debugIds)
             // 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);
+    if (m_objectTreeQuery) {
+        if (!m_objectTreeQuery->isWaiting()) {
+            objectTreeFetched();
+        } else {
+            connect(m_objectTreeQuery,
+                    SIGNAL(stateChanged(QDeclarativeDebugQuery::State)),
+                    SLOT(objectTreeFetched(QDeclarativeDebugQuery::State)));
+        }
+    } else {
+        emit selectedItemsChanged(selectedItems);
+    }
 }
 
 void ClientProxy::disconnectFromViewer()
@@ -358,49 +360,58 @@ void ClientProxy::objectTreeFetched(QDeclarativeDebugQuery::State state)
     m_objectTreeQuery = 0;
 
     emit objectTreeUpdated(m_rootObject);
+
+    if (isDesignClientConnected() && !m_designClient->selectedItemIds().isEmpty()) {
+        onCurrentObjectsChanged(m_designClient->selectedItemIds());
+    }
+
 }
 
 void ClientProxy::reloadQmlViewer()
 {
-    if (m_designClient && m_conn->isConnected())
+    if (isDesignClientConnected())
         m_designClient->reloadViewer();
 }
 
 void ClientProxy::setDesignModeBehavior(bool inDesignMode)
 {
-    if (m_designClient && m_conn->isConnected())
+    if (isDesignClientConnected())
         m_designClient->setDesignModeBehavior(inDesignMode);
 }
 
 void ClientProxy::setAnimationSpeed(qreal slowdownFactor)
 {
-    if (m_designClient && m_conn->isConnected())
+    if (isDesignClientConnected())
         m_designClient->setAnimationSpeed(slowdownFactor);
 }
 
 void ClientProxy::changeToColorPickerTool()
 {
-    if (m_designClient && m_conn->isConnected())
+    if (isDesignClientConnected())
         m_designClient->changeToColorPickerTool();
 }
 
 void ClientProxy::changeToZoomTool()
 {
-    if (m_designClient && m_conn->isConnected())
+    if (isDesignClientConnected())
         m_designClient->changeToZoomTool();
 }
 void ClientProxy::changeToSelectTool()
 {
-    if (m_designClient && m_conn->isConnected())
+    if (isDesignClientConnected())
         m_designClient->changeToSelectTool();
 }
 
 void ClientProxy::changeToSelectMarqueeTool()
 {
-    if (m_designClient && m_conn->isConnected())
+    if (isDesignClientConnected())
         m_designClient->changeToSelectMarqueeTool();
 }
 
+bool ClientProxy::isDesignClientConnected() const
+{
+    return (m_designClient && m_conn->isConnected());
+}
 
 void ClientProxy::reloadEngines()
 {
diff --git a/src/plugins/qmljsinspector/qmljsclientproxy.h b/src/plugins/qmljsinspector/qmljsclientproxy.h
index b8a4bbe595b..fac72cf2ad0 100644
--- a/src/plugins/qmljsinspector/qmljsclientproxy.h
+++ b/src/plugins/qmljsinspector/qmljsclientproxy.h
@@ -117,6 +117,7 @@ private slots:
     void objectTreeFetched(QDeclarativeDebugQuery::State state = QDeclarativeDebugQuery::Completed);
 
 private:
+    bool isDesignClientConnected() const;
     void reloadEngines();
     QList<QDeclarativeDebugObjectReference> objectReferences(const QUrl &url, const QDeclarativeDebugObjectReference &objectRef) const;
     QDeclarativeDebugObjectReference objectReferenceForId(int debugId, const QDeclarativeDebugObjectReference &ref) const;
diff --git a/src/plugins/qmljsinspector/qmljsdesigndebugclient.cpp b/src/plugins/qmljsinspector/qmljsdesigndebugclient.cpp
index e422b8254a1..e4de7efe97e 100644
--- a/src/plugins/qmljsinspector/qmljsdesigndebugclient.cpp
+++ b/src/plugins/qmljsinspector/qmljsdesigndebugclient.cpp
@@ -64,17 +64,17 @@ void QmlJSDesignDebugClient::messageReceived(const QByteArray &message)
     if (type == "CURRENT_OBJECTS_CHANGED") {
         int objectCount;
         ds >> objectCount;
-        QList<int> debugIds;
+        m_selectedItemIds.clear();
 
         for(int i = 0; i < objectCount; ++i) {
             int debugId;
             ds >> debugId;
             if (debugId != -1) {
-                debugIds << debugId;
+                m_selectedItemIds  << debugId;
             }
         }
 
-        emit currentObjectsChanged(debugIds);
+        emit currentObjectsChanged(m_selectedItemIds);
     } else if (type == "TOOL_CHANGED") {
         int toolId;
         ds >> toolId;
@@ -99,6 +99,11 @@ void QmlJSDesignDebugClient::messageReceived(const QByteArray &message)
     }
 }
 
+QList<int> QmlJSDesignDebugClient::selectedItemIds() const
+{
+    return m_selectedItemIds;
+}
+
 void QmlJSDesignDebugClient::setSelectedItemsByObjectId(const QList<QDeclarativeDebugObjectReference> &objects)
 {
     if (!m_connection || !m_connection->isConnected())
diff --git a/src/plugins/qmljsinspector/qmljsdesigndebugclient.h b/src/plugins/qmljsinspector/qmljsdesigndebugclient.h
index 3488bad96ed..f4124b1a815 100644
--- a/src/plugins/qmljsinspector/qmljsdesigndebugclient.h
+++ b/src/plugins/qmljsinspector/qmljsdesigndebugclient.h
@@ -67,6 +67,8 @@ public:
     void applyChangesToQmlFile();
     void applyChangesFromQmlFile();
 
+    QList<int> selectedItemIds() const;
+
 signals:
     void currentObjectsChanged(const QList<int> &debugIds);
     void colorPickerActivated();
@@ -80,6 +82,7 @@ protected:
     virtual void messageReceived(const QByteArray &);
 
 private:
+    QList<int> m_selectedItemIds;
     QDeclarativeDebugConnection *m_connection;
 };
 
-- 
GitLab