From fc79c737b3edbdfa6031ef21990d2f0d8b2b2f4c Mon Sep 17 00:00:00 2001
From: con <qtc-committer@nokia.com>
Date: Thu, 19 Mar 2009 14:18:26 +0100
Subject: [PATCH] Add last edit position to navigation history.

Task:     240811
---
 .../editormanager/editormanager.cpp           | 14 +++++++---
 .../coreplugin/editormanager/editormanager.h  |  2 +-
 src/plugins/texteditor/basetexteditor.cpp     | 26 ++++++++++++++-----
 src/plugins/texteditor/basetexteditor.h       |  5 ++--
 src/plugins/texteditor/basetexteditor_p.h     |  1 +
 5 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp
index eded47cf5c3..f003a62019c 100644
--- a/src/plugins/coreplugin/editormanager/editormanager.cpp
+++ b/src/plugins/coreplugin/editormanager/editormanager.cpp
@@ -719,8 +719,8 @@ bool EditorManager::closeEditors(const QList<IEditor*> editorsToClose, bool askA
         }
     }
 
-
     emit editorsClosed(acceptedEditors);
+
     foreach (IEditor *editor, acceptedEditors) {
         delete editor;
     }
@@ -1358,7 +1358,7 @@ QList<IEditor*> EditorManager::editorHistory() const
     return m_d->m_editorHistory;
 }
 
-void EditorManager::addCurrentPositionToNavigationHistory(bool compress)
+void EditorManager::addCurrentPositionToNavigationHistory(const QByteArray &saveState)
 {
     IEditor *editor = currentEditor();
     if (!editor)
@@ -1367,7 +1367,15 @@ void EditorManager::addCurrentPositionToNavigationHistory(bool compress)
         return;
     
     QString fileName = editor->file()->fileName();
-    QByteArray state = editor->saveState();
+    bool compress;
+    QByteArray state;
+    if (saveState.isNull()) {
+        state = editor->saveState();
+        compress = false;
+    } else {
+        state = saveState;
+        compress = true;
+    }
     // cut existing
     int firstIndexToRemove;
     if (compress && m_d->currentNavigationHistoryPosition > 0) {
diff --git a/src/plugins/coreplugin/editormanager/editormanager.h b/src/plugins/coreplugin/editormanager/editormanager.h
index 401e94b00d7..46eb010f47c 100644
--- a/src/plugins/coreplugin/editormanager/editormanager.h
+++ b/src/plugins/coreplugin/editormanager/editormanager.h
@@ -132,7 +132,7 @@ public:
     QList<IEditor*> editorsForFiles(QList<IFile*> files) const;
     //QList<EditorGroup *> editorGroups() const;
     QList<IEditor*> editorHistory() const;
-    void addCurrentPositionToNavigationHistory(bool compress = false);
+    void addCurrentPositionToNavigationHistory(const QByteArray &saveState = QByteArray());
 
     bool saveEditor(IEditor *editor);
 
diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index 52c0ad7101b..88f5f06989f 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -564,8 +564,6 @@ Core::IFile *BaseTextEditor::file()
 void BaseTextEditor::editorContentsChange(int position, int charsRemoved, int charsAdded)
 {
     d->m_contentsChanged = true;
-    // add last edit position to history
-    Core::EditorManager::instance()->addCurrentPositionToNavigationHistory(true);
 
     // Keep the line numbers and the block information for the text marks updated
     if (charsRemoved != 0) {
@@ -750,7 +748,6 @@ void BaseTextEditor::cleanWhitespace()
 
 void BaseTextEditor::keyPressEvent(QKeyEvent *e)
 {
-
     d->clearVisibleCollapsedBlock();
 
     QKeyEvent *original_e = e;
@@ -764,8 +761,6 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
         return;
     }
 
-    d->m_contentsChanged = false;
-
     bool ro = isReadOnly();
 
     if (d->m_inBlockSelectionMode) {
@@ -964,7 +959,7 @@ void BaseTextEditor::setTextCursor(const QTextCursor &cursor)
         slotSelectionChanged();
 }
 
-void BaseTextEditor::gotoLine(int line, int column)
+void BaseTextEditor::gotoLine(int line, int column, bool saveNewPosition)
 {
     const int blockNumber = line - 1;
     const QTextBlock &block = document()->findBlockByNumber(blockNumber);
@@ -982,6 +977,8 @@ void BaseTextEditor::gotoLine(int line, int column)
         setTextCursor(cursor);
         centerCursor();
     }
+    if (saveNewPosition)
+        saveCurrentCursorPositionForNavigation();
 }
 
 int BaseTextEditor::position(ITextEditor::PositionOperation posOp, int at) const
@@ -1034,6 +1031,7 @@ QChar BaseTextEditor::characterAt(int pos) const
 
 bool BaseTextEditor::event(QEvent *e)
 {
+    d->m_contentsChanged = false;
     switch (e->type()) {
     case QEvent::ShortcutOverride:
         e->ignore(); // we are a really nice citizen
@@ -1117,7 +1115,7 @@ bool BaseTextEditor::restoreState(const QByteArray &state)
     stream >> hval;
     stream >> lval;
     stream >> cval;
-    gotoLine(lval, cval);
+    gotoLine(lval, cval, false);
     verticalScrollBar()->setValue(vval);
     horizontalScrollBar()->setValue(hval);
     return true;
@@ -1251,6 +1249,7 @@ int BaseTextEditor::visibleWrapColumn() const
 BaseTextEditorPrivate::BaseTextEditorPrivate()
     :
     m_contentsChanged(false),
+    m_lastCursorChangeWasInteresting(false),
     m_document(new BaseTextDocument()),
     m_parenthesesMatchingEnabled(false),
     m_extraArea(0),
@@ -1436,6 +1435,7 @@ QRectF TextEditDocumentLayout::blockBoundingRect(const QTextBlock &block) const
 
 bool BaseTextEditor::viewportEvent(QEvent *event)
 {
+    d->m_contentsChanged = false;
     if (event->type() == QEvent::ContextMenu) {
         const QContextMenuEvent *ce = static_cast<QContextMenuEvent*>(event);
         if (ce->reason() == QContextMenuEvent::Mouse && !textCursor().hasSelection())
@@ -2322,8 +2322,20 @@ void BaseTextEditor::slotUpdateRequest(const QRect &r, int dy)
         slotUpdateExtraAreaWidth();
 }
 
+void BaseTextEditor::saveCurrentCursorPositionForNavigation()
+{
+    d->m_lastCursorChangeWasInteresting = true;
+    d->m_tempState = saveState();
+}
+
 void BaseTextEditor::slotCursorPositionChanged()
 {
+    if (!d->m_contentsChanged && d->m_lastCursorChangeWasInteresting) {
+        Core::EditorManager::instance()->addCurrentPositionToNavigationHistory(d->m_tempState);
+        d->m_lastCursorChangeWasInteresting = false;
+    } else if (d->m_contentsChanged) {
+        saveCurrentCursorPositionForNavigation();
+    }
     QList<QTextEdit::ExtraSelection> extraSelections;
     setExtraSelections(ParenthesesMatchingSelection, extraSelections); // clear
     if (d->m_parenthesesMatchingEnabled)
diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h
index 1813f4e4a91..0e7029a9b02 100644
--- a/src/plugins/texteditor/basetexteditor.h
+++ b/src/plugins/texteditor/basetexteditor.h
@@ -241,7 +241,7 @@ public:
 
     // ITextEditor
 
-    void gotoLine(int line, int column = 0);
+    void gotoLine(int line, int column = 0, bool saveNewPosition = true);
 
     int position(
         ITextEditor::PositionOperation posOp = ITextEditor::Current
@@ -454,6 +454,7 @@ private:
     void handleHomeKey(bool anchor);
     void handleBackspaceKey();
     void moveLineUpDown(bool up);
+    void saveCurrentCursorPositionForNavigation();
 
     void toggleBlockVisible(const QTextBlock &block);
     QRect collapseBox(const QTextBlock &block);
@@ -498,7 +499,7 @@ public:
 
     int currentLine() const;
     int currentColumn() const;
-    inline void gotoLine(int line, int column = 0) { e->gotoLine(line, column); }
+    void gotoLine(int line, int column = 0) { e->gotoLine(line, column); }
 
     inline int position(
         ITextEditor::PositionOperation posOp = ITextEditor::Current
diff --git a/src/plugins/texteditor/basetexteditor_p.h b/src/plugins/texteditor/basetexteditor_p.h
index 9609d3c6ea1..638f2faba22 100644
--- a/src/plugins/texteditor/basetexteditor_p.h
+++ b/src/plugins/texteditor/basetexteditor_p.h
@@ -140,6 +140,7 @@ public:
 
     BaseTextEditor *q;
     bool m_contentsChanged;
+    bool m_lastCursorChangeWasInteresting;
 
     QList<QTextEdit::ExtraSelection> m_syntaxHighlighterSelections;
     QTextEdit::ExtraSelection m_lineSelection;
-- 
GitLab