Core: Reopen last closed editor

The short-cut to reopen the last closed editor is not set by default
to allow users to set by themselves.

Change-Id: I1f57e34c3b1a30873fd550f5cb008e5640e6a1c5
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Artem Sokolovskii
2024-05-08 12:17:31 +02:00
parent c96f502c6c
commit 01e0e43443
5 changed files with 66 additions and 0 deletions

View File

@@ -119,6 +119,7 @@ const char GOTOPREVINHISTORY[] = "QtCreator.GotoPreviousInHistory";
const char GO_BACK[] = "QtCreator.GoBack";
const char GO_FORWARD[] = "QtCreator.GoForward";
const char GOTOLASTEDIT[] = "QtCreator.GotoLastEdit";
const char REOPEN_CLOSED_EDITOR[] = "QtCreator.ReopenClosedEditor";
const char ABOUT_QTCREATOR[] = "QtCreator.AboutQtCreator";
const char ABOUT_PLUGINS[] = "QtCreator.AboutPlugins";
const char CHANGE_LOG[] = "QtCreator.ChangeLog";

View File

@@ -596,6 +596,14 @@ void EditorManagerPrivate::init()
goForward.addToContainer(Constants::M_WINDOW, Constants::G_WINDOW_NAVIGATE);
goForward.addOnTriggered(this, &EditorManager::goForwardInNavigationHistory);
// Reopen last closed document
ActionBuilder reopenLastClosedDocument(this, Constants::REOPEN_CLOSED_EDITOR);
reopenLastClosedDocument.setText(::Core::Tr::tr("Reopen Last Closed Document"));
reopenLastClosedDocument.bindContextAction(&m_reopenLastClosedDocumenAction);
reopenLastClosedDocument.setContext(editDesignContext);
reopenLastClosedDocument.addToContainer(Constants::M_WINDOW, Constants::G_WINDOW_NAVIGATE);
reopenLastClosedDocument.addOnTriggered(this, &EditorManagerPrivate::reopenLastClosedDocument);
// Go to last edit
ActionBuilder gotoLastEdit(this, Constants::GOTOLASTEDIT);
gotoLastEdit.setText(::Core::Tr::tr("Go to Last Edit"));
@@ -2075,6 +2083,7 @@ void EditorManagerPrivate::updateActions()
EditorView *view = currentEditorView();
d->m_goBackAction->setEnabled(view ? view->canGoBack() : false);
d->m_goForwardAction->setEnabled(view ? view->canGoForward() : false);
d->m_reopenLastClosedDocumenAction->setEnabled(view ? view->canReopen() : false);
SplitterOrView *viewParent = (view ? view->parentSplitterOrView() : nullptr);
SplitterOrView *parentSplitter = (viewParent ? viewParent->findParentSplitter() : nullptr);
@@ -2227,6 +2236,22 @@ void EditorManagerPrivate::gotoPreviousSplit()
activateView(prevView);
}
void EditorManagerPrivate::addClosedDocumentToCloseHistory(IEditor *editor)
{
EditorView *view = EditorManagerPrivate::viewForEditor(editor);
QTC_ASSERT(view, return);
view->addClosedEditorToCloseHistory(editor);
EditorManagerPrivate::updateActions();
}
void EditorManagerPrivate::reopenLastClosedDocument()
{
EditorView *view = EditorManagerPrivate::currentEditorView();
QTC_ASSERT(view, return);
view->reopenLastClosedDocument();
EditorManagerPrivate::updateActions();
}
void EditorManagerPrivate::makeCurrentEditorWritable()
{
if (IDocument* doc = EditorManager::currentDocument())
@@ -3031,6 +3056,9 @@ bool EditorManager::closeDocuments(const QList<DocumentModel::Entry *> &entries)
*/
bool EditorManager::closeEditors(const QList<IEditor*> &editorsToClose, bool askAboutModifiedEditors)
{
for (IEditor *editor : editorsToClose)
EditorManagerPrivate::addClosedDocumentToCloseHistory(editor);
return EditorManagerPrivate::closeEditors(editorsToClose,
askAboutModifiedEditors ? EditorManagerPrivate::CloseFlag::CloseWithAsking
: EditorManagerPrivate::CloseFlag::CloseWithoutAsking);

View File

@@ -124,6 +124,8 @@ public:
const Utils::FilePath &newFilePath,
Utils::Id originalType = {});
static void addClosedDocumentToCloseHistory(IEditor *editor);
public slots:
static bool saveDocument(Core::IDocument *document);
static bool saveDocumentAs(Core::IDocument *document);
@@ -133,6 +135,8 @@ public slots:
static void gotoPreviousSplit();
static void gotoNextSplit();
static void reopenLastClosedDocument();
void handleDocumentStateChange(Core::IDocument *document);
void editorAreaDestroyed(QObject *area);
@@ -216,6 +220,7 @@ private:
QAction *m_gotoPreviousDocHistoryAction = nullptr;
QAction *m_goBackAction = nullptr;
QAction *m_goForwardAction = nullptr;
QAction *m_reopenLastClosedDocumenAction = nullptr;
QAction *m_gotoLastEditAction = nullptr;
QAction *m_splitAction = nullptr;
QAction *m_splitSideBySideAction = nullptr;

View File

@@ -264,6 +264,11 @@ bool EditorView::canGoBack() const
return m_currentNavigationHistoryPosition > 0;
}
bool EditorView::canReopen() const
{
return !m_closedEditorHistory.isEmpty();
}
void EditorView::updateEditorHistory(IEditor *editor, QList<EditLocation> &history)
{
IDocument *document = editor ? editor->document() : nullptr;
@@ -505,6 +510,21 @@ void EditorView::addCurrentPositionToNavigationHistory(const QByteArray &saveSta
updateNavigatorActions();
}
void EditorView::addClosedEditorToCloseHistory(IEditor *editor)
{
static const int MAX_ITEMS = 20;
if (!editor || !editor->document())
return;
EditLocation location = EditLocation::forEditor(editor);
m_closedEditorHistory.push_back(location);
if (m_closedEditorHistory.size() > MAX_ITEMS)
m_closedEditorHistory.removeFirst();
}
void EditorView::cutForwardNavigationHistory()
{
while (m_currentNavigationHistoryPosition < m_navigationHistory.size() - 1)
@@ -636,6 +656,13 @@ void EditorView::goForwardInNavigationHistory()
updateNavigatorActions();
}
void EditorView::reopenLastClosedDocument()
{
if (m_closedEditorHistory.isEmpty())
return;
goToEditLocation(m_closedEditorHistory.takeLast());
}
void EditorView::goToEditLocation(const EditLocation &location)
{
IEditor *editor = nullptr;

View File

@@ -85,13 +85,17 @@ public:
bool canGoForward() const;
bool canGoBack() const;
bool canReopen() const;
void goBackInNavigationHistory();
void goForwardInNavigationHistory();
void reopenLastClosedDocument();
void goToEditLocation(const EditLocation &location);
void addCurrentPositionToNavigationHistory(const QByteArray &saveState = QByteArray());
void addClosedEditorToCloseHistory(IEditor *editor);
void cutForwardNavigationHistory();
QList<EditLocation> editorHistory() const { return m_editorHistory; }
@@ -148,6 +152,7 @@ private:
QMenu *m_backMenu;
QMenu *m_forwardMenu;
QList<EditLocation> m_editorHistory;
QList<EditLocation> m_closedEditorHistory;
int m_currentNavigationHistoryPosition = 0;
};