OSDN Git Service

QmlLivePreview: removing and inserting animations and transitions
authorChristiaan Janssen <christiaan.janssen@nokia.com>
Tue, 24 May 2011 09:40:17 +0000 (11:40 +0200)
committerKai Koehne <kai.koehne@nokia.com>
Tue, 24 May 2011 11:03:43 +0000 (13:03 +0200)
Change-Id: Ic776f63f5d7925ac7dfd99be53c10b9af4cb9545
Reviewed-on: http://codereview.qt.nokia.com/75
Reviewed-by: Kai Koehne <kai.koehne@nokia.com>
share/qtcreator/qml/qmljsdebugger/include/qdeclarativeobserverservice.h
share/qtcreator/qml/qmljsdebugger/qdeclarativeobserverservice.cpp
share/qtcreator/qml/qmljsdebugger/qdeclarativeviewobserver.cpp
share/qtcreator/qml/qmljsdebugger/qdeclarativeviewobserver_p.h
src/libs/qmljs/qmljsdelta.cpp
src/libs/qmljs/qmljsdelta.h
src/plugins/qmljsinspector/qmljsclientproxy.cpp
src/plugins/qmljsinspector/qmljsclientproxy.h
src/plugins/qmljsinspector/qmljslivetextpreview.cpp
src/plugins/qmljsinspector/qmljsobserverclient.cpp
src/plugins/qmljsinspector/qmljsobserverclient.h

index ae9f48e..5d05f62 100644 (file)
@@ -82,8 +82,9 @@ Q_SIGNALS:
     void colorPickerToolRequested();
 
     void objectCreationRequested(const QString &qml, QObject *parent,
-                                 const QStringList &imports, const QString &filename = QString());
+                                 const QStringList &imports, const QString &filename = QString(), int order = -1);
     void objectReparentRequested(QObject *object, QObject *newParent);
+    void objectDeletionRequested(QObject *object);
 
     // 1 = normal speed,
     // 1 < x < 16 = slowdown by some factor
index 54b7e73..5249329 100644 (file)
@@ -135,14 +135,19 @@ void QDeclarativeObserverService::messageReceived(const QByteArray &message)
         QString filename;
         QStringList imports;
         ds >> qml >> parentId >> imports >> filename;
-        emit objectCreationRequested(qml, objectForId(parentId), imports, filename);
+        int order = -1;
+        if (!ds.atEnd()) {
+            ds >> order;
+        }
+        emit objectCreationRequested(qml, objectForId(parentId), imports, filename, order);
         break;
     }
     case ObserverProtocol::DestroyObject: {
         int debugId;
         ds >> debugId;
-        if (QObject* obj = objectForId(debugId))
-            obj->deleteLater();
+        if (QObject* obj = objectForId(debugId)) {
+            emit objectDeletionRequested(obj);
+        }
         break;
     }
     case ObserverProtocol::MoveObject: {
index 00ba6b2..4a13f26 100644 (file)
@@ -159,8 +159,10 @@ QDeclarativeViewObserver::QDeclarativeViewObserver(QDeclarativeView *view, QObje
     connect(data->debugService, SIGNAL(selectToolRequested()), data.data(), SLOT(_q_changeToSingleSelectTool()));
     connect(data->debugService, SIGNAL(zoomToolRequested()), data.data(), SLOT(_q_changeToZoomTool()));
     connect(data->debugService,
-            SIGNAL(objectCreationRequested(QString,QObject*,QStringList,QString)),
-            data.data(), SLOT(_q_createQmlObject(QString,QObject*,QStringList,QString)));
+            SIGNAL(objectCreationRequested(QString,QObject*,QStringList,QString,int)),
+            data.data(), SLOT(_q_createQmlObject(QString,QObject*,QStringList,QString,int)));
+    connect(data->debugService,
+            SIGNAL(objectDeletionRequested(QObject *)), data.data(), SLOT(_q_deleteQmlObject(QObject *)));
     connect(data->debugService,
             SIGNAL(objectReparentRequested(QObject *, QObject *)),
             data.data(), SLOT(_q_reparentQmlObject(QObject *, QObject *)));
@@ -400,9 +402,56 @@ bool QDeclarativeViewObserver::keyReleaseEvent(QKeyEvent *event)
     return true;
 }
 
+bool insertObjectInListProperty(QDeclarativeListReference &fromList, int position, QObject *object)
+{
+    QList<QObject *> tmpList;
+    int i;
+
+    if (!(fromList.canCount() && fromList.canAt() && fromList.canAppend() && fromList.canClear()))
+        return false;
+
+    if (position == fromList.count()) {
+        fromList.append(object);
+        return true;
+    }
+
+    for (i=0; i<fromList.count(); ++i)
+        tmpList << fromList.at(i);
+
+    fromList.clear();
+    for (i=0; i<position; ++i)
+        fromList.append(tmpList.at(i));
+
+    fromList.append(object);
+    for (; i<tmpList.count(); ++i)
+        fromList.append(tmpList.at(i));
+
+    return true;
+}
+
+bool removeObjectFromListProperty(QDeclarativeListReference &fromList, QObject *object)
+{
+    QList<QObject *> tmpList;
+    int i;
+
+    if (!(fromList.canCount() && fromList.canAt() && fromList.canAppend() && fromList.canClear()))
+        return false;
+
+    for (i=0; i<fromList.count(); ++i)
+        if (object != fromList.at(i))
+            tmpList << fromList.at(i);
+
+    fromList.clear();
+
+    foreach (QObject *item, tmpList)
+        fromList.append(item);
+
+    return true;
+}
+
 void QDeclarativeViewObserverPrivate::_q_createQmlObject(const QString &qml, QObject *parent,
                                                          const QStringList &importList,
-                                                         const QString &filename)
+                                                         const QString &filename, int order)
 {
     if (!parent)
         return;
@@ -458,15 +507,22 @@ void QDeclarativeViewObserverPrivate::_q_createQmlObject(const QString &qml, QOb
             if (parent->inherits("QDeclarativeAnimationGroup") &&
                     newObject->inherits("QDeclarativeAbstractAnimation")) {
                 QDeclarativeListReference animationsList(parent, "animations");
-                animationsList.append(newObject);
+                if (order==-1) {
+                    animationsList.append(newObject);
+                } else {
+                    if (!insertObjectInListProperty(animationsList, order, newObject)) {
+                        animationsList.append(newObject);
+                    }
+                }
                 break;
             }
 
             // add transition
             if (parentItem && newObject->inherits("QDeclarativeTransition")) {
                 QDeclarativeListReference transitionsList(parentItem,"transitions");
-                if (transitionsList.count() == 1 && transitionsList.at(0) == 0)
+                if (transitionsList.count() == 1 && transitionsList.at(0) == 0) {
                     transitionsList.clear();
+                }
                 transitionsList.append(newObject);
                 break;
             }
@@ -487,6 +543,26 @@ void QDeclarativeViewObserverPrivate::_q_reparentQmlObject(QObject *object, QObj
         item->setParentItem(newParentItem);
 }
 
+void QDeclarativeViewObserverPrivate::_q_deleteQmlObject(QObject *object)
+{
+    // special cases for transitions/animations
+    if (object->inherits("QDeclarativeAbstractAnimation")) {
+        if (object->parent()) {
+            QDeclarativeListReference animationsList(object->parent(), "animations");
+            if (removeObjectFromListProperty(animationsList, object))
+                object->deleteLater();
+            return;
+        }
+    }
+
+    if (object->inherits("QDeclarativeTransition")) {
+        QDeclarativeListReference transitionsList(object->parent(), "transitions");
+        if (removeObjectFromListProperty(transitionsList, object))
+            object->deleteLater();
+        return;
+    }
+}
+
 void QDeclarativeViewObserverPrivate::_q_clearComponentCache()
 {
     view->engine()->clearComponentCache();
index aac988f..4b447c5 100644 (file)
@@ -130,8 +130,9 @@ public slots:
     void _q_onCurrentObjectsChanged(QList<QObject*> objects);
     void _q_applyChangesFromClient();
     void _q_createQmlObject(const QString &qml, QObject *parent,
-                            const QStringList &imports, const QString &filename = QString());
+                            const QStringList &imports, const QString &filename = QString(), int order = 0);
     void _q_reparentQmlObject(QObject *, QObject *);
+    void _q_deleteQmlObject(QObject *);
 
     void _q_changeToSingleSelectTool();
     void _q_changeToMarqueeSelectTool();
index 75caaab..dbc0bfc 100644 (file)
@@ -391,14 +391,20 @@ void Delta::insert(UiObjectMember *member, UiObjectMember *parentMember, const Q
                          + QLatin1Char(':') + QString::number(startLine-importList.count());
         foreach(DebugId debugId, debugReferences) {
             if (debugId != -1) {
-                createObject(qmlText, debugId, importList, filename);
+                int order = 0;
+                // skip children which are not objects
+                foreach (const UiObjectMember *child, children(parentMember)) {
+                    if (child == member) break;
+                    if (child->kind == AST::Node::Kind_UiObjectDefinition)
+                        order++;
+                }
+                createObject(qmlText, debugId, importList, filename, order);
             }
         }
         newObjects += member;
     }
 }
 
-
 void Delta::update(UiObjectMember* oldObject, const QmlJS::Document::Ptr& oldDoc,
                    UiObjectMember* newObject, const QmlJS::Document::Ptr& newDoc,
                    const QList<DebugId>& debugReferences)
@@ -567,7 +573,7 @@ Document::Ptr Delta::previousDocument() const
     return m_previousDoc;
 }
 
-void Delta::createObject(const QString &, DebugId, const QStringList &, const QString&)
+void Delta::createObject(const QString &, DebugId, const QStringList &, const QString&, int)
 {}
 void Delta::removeObject(int)
 {}
index f69d892..fdde3bb 100644 (file)
@@ -74,7 +74,7 @@ protected:
     virtual void removeObject(int debugId);
     virtual void reparentObject(int debugId, int newParent);
     virtual void createObject(const QString &qmlText, DebugId ref,
-                              const QStringList &importList, const QString &filename);
+                              const QStringList &importList, const QString &filename, int order = 0);
     virtual void notifyUnsyncronizableElementChange(AST::UiObjectMember *parent);
 
 private:
index a3291d5..83efb1d 100644 (file)
@@ -581,10 +581,10 @@ void ClientProxy::showAppOnTop(bool showOnTop)
 }
 
 void ClientProxy::createQmlObject(const QString &qmlText, int parentDebugId,
-                                  const QStringList &imports, const QString &filename)
+                                  const QStringList &imports, const QString &filename, int order)
 {
     if (isConnected())
-        m_observerClient->createQmlObject(qmlText, parentDebugId, imports, filename);
+        m_observerClient->createQmlObject(qmlText, parentDebugId, imports, filename, order);
 }
 
 void ClientProxy::destroyQmlObject(int debugId)
index ce37887..b5d79bd 100644 (file)
@@ -130,7 +130,7 @@ public slots:
     void changeToSelectMarqueeTool();
     void showAppOnTop(bool showOnTop);
     void createQmlObject(const QString &qmlText, int parentDebugId,
-                         const QStringList &imports, const QString &filename = QString());
+                         const QStringList &imports, const QString &filename = QString(), int order = 0);
     void destroyQmlObject(int debugId);
     void reparentQmlObject(int debugId, int newParent);
     void setContextPathIndex(int contextIndex);
index 3a7e1e0..687edf7 100644 (file)
@@ -480,11 +480,11 @@ protected:
     }
 
     virtual void createObject(const QString& qmlText, DebugId ref,
-                         const QStringList& importList, const QString& filename)
+                         const QStringList& importList, const QString& filename, int order)
     {
         appliedChangesToViewer = true;
         referenceRefreshRequired = true;
-        m_clientProxy->createQmlObject(qmlText, ref, importList, filename);
+        m_clientProxy->createQmlObject(qmlText, ref, importList, filename, order);
     }
 
     virtual void reparentObject(int debugId, int newParent)
index 8f740d8..4c25c0b 100644 (file)
@@ -414,7 +414,7 @@ void QmlJSObserverClient::showAppOnTop(bool showOnTop)
 }
 
 void QmlJSObserverClient::createQmlObject(const QString &qmlText, int parentDebugId,
-                                             const QStringList &imports, const QString &filename)
+                                             const QStringList &imports, const QString &filename, int order)
 {
     if (!m_connection || !m_connection->isConnected())
         return;
@@ -427,7 +427,8 @@ void QmlJSObserverClient::createQmlObject(const QString &qmlText, int parentDebu
        << qmlText
        << parentDebugId
        << imports
-       << filename;
+       << filename
+       << order;
 
     log(LogSend, cmd, QString("%1 %2 [%3] %4").arg(qmlText, QString::number(parentDebugId),
                                                    imports.join(","), filename));
index 4ed64aa..7772de3 100644 (file)
@@ -58,7 +58,7 @@ public:
     void showAppOnTop(bool showOnTop);
 
     void createQmlObject(const QString &qmlText, int parentDebugId,
-                         const QStringList &imports, const QString &filename);
+                         const QStringList &imports, const QString &filename, int order);
     void destroyQmlObject(int debugId);
     void reparentQmlObject(int debugId, int newParent);