diff --git a/src/plugins/coreplugin/editormanager/documentmodel.cpp b/src/plugins/coreplugin/editormanager/documentmodel.cpp index 79652c1d380..ea3e53f492f 100644 --- a/src/plugins/coreplugin/editormanager/documentmodel.cpp +++ b/src/plugins/coreplugin/editormanager/documentmodel.cpp @@ -113,6 +113,17 @@ private: QHash m_entryByFixedPath; }; +class RestoredDocument : public IDocument +{ +public: + bool save(QString *, const QString &, bool) { return false; } + QString defaultPath() const { return filePath().toFileInfo().absolutePath(); } + QString suggestedFileName() const { return filePath().fileName(); } + bool isModified() const { return false; } + bool isSaveAsAllowed() const { return false; } + bool reload(QString *, ReloadFlag, ChangeType) { return true; } +}; + DocumentModelPrivate::DocumentModelPrivate() : m_lockedIcon(QLatin1String(":/core/images/locked.png")), m_unlockedIcon(QLatin1String(":/core/images/unlocked.png")) @@ -127,10 +138,17 @@ DocumentModelPrivate::~DocumentModelPrivate() static DocumentModelPrivate *d; DocumentModel::Entry::Entry() : - document(0) + document(0), + isRestored(false) { } +DocumentModel::Entry::~Entry() +{ + if (isRestored) + delete document; +} + DocumentModel::DocumentModel() { } @@ -162,22 +180,22 @@ QAbstractItemModel *DocumentModel::model() Utils::FileName DocumentModel::Entry::fileName() const { - return document ? document->filePath() : m_fileName; + return document->filePath(); } QString DocumentModel::Entry::displayName() const { - return document ? document->displayName() : m_displayName; + return document->displayName(); } QString DocumentModel::Entry::plainDisplayName() const { - return document ? document->plainDisplayName() : m_displayName; + return document->plainDisplayName(); } Id DocumentModel::Entry::id() const { - return document ? document->id() : m_id; + return document->id(); } int DocumentModelPrivate::columnCount(const QModelIndex &parent) const @@ -214,15 +232,17 @@ void DocumentModel::addEditor(IEditor *editor, bool *isNewDocument) void DocumentModel::addRestoredDocument(const QString &fileName, const QString &displayName, Id id) { Entry *entry = new Entry; - entry->m_fileName = Utils::FileName::fromString(fileName); - entry->m_displayName = displayName; - entry->m_id = id; + entry->document = new RestoredDocument; + entry->document->setFilePath(Utils::FileName::fromString(fileName)); + entry->document->setPreferredDisplayName(displayName); + entry->document->setId(id); + entry->isRestored = true; d->addEntry(entry); } DocumentModel::Entry *DocumentModel::firstRestoredEntry() { - return Utils::findOrDefault(d->m_entries, [](Entry *entry) { return !entry->document; }); + return Utils::findOrDefault(d->m_entries, [](Entry *entry) { return entry->isRestored; }); } void DocumentModelPrivate::addEntry(DocumentModel::Entry *entry) @@ -236,7 +256,7 @@ void DocumentModelPrivate::addEntry(DocumentModel::Entry *entry) int previousIndex = indexOfFilePath(fileName); if (previousIndex >= 0) { DocumentModel::Entry *previousEntry = m_entries.at(previousIndex); - const bool replace = entry->document && !previousEntry->document; + const bool replace = !entry->isRestored && previousEntry->isRestored; if (replace) { delete previousEntry; m_entries[previousIndex] = entry; @@ -268,8 +288,7 @@ void DocumentModelPrivate::addEntry(DocumentModel::Entry *entry) disambiguateDisplayNames(entry); if (!fixedPath.isEmpty()) m_entryByFixedPath[fixedPath] = entry; - if (entry->document) - connect(entry->document, SIGNAL(changed()), this, SLOT(itemChanged())); + connect(entry->document, SIGNAL(changed()), this, SLOT(itemChanged())); endInsertRows(); } @@ -282,8 +301,6 @@ bool DocumentModelPrivate::disambiguateDisplayNames(DocumentModel::Entry *entry) for (int i = 0, total = m_entries.count(); i < total; ++i) { DocumentModel::Entry *e = m_entries.at(i); - if (!e->document) - continue; if (e == entry || e->plainDisplayName() == displayName) { e->document->setUniqueDisplayName(QString()); dups += DynamicEntry(e); @@ -349,7 +366,8 @@ int DocumentModelPrivate::indexOfFilePath(const Utils::FileName &filePath) const void DocumentModel::removeEntry(DocumentModel::Entry *entry) { - QTC_ASSERT(!entry->document, return); // we wouldn't know what to do with the associated editors + // For non restored entries, we wouldn't know what to do with the associated editors + QTC_ASSERT(entry->isRestored, return); int index = d->m_entries.indexOf(entry); d->removeDocument(index); } @@ -373,7 +391,8 @@ void DocumentModel::removeEditor(IEditor *editor, bool *lastOneForDocument) void DocumentModel::removeDocument(const QString &fileName) { int index = d->indexOfFilePath(Utils::FileName::fromString(fileName)); - QTC_ASSERT(!d->m_entries.at(index)->document, return); // we wouldn't know what to do with the associated editors + // For non restored entries, we wouldn't know what to do with the associated editors + QTC_ASSERT(d->m_entries.at(index)->isRestored, return); d->removeDocument(index); } @@ -393,8 +412,7 @@ void DocumentModelPrivate::removeDocument(int idx) DocumentManager::ResolveLinks); m_entryByFixedPath.remove(fixedPath); } - if (IDocument *document = entry->document) - disconnect(document, SIGNAL(changed()), this, SLOT(itemChanged())); + disconnect(entry->document, SIGNAL(changed()), this, SLOT(itemChanged())); disambiguateDisplayNames(entry); delete entry; } @@ -402,7 +420,7 @@ void DocumentModelPrivate::removeDocument(int idx) void DocumentModel::removeAllRestoredEntries() { for (int i = d->m_entries.count()-1; i >= 0; --i) { - if (!d->m_entries.at(i)->document) { + if (d->m_entries.at(i)->isRestored) { int row = i + 1/**/; d->beginRemoveRows(QModelIndex(), row, row); delete d->m_entries.takeAt(i); @@ -527,19 +545,12 @@ QVariant DocumentModelPrivate::data(const QModelIndex &index, int role) const switch (role) { case Qt::DisplayRole: { QString name = e->displayName(); - if (e->document && e->document->isModified()) + if (e->document->isModified()) name += QLatin1Char('*'); return name; } case Qt::DecorationRole: - { - bool showLock = false; - if (e->document) - showLock = e->document->filePath().isEmpty() ? false : e->document->isFileReadOnly(); - else - showLock = !e->m_fileName.toFileInfo().isWritable(); - return showLock ? m_lockedIcon : QIcon(); - } + return e->document->isFileReadOnly() ? m_lockedIcon : QIcon(); case Qt::ToolTipRole: return e->fileName().isEmpty() ? e->displayName() : e->fileName().toUserOutput(); default: diff --git a/src/plugins/coreplugin/editormanager/documentmodel.h b/src/plugins/coreplugin/editormanager/documentmodel.h index 28d1f9348ea..b8c47a35b23 100644 --- a/src/plugins/coreplugin/editormanager/documentmodel.h +++ b/src/plugins/coreplugin/editormanager/documentmodel.h @@ -59,15 +59,15 @@ public: struct CORE_EXPORT Entry { Entry(); - IDocument *document; + ~Entry(); Utils::FileName fileName() const; QString displayName() const; QString plainDisplayName() const; QString uniqueDisplayName() const; Id id() const; - Utils::FileName m_fileName; - QString m_displayName; - Id m_id; + + IDocument *document; + bool isRestored; }; static Entry *entryAtRow(int row); diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp index 82a156ab421..95721873f41 100644 --- a/src/plugins/coreplugin/editormanager/editormanager.cpp +++ b/src/plugins/coreplugin/editormanager/editormanager.cpp @@ -1050,7 +1050,7 @@ void EditorManagerPrivate::activateEditorForEntry(EditorView *view, DocumentMode return; } IDocument *document = entry->document; - if (document) { + if (!entry->isRestored) { activateEditorForDocument(view, document, flags); return; } @@ -2031,10 +2031,10 @@ void EditorManager::closeDocument(DocumentModel::Entry *entry) { if (!entry) return; - if (entry->document) - closeDocuments(QList() << entry->document); - else + if (entry->isRestored) DocumentModel::removeEntry(entry); + else + closeDocuments(QList() << entry->document); } bool EditorManager::closeEditors(const QList &editorsToClose, bool askAboutModifiedEditors) @@ -2520,15 +2520,15 @@ QByteArray EditorManager::saveState() int entriesCount = 0; foreach (DocumentModel::Entry *entry, entries) { // The editor may be 0 if it was not loaded yet: In that case it is not temporary - if (!entry->document || !entry->document->isTemporary()) + if (!entry->document->isTemporary()) ++entriesCount; } stream << entriesCount; foreach (DocumentModel::Entry *entry, entries) { - if (!entry->document || !entry->document->isTemporary()) - stream << entry->fileName().toString() << entry->displayName() << entry->id(); + if (!entry->document->isTemporary()) + stream << entry->fileName().toString() << entry->plainDisplayName() << entry->id(); } stream << d->m_editorAreas.first()->saveState(); // TODO diff --git a/src/plugins/coreplugin/editormanager/openeditorswindow.cpp b/src/plugins/coreplugin/editormanager/openeditorswindow.cpp index b039cedaaa1..30ff1d4c001 100644 --- a/src/plugins/coreplugin/editormanager/openeditorswindow.cpp +++ b/src/plugins/coreplugin/editormanager/openeditorswindow.cpp @@ -249,7 +249,7 @@ void OpenEditorsWindow::addHistoryItems(const QList &history, Edit void OpenEditorsWindow::addRestoredItems() { foreach (DocumentModel::Entry *entry, DocumentModel::entries()) { - if (entry->document) + if (!entry->isRestored) continue; QTreeWidgetItem *item = new QTreeWidgetItem(); QString title = entry->displayName(); diff --git a/src/plugins/coreplugin/locator/opendocumentsfilter.cpp b/src/plugins/coreplugin/locator/opendocumentsfilter.cpp index 61b9c46117f..94a7fe5c151 100644 --- a/src/plugins/coreplugin/locator/opendocumentsfilter.cpp +++ b/src/plugins/coreplugin/locator/opendocumentsfilter.cpp @@ -73,13 +73,13 @@ QList OpenDocumentsFilter::matchesFor(QFutureInterfacedisplayName(); - entry.m_fileName = e->fileName(); + entry.displayName = e->displayName(); + entry.fileName = e->fileName(); m_editors.append(entry); } } -QList OpenDocumentsFilter::editors() const +QList OpenDocumentsFilter::editors() const { QMutexLocker lock(&m_mutex); Q_UNUSED(lock) return m_editors; diff --git a/src/plugins/coreplugin/locator/opendocumentsfilter.h b/src/plugins/coreplugin/locator/opendocumentsfilter.h index d77a77ef4d6..06008387e36 100644 --- a/src/plugins/coreplugin/locator/opendocumentsfilter.h +++ b/src/plugins/coreplugin/locator/opendocumentsfilter.h @@ -57,10 +57,17 @@ public slots: void refreshInternally(); private: - QList editors() const; + class Entry + { + public: + Utils::FileName fileName; + QString displayName; + }; + + QList editors() const; mutable QMutex m_mutex; - QList m_editors; + QList m_editors; }; } // namespace Internal