Commit 6dbe374b authored by Eike Ziller's avatar Eike Ziller

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: default avatarDavid Schulz <david.schulz@digia.com>
parent 456a56bc
......@@ -512,8 +512,8 @@ void EditorManager::setCurrentEditor(IEditor *editor, bool ignoreNavigationHisto
d->m_currentEditor = editor;
if (editor) {
if (SplitterOrView *splitterOrView = d->m_splitter->findView(editor))
splitterOrView->view()->setCurrentEditor(editor);
if (EditorView *view = viewForEditor(editor))
view->setCurrentEditor(editor);
d->m_view->updateEditorHistory(editor); // the global view should have a complete history
}
updateActions();
......@@ -545,9 +545,8 @@ Core::Internal::SplitterOrView *EditorManager::currentSplitterOrView() const
{
SplitterOrView *view = d->m_currentView;
if (!view)
view = d->m_currentEditor?
d->m_splitter->findView(d->m_currentEditor):
d->m_splitter->findFirstView();
view = d->m_currentEditor ? viewForEditor(d->m_currentEditor)->parentSplitterOrView()
: d->m_splitter->findFirstView();
if (!view)
return d->m_splitter;
return view;
......@@ -558,6 +557,17 @@ Core::Internal::EditorView *EditorManager::currentEditorView() const
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 *> found;
......@@ -895,10 +905,10 @@ bool EditorManager::closeEditors(const QList<IEditor*> &editorsToClose, bool ask
}
removeEditor(editor);
if (SplitterOrView *view = d->m_splitter->findView(editor)) {
if (editor == view->view()->currentEditor())
closedViews += view->view();
view->view()->removeEditor(editor);
if (EditorView *view = viewForEditor(editor)) {
if (editor == view->currentEditor())
closedViews += view;
view->removeEditor(editor);
}
}
......@@ -960,8 +970,7 @@ void EditorManager::closeDuplicate(Core::IEditor *editor)
emit editorAboutToClose(editor);
if (d->m_splitter->findView(editor)) {
EditorView *view = d->m_splitter->findView(editor)->view();
if (EditorView *view = viewForEditor(editor)) {
removeEditor(editor);
view->removeEditor(editor);
......@@ -988,8 +997,8 @@ void EditorManager::closeDuplicate(Core::IEditor *editor)
Core::IEditor *EditorManager::pickUnusedEditor() const
{
foreach (IEditor *editor, openedEditors()) {
SplitterOrView *view = d->m_splitter->findView(editor);
if (!view || view->editor() != editor)
EditorView *view = viewForEditor(editor);
if (!view || view->currentEditor() != editor)
return editor;
}
return 0;
......@@ -1024,14 +1033,14 @@ Core::IEditor *EditorManager::placeEditor(Core::Internal::EditorView *view, Core
if (!view->hasEditor(editor)) {
bool duplicateSupported = editor->duplicateSupported();
if (SplitterOrView *sourceView = d->m_splitter->findView(editor)) {
if (editor != sourceView->editor() || !duplicateSupported) {
sourceView->view()->removeEditor(editor);
if (EditorView *sourceView = viewForEditor(editor)) {
if (editor != sourceView->currentEditor() || !duplicateSupported) {
sourceView->removeEditor(editor);
view->addEditor(editor);
view->setCurrentEditor(editor);
if (!sourceView->editor()) {
if (!sourceView->currentEditor()) {
if (IEditor *replacement = pickUnusedEditor())
sourceView->view()->addEditor(replacement);
sourceView->addEditor(replacement);
}
return editor;
} else if (duplicateSupported) {
......@@ -1047,8 +1056,7 @@ Core::IEditor *EditorManager::placeEditor(Core::Internal::EditorView *view, Core
void EditorManager::activateEditor(Core::IEditor *editor, OpenEditorFlags flags)
{
SplitterOrView *splitterOrView = m_instance->d->m_splitter->findView(editor);
EditorView *view = (splitterOrView ? splitterOrView->view() : 0);
EditorView *view = viewForEditor(editor);
// TODO an IEditor doesn't have to belong to a view, which makes this method a bit funny
if (!view)
view = m_instance->currentEditorView();
......
......@@ -251,19 +251,21 @@ private:
IEditor *placeEditor(Internal::EditorView *view, 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);
void activateEditorForIndex(Internal::EditorView *view, const QModelIndex &index, OpenEditorFlags = 0);
IEditor *openEditor(Internal::EditorView *view, const QString &fileName,
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::EditorView *currentEditorView() const;
static Internal::EditorView *viewForEditor(IEditor *editor);
void closeEditor(IEditor *editor);
void closeDuplicate(IEditor *editor);
void closeView(Internal::EditorView *view);
void emptyView(Internal::EditorView *view);
Internal::EditorView *currentEditorView() const;
IEditor *pickUnusedEditor() const;
void addDocumentToRecentFiles(IDocument *document);
void switchToPreferedMode();
......
......@@ -514,20 +514,6 @@ SplitterOrView *SplitterOrView::findFirstView()
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)
{
if (m_splitter) {
......
......@@ -180,7 +180,6 @@ public:
QByteArray saveState() const;
void restoreState(const QByteArray &);
SplitterOrView *findView(Core::IEditor *editor);
SplitterOrView *findFirstView();
SplitterOrView *findSplitter(SplitterOrView *child);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment