From 503f720f85b7b0c1650d8590bbdda8d049ef8c96 Mon Sep 17 00:00:00 2001
From: Kai Koehne <kai.koehne@nokia.com>
Date: Mon, 12 Jul 2010 16:40:15 +0200
Subject: [PATCH] Outline: Make recalculation of current model index more
 robust

---
 src/plugins/cppeditor/cppeditor.cpp      | 21 ++++++++++++---------
 src/plugins/cppeditor/cppeditor.h        |  4 ++--
 src/plugins/cppeditor/cppoutline.cpp     |  1 -
 src/plugins/qmljseditor/qmljseditor.cpp  | 15 ++++++++-------
 src/plugins/qmljseditor/qmljseditor.h    |  2 +-
 src/plugins/qmljseditor/qmljsoutline.cpp |  1 -
 6 files changed, 23 insertions(+), 21 deletions(-)

diff --git a/src/plugins/cppeditor/cppeditor.cpp b/src/plugins/cppeditor/cppeditor.cpp
index cb07da00ce9..b68a55a3318 100644
--- a/src/plugins/cppeditor/cppeditor.cpp
+++ b/src/plugins/cppeditor/cppeditor.cpp
@@ -832,6 +832,7 @@ void CPPEditor::onDocumentUpdated(Document::Ptr doc)
     }
 
     m_overviewModel->rebuild(doc);
+
     OverviewTreeView *treeView = static_cast<OverviewTreeView *>(m_methodCombo->view());
     treeView->sync();
     updateMethodBoxIndexNow();
@@ -1092,14 +1093,9 @@ void CPPEditor::updateMethodBoxIndexNow()
 
     m_updateMethodBoxTimer->stop();
 
-    int line = 0, column = 0;
-    convertPosition(position(), &line, &column);
-
-    QModelIndex overviewModelIndex = indexForPosition(line, column);
-    emit overviewModelIndexChanged(overviewModelIndex);
-
+    m_overviewModelIndex = QModelIndex(); //invalidate
     // ComboBox only let's you select top level indexes!
-    QModelIndex comboIndex = overviewModelIndex;
+    QModelIndex comboIndex = overviewModelIndex();
     while (comboIndex.parent().isValid())
         comboIndex = comboIndex.parent();
 
@@ -1444,8 +1440,15 @@ CPlusPlus::OverviewModel *CPPEditor::overviewModel() const
     return m_overviewModel;
 }
 
-QModelIndex CPPEditor::overviewModelIndex() const
+QModelIndex CPPEditor::overviewModelIndex()
 {
+    if (!m_overviewModelIndex.isValid()) {
+        int line = 0, column = 0;
+        convertPosition(position(), &line, &column);
+        m_overviewModelIndex = indexForPosition(line, column);
+        emit overviewModelIndexChanged(m_overviewModelIndex);
+    }
+
     return m_overviewModelIndex;
 }
 
@@ -2194,7 +2197,7 @@ SemanticInfo SemanticHighlighter::semanticInfo(const Source &source)
     return semanticInfo;
 }
 
-QModelIndex CPPEditor::indexForPosition(int line, int column, const QModelIndex &rootIndex)
+QModelIndex CPPEditor::indexForPosition(int line, int column, const QModelIndex &rootIndex) const
 {
     QModelIndex lastIndex = rootIndex;
 
diff --git a/src/plugins/cppeditor/cppeditor.h b/src/plugins/cppeditor/cppeditor.h
index 78730998b93..0ff2801cbc9 100644
--- a/src/plugins/cppeditor/cppeditor.h
+++ b/src/plugins/cppeditor/cppeditor.h
@@ -168,7 +168,7 @@ public:
     SemanticInfo semanticInfo() const;
 
     CPlusPlus::OverviewModel *overviewModel() const;
-    QModelIndex overviewModelIndex() const;
+    QModelIndex overviewModelIndex();
 
     virtual void paste(); // reimplemented from BaseTextEditor
     virtual void cut(); // reimplemented from BaseTextEditor
@@ -265,7 +265,7 @@ private:
     bool openLink(const Link &link) { return openCppEditorAt(link); }
     bool openCppEditorAt(const Link &);
 
-    QModelIndex indexForPosition(int line, int column, const QModelIndex &rootIndex = QModelIndex());
+    QModelIndex indexForPosition(int line, int column, const QModelIndex &rootIndex = QModelIndex()) const;
 
     static Link linkToSymbol(CPlusPlus::Symbol *symbol);
 
diff --git a/src/plugins/cppeditor/cppoutline.cpp b/src/plugins/cppeditor/cppoutline.cpp
index e0202231cd0..afb9f8e5e1e 100644
--- a/src/plugins/cppeditor/cppoutline.cpp
+++ b/src/plugins/cppeditor/cppoutline.cpp
@@ -90,7 +90,6 @@ void CppOutlineWidget::setCursorSynchronization(bool syncWithCursor)
 void CppOutlineWidget::modelUpdated()
 {
     m_treeView->expandAll();
-    updateSelectionInTree(m_editor->overviewModelIndex());
 }
 
 void CppOutlineWidget::updateSelectionInTree(const QModelIndex &index)
diff --git a/src/plugins/qmljseditor/qmljseditor.cpp b/src/plugins/qmljseditor/qmljseditor.cpp
index d12ffd30601..47c7f6c19c4 100644
--- a/src/plugins/qmljseditor/qmljseditor.cpp
+++ b/src/plugins/qmljseditor/qmljseditor.cpp
@@ -697,8 +697,12 @@ QmlOutlineModel *QmlJSTextEditor::outlineModel() const
     return m_outlineModel;
 }
 
-QModelIndex QmlJSTextEditor::outlineModelIndex() const
+QModelIndex QmlJSTextEditor::outlineModelIndex()
 {
+    if (!m_outlineModelIndex.isValid()) {
+        m_outlineModelIndex = indexForPosition(position());
+        emit outlineModelIndexChanged(m_outlineModelIndex);
+    }
     return m_outlineModelIndex;
 }
 
@@ -822,10 +826,8 @@ void QmlJSTextEditor::jumpToMethod(int /*index*/)
 
 void QmlJSTextEditor::updateMethodBoxIndex()
 {
-    m_outlineModelIndex = indexForPosition(position());
-    emit outlineModelIndexChanged(m_outlineModelIndex);
-
-    QModelIndex comboIndex = m_outlineModelIndex;
+    m_outlineModelIndex = QModelIndex(); // invalidate
+    QModelIndex comboIndex = outlineModelIndex();
 
     if (comboIndex.isValid()) {
         bool blocked = m_methodCombo->blockSignals(true);
@@ -1364,8 +1366,6 @@ void QmlJSTextEditor::updateSemanticInfo(const SemanticInfo &semanticInfo)
 
         QTreeView *treeView = static_cast<QTreeView*>(m_methodCombo->view());
         treeView->expandAll();
-        // ComboBox only let's you select top level indexes for a QAbstractItemModel!
-        // therefore we've to fake a treeview by listview + indentation
 
         if (m_contextPane) {
             Node *newNode = m_semanticInfo.declaringMember(position());
@@ -1542,3 +1542,4 @@ void SemanticHighlighter::setModelManager(QmlJS::ModelManagerInterface *modelMan
 {
     m_modelManager = modelManager;
 }
+
diff --git a/src/plugins/qmljseditor/qmljseditor.h b/src/plugins/qmljseditor/qmljseditor.h
index 49910dbf545..9f29b6266dd 100644
--- a/src/plugins/qmljseditor/qmljseditor.h
+++ b/src/plugins/qmljseditor/qmljseditor.h
@@ -217,7 +217,7 @@ public:
     bool isOutdated() const;
 
     QmlOutlineModel *outlineModel() const;
-    QModelIndex outlineModelIndex() const;
+    QModelIndex outlineModelIndex();
 
 public slots:
     void followSymbolUnderCursor();
diff --git a/src/plugins/qmljseditor/qmljsoutline.cpp b/src/plugins/qmljseditor/qmljsoutline.cpp
index a283ed8d16a..8c8d83357d3 100644
--- a/src/plugins/qmljseditor/qmljsoutline.cpp
+++ b/src/plugins/qmljseditor/qmljsoutline.cpp
@@ -67,7 +67,6 @@ void QmlJSOutlineWidget::setCursorSynchronization(bool syncWithCursor)
 void QmlJSOutlineWidget::modelUpdated()
 {
     m_treeView->expandAll();
-    updateSelectionInTree(m_editor.data()->outlineModelIndex());
 }
 
 void QmlJSOutlineWidget::updateSelectionInTree(const QModelIndex &index)
-- 
GitLab