diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp index 8893fe74831..65b38d7bc2d 100644 --- a/src/plugins/coreplugin/editormanager/editormanager.cpp +++ b/src/plugins/coreplugin/editormanager/editormanager.cpp @@ -507,9 +507,9 @@ EditorToolBar *EditorManager::createToolBar(QWidget *parent) void EditorManager::removeEditor(IEditor *editor) { - bool isDuplicate = d->m_editorModel->isDuplicate(editor); - d->m_editorModel->removeEditor(editor); - if (!isDuplicate) + bool lastOneForDocument = false; + d->m_editorModel->removeEditor(editor, &lastOneForDocument); + if (lastOneForDocument) DocumentManager::removeDocument(editor->document()); ICore::removeContextObject(editor); } @@ -660,21 +660,17 @@ void EditorManager::emptyView(Core::Internal::EditorView *view) QList editors = view->editors(); foreach (IEditor *editor, editors) { - if (!d->m_editorModel->isDuplicate(editor)) { - QList duplicates = d->m_editorModel->duplicatesFor(editor); - if (!duplicates.isEmpty()) { - d->m_editorModel->makeOriginal(duplicates.first()); - } else { - // it's the only editor for that file and it's not a duplicate, - // so we need to keep it around (--> in the editor model) - if (currentEditor() == editor) { - // we don't want a current editor that is not open in a view - setCurrentEditor(0); - } - editors.removeAll(editor); - view->removeEditor(editor); - continue; // don't close the editor + if (d->m_editorModel->editorsForDocument(editor->document()).size() == 1) { + // it's the only editor for that file + // so we need to keep it around (--> in the editor model) + if (currentEditor() == editor) { + // we don't want a current editor that is not open in a view + setCurrentView(view); + setCurrentEditor(0); } + editors.removeAll(editor); + view->removeEditor(editor); + continue; // don't close the editor } emit editorAboutToClose(editor); removeEditor(editor); @@ -743,33 +739,6 @@ void EditorManager::closeView(Core::Internal::EditorView *view) } } -QList - EditorManager::editorsForDocuments(QList documents) const -{ - const QList editors = openedEditors(); - QSet found; - foreach (IDocument *document, documents) { - foreach (IEditor *editor, editors) { - if (editor->document() == document && !found.contains(editor)) - found << editor; - } - } - return found.toList(); -} - -QList EditorManager::documentsForEditors(QList editors) const -{ - QSet handledEditors; - QList documents; - foreach (IEditor *editor, editors) { - if (!handledEditors.contains(editor)) { - documents << editor->document(); - handledEditors.insert(editor); - } - } - return documents; -} - bool EditorManager::closeAllEditors(bool askAboutModifiedEditors) { d->m_editorModel->removeAllRestoredEditors(); @@ -780,19 +749,21 @@ bool EditorManager::closeAllEditors(bool askAboutModifiedEditors) return false; } -void EditorManager::closeOtherEditors(IEditor *editor) +void EditorManager::closeOtherEditors(IDocument *document) { d->m_editorModel->removeAllRestoredEditors(); - QList editors = openedEditors(); - editors.removeAll(editor); - closeEditors(editors, true); + QList editorsToClose; + foreach (IEditor *editor, openedEditors()) + if (editor->document() != document) + editorsToClose << editor; + closeEditors(editorsToClose, true); } void EditorManager::closeOtherEditors() { IEditor *current = currentEditor(); QTC_ASSERT(current, return); - closeOtherEditors(current); + closeOtherEditors(current->document()); } // SLOT connected to action @@ -822,9 +793,9 @@ void EditorManager::addSaveAndCloseEditorActions(QMenu *contextMenu, OpenEditors assignAction(d->m_saveAsCurrentEditorContextAction, ActionManager::command(Constants::SAVEAS)->action()); assignAction(d->m_revertToSavedCurrentEditorContextAction, ActionManager::command(Constants::REVERTTOSAVED)->action()); - IEditor *editor = entry ? entry->editor : 0; + IDocument *document = entry ? entry->document : 0; - setupSaveActions(editor, + setupSaveActions(document, d->m_saveCurrentEditorContextAction, d->m_saveAsCurrentEditorContextAction, d->m_revertToSavedCurrentEditorContextAction); @@ -955,36 +926,36 @@ void EditorManager::doEscapeKeyFocusMoveMagic() void EditorManager::saveDocumentFromContextMenu() { - IEditor *editor = d->m_contextMenuEntry ? d->m_contextMenuEntry->editor : 0; - if (editor) - saveDocument(editor->document()); + IDocument *document = d->m_contextMenuEntry ? d->m_contextMenuEntry->document : 0; + if (document) + saveDocument(document); } void EditorManager::saveDocumentAsFromContextMenu() { - IEditor *editor = d->m_contextMenuEntry ? d->m_contextMenuEntry->editor : 0; - if (editor) - saveDocumentAs(editor->document()); + IDocument *document = d->m_contextMenuEntry ? d->m_contextMenuEntry->document : 0; + if (document) + saveDocumentAs(document); } void EditorManager::revertToSavedFromContextMenu() { - IEditor *editor = d->m_contextMenuEntry ? d->m_contextMenuEntry->editor : 0; - if (editor) - revertToSaved(editor); + IDocument *document = d->m_contextMenuEntry ? d->m_contextMenuEntry->document : 0; + if (document) + revertToSaved(document); } void EditorManager::closeEditorFromContextMenu() { - IEditor *editor = d->m_contextMenuEntry ? d->m_contextMenuEntry->editor : 0; - if (editor) - closeEditor(editor); + IDocument *document = d->m_contextMenuEntry ? d->m_contextMenuEntry->document : 0; + if (document) + closeEditors(d->m_editorModel->editorsForDocument(document)); } void EditorManager::closeOtherEditorsFromContextMenu() { - IEditor *editor = d->m_contextMenuEntry ? d->m_contextMenuEntry->editor : 0; - closeOtherEditors(editor); + IDocument *document = d->m_contextMenuEntry ? d->m_contextMenuEntry->document : 0; + closeOtherEditors(document); } void EditorManager::showInGraphicalShell() @@ -1068,8 +1039,8 @@ void EditorManager::closeEditor(OpenEditorsModel::Entry *entry) { if (!entry) return; - if (entry->editor) - closeEditor(entry->editor); + if (entry->document) + closeEditors(d->m_editorModel->editorsForDocument(entry->document)); else d->m_editorModel->removeEntry(entry); } @@ -1082,14 +1053,13 @@ bool EditorManager::closeEditors(const QList &editorsToClose, bool ask EditorView *currentView = currentEditorView(); bool closingFailed = false; - QList acceptedEditors; + QSet acceptedEditors; + QSet acceptedDocuments; //ask all core listeners to check whether the editor can be closed const QList listeners = ExtensionSystem::PluginManager::getObjects(); foreach (IEditor *editor, editorsToClose) { bool editorAccepted = true; - if (d->m_editorModel->isDuplicate(editor)) - editor = d->m_editorModel->originalForDuplicate(editor); foreach (ICoreListener *listener, listeners) { if (!listener->editorAboutToClose(editor)) { editorAccepted = false; @@ -1097,21 +1067,24 @@ bool EditorManager::closeEditors(const QList &editorsToClose, bool ask break; } } - if (editorAccepted) - acceptedEditors.append(editor); + if (editorAccepted) { + acceptedEditors += d->m_editorModel->editorsForDocument(editor->document()).toSet(); + acceptedDocuments.insert(editor->document()); + } } if (acceptedEditors.isEmpty()) return false; //ask whether to save modified files if (askAboutModifiedEditors) { bool cancelled = false; - QList list = DocumentManager::saveModifiedDocuments(documentsForEditors(acceptedEditors), &cancelled); + QList list = DocumentManager::saveModifiedDocuments(acceptedDocuments.toList(), &cancelled); if (cancelled) return false; if (!list.isEmpty()) { closingFailed = true; - QSet skipSet = editorsForDocuments(list).toSet(); - acceptedEditors = acceptedEditors.toSet().subtract(skipSet).toList(); + acceptedDocuments.subtract(list.toSet()); + QSet skipSet = d->m_editorModel->editorsForDocuments(list).toSet(); + acceptedEditors = acceptedEditors.subtract(skipSet); } } if (acceptedEditors.isEmpty()) @@ -1120,12 +1093,6 @@ bool EditorManager::closeEditors(const QList &editorsToClose, bool ask // close Editor History list windowPopup()->setVisible(false); - // add duplicates - QList duplicates; - foreach (IEditor *editor, acceptedEditors) - duplicates += d->m_editorModel->duplicatesFor(editor); - acceptedEditors += duplicates; - QList closedViews; // remove the editors @@ -1168,14 +1135,18 @@ bool EditorManager::closeEditors(const QList &editorsToClose, bool ask if (entry) { activateEditorForEntry(view, entry, flags); } else { - const QList editors = d->m_editorModel->editors(); - if (!editors.isEmpty()) - activateEditor(view, editors.last(), flags); + // no "restored" ones, so any entry left should have a document + const QList documents = d->m_editorModel->entries(); + if (!documents.isEmpty()) { + IDocument *document = documents.last()->document; + if (document) + activateEditorForDocument(view, document, flags); + } } } } - emit editorsClosed(acceptedEditors); + emit editorsClosed(acceptedEditors.toList()); foreach (IEditor *editor, acceptedEditors) { delete editor; @@ -1197,50 +1168,6 @@ bool EditorManager::closeEditors(const QList &editorsToClose, bool ask return !closingFailed; } -void EditorManager::closeDuplicate(Core::IEditor *editor) -{ - - IEditor *original = editor; - if (d->m_editorModel->isDuplicate(editor)) - original= d->m_editorModel->originalForDuplicate(editor); - QList duplicates = d->m_editorModel->duplicatesFor(original); - - if (duplicates.isEmpty()) { - closeEditor(editor); - return; - } - - if (original== editor) - d->m_editorModel->makeOriginal(duplicates.first()); - - EditorView *currentView = currentEditorView(); - - emit editorAboutToClose(editor); - - if (EditorView *view = viewForEditor(editor)) { - removeEditor(editor); - view->removeEditor(editor); - - IEditor *newCurrent = view->currentEditor(); - if (!newCurrent) - newCurrent = pickUnusedEditor(); - if (newCurrent) { - activateEditor(view, newCurrent, DoNotChangeCurrentEditor); - } else { - OpenEditorsModel::Entry *entry = d->m_editorModel->firstRestoredEditor(); - if (entry) - activateEditorForEntry(view, entry, DoNotChangeCurrentEditor); - } - } - - emit editorsClosed(QList() << editor); - delete editor; - if (currentView) { - if (IEditor *currentEditor = currentView->currentEditor()) - activateEditor(currentView, currentEditor); - } -} - Core::IEditor *EditorManager::pickUnusedEditor() const { foreach (IEditor *editor, openedEditors()) { @@ -1265,9 +1192,9 @@ void EditorManager::activateEditorForEntry(Internal::EditorView *view, OpenEdito setCurrentEditor(0); return; } - IEditor *editor = entry->editor; - if (editor) { - activateEditor(view, editor, flags); + IDocument *document = entry->document; + if (document) { + activateEditorForDocument(view, document, flags); return; } @@ -1298,6 +1225,7 @@ Core::IEditor *EditorManager::placeEditor(Core::Internal::EditorView *view, Core bool duplicateSupported = editor->duplicateSupported(); if (EditorView *sourceView = viewForEditor(editor)) { if (editor != sourceView->currentEditor() || !duplicateSupported) { + // pull the IEditor over to the new view sourceView->removeEditor(editor); view->addEditor(editor); view->setCurrentEditor(editor); @@ -1309,7 +1237,6 @@ Core::IEditor *EditorManager::placeEditor(Core::Internal::EditorView *view, Core } else if (duplicateSupported) { editor = duplicateEditor(editor); Q_ASSERT(editor); - d->m_editorModel->makeOriginal(editor); } } view->addEditor(editor); @@ -1475,14 +1402,15 @@ IEditor *EditorManager::createEditor(const Id &editorId, const QString &fileName return editor; } -void EditorManager::addEditor(IEditor *editor, bool isDuplicate) +void EditorManager::addEditor(IEditor *editor) { if (!editor) return; ICore::addContextObject(editor); - d->m_editorModel->addEditor(editor, isDuplicate); - if (!isDuplicate) { + bool isNewDocument = false; + d->m_editorModel->addEditor(editor, &isNewDocument); + if (isNewDocument) { const bool isTemporary = editor->isTemporary(); const bool addWatcher = !isTemporary; DocumentManager::addDocument(editor->document(), addWatcher); @@ -2035,14 +1963,14 @@ void EditorManager::updateMakeWritableWarning() } } -void EditorManager::setupSaveActions(IEditor *editor, QAction *saveAction, QAction *saveAsAction, QAction *revertToSavedAction) +void EditorManager::setupSaveActions(IDocument *document, QAction *saveAction, QAction *saveAsAction, QAction *revertToSavedAction) { - saveAction->setEnabled(editor != 0 && editor->document()->isModified()); - saveAsAction->setEnabled(editor != 0 && editor->document()->isSaveAsAllowed()); - revertToSavedAction->setEnabled(editor != 0 - && !editor->document()->filePath().isEmpty() && editor->document()->isModified()); + saveAction->setEnabled(document != 0 && document->isModified()); + saveAsAction->setEnabled(document != 0 && document->isSaveAsAllowed()); + revertToSavedAction->setEnabled(document != 0 + && !document->filePath().isEmpty() && document->isModified()); - const QString documentName = editor ? editor->document()->displayName() : QString(); + const QString documentName = document ? document->displayName() : QString(); QString quotedName; if (!documentName.isEmpty()) { @@ -2056,11 +1984,12 @@ void EditorManager::setupSaveActions(IEditor *editor, QAction *saveAction, QActi void EditorManager::updateActions() { IEditor *curEditor = currentEditor(); + IDocument *curDocument = curEditor ? curEditor->document() : 0; int openedCount = d->m_editorModel->openDocumentCount(); - if (curEditor) { + if (curDocument) { if (HostOsInfo::isMacHost()) - window()->setWindowModified(curEditor->document()->isModified()); + window()->setWindowModified(curDocument->isModified()); updateMakeWritableWarning(); } else /* curEditor */ if (HostOsInfo::isMacHost()) { window()->setWindowModified(false); @@ -2070,11 +1999,11 @@ void EditorManager::updateActions() setCloseSplitEnabled(root, root->isSplitter()); QString quotedName; - if (curEditor) - quotedName = QLatin1Char('"') + curEditor->document()->displayName() + QLatin1Char('"'); - setupSaveActions(curEditor, d->m_saveAction, d->m_saveAsAction, d->m_revertToSavedAction); + if (curDocument) + quotedName = QLatin1Char('"') + curDocument->displayName() + QLatin1Char('"'); + setupSaveActions(curDocument, d->m_saveAction, d->m_saveAsAction, d->m_revertToSavedAction); - d->m_closeCurrentEditorAction->setEnabled(curEditor != 0); + d->m_closeCurrentEditorAction->setEnabled(curDocument); d->m_closeCurrentEditorAction->setText(tr("Close %1").arg(quotedName)); d->m_closeAllEditorsAction->setEnabled(openedCount > 0); d->m_closeOtherEditorsAction->setEnabled(openedCount > 1); @@ -2139,9 +2068,10 @@ QList EditorManager::visibleEditors() const return editors; } +// TODO code using this should probably better use OpenEditorsModel QList EditorManager::openedEditors() const { - return d->m_editorModel->editors(); + return d->m_editorModel->oneEditorForEachDocument(); } OpenEditorsModel *EditorManager::openedEditorsModel() const @@ -2232,15 +2162,22 @@ QByteArray EditorManager::saveState() const QList entries = d->m_editorModel->entries(); int entriesCount = 0; foreach (OpenEditorsModel::Entry *entry, entries) { + // TODO: isTemporary should move to IDocument + IEditor *editor = entry->document + ? d->m_editorModel->editorsForDocument(entry->document).first() + : 0; // The editor may be 0 if it was not loaded yet: In that case it is not temporary - if (!entry->editor || !entry->editor->isTemporary()) + if (!editor || !editor->isTemporary()) ++entriesCount; } stream << entriesCount; foreach (OpenEditorsModel::Entry *entry, entries) { - if (!entry->editor || !entry->editor->isTemporary()) + IEditor *editor = entry->document + ? d->m_editorModel->editorsForDocument(entry->document).first() + : 0; + if (!editor || !editor->isTemporary()) stream << entry->fileName() << entry->displayName() << entry->id(); } @@ -2352,17 +2289,18 @@ void EditorManager::readSettings() void EditorManager::revertToSaved() { - revertToSaved(currentEditor()); + IEditor *editor = currentEditor(); + revertToSaved(editor ? editor->document() : 0); } -void EditorManager::revertToSaved(Core::IEditor *editor) +void EditorManager::revertToSaved(Core::IDocument *document) { - if (!editor) + if (!document) return; - const QString fileName = editor->document()->filePath(); + const QString fileName = document->filePath(); if (fileName.isEmpty()) return; - if (editor->document()->isModified()) { + if (document->isModified()) { QMessageBox msgBox(QMessageBox::Question, tr("Revert to Saved"), tr("You will lose your current changes if you proceed reverting %1.").arg(QDir::toNativeSeparators(fileName)), QMessageBox::Yes|QMessageBox::No, ICore::mainWindow()); @@ -2375,7 +2313,7 @@ void EditorManager::revertToSaved(Core::IEditor *editor) } QString errorString; - if (!editor->document()->reload(&errorString, IDocument::FlagReload, IDocument::TypeContents)) + if (!document->reload(&errorString, IDocument::FlagReload, IDocument::TypeContents)) QMessageBox::critical(ICore::mainWindow(), tr("File Error"), errorString); } @@ -2444,7 +2382,7 @@ Core::IEditor *EditorManager::duplicateEditor(Core::IEditor *editor) IEditor *duplicate = editor->duplicate(0); duplicate->restoreState(editor->saveState()); emit editorCreated(duplicate, duplicate->document()->filePath()); - addEditor(duplicate, true); + addEditor(duplicate); return duplicate; } diff --git a/src/plugins/coreplugin/editormanager/editormanager.h b/src/plugins/coreplugin/editormanager/editormanager.h index 0102abb1e92..35c70bf62c3 100644 --- a/src/plugins/coreplugin/editormanager/editormanager.h +++ b/src/plugins/coreplugin/editormanager/editormanager.h @@ -136,9 +136,8 @@ public: OpenEditorsModel *openedEditorsModel() const; void closeEditor(OpenEditorsModel::Entry *entry); - void closeOtherEditors(IEditor *editor); + void closeOtherEditors(IDocument *document); - QList editorsForDocuments(QList documents) const; void addCurrentPositionToNavigationHistory(IEditor *editor = 0, const QByteArray &saveState = QByteArray()); void cutForwardNavigationHistory(); @@ -203,7 +202,7 @@ public slots: bool saveDocument(Core::IDocument *documentParam = 0); bool saveDocumentAs(Core::IDocument *documentParam = 0); void revertToSaved(); - void revertToSaved(Core::IEditor *editor); + void revertToSaved(IDocument *document); void closeEditor(); void closeOtherEditors(); void doEscapeKeyFocusMoveMagic(); @@ -251,9 +250,8 @@ private: virtual ~EditorManager(); void init(); - QList documentsForEditors(QList editors) const; static IEditor *createEditor(const Id &id = Id(), const QString &fileName = QString()); - void addEditor(IEditor *editor, bool isDuplicate = false); + void addEditor(IEditor *editor); void removeEditor(IEditor *editor); void restoreEditorState(IEditor *editor); @@ -273,7 +271,6 @@ private: static Internal::SplitterOrView *findRoot(const Internal::EditorView *view, int *rootIndex = 0); void closeEditor(IEditor *editor); - void closeDuplicate(IEditor *editor); void closeView(Internal::EditorView *view); void emptyView(Internal::EditorView *view); static void splitNewWindow(Internal::EditorView *view); @@ -282,7 +279,7 @@ private: void updateAutoSave(); void setCloseSplitEnabled(Internal::SplitterOrView *splitterOrView, bool enable); void updateMakeWritableWarning(); - void setupSaveActions(IEditor *editor, QAction *saveAction, QAction *saveAsAction, QAction *revertToSavedAction); + void setupSaveActions(IDocument *document, QAction *saveAction, QAction *saveAsAction, QAction *revertToSavedAction); friend class Core::Internal::MainWindow; friend class Core::Internal::SplitterOrView; diff --git a/src/plugins/coreplugin/editormanager/openeditorsmodel.cpp b/src/plugins/coreplugin/editormanager/openeditorsmodel.cpp index 33efe4032c6..e5ebf33f67f 100644 --- a/src/plugins/coreplugin/editormanager/openeditorsmodel.cpp +++ b/src/plugins/coreplugin/editormanager/openeditorsmodel.cpp @@ -45,8 +45,8 @@ struct OpenEditorsModelPrivate const QIcon m_lockedIcon; const QIcon m_unlockedIcon; - QList m_editors; - QList m_duplicateEditors; + QList m_documents; + QMap > m_editors; }; OpenEditorsModelPrivate::OpenEditorsModelPrivate() : @@ -56,7 +56,7 @@ OpenEditorsModelPrivate::OpenEditorsModelPrivate() : } OpenEditorsModel::Entry::Entry() : - editor(0) + document(0) { } @@ -81,16 +81,16 @@ QIcon OpenEditorsModel::unlockedIcon() const } QString OpenEditorsModel::Entry::fileName() const { - return editor ? editor->document()->filePath() : m_fileName; + return document ? document->filePath() : m_fileName; } QString OpenEditorsModel::Entry::displayName() const { - return editor ? editor->document()->displayName() : m_displayName; + return document ? document->displayName() : m_displayName; } Id OpenEditorsModel::Entry::id() const { - return editor ? editor->id() : m_id; + return m_id; } int OpenEditorsModel::columnCount(const QModelIndex &parent) const @@ -103,31 +103,32 @@ int OpenEditorsModel::columnCount(const QModelIndex &parent) const int OpenEditorsModel::rowCount(const QModelIndex &parent) const { if (!parent.isValid()) - return d->m_editors.count() + 1/**/; + return d->m_documents.count() + 1/**/; return 0; } -QList OpenEditorsModel::editors() const +// TODO remove +QList OpenEditorsModel::oneEditorForEachDocument() const { QList result; - foreach (const Entry *entry, d->m_editors) - if (entry->editor) - result += entry->editor; + QMapIterator > it(d->m_editors); + while (it.hasNext()) + result << it.next().value().first(); return result; } -void OpenEditorsModel::addEditor(IEditor *editor, bool isDuplicate) +void OpenEditorsModel::addEditor(IEditor *editor, bool *isNewDocument) { if (!editor) return; - if (isDuplicate) { - d->m_duplicateEditors.append(editor); - return; - } - + QList &editorList = d->m_editors[editor->document()]; + if (isNewDocument) + *isNewDocument = editorList.isEmpty(); + editorList << editor; Entry *entry = new Entry; - entry->editor = editor; + entry->document = editor->document(); + entry->m_id = editor->id(); addEntry(entry); } @@ -142,9 +143,9 @@ void OpenEditorsModel::addRestoredEditor(const QString &fileName, const QString OpenEditorsModel::Entry *OpenEditorsModel::firstRestoredEditor() const { - for (int i = 0; i < d->m_editors.count(); ++i) - if (!d->m_editors.at(i)->editor) - return d->m_editors.at(i); + for (int i = 0; i < d->m_documents.count(); ++i) + if (!d->m_documents.at(i)->document) + return d->m_documents.at(i); return 0; } @@ -152,43 +153,34 @@ void OpenEditorsModel::addEntry(Entry *entry) { QString fileName = entry->fileName(); + // replace a non-loaded entry (aka 'restored') if possible int previousIndex = findFileName(fileName); if (previousIndex >= 0) { - if (entry->editor && d->m_editors.at(previousIndex)->editor == 0) { - d->m_editors[previousIndex] = entry; - connect(entry->editor->document(), SIGNAL(changed()), this, SLOT(itemChanged())); + if (entry->document && d->m_documents.at(previousIndex)->document == 0) { + d->m_documents[previousIndex] = entry; + connect(entry->document, SIGNAL(changed()), this, SLOT(itemChanged())); } return; } int index; QString displayName = entry->displayName(); - for (index = 0; index < d->m_editors.count(); ++index) { - if (displayName < d->m_editors.at(index)->displayName()) + for (index = 0; index < d->m_documents.count(); ++index) { + if (displayName < d->m_documents.at(index)->displayName()) break; } int row = index + 1/**/; beginInsertRows(QModelIndex(), row, row); - d->m_editors.insert(index, entry); - if (entry->editor) - connect(entry->editor->document(), SIGNAL(changed()), this, SLOT(itemChanged())); + d->m_documents.insert(index, entry); + if (entry->document) + connect(entry->document, SIGNAL(changed()), this, SLOT(itemChanged())); endInsertRows(); } int OpenEditorsModel::findDocument(IDocument *document) const { - for (int i = 0; i < d->m_editors.count(); ++i) { - IEditor *editor = d->m_editors.at(i)->editor; - if (editor && editor->document() == document) - return i; - } - return -1; -} - -int OpenEditorsModel::findEditor(IEditor *editor) const -{ - for (int i = 0; i < d->m_editors.count(); ++i) - if (d->m_editors.at(i)->editor == editor) + for (int i = 0; i < d->m_documents.count(); ++i) + if (d->m_documents.at(i)->document == document) return i; return -1; } @@ -197,8 +189,8 @@ int OpenEditorsModel::findFileName(const QString &filename) const { if (filename.isEmpty()) return -1; - for (int i = 0; i < d->m_editors.count(); ++i) { - if (d->m_editors.at(i)->fileName() == filename) + for (int i = 0; i < d->m_documents.count(); ++i) { + if (d->m_documents.at(i)->fileName() == filename) return i; } return -1; @@ -206,116 +198,107 @@ int OpenEditorsModel::findFileName(const QString &filename) const void OpenEditorsModel::removeEntry(OpenEditorsModel::Entry *entry) { - int index = d->m_editors.indexOf(entry); - removeEditor(index); + QTC_ASSERT(!entry->document, return); // we wouldn't know what to do with the associated editors + int index = d->m_documents.indexOf(entry); + removeDocument(index); } -void OpenEditorsModel::removeEditor(IEditor *editor) +void OpenEditorsModel::removeEditor(IEditor *editor, bool *lastOneForDocument) { - d->m_duplicateEditors.removeAll(editor); - removeEditor(findEditor(editor)); + if (lastOneForDocument) + *lastOneForDocument = false; + QTC_ASSERT(editor, return); + IDocument *document = editor->document(); + QTC_ASSERT(d->m_editors.contains(document), return); + d->m_editors[document].removeAll(editor); + if (d->m_editors.value(document).isEmpty()) { + if (lastOneForDocument) + *lastOneForDocument = true; + d->m_editors.remove(document); + removeDocument(findDocument(document)); + } } -void OpenEditorsModel::removeEditor(const QString &fileName) +void OpenEditorsModel::removeDocument(const QString &fileName) { - removeEditor(findFileName(fileName)); + int index = findFileName(fileName); + QTC_ASSERT(!d->m_documents.at(index)->document, return); // we wouldn't know what to do with the associated editors + removeDocument(index); } -void OpenEditorsModel::removeEditor(int idx) +void OpenEditorsModel::removeDocument(int idx) { if (idx < 0) return; - QTC_ASSERT(idx < d->m_editors.size(), return); - IEditor *editor = d->m_editors.at(idx)->editor; + QTC_ASSERT(idx < d->m_documents.size(), return); + IDocument *document = d->m_documents.at(idx)->document; int row = idx + 1/**/; beginRemoveRows(QModelIndex(), row, row); - d->m_editors.removeAt(idx); + d->m_documents.removeAt(idx); endRemoveRows(); - if (editor) - disconnect(editor->document(), SIGNAL(changed()), this, SLOT(itemChanged())); + if (document) + disconnect(document, SIGNAL(changed()), this, SLOT(itemChanged())); } void OpenEditorsModel::removeAllRestoredEditors() { - for (int i = d->m_editors.count()-1; i >= 0; --i) { - if (!d->m_editors.at(i)->editor) { + for (int i = d->m_documents.count()-1; i >= 0; --i) { + if (!d->m_documents.at(i)->document) { int row = i + 1/**/; beginRemoveRows(QModelIndex(), row, row); - d->m_editors.removeAt(i); + d->m_documents.removeAt(i); endRemoveRows(); } } } -bool OpenEditorsModel::isDuplicate(IEditor *editor) const +QList OpenEditorsModel::editorsForDocument(IDocument *document) const { - return editor && d->m_duplicateEditors.contains(editor); + return d->m_editors.value(document); } -IEditor *OpenEditorsModel::originalForDuplicate(IEditor *duplicate) const -{ - IDocument *document = duplicate->document(); - foreach (const Entry *e, d->m_editors) - if (e->editor && e->editor->document() == document) - return e->editor; - return 0; -} - -QList OpenEditorsModel::duplicatesFor(IEditor *editor) const +QList OpenEditorsModel::editorsForDocuments(const QList &documents) const { QList result; - IDocument *document = editor->document(); - foreach (IEditor *e, d->m_duplicateEditors) - if (e->document() == document) - result += e; + foreach (IDocument *document, documents) + result += d->m_editors.value(document); return result; } -void OpenEditorsModel::makeOriginal(IEditor *duplicate) +int OpenEditorsModel::indexOfDocument(IDocument *document) const { - Q_ASSERT(duplicate && isDuplicate(duplicate)); - IEditor *original = originalForDuplicate(duplicate); - Q_ASSERT(original); - int i = findEditor(original); - d->m_editors[i]->editor = duplicate; - d->m_duplicateEditors.removeOne(duplicate); - d->m_duplicateEditors.append(original); -} - -int OpenEditorsModel::indexOfEditor(IEditor *editor) const -{ - if (!editor) + if (!document) return -1; - return findEditor(editor); + return findDocument(document); } QModelIndex OpenEditorsModel::index(int row, int column, const QModelIndex &parent) const { Q_UNUSED(parent) - if (column < 0 || column > 1 || row < 0 || row >= d->m_editors.count() + 1/**/) + if (column < 0 || column > 1 || row < 0 || row >= d->m_documents.count() + 1/**/) return QModelIndex(); return createIndex(row, column); } OpenEditorsModel::Entry *OpenEditorsModel::entryAtRow(int row) const { - int editorIndex = row - 1/**/; - if (editorIndex < 0) + int entryIndex = row - 1/**/; + if (entryIndex < 0) return 0; - return d->m_editors[editorIndex]; + return d->m_documents[entryIndex]; } int OpenEditorsModel::openDocumentCount() const { - return d->m_editors.count(); + return d->m_documents.count(); } QVariant OpenEditorsModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || (index.column() != 0 && role < Qt::UserRole)) return QVariant(); - int editorIndex = index.row() - 1/**/; - if (editorIndex < 0) { + int entryIndex = index.row() - 1/**/; + if (entryIndex < 0) { // entry switch (role) { case Qt::DisplayRole: @@ -326,19 +309,19 @@ QVariant OpenEditorsModel::data(const QModelIndex &index, int role) const return QVariant(); } } - const Entry *e = d->m_editors.at(editorIndex); + const Entry *e = d->m_documents.at(entryIndex); switch (role) { case Qt::DisplayRole: - return (e->editor && e->editor->document()->isModified()) + return (e->document && e->document->isModified()) ? e->displayName() + QLatin1Char('*') : e->displayName(); case Qt::DecorationRole: { bool showLock = false; - if (e->editor) { - showLock = e->editor->document()->filePath().isEmpty() + if (e->document) { + showLock = e->document->filePath().isEmpty() ? false - : e->editor->document()->isFileReadOnly(); + : e->document->isFileReadOnly(); } else { showLock = !QFileInfo(e->m_fileName).isWritable(); } @@ -348,23 +331,17 @@ QVariant OpenEditorsModel::data(const QModelIndex &index, int role) const return e->fileName().isEmpty() ? e->displayName() : QDir::toNativeSeparators(e->fileName()); - case Qt::UserRole: - return qVariantFromValue(e->editor); - case Qt::UserRole + 1: - return e->fileName(); - case Qt::UserRole + 2: - return QVariant::fromValue(e->editor ? Core::Id(e->editor->id()) : e->id()); default: return QVariant(); } return QVariant(); } -int OpenEditorsModel::rowOfEditor(IEditor *editor) const +int OpenEditorsModel::rowOfDocument(IDocument *document) const { - if (!editor) + if (!document) return 0 /**/; - return findEditor(originalForDuplicate(editor)) + 1/**/; + return findDocument(document) + 1/**/; } void OpenEditorsModel::itemChanged() @@ -380,7 +357,7 @@ void OpenEditorsModel::itemChanged() QList OpenEditorsModel::entries() const { - return d->m_editors; + return d->m_documents; } } // namespace Core diff --git a/src/plugins/coreplugin/editormanager/openeditorsmodel.h b/src/plugins/coreplugin/editormanager/openeditorsmodel.h index 0df21296c69..4ff45592284 100644 --- a/src/plugins/coreplugin/editormanager/openeditorsmodel.h +++ b/src/plugins/coreplugin/editormanager/openeditorsmodel.h @@ -62,7 +62,7 @@ public: struct CORE_EXPORT Entry { Entry(); - IEditor *editor; + IDocument *document; QString fileName() const; QString displayName() const; Id id() const; @@ -72,38 +72,34 @@ public: }; Entry *entryAtRow(int row) const; - int rowOfEditor(IEditor *editor) const; + int rowOfDocument(IDocument *document) const; int openDocumentCount() const; - void addEditor(IEditor *editor, bool isDuplicate = false); - void addRestoredEditor(const QString &fileName, const QString &displayName, const Id &id); - Entry *firstRestoredEditor() const; - QList entries() const; + QList editorsForDocument(IDocument *document) const; + QList editorsForDocuments(const QList &documents) const; + QList oneEditorForEachDocument() const; + int indexOfDocument(IDocument *document) const; + + // editor manager related methods, nobody else should call it + void addEditor(IEditor *editor, bool *isNewDocument); + void addRestoredEditor(const QString &fileName, const QString &displayName, const Id &id); + Entry *firstRestoredEditor() const; void removeEntry(Entry *entry); - void removeEditor(IEditor *editor); - void removeEditor(const QString &fileName); - + void removeEditor(IEditor *editor, bool *lastOneForDocument); + void removeDocument(const QString &fileName); void removeAllRestoredEditors(); - QList editors() const; - bool isDuplicate(IEditor *editor) const; - QList duplicatesFor(IEditor *editor) const; - IEditor *originalForDuplicate(IEditor *duplicate) const; - void makeOriginal(IEditor *duplicate); - int indexOfEditor(IEditor *editor) const; - private slots: void itemChanged(); private: void addEntry(Entry *entry); int findDocument(IDocument *document) const; - int findEditor(IEditor *editor) const; int findFileName(const QString &filename) const; - void removeEditor(int idx); + void removeDocument(int idx); OpenEditorsModelPrivate *d; }; diff --git a/src/plugins/coreplugin/editormanager/openeditorsview.cpp b/src/plugins/coreplugin/editormanager/openeditorsview.cpp index 46cb926c68e..cf9ce1c679d 100644 --- a/src/plugins/coreplugin/editormanager/openeditorsview.cpp +++ b/src/plugins/coreplugin/editormanager/openeditorsview.cpp @@ -29,6 +29,7 @@ #include "openeditorsview.h" #include "editormanager.h" +#include "ieditor.h" #include "openeditorsmodel.h" #include @@ -126,8 +127,9 @@ OpenEditorsWidget::~OpenEditorsWidget() void OpenEditorsWidget::updateCurrentItem(Core::IEditor *editor) { + IDocument *document = editor ? editor->document() : 0; EditorManager *em = EditorManager::instance(); - QModelIndex index = m_model->index(em->openedEditorsModel()->indexOfEditor(editor), 0); + QModelIndex index = m_model->index(em->openedEditorsModel()->indexOfDocument(document), 0); if (!index.isValid()) { clearSelection(); return; diff --git a/src/plugins/coreplugin/editormanager/openeditorswindow.cpp b/src/plugins/coreplugin/editormanager/openeditorswindow.cpp index 0b1629dade4..a43166e6d49 100644 --- a/src/plugins/coreplugin/editormanager/openeditorswindow.cpp +++ b/src/plugins/coreplugin/editormanager/openeditorswindow.cpp @@ -204,7 +204,7 @@ void OpenEditorsWindow::setEditors(const QList &globalHistory, Edi // add purely restored editors which are not initialised yet foreach (OpenEditorsModel::Entry *entry, model->entries()) { - if (entry->editor) + if (entry->document) continue; QTreeWidgetItem *item = new QTreeWidgetItem(); QString title = entry->displayName(); @@ -229,7 +229,7 @@ void OpenEditorsWindow::selectEditor(QTreeWidgetItem *item) } else { if (!EditorManager::openEditor( item->toolTip(0), item->data(0, Qt::UserRole+2).value())) { - EditorManager::instance()->openedEditorsModel()->removeEditor(item->toolTip(0)); + EditorManager::instance()->openedEditorsModel()->removeDocument(item->toolTip(0)); delete item; } } diff --git a/src/plugins/coreplugin/editortoolbar.cpp b/src/plugins/coreplugin/editortoolbar.cpp index 905d3e68ecf..3fb29a3a2e7 100644 --- a/src/plugins/coreplugin/editortoolbar.cpp +++ b/src/plugins/coreplugin/editortoolbar.cpp @@ -290,7 +290,8 @@ void EditorToolBar::setToolbarCreationFlags(ToolbarCreationFlags flags) void EditorToolBar::setCurrentEditor(IEditor *editor) { - d->m_editorList->setCurrentIndex(d->m_editorsListModel->rowOfEditor(editor)); + IDocument *document = editor ? editor->document() : 0; + d->m_editorList->setCurrentIndex(d->m_editorsListModel->rowOfDocument(document)); // If we never added the toolbar from the editor, we will never change // the editor, so there's no need to update the toolbar either. @@ -303,7 +304,7 @@ void EditorToolBar::setCurrentEditor(IEditor *editor) void EditorToolBar::updateEditorListSelection(IEditor *newSelection) { if (newSelection) - d->m_editorList->setCurrentIndex(d->m_editorsListModel->rowOfEditor(newSelection)); + d->m_editorList->setCurrentIndex(d->m_editorsListModel->rowOfDocument(newSelection->document())); } void EditorToolBar::changeActiveEditor(int row) @@ -375,7 +376,7 @@ void EditorToolBar::updateEditorStatus(IEditor *editor) return; } - d->m_editorList->setCurrentIndex(d->m_editorsListModel->rowOfEditor(editor)); + d->m_editorList->setCurrentIndex(d->m_editorsListModel->rowOfDocument(editor->document())); if (editor->document()->filePath().isEmpty()) { d->m_lockButton->setIcon(QIcon()); diff --git a/src/plugins/fakevim/fakevimplugin.cpp b/src/plugins/fakevim/fakevimplugin.cpp index 1e68d0e6019..6261728c458 100644 --- a/src/plugins/fakevim/fakevimplugin.cpp +++ b/src/plugins/fakevim/fakevimplugin.cpp @@ -1999,7 +1999,9 @@ int FakeVimPluginPrivate::currentFile() const { OpenEditorsModel *model = EditorManager::instance()->openedEditorsModel(); IEditor *editor = EditorManager::currentEditor(); - return model->indexOfEditor(editor); + if (!editor) + return -1; + return model->indexOfDocument(editor->document()); } void FakeVimPluginPrivate::switchToFile(int n)