From 70e27bb8ead8e42a3153af0d6c0e076709f4f632 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Mon, 30 Apr 2018 15:55:38 +0200 Subject: [PATCH] Insert new bookmarks after the "current" bookmark Makes it more useful as a navigation instrument. If new bookmarks are always added to the end of the list, the "previous" bookmark will have nothing to do with the context of the bookmark that was previously the "current" one. Task-number: QTCREATORBUG-20061 Change-Id: I0ea38391750371dec80872206d97f4934a37f7b4 Reviewed-by: David Schulz --- src/plugins/bookmarks/bookmarkmanager.cpp | 28 +++++++++++++++-------- src/plugins/bookmarks/bookmarkmanager.h | 1 + 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/plugins/bookmarks/bookmarkmanager.cpp b/src/plugins/bookmarks/bookmarkmanager.cpp index f0b2f9d71f3..a503e97123a 100644 --- a/src/plugins/bookmarks/bookmarkmanager.cpp +++ b/src/plugins/bookmarks/bookmarkmanager.cpp @@ -455,7 +455,10 @@ void BookmarkManager::toggleBookmark(const FileName &fileName, int lineNumber) // Add a new bookmark if no bookmark existed on this line Bookmark *mark = new Bookmark(lineNumber, this); mark->updateFileName(fileName.toString()); - addBookmark(mark); + const QModelIndex currentIndex = selectionModel()->currentIndex(); + const int insertionIndex = currentIndex.isValid() ? currentIndex.row() + 1 + : m_bookmarksList.size(); + insertBookmark(insertionIndex, mark); } void BookmarkManager::updateBookmark(Bookmark *bookmark) @@ -731,23 +734,30 @@ Bookmark *BookmarkManager::findBookmark(const FileName &filePath, int lineNumber Utils::equal(&Bookmark::lineNumber, lineNumber)); } -/* Adds a bookmark to the internal data structures. The 'userset' parameter - * determines whether action status should be updated and whether the bookmarks - * should be saved to the session settings. - */ -void BookmarkManager::addBookmark(Bookmark *bookmark, bool userset) +void BookmarkManager::insertBookmark(int idx, Bookmark *bookmark, bool userset) { - beginInsertRows(QModelIndex(), m_bookmarksList.size(), m_bookmarksList.size()); + idx = qBound(0, idx, m_bookmarksList.size()); + beginInsertRows(QModelIndex(), idx, idx); m_bookmarksMap[FileName::fromString(bookmark->fileName())].append(bookmark); - m_bookmarksList.append(bookmark); + m_bookmarksList.insert(idx, bookmark); endInsertRows(); if (userset) { updateActionStatus(); saveBookmarks(); } - selectionModel()->setCurrentIndex(index(m_bookmarksList.size()-1 , 0, QModelIndex()), QItemSelectionModel::Select | QItemSelectionModel::Clear); + selectionModel()->setCurrentIndex(index(idx, 0, QModelIndex()), + QItemSelectionModel::Select | QItemSelectionModel::Clear); +} + +/* Adds a bookmark to the internal data structures. The 'userset' parameter + * determines whether action status should be updated and whether the bookmarks + * should be saved to the session settings. + */ +void BookmarkManager::addBookmark(Bookmark *bookmark, bool userset) +{ + insertBookmark(m_bookmarksList.size(), bookmark, userset); } /* Adds a new bookmark based on information parsed from the string. */ diff --git a/src/plugins/bookmarks/bookmarkmanager.h b/src/plugins/bookmarks/bookmarkmanager.h index 0bbd2468f68..40219420017 100644 --- a/src/plugins/bookmarks/bookmarkmanager.h +++ b/src/plugins/bookmarks/bookmarkmanager.h @@ -110,6 +110,7 @@ private: void documentPrevNext(bool next); Bookmark *findBookmark(const Utils::FileName &filePath, int lineNumber); + void insertBookmark(int index, Bookmark *bookmark, bool userset = true); void addBookmark(Bookmark *bookmark, bool userset = true); void addBookmark(const QString &s); static QString bookmarkToString(const Bookmark *b);