From a25d66a9448fadf0121532aad88a926c23d6e572 Mon Sep 17 00:00:00 2001
From: Eike Ziller <eike.ziller@digia.com>
Date: Thu, 25 Apr 2013 20:05:18 +0200
Subject: [PATCH] EditorManager: Get rid of weird logic when finding next view

Removes another algorithm that was starting from the root and took the
whole tree into account.
Instead, make findNextView a method of EditorView, and avoid any
explicit usage of a single root splitter.

Change-Id: I343030521472741a8dfd7134ed16d9beeb10d10a
Reviewed-by: David Schulz <david.schulz@digia.com>
---
 .../editormanager/editormanager.cpp           |  6 +-
 .../coreplugin/editormanager/editorview.cpp   | 55 ++++++++-----------
 .../coreplugin/editormanager/editorview.h     |  4 +-
 3 files changed, 26 insertions(+), 39 deletions(-)

diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp
index bbde9d6414f..d837c38151f 100644
--- a/src/plugins/coreplugin/editormanager/editormanager.cpp
+++ b/src/plugins/coreplugin/editormanager/editormanager.cpp
@@ -1871,7 +1871,7 @@ QList<IEditor*> EditorManager::visibleEditors() const
             do {
                 if (view->currentEditor())
                     editors.append(view->currentEditor());
-                view = d->m_splitter->findNextView(view);
+                view = view->findNextView();
             } while (view && view != firstView);
         }
     } else {
@@ -2224,9 +2224,7 @@ void EditorManager::gotoOtherSplit()
         splitSideBySide();
 
     EditorView *view = currentEditorView();
-    view = d->m_splitter->findNextView(view);
-    if (!view)
-        view = d->m_splitter->findFirstView();
+    view = view->findNextView();
     if (view) {
         if (IEditor *editor = view->currentEditor()) {
             setCurrentEditor(editor, true);
diff --git a/src/plugins/coreplugin/editormanager/editorview.cpp b/src/plugins/coreplugin/editormanager/editorview.cpp
index bbaff3a5ab9..1645fb4fc8b 100644
--- a/src/plugins/coreplugin/editormanager/editorview.cpp
+++ b/src/plugins/coreplugin/editormanager/editorview.cpp
@@ -124,6 +124,29 @@ SplitterOrView *EditorView::parentSplitterOrView() const
     return m_parentSplitterOrView;
 }
 
+EditorView *EditorView::findNextView()
+{
+    SplitterOrView *current = parentSplitterOrView();
+    QTC_ASSERT(current, return this);
+    SplitterOrView *parent = current->findParentSplitter();
+    while (parent) {
+        QSplitter *splitter = parent->splitter();
+        QTC_ASSERT(splitter, return this);
+        QTC_ASSERT(splitter->count() == 2, return this);
+        // is current the first child? then the next view is the first one in current's sibling
+        if (splitter->widget(0) == current) {
+            SplitterOrView *second = qobject_cast<SplitterOrView *>(splitter->widget(1));
+            QTC_ASSERT(second, return this);
+            return second->findFirstView();
+        }
+        // otherwise go up the hierarchy
+        current = parent;
+        parent = current->findParentSplitter();
+    }
+    // current has no parent, so just take the very first view
+    return current->findFirstView();
+}
+
 void EditorView::closeView()
 {
     EditorManager *em = ICore::editorManager();
@@ -527,38 +550,6 @@ SplitterOrView *SplitterOrView::findParentSplitter() const
     return 0;
 }
 
-EditorView *SplitterOrView::findNextView(EditorView *view)
-{
-    if (!view)
-        return 0;
-    bool found = false;
-    SplitterOrView *splitterOrView = findNextView_helper(view->parentSplitterOrView(), &found);
-    if (splitterOrView)
-        return splitterOrView->view();
-    return 0;
-}
-
-SplitterOrView *SplitterOrView::findNextView_helper(SplitterOrView *view, bool *found)
-{
-    if (*found && m_view)
-        return this;
-
-    if (this == view) {
-        *found = true;
-        return 0;
-    }
-
-    if (m_splitter) {
-        for (int i = 0; i < m_splitter->count(); ++i) {
-            if (SplitterOrView *splitterOrView = qobject_cast<SplitterOrView*>(m_splitter->widget(i))) {
-                if (SplitterOrView *result = splitterOrView->findNextView_helper(view, found))
-                    return result;
-            }
-        }
-    }
-    return 0;
-}
-
 QSize SplitterOrView::minimumSizeHint() const
 {
     if (m_splitter)
diff --git a/src/plugins/coreplugin/editormanager/editorview.h b/src/plugins/coreplugin/editormanager/editorview.h
index cb90be3482a..606b39e7c37 100644
--- a/src/plugins/coreplugin/editormanager/editorview.h
+++ b/src/plugins/coreplugin/editormanager/editorview.h
@@ -80,6 +80,7 @@ public:
     virtual ~EditorView();
 
     SplitterOrView *parentSplitterOrView() const;
+    EditorView *findNextView();
 
     int editorCount() const;
     void addEditor(IEditor *editor);
@@ -184,8 +185,6 @@ public:
     EditorView *findFirstView();
     SplitterOrView *findParentSplitter() const;
 
-    EditorView *findNextView(EditorView *view);
-
     QSize sizeHint() const { return minimumSizeHint(); }
     QSize minimumSizeHint() const;
 
@@ -193,7 +192,6 @@ public:
 
 private:
     void unsplitAll_helper();
-    SplitterOrView *findNextView_helper(SplitterOrView *view, bool *found);
     bool m_isRoot;
     QStackedLayout *m_layout;
     EditorView *m_view;
-- 
GitLab