From a17f96d42a9dda46d6cab3f594cdff36a8335cec Mon Sep 17 00:00:00 2001
From: Thomas Hartmann <Thomas.Hartmann@digia.com>
Date: Wed, 6 Feb 2013 13:12:55 +0100
Subject: [PATCH] QmlDesigner: Fix crumble bar

Change-Id: I8eefba1b5b9839b28be5dbae379366714f40ed27
Reviewed-by: Thomas Hartmann <Thomas.Hartmann@digia.com>
---
 .../formeditor/formeditorcrumblebar.cpp       | 37 ++++++++++++++-----
 .../integration/componentaction.cpp           | 21 +++++------
 .../components/integration/componentaction.h  |  1 +
 .../components/integration/componentview.cpp  |  9 -----
 .../components/integration/componentview.h    |  1 -
 .../components/integration/designdocument.cpp | 24 +++++-------
 .../components/integration/designdocument.h   |  3 +-
 src/plugins/qmldesigner/qmldesignerplugin.cpp |  2 +-
 8 files changed, 50 insertions(+), 48 deletions(-)

diff --git a/src/plugins/qmldesigner/components/formeditor/formeditorcrumblebar.cpp b/src/plugins/qmldesigner/components/formeditor/formeditorcrumblebar.cpp
index 113f9a2697f..a90b169458b 100644
--- a/src/plugins/qmldesigner/components/formeditor/formeditorcrumblebar.cpp
+++ b/src/plugins/qmldesigner/components/formeditor/formeditorcrumblebar.cpp
@@ -49,8 +49,15 @@ FormEditorCrumbleBar::FormEditorCrumbleBar(QObject *parent) :
 
 void FormEditorCrumbleBar::pushFile(const QString &fileName)
 {
-    if (m_isInternalCalled == false)
+    if (m_isInternalCalled == false) {
         crumblePath()->clear();
+    } else {
+        CrumbleBarInfo lastElementCrumbleBarInfo = crumblePath()->dataForLastIndex().value<CrumbleBarInfo>();
+
+        if (!lastElementCrumbleBarInfo.componentId.isEmpty()
+                && lastElementCrumbleBarInfo.fileName == fileName)
+            crumblePath()->popElement();
+    }
 
     CrumbleBarInfo crumbleBarInfo;
     crumbleBarInfo.fileName = fileName;
@@ -77,6 +84,8 @@ void FormEditorCrumbleBar::pushInFileComponent(const QString &componentId)
         crumblePath()->popElement();
 
     crumblePath()->pushElement(componentId, QVariant::fromValue(crumbleBarInfo));
+
+    m_isInternalCalled = false;
 }
 
 void FormEditorCrumbleBar::nextFileIsCalledInternally()
@@ -91,23 +100,33 @@ Utils::CrumblePath *FormEditorCrumbleBar::crumblePath()
 
 void FormEditorCrumbleBar::onCrumblePathElementClicked(const QVariant &data)
 {
-    CrumbleBarInfo crumbleBarInfo = data.value<CrumbleBarInfo>();
+    CrumbleBarInfo clickedCrumbleBarInfo = data.value<CrumbleBarInfo>();
 
-    if (crumbleBarInfo == crumblePath()->dataForLastIndex().value<CrumbleBarInfo>())
+    if (clickedCrumbleBarInfo ==  crumblePath()->dataForLastIndex().value<CrumbleBarInfo>())
         return;
 
-    while (crumbleBarInfo != crumblePath()->dataForLastIndex().value<CrumbleBarInfo>())
+    while (clickedCrumbleBarInfo != crumblePath()->dataForLastIndex().value<CrumbleBarInfo>())
         crumblePath()->popElement();
 
-    if (!crumbleBarInfo.componentId.isEmpty())
+    if (!crumblePath()->dataForLastIndex().value<CrumbleBarInfo>().componentId.isEmpty())
         crumblePath()->popElement();
 
-    crumblePath()->popElement();
 
     m_isInternalCalled = true;
-    Core::EditorManager::openEditor(crumbleBarInfo.fileName);
-    if (!crumbleBarInfo.componentId.isEmpty())
-        currentDesignDocument()->changeToSubComponent(currentDesignDocument()->rewriterView()->modelNodeForId(crumbleBarInfo.componentId));
+    if (clickedCrumbleBarInfo.componentId.isEmpty()
+            && clickedCrumbleBarInfo.fileName == currentDesignDocument()->fileName()) {
+        nextFileIsCalledInternally();
+        currentDesignDocument()->changeToDocumentModel();
+    } else {
+        crumblePath()->popElement();
+        nextFileIsCalledInternally();
+        Core::EditorManager::openEditor(clickedCrumbleBarInfo.fileName);
+        if (!clickedCrumbleBarInfo.componentId.isEmpty()) {
+            currentDesignDocument()->changeToSubComponent(
+                        currentDesignDocument()->rewriterView()->modelNodeForId(clickedCrumbleBarInfo.componentId));
+            pushInFileComponent(clickedCrumbleBarInfo.componentId);
+        }
+    }
 }
 
 bool operator ==(const CrumbleBarInfo &first, const CrumbleBarInfo &second)
diff --git a/src/plugins/qmldesigner/components/integration/componentaction.cpp b/src/plugins/qmldesigner/components/integration/componentaction.cpp
index 6ba366532e1..ec5b72e8ed8 100644
--- a/src/plugins/qmldesigner/components/integration/componentaction.cpp
+++ b/src/plugins/qmldesigner/components/integration/componentaction.cpp
@@ -39,13 +39,16 @@ namespace QmlDesigner {
 
 ComponentAction::ComponentAction(ComponentView  *componentView)
   :  QWidgetAction(componentView),
-     m_componentView(componentView)
+     m_componentView(componentView),
+     dontEmitCurrentComponentChanged(false)
 {
 }
 
 void ComponentAction::setCurrentIndex(int index)
 {
+    dontEmitCurrentComponentChanged = true;
     emit currentIndexChanged(index);
+    dontEmitCurrentComponentChanged = false;
 }
 
 QWidget  *ComponentAction::createWidget(QWidget *parent)
@@ -54,25 +57,19 @@ QWidget  *ComponentAction::createWidget(QWidget *parent)
     comboBox->setMinimumWidth(120);
     comboBox->setToolTip(tr("Edit sub components defined in this file"));
     comboBox->setModel(m_componentView->standardItemModel());
-    connect(comboBox, SIGNAL(currentIndexChanged(int)), SLOT(emitCurrentComponentChanged(int)));
+    comboBox->setCurrentIndex(-1);
+    connect(comboBox, SIGNAL(activated(int)), SLOT(emitCurrentComponentChanged(int)));
     connect(this, SIGNAL(currentIndexChanged(int)), comboBox, SLOT(setCurrentIndex(int)));
 
     return comboBox;
 }
 
-static const QString fileNameOfCurrentDocument()
-{
-    return QmlDesignerPlugin::instance()->documentManager().currentDesignDocument()->textEditor()->document()->fileName();
-}
-
 void ComponentAction::emitCurrentComponentChanged(int index)
 {
-    ModelNode componentNode = m_componentView->modelNode(index);
+    if (dontEmitCurrentComponentChanged)
+        return;
 
-    if ( index > 0)
-        QmlDesignerPlugin::instance()->viewManager().pushInFileComponentOnCrambleBar(componentNode.id());
-    else
-         QmlDesignerPlugin::instance()->viewManager().pushFileOnCrambleBar(fileNameOfCurrentDocument());
+    ModelNode componentNode = m_componentView->modelNode(index);
 
     emit currentComponentChanged(componentNode);
 }
diff --git a/src/plugins/qmldesigner/components/integration/componentaction.h b/src/plugins/qmldesigner/components/integration/componentaction.h
index 45df922140e..d421d2f5dae 100644
--- a/src/plugins/qmldesigner/components/integration/componentaction.h
+++ b/src/plugins/qmldesigner/components/integration/componentaction.h
@@ -63,6 +63,7 @@ public slots:
 
 private:
     QWeakPointer<ComponentView> m_componentView;
+    bool dontEmitCurrentComponentChanged;
 };
 
 } // namespace QmlDesigner
diff --git a/src/plugins/qmldesigner/components/integration/componentview.cpp b/src/plugins/qmldesigner/components/integration/componentview.cpp
index 888a13317fd..583c1910255 100644
--- a/src/plugins/qmldesigner/components/integration/componentview.cpp
+++ b/src/plugins/qmldesigner/components/integration/componentview.cpp
@@ -78,14 +78,6 @@ QWidget *ComponentView::widget()
     return 0;
 }
 
-void ComponentView::appendWholeDocumentAsComponent()
-{
-    QStandardItem *item = new QStandardItem(tr("whole document"));
-    item->setData(QVariant::fromValue(rootModelNode()), ModelNodeRole);
-    item->setEditable(false);
-    m_standardItemModel->appendRow(item);
-}
-
 void ComponentView::removeSingleNodeFromList(const ModelNode &node)
 {
     for (int row = 0; row < m_standardItemModel->rowCount(); row++) {
@@ -114,7 +106,6 @@ void ComponentView::modelAttached(Model *model)
 
     AbstractView::modelAttached(model);
 
-    appendWholeDocumentAsComponent();
     searchForComponentAndAddToList(rootModelNode());
 
     m_componentAction->blockSignals(block);
diff --git a/src/plugins/qmldesigner/components/integration/componentview.h b/src/plugins/qmldesigner/components/integration/componentview.h
index 2e19948ef3e..2d8c2dcba65 100644
--- a/src/plugins/qmldesigner/components/integration/componentview.h
+++ b/src/plugins/qmldesigner/components/integration/componentview.h
@@ -116,7 +116,6 @@ private: //functions
     void updateModel();
     void searchForComponentAndAddToList(const ModelNode &node);
     void searchForComponentAndRemoveFromList(const ModelNode &node);
-    void appendWholeDocumentAsComponent();
     void removeSingleNodeFromList(const ModelNode &node);
     int indexForNode(const ModelNode &node);
 
diff --git a/src/plugins/qmldesigner/components/integration/designdocument.cpp b/src/plugins/qmldesigner/components/integration/designdocument.cpp
index c75988a2ed5..2cfa231be5d 100644
--- a/src/plugins/qmldesigner/components/integration/designdocument.cpp
+++ b/src/plugins/qmldesigner/components/integration/designdocument.cpp
@@ -236,7 +236,7 @@ QList<RewriterView::Error> DesignDocument::qmlSyntaxErrors() const
 
 bool DesignDocument::hasQmlSyntaxErrors() const
 {
-    return !m_currentModel->rewriterView()->errors().isEmpty();
+    return m_currentModel->rewriterView() && !m_currentModel->rewriterView()->errors().isEmpty();
 }
 
 QString DesignDocument::displayName() const
@@ -314,26 +314,18 @@ void DesignDocument::loadDocument(QPlainTextEdit *edit)
     m_documentLoaded = true;
 }
 
+static const QString fileNameOfCurrentDocument()
+{
+    return QmlDesignerPlugin::instance()->documentManager().currentDesignDocument()->textEditor()->document()->fileName();
+}
+
 void DesignDocument::changeCurrentModelTo(const ModelNode &node)
 {
     if (QmlDesignerPlugin::instance()->currentDesignDocument() != this)
         return;
 
-    if (rootModelNode() == node) {
-        changeToDocumentModel();
-    } else {
-        changeToSubComponent(node);
-    }
-
-
 
-//    s_clearCrumblePath = false;
-//    while (m_formEditorView->crumblePath()->dataForLastIndex().value<CrumbleBarInfo>().modelNode.isValid() &&
-//        !m_formEditorView->crumblePath()->dataForLastIndex().value<CrumbleBarInfo>().modelNode.isRootNode())
-//        m_formEditorView->crumblePath()->popElement();
-//    if (node.isRootNode() && m_formEditorView->crumblePath()->dataForLastIndex().isValid())
-//        m_formEditorView->crumblePath()->popElement();
-//    s_clearCrumblePath = true;
+    changeToSubComponent(node);
 }
 
 void DesignDocument::changeToSubComponent(const ModelNode &componentNode)
@@ -354,6 +346,8 @@ void DesignDocument::changeToSubComponent(const ModelNode &componentNode)
 
         activateCurrentModel(m_inFileComponentTextModifier.data());
     }
+    if (!componentNode.id().isEmpty())
+        QmlDesignerPlugin::instance()->viewManager().pushInFileComponentOnCrambleBar(componentNode.id());
 }
 
 void DesignDocument::changeToExternalSubComponent(const QString &fileName)
diff --git a/src/plugins/qmldesigner/components/integration/designdocument.h b/src/plugins/qmldesigner/components/integration/designdocument.h
index 5296c538a8d..119706830b5 100644
--- a/src/plugins/qmldesigner/components/integration/designdocument.h
+++ b/src/plugins/qmldesigner/components/integration/designdocument.h
@@ -101,6 +101,8 @@ public:
 
     void goIntoComponent();
 
+    void changeToDocumentModel();
+
 signals:
     void displayNameChanged(const QString &newFileName);
     void dirtyStateChanged(bool newState);
@@ -127,7 +129,6 @@ private slots:
     void updateFileName(const QString &oldFileName, const QString &newFileName);
 
 private: // functions
-    void changeToDocumentModel();
     void changeToInFileComponentModel();
 
     QWidget *centralWidget() const;
diff --git a/src/plugins/qmldesigner/qmldesignerplugin.cpp b/src/plugins/qmldesigner/qmldesignerplugin.cpp
index 2018786bd38..43952f59e33 100644
--- a/src/plugins/qmldesigner/qmldesignerplugin.cpp
+++ b/src/plugins/qmldesigner/qmldesignerplugin.cpp
@@ -242,8 +242,8 @@ void QmlDesignerPlugin::changeEditor()
     m_shortCutManager.connectUndoActions(currentDesignDocument());
 
     if (m_documentManager.hasCurrentDesignDocument()) {
-        m_viewManager.pushFileOnCrambleBar(m_documentManager.currentDesignDocument()->fileName());
         activateAutoSynchronization();
+        m_viewManager.pushFileOnCrambleBar(m_documentManager.currentDesignDocument()->fileName());
     }
 
     m_shortCutManager.updateUndoActions(currentDesignDocument());
-- 
GitLab