From 6292f1a5f7fabba79ccfbcce8d4e933979c36c12 Mon Sep 17 00:00:00 2001
From: Olivier Goffart <olivier.goffart@nokia.com>
Date: Mon, 19 Jul 2010 10:42:39 +0200
Subject: [PATCH] QmlJsDelta: Add ability to remove object

---
 src/plugins/qmljsinspector/qmljsclientproxy.cpp |  7 +++++++
 src/plugins/qmljsinspector/qmljsclientproxy.h   |  1 +
 src/plugins/qmljsinspector/qmljsdelta.cpp       | 17 ++++++++++++++---
 src/plugins/qmljsinspector/qmljsdelta.h         |  9 ++++++---
 .../qmljsinspector/qmljsdesigndebugclient.cpp   | 13 +++++++++++++
 .../qmljsinspector/qmljsdesigndebugclient.h     |  1 +
 .../qdeclarativedesigndebugserver.cpp           |  5 +++++
 7 files changed, 47 insertions(+), 6 deletions(-)

diff --git a/src/plugins/qmljsinspector/qmljsclientproxy.cpp b/src/plugins/qmljsinspector/qmljsclientproxy.cpp
index 7ab52f51d34..0c5996c0703 100644
--- a/src/plugins/qmljsinspector/qmljsclientproxy.cpp
+++ b/src/plugins/qmljsinspector/qmljsclientproxy.cpp
@@ -439,6 +439,13 @@ void ClientProxy::createQmlObject(const QString &qmlText, const QDeclarativeDebu
         m_designClient->createQmlObject(qmlText, parentRef, imports, filename);
 }
 
+void QmlJSInspector::Internal::ClientProxy::destroyQmlObject(int debugId)
+{
+    if (isDesignClientConnected())
+        m_designClient->destroyQmlObject(debugId);
+}
+
+
 bool ClientProxy::isDesignClientConnected() const
 {
     return (m_designClient && m_conn->isConnected());
diff --git a/src/plugins/qmljsinspector/qmljsclientproxy.h b/src/plugins/qmljsinspector/qmljsclientproxy.h
index cfd2634c23d..6a205ca57ee 100644
--- a/src/plugins/qmljsinspector/qmljsclientproxy.h
+++ b/src/plugins/qmljsinspector/qmljsclientproxy.h
@@ -106,6 +106,7 @@ public slots:
     void changeToSelectMarqueeTool();
     void createQmlObject(const QString &qmlText, const QDeclarativeDebugObjectReference &parent,
                          const QStringList &imports, const QString &filename = QString());
+    void destroyQmlObject(int debugId);
 
 private slots:
     void contextChanged();
diff --git a/src/plugins/qmljsinspector/qmljsdelta.cpp b/src/plugins/qmljsinspector/qmljsdelta.cpp
index 1a45632bec1..18cbc70e2c8 100644
--- a/src/plugins/qmljsinspector/qmljsdelta.cpp
+++ b/src/plugins/qmljsinspector/qmljsdelta.cpp
@@ -310,7 +310,7 @@ void Delta::insert(UiObjectMember *member, UiObjectMember *parentMember, const Q
     }
 }
 
-void QmlJSInspector::Internal::Delta::update(UiObjectDefinition* oldObject, const QmlJS::Document::Ptr& oldDoc,
+void Delta::update(UiObjectDefinition* oldObject, const QmlJS::Document::Ptr& oldDoc,
                                              UiObjectDefinition* newObject, const QmlJS::Document::Ptr& newDoc,
                                              const QList< QDeclarativeDebugObjectReference >& debugReferences)
 {
@@ -396,6 +396,15 @@ void QmlJSInspector::Internal::Delta::update(UiObjectDefinition* oldObject, cons
     }
 }
 
+void Delta::remove(const QList< QDeclarativeDebugObjectReference >& debugReferences)
+{
+    foreach (const QDeclarativeDebugObjectReference &ref, debugReferences) {
+        if (ref.debugId() != -1)
+            ClientProxy::instance()->destroyQmlObject(ref.debugId()); // ### remove
+    }
+}
+
+
 
 
 Delta::DebugIdMap Delta::operator()(const Document::Ptr &doc1, const Document::Ptr &doc2, const DebugIdMap &debugIds)
@@ -438,7 +447,6 @@ Delta::DebugIdMap Delta::operator()(const Document::Ptr &doc1, const Document::P
         }
         //qDebug() << "Delta::operator():  match "<< label(x, doc1) << "with parent " << label(parents1.parent.value(x), doc1)
         //     << " to "<< label(y, doc2) << "with parent " << label(parents2.parent.value(y), doc2);
-        
 
         if (!M.contains(parents1.parent.value(x),parents2.parent.value(y))) {
             qDebug () << "Delta::operator():  move " << label(y, doc2) << " from " << label(parents1.parent.value(x), doc1)
@@ -454,7 +462,10 @@ Delta::DebugIdMap Delta::operator()(const Document::Ptr &doc1, const Document::P
         if (!cast<UiObjectDefinition *>(x))
             continue;
         if (!M.way1.contains(x)) {
-            qDebug () << "Delta::operator():  remove " << label(x, doc1) << " ### TODO";
+            qDebug () << "Delta::operator():  remove " << label(x, doc1);
+            QList< QDeclarativeDebugObjectReference > ids = debugIds.value(x);
+            if (!ids.isEmpty())
+                remove(ids);
             continue;
         }
     }
diff --git a/src/plugins/qmljsinspector/qmljsdelta.h b/src/plugins/qmljsinspector/qmljsdelta.h
index d58d9c5f3bc..c03769bccd0 100644
--- a/src/plugins/qmljsinspector/qmljsdelta.h
+++ b/src/plugins/qmljsinspector/qmljsdelta.h
@@ -72,9 +72,15 @@ public:
     static bool compare(QmlJS::AST::UiQualifiedId *id, QmlJS::AST::UiQualifiedId *other);
     static QmlJS::AST::UiObjectMemberList *objectMembers(QmlJS::AST::UiObjectMember *object);
 
+
 private:
     void insert(UiObjectMember *member, UiObjectMember *parentMember,
                 const QList<QDeclarativeDebugObjectReference> &debugReferences, const Document::Ptr &doc);
+    void update(UiObjectDefinition* oldObject, const QmlJS::Document::Ptr& oldDoc,
+                UiObjectDefinition* newObject, const QmlJS::Document::Ptr& newDoc,
+                const QList<QDeclarativeDebugObjectReference >& debugReferences);
+    void remove(const QList< QDeclarativeDebugObjectReference > &debugReferences);
+
     void updateScriptBinding(const QDeclarativeDebugObjectReference &objectReference,
                              QmlJS::AST::UiScriptBinding *scriptBinding,
                              const QString &propertyName,
@@ -84,9 +90,6 @@ private:
                             const QString &methodName,
                             const QString &methodBody);
 
-    void update(UiObjectDefinition* oldObject, const QmlJS::Document::Ptr& oldDoc,
-                UiObjectDefinition* newObject, const QmlJS::Document::Ptr& newDoc,
-                const QList<QDeclarativeDebugObjectReference >& debugReferences);
 
 private:
     QmlJS::Document::Ptr _doc;
diff --git a/src/plugins/qmljsinspector/qmljsdesigndebugclient.cpp b/src/plugins/qmljsinspector/qmljsdesigndebugclient.cpp
index 0e8a2c9a464..eb5315be280 100644
--- a/src/plugins/qmljsinspector/qmljsdesigndebugclient.cpp
+++ b/src/plugins/qmljsinspector/qmljsdesigndebugclient.cpp
@@ -231,6 +231,19 @@ void QmlJSDesignDebugClient::createQmlObject(const QString &qmlText, const QDecl
     sendMessage(message);
 }
 
+void QmlJSDesignDebugClient::destroyQmlObject(int debugId)
+{
+    if (!m_connection || !m_connection->isConnected())
+        return;
+    QByteArray message;
+    QDataStream ds(&message, QIODevice::WriteOnly);
+
+    ds << QByteArray("DESTROY_OBJECT")  << debugId;
+
+    sendMessage(message);
+}
+
+
 void QmlJSDesignDebugClient::applyChangesToQmlFile()
 {
     if (!m_connection || !m_connection->isConnected())
diff --git a/src/plugins/qmljsinspector/qmljsdesigndebugclient.h b/src/plugins/qmljsinspector/qmljsdesigndebugclient.h
index 32c0fcbf4a8..a37a8745d2e 100644
--- a/src/plugins/qmljsinspector/qmljsdesigndebugclient.h
+++ b/src/plugins/qmljsinspector/qmljsdesigndebugclient.h
@@ -66,6 +66,7 @@ public:
 
     void createQmlObject(const QString &qmlText, const QDeclarativeDebugObjectReference &parentRef,
                          const QStringList &imports, const QString &filename);
+    void destroyQmlObject(int debugId);
 
     void applyChangesToQmlFile();
     void applyChangesFromQmlFile();
diff --git a/src/tools/qml/qmlobserver/qdeclarativedesigndebugserver.cpp b/src/tools/qml/qmlobserver/qdeclarativedesigndebugserver.cpp
index 8930e317994..5242826c0fa 100644
--- a/src/tools/qml/qmlobserver/qdeclarativedesigndebugserver.cpp
+++ b/src/tools/qml/qmlobserver/qdeclarativedesigndebugserver.cpp
@@ -60,6 +60,11 @@ void QDeclarativeDesignDebugServer::messageReceived(const QByteArray &message)
         QStringList imports;
         ds >> qml >> parentId >> imports >> filename;
         emit objectCreationRequested(qml, objectForId(parentId), imports, filename);
+    } else if (type == "DESTROY_OBJECT") {
+        int debugId;
+        ds >> debugId;
+        if (QObject* obj = objectForId(debugId))
+            obj->deleteLater();
     }
 }
 
-- 
GitLab