forked from qt-creator/qt-creator
Editor manager: Invert logic for finding view for editor
Instead of traversing the split hierarchy down from the root, including all the branching etc, traverse the hierarchy up from the editor. Faster in all cases, and works automatically when we introduce multiple "roots" later Change-Id: I50eb0fac683fb249c7ff4a63ba9fecea8d652873 Reviewed-by: David Schulz <david.schulz@digia.com>
This commit is contained in:
@@ -512,8 +512,8 @@ void EditorManager::setCurrentEditor(IEditor *editor, bool ignoreNavigationHisto
|
|||||||
|
|
||||||
d->m_currentEditor = editor;
|
d->m_currentEditor = editor;
|
||||||
if (editor) {
|
if (editor) {
|
||||||
if (SplitterOrView *splitterOrView = d->m_splitter->findView(editor))
|
if (EditorView *view = viewForEditor(editor))
|
||||||
splitterOrView->view()->setCurrentEditor(editor);
|
view->setCurrentEditor(editor);
|
||||||
d->m_view->updateEditorHistory(editor); // the global view should have a complete history
|
d->m_view->updateEditorHistory(editor); // the global view should have a complete history
|
||||||
}
|
}
|
||||||
updateActions();
|
updateActions();
|
||||||
@@ -545,9 +545,8 @@ Core::Internal::SplitterOrView *EditorManager::currentSplitterOrView() const
|
|||||||
{
|
{
|
||||||
SplitterOrView *view = d->m_currentView;
|
SplitterOrView *view = d->m_currentView;
|
||||||
if (!view)
|
if (!view)
|
||||||
view = d->m_currentEditor?
|
view = d->m_currentEditor ? viewForEditor(d->m_currentEditor)->parentSplitterOrView()
|
||||||
d->m_splitter->findView(d->m_currentEditor):
|
: d->m_splitter->findFirstView();
|
||||||
d->m_splitter->findFirstView();
|
|
||||||
if (!view)
|
if (!view)
|
||||||
return d->m_splitter;
|
return d->m_splitter;
|
||||||
return view;
|
return view;
|
||||||
@@ -558,6 +557,17 @@ Core::Internal::EditorView *EditorManager::currentEditorView() const
|
|||||||
return currentSplitterOrView()->view();
|
return currentSplitterOrView()->view();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EditorView *EditorManager::viewForEditor(IEditor *editor)
|
||||||
|
{
|
||||||
|
QWidget *w = editor->widget();
|
||||||
|
while (w) {
|
||||||
|
w = w->parentWidget();
|
||||||
|
if (EditorView *view = qobject_cast<EditorView *>(w))
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
QList<IEditor *> EditorManager::editorsForFileName(const QString &filename) const
|
QList<IEditor *> EditorManager::editorsForFileName(const QString &filename) const
|
||||||
{
|
{
|
||||||
QList<IEditor *> found;
|
QList<IEditor *> found;
|
||||||
@@ -895,10 +905,10 @@ bool EditorManager::closeEditors(const QList<IEditor*> &editorsToClose, bool ask
|
|||||||
}
|
}
|
||||||
|
|
||||||
removeEditor(editor);
|
removeEditor(editor);
|
||||||
if (SplitterOrView *view = d->m_splitter->findView(editor)) {
|
if (EditorView *view = viewForEditor(editor)) {
|
||||||
if (editor == view->view()->currentEditor())
|
if (editor == view->currentEditor())
|
||||||
closedViews += view->view();
|
closedViews += view;
|
||||||
view->view()->removeEditor(editor);
|
view->removeEditor(editor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -960,8 +970,7 @@ void EditorManager::closeDuplicate(Core::IEditor *editor)
|
|||||||
|
|
||||||
emit editorAboutToClose(editor);
|
emit editorAboutToClose(editor);
|
||||||
|
|
||||||
if (d->m_splitter->findView(editor)) {
|
if (EditorView *view = viewForEditor(editor)) {
|
||||||
EditorView *view = d->m_splitter->findView(editor)->view();
|
|
||||||
removeEditor(editor);
|
removeEditor(editor);
|
||||||
view->removeEditor(editor);
|
view->removeEditor(editor);
|
||||||
|
|
||||||
@@ -988,8 +997,8 @@ void EditorManager::closeDuplicate(Core::IEditor *editor)
|
|||||||
Core::IEditor *EditorManager::pickUnusedEditor() const
|
Core::IEditor *EditorManager::pickUnusedEditor() const
|
||||||
{
|
{
|
||||||
foreach (IEditor *editor, openedEditors()) {
|
foreach (IEditor *editor, openedEditors()) {
|
||||||
SplitterOrView *view = d->m_splitter->findView(editor);
|
EditorView *view = viewForEditor(editor);
|
||||||
if (!view || view->editor() != editor)
|
if (!view || view->currentEditor() != editor)
|
||||||
return editor;
|
return editor;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@@ -1024,14 +1033,14 @@ Core::IEditor *EditorManager::placeEditor(Core::Internal::EditorView *view, Core
|
|||||||
|
|
||||||
if (!view->hasEditor(editor)) {
|
if (!view->hasEditor(editor)) {
|
||||||
bool duplicateSupported = editor->duplicateSupported();
|
bool duplicateSupported = editor->duplicateSupported();
|
||||||
if (SplitterOrView *sourceView = d->m_splitter->findView(editor)) {
|
if (EditorView *sourceView = viewForEditor(editor)) {
|
||||||
if (editor != sourceView->editor() || !duplicateSupported) {
|
if (editor != sourceView->currentEditor() || !duplicateSupported) {
|
||||||
sourceView->view()->removeEditor(editor);
|
sourceView->removeEditor(editor);
|
||||||
view->addEditor(editor);
|
view->addEditor(editor);
|
||||||
view->setCurrentEditor(editor);
|
view->setCurrentEditor(editor);
|
||||||
if (!sourceView->editor()) {
|
if (!sourceView->currentEditor()) {
|
||||||
if (IEditor *replacement = pickUnusedEditor())
|
if (IEditor *replacement = pickUnusedEditor())
|
||||||
sourceView->view()->addEditor(replacement);
|
sourceView->addEditor(replacement);
|
||||||
}
|
}
|
||||||
return editor;
|
return editor;
|
||||||
} else if (duplicateSupported) {
|
} else if (duplicateSupported) {
|
||||||
@@ -1047,8 +1056,7 @@ Core::IEditor *EditorManager::placeEditor(Core::Internal::EditorView *view, Core
|
|||||||
|
|
||||||
void EditorManager::activateEditor(Core::IEditor *editor, OpenEditorFlags flags)
|
void EditorManager::activateEditor(Core::IEditor *editor, OpenEditorFlags flags)
|
||||||
{
|
{
|
||||||
SplitterOrView *splitterOrView = m_instance->d->m_splitter->findView(editor);
|
EditorView *view = viewForEditor(editor);
|
||||||
EditorView *view = (splitterOrView ? splitterOrView->view() : 0);
|
|
||||||
// TODO an IEditor doesn't have to belong to a view, which makes this method a bit funny
|
// TODO an IEditor doesn't have to belong to a view, which makes this method a bit funny
|
||||||
if (!view)
|
if (!view)
|
||||||
view = m_instance->currentEditorView();
|
view = m_instance->currentEditorView();
|
||||||
|
|||||||
@@ -251,19 +251,21 @@ private:
|
|||||||
|
|
||||||
IEditor *placeEditor(Internal::EditorView *view, IEditor *editor);
|
IEditor *placeEditor(Internal::EditorView *view, IEditor *editor);
|
||||||
IEditor *duplicateEditor(IEditor *editor);
|
IEditor *duplicateEditor(IEditor *editor);
|
||||||
void setCurrentEditor(IEditor *editor, bool ignoreNavigationHistory = false);
|
|
||||||
void setCurrentView(Internal::SplitterOrView *view);
|
|
||||||
IEditor *activateEditor(Internal::EditorView *view, IEditor *editor, OpenEditorFlags flags = 0);
|
IEditor *activateEditor(Internal::EditorView *view, IEditor *editor, OpenEditorFlags flags = 0);
|
||||||
void activateEditorForIndex(Internal::EditorView *view, const QModelIndex &index, OpenEditorFlags = 0);
|
void activateEditorForIndex(Internal::EditorView *view, const QModelIndex &index, OpenEditorFlags = 0);
|
||||||
IEditor *openEditor(Internal::EditorView *view, const QString &fileName,
|
IEditor *openEditor(Internal::EditorView *view, const QString &fileName,
|
||||||
const Id &id = Id(), OpenEditorFlags flags = 0, bool *newEditor = 0);
|
const Id &id = Id(), OpenEditorFlags flags = 0, bool *newEditor = 0);
|
||||||
|
|
||||||
|
void setCurrentEditor(IEditor *editor, bool ignoreNavigationHistory = false);
|
||||||
|
void setCurrentView(Internal::SplitterOrView *view);
|
||||||
Internal::SplitterOrView *currentSplitterOrView() const;
|
Internal::SplitterOrView *currentSplitterOrView() const;
|
||||||
|
Internal::EditorView *currentEditorView() const;
|
||||||
|
static Internal::EditorView *viewForEditor(IEditor *editor);
|
||||||
|
|
||||||
void closeEditor(IEditor *editor);
|
void closeEditor(IEditor *editor);
|
||||||
void closeDuplicate(IEditor *editor);
|
void closeDuplicate(IEditor *editor);
|
||||||
void closeView(Internal::EditorView *view);
|
void closeView(Internal::EditorView *view);
|
||||||
void emptyView(Internal::EditorView *view);
|
void emptyView(Internal::EditorView *view);
|
||||||
Internal::EditorView *currentEditorView() const;
|
|
||||||
IEditor *pickUnusedEditor() const;
|
IEditor *pickUnusedEditor() const;
|
||||||
void addDocumentToRecentFiles(IDocument *document);
|
void addDocumentToRecentFiles(IDocument *document);
|
||||||
void switchToPreferedMode();
|
void switchToPreferedMode();
|
||||||
|
|||||||
@@ -514,20 +514,6 @@ SplitterOrView *SplitterOrView::findFirstView()
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
SplitterOrView *SplitterOrView::findView(Core::IEditor *editor)
|
|
||||||
{
|
|
||||||
if (!editor || hasEditor(editor))
|
|
||||||
return this;
|
|
||||||
if (m_splitter) {
|
|
||||||
for (int i = 0; i < m_splitter->count(); ++i) {
|
|
||||||
if (SplitterOrView *splitterOrView = qobject_cast<SplitterOrView*>(m_splitter->widget(i)))
|
|
||||||
if (SplitterOrView *result = splitterOrView->findView(editor))
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
SplitterOrView *SplitterOrView::findSplitter(SplitterOrView *child)
|
SplitterOrView *SplitterOrView::findSplitter(SplitterOrView *child)
|
||||||
{
|
{
|
||||||
if (m_splitter) {
|
if (m_splitter) {
|
||||||
|
|||||||
@@ -180,7 +180,6 @@ public:
|
|||||||
QByteArray saveState() const;
|
QByteArray saveState() const;
|
||||||
void restoreState(const QByteArray &);
|
void restoreState(const QByteArray &);
|
||||||
|
|
||||||
SplitterOrView *findView(Core::IEditor *editor);
|
|
||||||
SplitterOrView *findFirstView();
|
SplitterOrView *findFirstView();
|
||||||
SplitterOrView *findSplitter(SplitterOrView *child);
|
SplitterOrView *findSplitter(SplitterOrView *child);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user