forked from qt-creator/qt-creator
Editors: Add menus for back/forward buttons
Add menus to the back and forward buttons in the editor tool bar, that show history the history. Limited to one entry per consecutive same file path, because we currently don't have the means to visually distinguish multiple locations in the same file. Fixes: QTCREATORBUG-347 Change-Id: I69c5cfaf4c12ec8b59f98eb692c799babca0458a Reviewed-by: David Schulz <david.schulz@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
@@ -39,14 +39,16 @@ namespace Core::Internal {
|
|||||||
|
|
||||||
// EditorView
|
// EditorView
|
||||||
|
|
||||||
EditorView::EditorView(SplitterOrView *parentSplitterOrView, QWidget *parent) :
|
EditorView::EditorView(SplitterOrView *parentSplitterOrView, QWidget *parent)
|
||||||
QWidget(parent),
|
: QWidget(parent)
|
||||||
m_parentSplitterOrView(parentSplitterOrView),
|
, m_parentSplitterOrView(parentSplitterOrView)
|
||||||
m_toolBar(new EditorToolBar(this)),
|
, m_toolBar(new EditorToolBar(this))
|
||||||
m_container(new QStackedWidget(this)),
|
, m_container(new QStackedWidget(this))
|
||||||
m_infoBarDisplay(new InfoBarDisplay(this)),
|
, m_infoBarDisplay(new InfoBarDisplay(this))
|
||||||
m_statusHLine(Layouting::createHr(this)),
|
, m_statusHLine(Layouting::createHr(this))
|
||||||
m_statusWidget(new QFrame(this))
|
, m_statusWidget(new QFrame(this))
|
||||||
|
, m_backMenu(new QMenu(this))
|
||||||
|
, m_forwardMenu(new QMenu(this))
|
||||||
{
|
{
|
||||||
auto tl = new QVBoxLayout(this);
|
auto tl = new QVBoxLayout(this);
|
||||||
tl->setSpacing(0);
|
tl->setSpacing(0);
|
||||||
@@ -67,6 +69,8 @@ EditorView::EditorView(SplitterOrView *parentSplitterOrView, QWidget *parent) :
|
|||||||
connect(m_toolBar, &EditorToolBar::splitNewWindowClicked, this, &EditorView::splitNewWindow);
|
connect(m_toolBar, &EditorToolBar::splitNewWindowClicked, this, &EditorView::splitNewWindow);
|
||||||
connect(m_toolBar, &EditorToolBar::closeSplitClicked, this, &EditorView::closeSplit);
|
connect(m_toolBar, &EditorToolBar::closeSplitClicked, this, &EditorView::closeSplit);
|
||||||
m_toolBar->setMenuProvider([this](QMenu *menu) { fillListContextMenu(menu); });
|
m_toolBar->setMenuProvider([this](QMenu *menu) { fillListContextMenu(menu); });
|
||||||
|
m_toolBar->setGoBackMenu(m_backMenu);
|
||||||
|
m_toolBar->setGoForwardMenu(m_forwardMenu);
|
||||||
tl->addWidget(m_toolBar);
|
tl->addWidget(m_toolBar);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -509,6 +513,35 @@ void EditorView::cutForwardNavigationHistory()
|
|||||||
|
|
||||||
void EditorView::updateNavigatorActions()
|
void EditorView::updateNavigatorActions()
|
||||||
{
|
{
|
||||||
|
static const int MAX_ITEMS = 20;
|
||||||
|
FilePath last;
|
||||||
|
m_backMenu->clear();
|
||||||
|
int count = 0;
|
||||||
|
for (int i = m_currentNavigationHistoryPosition - 1; i >= 0; i--) {
|
||||||
|
const EditLocation &loc = m_navigationHistory.at(i);
|
||||||
|
if (loc.filePath != last) {
|
||||||
|
m_backMenu->addAction(loc.displayName(), this, [this, i] { goToNavigationHistory(i); });
|
||||||
|
last = loc.filePath;
|
||||||
|
++count;
|
||||||
|
if (count >= MAX_ITEMS)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
last = {};
|
||||||
|
m_forwardMenu->clear();
|
||||||
|
count = 0;
|
||||||
|
for (int i = m_currentNavigationHistoryPosition + 1; i < m_navigationHistory.size(); i++) {
|
||||||
|
const EditLocation &loc = m_navigationHistory.at(i);
|
||||||
|
if (loc.filePath != last) {
|
||||||
|
m_forwardMenu->addAction(loc.displayName(), this, [this, i] {
|
||||||
|
goToNavigationHistory(i);
|
||||||
|
});
|
||||||
|
last = loc.filePath;
|
||||||
|
++count;
|
||||||
|
if (count >= MAX_ITEMS)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
m_toolBar->setCanGoBack(canGoBack());
|
m_toolBar->setCanGoBack(canGoBack());
|
||||||
m_toolBar->setCanGoForward(canGoForward());
|
m_toolBar->setCanGoForward(canGoForward());
|
||||||
}
|
}
|
||||||
@@ -544,31 +577,45 @@ static bool fileNameWasRemoved(const FilePath &filePath)
|
|||||||
return !filePath.isEmpty() && !filePath.exists();
|
return !filePath.isEmpty() && !filePath.exists();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool EditorView::openEditorFromNavigationHistory(int index)
|
||||||
|
{
|
||||||
|
EditLocation location = m_navigationHistory.at(index);
|
||||||
|
IEditor *editor = nullptr;
|
||||||
|
if (location.document) {
|
||||||
|
editor = EditorManagerPrivate::activateEditorForDocument(
|
||||||
|
this, location.document, EditorManager::IgnoreNavigationHistory);
|
||||||
|
}
|
||||||
|
if (!editor) {
|
||||||
|
if (fileNameWasRemoved(location.filePath))
|
||||||
|
return false;
|
||||||
|
editor = EditorManagerPrivate::openEditor(
|
||||||
|
this, location.filePath, location.id, EditorManager::IgnoreNavigationHistory);
|
||||||
|
if (!editor)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
editor->restoreState(location.state);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EditorView::goToNavigationHistory(int index)
|
||||||
|
{
|
||||||
|
if (index >= m_navigationHistory.size())
|
||||||
|
return;
|
||||||
|
updateCurrentPositionInNavigationHistory();
|
||||||
|
if (!openEditorFromNavigationHistory(index))
|
||||||
|
m_navigationHistory.removeAt(index);
|
||||||
|
m_currentNavigationHistoryPosition = index;
|
||||||
|
updateNavigatorActions();
|
||||||
|
}
|
||||||
|
|
||||||
void EditorView::goBackInNavigationHistory()
|
void EditorView::goBackInNavigationHistory()
|
||||||
{
|
{
|
||||||
updateCurrentPositionInNavigationHistory();
|
updateCurrentPositionInNavigationHistory();
|
||||||
while (m_currentNavigationHistoryPosition > 0) {
|
while (m_currentNavigationHistoryPosition > 0) {
|
||||||
--m_currentNavigationHistoryPosition;
|
--m_currentNavigationHistoryPosition;
|
||||||
EditLocation location = m_navigationHistory.at(m_currentNavigationHistoryPosition);
|
if (openEditorFromNavigationHistory(m_currentNavigationHistoryPosition))
|
||||||
IEditor *editor = nullptr;
|
|
||||||
if (location.document) {
|
|
||||||
editor = EditorManagerPrivate::activateEditorForDocument(this, location.document,
|
|
||||||
EditorManager::IgnoreNavigationHistory);
|
|
||||||
}
|
|
||||||
if (!editor) {
|
|
||||||
if (fileNameWasRemoved(location.filePath)) {
|
|
||||||
m_navigationHistory.removeAt(m_currentNavigationHistoryPosition);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
editor = EditorManagerPrivate::openEditor(this, location.filePath, location.id,
|
|
||||||
EditorManager::IgnoreNavigationHistory);
|
|
||||||
if (!editor) {
|
|
||||||
m_navigationHistory.removeAt(m_currentNavigationHistoryPosition);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
editor->restoreState(location.state);
|
|
||||||
break;
|
break;
|
||||||
|
m_navigationHistory.removeAt(m_currentNavigationHistoryPosition);
|
||||||
}
|
}
|
||||||
updateNavigatorActions();
|
updateNavigatorActions();
|
||||||
}
|
}
|
||||||
@@ -580,26 +627,9 @@ void EditorView::goForwardInNavigationHistory()
|
|||||||
return;
|
return;
|
||||||
++m_currentNavigationHistoryPosition;
|
++m_currentNavigationHistoryPosition;
|
||||||
while (m_currentNavigationHistoryPosition < m_navigationHistory.size()) {
|
while (m_currentNavigationHistoryPosition < m_navigationHistory.size()) {
|
||||||
IEditor *editor = nullptr;
|
if (openEditorFromNavigationHistory(m_currentNavigationHistoryPosition))
|
||||||
EditLocation location = m_navigationHistory.at(m_currentNavigationHistoryPosition);
|
|
||||||
if (location.document) {
|
|
||||||
editor = EditorManagerPrivate::activateEditorForDocument(this, location.document,
|
|
||||||
EditorManager::IgnoreNavigationHistory);
|
|
||||||
}
|
|
||||||
if (!editor) {
|
|
||||||
if (fileNameWasRemoved(location.filePath)) {
|
|
||||||
m_navigationHistory.removeAt(m_currentNavigationHistoryPosition);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
editor = EditorManagerPrivate::openEditor(this, location.filePath, location.id,
|
|
||||||
EditorManager::IgnoreNavigationHistory);
|
|
||||||
if (!editor) {
|
|
||||||
m_navigationHistory.removeAt(m_currentNavigationHistoryPosition);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
editor->restoreState(location.state);
|
|
||||||
break;
|
break;
|
||||||
|
m_navigationHistory.removeAt(m_currentNavigationHistoryPosition);
|
||||||
}
|
}
|
||||||
if (m_currentNavigationHistoryPosition >= m_navigationHistory.size())
|
if (m_currentNavigationHistoryPosition >= m_navigationHistory.size())
|
||||||
m_currentNavigationHistoryPosition = qMax<int>(m_navigationHistory.size() - 1, 0);
|
m_currentNavigationHistoryPosition = qMax<int>(m_navigationHistory.size() - 1, 0);
|
||||||
@@ -1024,4 +1054,11 @@ EditLocation EditLocation::forEditor(const IEditor *editor, const QByteArray &sa
|
|||||||
return location;
|
return location;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString EditLocation::displayName() const
|
||||||
|
{
|
||||||
|
if (document)
|
||||||
|
return document->displayName();
|
||||||
|
return filePath.fileName();
|
||||||
|
}
|
||||||
|
|
||||||
} // Core::Internal
|
} // Core::Internal
|
||||||
|
@@ -42,6 +42,8 @@ public:
|
|||||||
static EditLocation load(const QByteArray &data);
|
static EditLocation load(const QByteArray &data);
|
||||||
static EditLocation forEditor(const IEditor *editor, const QByteArray &saveState = {});
|
static EditLocation forEditor(const IEditor *editor, const QByteArray &saveState = {});
|
||||||
|
|
||||||
|
QString displayName() const;
|
||||||
|
|
||||||
QPointer<IDocument> document;
|
QPointer<IDocument> document;
|
||||||
Utils::FilePath filePath;
|
Utils::FilePath filePath;
|
||||||
Utils::Id id;
|
Utils::Id id;
|
||||||
@@ -125,6 +127,8 @@ private:
|
|||||||
void checkProjectLoaded(IEditor *editor);
|
void checkProjectLoaded(IEditor *editor);
|
||||||
|
|
||||||
void updateCurrentPositionInNavigationHistory();
|
void updateCurrentPositionInNavigationHistory();
|
||||||
|
bool openEditorFromNavigationHistory(int index);
|
||||||
|
void goToNavigationHistory(int index);
|
||||||
|
|
||||||
SplitterOrView *m_parentSplitterOrView;
|
SplitterOrView *m_parentSplitterOrView;
|
||||||
EditorToolBar *m_toolBar;
|
EditorToolBar *m_toolBar;
|
||||||
@@ -141,6 +145,8 @@ private:
|
|||||||
QLabel *m_emptyViewLabel;
|
QLabel *m_emptyViewLabel;
|
||||||
|
|
||||||
QList<EditLocation> m_navigationHistory;
|
QList<EditLocation> m_navigationHistory;
|
||||||
|
QMenu *m_backMenu;
|
||||||
|
QMenu *m_forwardMenu;
|
||||||
QList<EditLocation> m_editorHistory;
|
QList<EditLocation> m_editorHistory;
|
||||||
int m_currentNavigationHistoryPosition = 0;
|
int m_currentNavigationHistoryPosition = 0;
|
||||||
};
|
};
|
||||||
|
@@ -347,6 +347,16 @@ void EditorToolBar::setCanGoForward(bool canGoForward)
|
|||||||
d->m_goForwardAction->setEnabled(canGoForward);
|
d->m_goForwardAction->setEnabled(canGoForward);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void EditorToolBar::setGoBackMenu(QMenu *menu)
|
||||||
|
{
|
||||||
|
d->m_backButton->setMenu(menu);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EditorToolBar::setGoForwardMenu(QMenu *menu)
|
||||||
|
{
|
||||||
|
d->m_forwardButton->setMenu(menu);
|
||||||
|
}
|
||||||
|
|
||||||
void EditorToolBar::updateActionShortcuts()
|
void EditorToolBar::updateActionShortcuts()
|
||||||
{
|
{
|
||||||
d->m_closeEditorButton->setToolTip(ActionManager::command(Constants::CLOSE)->stringWithAppendedShortcut(Tr::tr("Close Document")));
|
d->m_closeEditorButton->setToolTip(ActionManager::command(Constants::CLOSE)->stringWithAppendedShortcut(Tr::tr("Close Document")));
|
||||||
|
@@ -56,6 +56,8 @@ public:
|
|||||||
void setNavigationVisible(bool isVisible);
|
void setNavigationVisible(bool isVisible);
|
||||||
void setCanGoBack(bool canGoBack);
|
void setCanGoBack(bool canGoBack);
|
||||||
void setCanGoForward(bool canGoForward);
|
void setCanGoForward(bool canGoForward);
|
||||||
|
void setGoBackMenu(QMenu *menu);
|
||||||
|
void setGoForwardMenu(QMenu *menu);
|
||||||
void removeToolbarForEditor(IEditor *editor);
|
void removeToolbarForEditor(IEditor *editor);
|
||||||
void setCloseSplitEnabled(bool enable);
|
void setCloseSplitEnabled(bool enable);
|
||||||
void setCloseSplitIcon(const QIcon &icon);
|
void setCloseSplitIcon(const QIcon &icon);
|
||||||
|
Reference in New Issue
Block a user