From c1f643e387788f905f5f8b0466c70ccebb9bc58e Mon Sep 17 00:00:00 2001
From: Eike Ziller <eike.ziller@nokia.com>
Date: Mon, 5 Dec 2011 15:48:37 +0100
Subject: [PATCH] Fix search next/prev in search results.

Broke with ebb9e252779aff1ff874565c2b056f1c8319c2c8

Change-Id: Ifb5821d4712e780df70bc1ec798123dd7b63b108
Reviewed-by: Jarek Kobus <jaroslaw.kobus@nokia.com>
---
 src/plugins/find/searchresulttreemodel.cpp | 27 ++++++++++++++--------
 src/plugins/find/searchresulttreemodel.h   |  6 +++--
 src/plugins/find/searchresultwidget.cpp    |  9 +++++---
 3 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/src/plugins/find/searchresulttreemodel.cpp b/src/plugins/find/searchresulttreemodel.cpp
index 95336e8a563..dc7f3699562 100644
--- a/src/plugins/find/searchresulttreemodel.cpp
+++ b/src/plugins/find/searchresulttreemodel.cpp
@@ -492,6 +492,13 @@ QModelIndex SearchResultTreeModel::prevIndex(const QModelIndex &idx, bool *wrapp
     return current;
 }
 
+QModelIndex SearchResultTreeModel::followingIndex(const QModelIndex &idx, bool backward, bool includeGenerated, bool *wrapped)
+{
+    if (backward)
+        return prev(idx, includeGenerated, wrapped);
+    return next(idx, includeGenerated, wrapped);
+}
+
 QModelIndex SearchResultTreeModel::prev(const QModelIndex &idx, bool includeGenerated, bool *wrapped) const
 {
     QModelIndex value = idx;
@@ -502,7 +509,8 @@ QModelIndex SearchResultTreeModel::prev(const QModelIndex &idx, bool includeGene
 }
 
 QModelIndex SearchResultTreeModel::find(const QRegExp &expr, const QModelIndex &index,
-                                        QTextDocument::FindFlags flags, bool *wrapped)
+                                        QTextDocument::FindFlags flags,
+                                        bool startWithCurrentIndex, bool *wrapped)
 {
     QModelIndex resultIndex;
     QModelIndex currentIndex = index;
@@ -512,6 +520,8 @@ QModelIndex SearchResultTreeModel::find(const QRegExp &expr, const QModelIndex &
     bool anyWrapped = false;
     bool stepWrapped = false;
 
+    if (!startWithCurrentIndex)
+        currentIndex = followingIndex(currentIndex, backward, true, &stepWrapped);
     do {
         anyWrapped |= stepWrapped; // update wrapped state if we actually stepped to next/prev item
         if (currentIndex.isValid()) {
@@ -519,10 +529,7 @@ QModelIndex SearchResultTreeModel::find(const QRegExp &expr, const QModelIndex &
             if (expr.indexIn(text) != -1)
                 resultIndex = currentIndex;
         }
-        if (backward)
-            currentIndex = prev(currentIndex, true, &stepWrapped);
-        else
-            currentIndex = next(currentIndex, true, &stepWrapped);
+        currentIndex = followingIndex(currentIndex, backward, true, &stepWrapped);
     } while (!resultIndex.isValid() && currentIndex.isValid() && currentIndex != index);
     if (resultIndex.isValid() && wrapped)
         *wrapped = anyWrapped;
@@ -530,7 +537,8 @@ QModelIndex SearchResultTreeModel::find(const QRegExp &expr, const QModelIndex &
 }
 
 QModelIndex SearchResultTreeModel::find(const QString &term, const QModelIndex &index,
-                                        QTextDocument::FindFlags flags, bool *wrapped)
+                                        QTextDocument::FindFlags flags,
+                                        bool startWithCurrentIndex, bool *wrapped)
 {
     QModelIndex resultIndex;
     QModelIndex currentIndex = index;
@@ -541,6 +549,8 @@ QModelIndex SearchResultTreeModel::find(const QString &term, const QModelIndex &
     bool anyWrapped = false;
     bool stepWrapped = false;
 
+    if (!startWithCurrentIndex)
+        currentIndex = followingIndex(currentIndex, backward, true, &stepWrapped);
     do {
         anyWrapped |= stepWrapped; // update wrapped state if we actually stepped to next/prev item
         if (currentIndex.isValid()) {
@@ -549,10 +559,7 @@ QModelIndex SearchResultTreeModel::find(const QString &term, const QModelIndex &
             if (!doc.find(term, 0, flags).isNull())
                 resultIndex = currentIndex;
         }
-        if (backward)
-            currentIndex = prev(currentIndex, true, &stepWrapped);
-        else
-            currentIndex = next(currentIndex, true, &stepWrapped);
+        currentIndex = followingIndex(currentIndex, backward, true, &stepWrapped);
     } while (!resultIndex.isValid() && currentIndex.isValid() && currentIndex != index);
     if (resultIndex.isValid() && wrapped)
         *wrapped = anyWrapped;
diff --git a/src/plugins/find/searchresulttreemodel.h b/src/plugins/find/searchresulttreemodel.h
index 40c2d789a1c..e01a869785c 100644
--- a/src/plugins/find/searchresulttreemodel.h
+++ b/src/plugins/find/searchresulttreemodel.h
@@ -71,9 +71,9 @@ public:
     QList<QModelIndex> addResults(const QList<SearchResultItem> &items, SearchResult::AddMode mode);
 
     QModelIndex find(const QRegExp &expr, const QModelIndex &index,
-                     QTextDocument::FindFlags flags, bool *wrapped = 0);
+                     QTextDocument::FindFlags flags, bool startWithCurrentIndex, bool *wrapped = 0);
     QModelIndex find(const QString &term, const QModelIndex &index,
-                     QTextDocument::FindFlags flags, bool *wrapped = 0);
+                     QTextDocument::FindFlags flags, bool startWithCurrentIndex, bool *wrapped = 0);
 
 signals:
     void jumpToSearchResult(const QString &fileName, int lineNumber,
@@ -90,6 +90,8 @@ private:
     bool setCheckState(const QModelIndex &idx, Qt::CheckState checkState, bool firstCall = true);
     QModelIndex nextIndex(const QModelIndex &idx, bool *wrapped = 0) const;
     QModelIndex prevIndex(const QModelIndex &idx, bool *wrapped = 0) const;
+    QModelIndex followingIndex(const QModelIndex &idx, bool backward, bool includeGenerated = false,
+                               bool *wrapped = 0);
     SearchResultTreeItem *treeItemAtIndex(const QModelIndex &idx) const;
 
     SearchResultTreeItem *m_rootItem;
diff --git a/src/plugins/find/searchresultwidget.cpp b/src/plugins/find/searchresultwidget.cpp
index 34205ef4c7a..556bd2409b5 100644
--- a/src/plugins/find/searchresultwidget.cpp
+++ b/src/plugins/find/searchresultwidget.cpp
@@ -117,7 +117,7 @@ public:
         }
         m_view->setCurrentIndex(m_incrementalFindStart);
         bool wrapped = false;
-        IFindSupport::Result result = find(txt, findFlags, &wrapped);
+        IFindSupport::Result result = find(txt, findFlags, true/*startFromCurrent*/, &wrapped);
         if (wrapped != m_incrementalWrappedState) {
             m_incrementalWrappedState = wrapped;
             showWrapIndicator(m_view);
@@ -128,7 +128,7 @@ public:
     IFindSupport::Result findStep(const QString &txt, Find::FindFlags findFlags)
     {
         bool wrapped = false;
-        IFindSupport::Result result = find(txt, findFlags, &wrapped);
+        IFindSupport::Result result = find(txt, findFlags, false/*startFromNext*/, &wrapped);
         if (wrapped)
             showWrapIndicator(m_view);
         if (result == IFindSupport::Found) {
@@ -138,7 +138,8 @@ public:
         return result;
     }
 
-    IFindSupport::Result find(const QString &txt, Find::FindFlags findFlags, bool *wrapped)
+    IFindSupport::Result find(const QString &txt, Find::FindFlags findFlags,
+                              bool startFromCurrentIndex, bool *wrapped)
     {
         if (wrapped)
             *wrapped = false;
@@ -150,11 +151,13 @@ public:
             index = m_view->model()->find(QRegExp(txt, (sensitive ? Qt::CaseSensitive : Qt::CaseInsensitive)),
                                           m_view->currentIndex(),
                                           Find::textDocumentFlagsForFindFlags(findFlags),
+                                          startFromCurrentIndex,
                                           wrapped);
         } else {
             index = m_view->model()->find(txt,
                                           m_view->currentIndex(),
                                           Find::textDocumentFlagsForFindFlags(findFlags),
+                                          startFromCurrentIndex,
                                           wrapped);
         }
         if (index.isValid()) {
-- 
GitLab