Document model: Use optional for "indexOf" kind of methods

Change-Id: Iaffbb0b695f96b5b44c9fd0df63891c2797181b7
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2018-02-14 12:05:55 +01:00
parent 64233a4fae
commit 3060ddbcf3
6 changed files with 60 additions and 39 deletions

View File

@@ -76,9 +76,8 @@ void DocumentModelPrivate::addEntry(DocumentModel::Entry *entry)
fixedPath = DocumentManager::filePathKey(fileName.toString(), DocumentManager::ResolveLinks); fixedPath = DocumentManager::filePathKey(fileName.toString(), DocumentManager::ResolveLinks);
// replace a non-loaded entry (aka 'suspended') if possible // replace a non-loaded entry (aka 'suspended') if possible
int previousIndex = indexOfFilePath(fileName); DocumentModel::Entry *previousEntry = DocumentModel::entryForFilePath(fileName);
if (previousIndex >= 0) { if (previousEntry) {
DocumentModel::Entry *previousEntry = m_entries.at(previousIndex);
const bool replace = !entry->isSuspended && previousEntry->isSuspended; const bool replace = !entry->isSuspended && previousEntry->isSuspended;
if (replace) { if (replace) {
previousEntry->isSuspended = false; previousEntry->isSuspended = false;
@@ -180,13 +179,16 @@ QIcon DocumentModelPrivate::lockedIcon()
return icon; return icon;
} }
int DocumentModelPrivate::indexOfFilePath(const Utils::FileName &filePath) const Utils::optional<int> DocumentModelPrivate::indexOfFilePath(const Utils::FileName &filePath) const
{ {
if (filePath.isEmpty()) if (filePath.isEmpty())
return -1; return Utils::nullopt;
const QString fixedPath = DocumentManager::filePathKey(filePath.toString(), const QString fixedPath = DocumentManager::filePathKey(filePath.toString(),
DocumentManager::ResolveLinks); DocumentManager::ResolveLinks);
return m_entries.indexOf(m_entryByFixedPath.value(fixedPath)); const int index = m_entries.indexOf(m_entryByFixedPath.value(fixedPath));
if (index < 0)
return Utils::nullopt;
return index;
} }
void DocumentModelPrivate::removeDocument(int idx) void DocumentModelPrivate::removeDocument(int idx)
@@ -210,11 +212,14 @@ void DocumentModelPrivate::removeDocument(int idx)
delete entry; delete entry;
} }
int DocumentModelPrivate::indexOfDocument(IDocument *document) const Utils::optional<int> DocumentModelPrivate::indexOfDocument(IDocument *document) const
{ {
return Utils::indexOf(m_entries, [&document](DocumentModel::Entry *entry) { const int index = Utils::indexOf(m_entries, [&document](DocumentModel::Entry *entry) {
return entry->document == document; return entry->document == document;
}); });
if (index < 0)
return Utils::nullopt;
return index;
} }
Qt::ItemFlags DocumentModelPrivate::flags(const QModelIndex &index) const Qt::ItemFlags DocumentModelPrivate::flags(const QModelIndex &index) const
@@ -292,14 +297,14 @@ void DocumentModelPrivate::itemChanged()
{ {
IDocument *document = qobject_cast<IDocument *>(sender()); IDocument *document = qobject_cast<IDocument *>(sender());
int idx = indexOfDocument(document); const Utils::optional<int> idx = indexOfDocument(document);
if (idx < 0) if (!idx)
return; return;
const QString fileName = document->filePath().toString(); const QString fileName = document->filePath().toString();
QString fixedPath; QString fixedPath;
if (!fileName.isEmpty()) if (!fileName.isEmpty())
fixedPath = DocumentManager::filePathKey(fileName, DocumentManager::ResolveLinks); fixedPath = DocumentManager::filePathKey(fileName, DocumentManager::ResolveLinks);
DocumentModel::Entry *entry = m_entries.at(idx); DocumentModel::Entry *entry = m_entries.at(idx.value());
bool found = false; bool found = false;
// The entry's fileName might have changed, so find the previous fileName that was associated // The entry's fileName might have changed, so find the previous fileName that was associated
// with it and remove it, then add the new fileName. // with it and remove it, then add the new fileName.
@@ -316,8 +321,8 @@ void DocumentModelPrivate::itemChanged()
} }
if (!found && !fixedPath.isEmpty()) if (!found && !fixedPath.isEmpty())
m_entryByFixedPath[fixedPath] = entry; m_entryByFixedPath[fixedPath] = entry;
if (!disambiguateDisplayNames(m_entries.at(idx))) { if (!disambiguateDisplayNames(m_entries.at(idx.value()))) {
QModelIndex mindex = index(idx + 1/*<no document>*/, 0); QModelIndex mindex = index(idx.value() + 1/*<no document>*/, 0);
emit dataChanged(mindex, mindex); emit dataChanged(mindex, mindex);
} }
} }
@@ -507,7 +512,7 @@ QList<IEditor *> DocumentModel::editorsForDocuments(const QList<IDocument *> &do
return result; return result;
} }
int DocumentModel::indexOfDocument(IDocument *document) Utils::optional<int> DocumentModel::indexOfDocument(IDocument *document)
{ {
return d->indexOfDocument(document); return d->indexOfDocument(document);
} }
@@ -520,10 +525,10 @@ DocumentModel::Entry *DocumentModel::entryForDocument(IDocument *document)
DocumentModel::Entry *DocumentModel::entryForFilePath(const Utils::FileName &filePath) DocumentModel::Entry *DocumentModel::entryForFilePath(const Utils::FileName &filePath)
{ {
const int index = d->indexOfFilePath(filePath); const Utils::optional<int> index = d->indexOfFilePath(filePath);
if (index < 0) if (!index)
return nullptr; return nullptr;
return d->m_entries.at(index); return d->m_entries.at(index.value());
} }
QList<IDocument *> DocumentModel::openedDocuments() QList<IDocument *> DocumentModel::openedDocuments()
@@ -533,10 +538,10 @@ QList<IDocument *> DocumentModel::openedDocuments()
IDocument *DocumentModel::documentForFilePath(const QString &filePath) IDocument *DocumentModel::documentForFilePath(const QString &filePath)
{ {
const int index = d->indexOfFilePath(Utils::FileName::fromString(filePath)); const Utils::optional<int> index = d->indexOfFilePath(Utils::FileName::fromString(filePath));
if (index < 0) if (!index)
return 0; return nullptr;
return d->m_entries.at(index)->document; return d->m_entries.at(index.value())->document;
} }
QList<IEditor *> DocumentModel::editorsForFilePath(const QString &filePath) QList<IEditor *> DocumentModel::editorsForFilePath(const QString &filePath)
@@ -560,11 +565,14 @@ int DocumentModel::entryCount()
return d->m_entries.count(); return d->m_entries.count();
} }
int DocumentModel::rowOfDocument(IDocument *document) Utils::optional<int> DocumentModel::rowOfDocument(IDocument *document)
{ {
if (!document) if (!document)
return 0 /*<no document>*/; return 0 /*<no document>*/;
return indexOfDocument(document) + 1/*<no document>*/; const Utils::optional<int> index = indexOfDocument(document);
if (index)
return index.value() + 1/*correction for <no document>*/;
return Utils::nullopt;
} }
QList<DocumentModel::Entry *> DocumentModel::entries() QList<DocumentModel::Entry *> DocumentModel::entries()

View File

@@ -29,6 +29,7 @@
#include "../id.h" #include "../id.h"
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/optional.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QAbstractItemModel; class QAbstractItemModel;
@@ -63,11 +64,11 @@ public:
}; };
static Entry *entryAtRow(int row); static Entry *entryAtRow(int row);
static int rowOfDocument(IDocument *document); static Utils::optional<int> rowOfDocument(IDocument *document);
static int entryCount(); static int entryCount();
static QList<Entry *> entries(); static QList<Entry *> entries();
static int indexOfDocument(IDocument *document); static Utils::optional<int> indexOfDocument(IDocument *document);
static Entry *entryForDocument(IDocument *document); static Entry *entryForDocument(IDocument *document);
static Entry *entryForFilePath(const Utils::FileName &filePath); static Entry *entryForFilePath(const Utils::FileName &filePath);
static QList<IDocument *> openedDocuments(); static QList<IDocument *> openedDocuments();

View File

@@ -58,8 +58,8 @@ public:
void addEntry(DocumentModel::Entry *entry); void addEntry(DocumentModel::Entry *entry);
void removeDocument(int idx); void removeDocument(int idx);
int indexOfFilePath(const Utils::FileName &filePath) const; Utils::optional<int> indexOfFilePath(const Utils::FileName &filePath) const;
int indexOfDocument(IDocument *document) const; Utils::optional<int> indexOfDocument(IDocument *document) const;
bool disambiguateDisplayNames(DocumentModel::Entry *entry); bool disambiguateDisplayNames(DocumentModel::Entry *entry);

View File

@@ -30,6 +30,7 @@
#include <coreplugin/actionmanager/actionmanager.h> #include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h> #include <coreplugin/actionmanager/command.h>
#include <utils/qtcassert.h>
#include <QApplication> #include <QApplication>
#include <QMenu> #include <QMenu>
@@ -70,13 +71,13 @@ OpenEditorsWidget::~OpenEditorsWidget()
void OpenEditorsWidget::updateCurrentItem(IEditor *editor) void OpenEditorsWidget::updateCurrentItem(IEditor *editor)
{ {
IDocument *document = editor ? editor->document() : 0; if (!editor) {
QModelIndex index = m_model->index(DocumentModel::indexOfDocument(document), 0);
if (!index.isValid()) {
clearSelection(); clearSelection();
return; return;
} }
setCurrentIndex(index); const Utils::optional<int> index = DocumentModel::indexOfDocument(editor->document());
if (QTC_GUARD(index))
setCurrentIndex(m_model->index(index.value(), 0));
selectionModel()->select(currentIndex(), selectionModel()->select(currentIndex(),
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
scrollTo(currentIndex()); scrollTo(currentIndex());

View File

@@ -320,7 +320,9 @@ void EditorToolBar::setMenuProvider(const EditorToolBar::MenuProvider &provider)
void EditorToolBar::setCurrentEditor(IEditor *editor) void EditorToolBar::setCurrentEditor(IEditor *editor)
{ {
IDocument *document = editor ? editor->document() : 0; IDocument *document = editor ? editor->document() : 0;
d->m_editorList->setCurrentIndex(DocumentModel::rowOfDocument(document)); const Utils::optional<int> index = DocumentModel::rowOfDocument(document);
if (QTC_GUARD(index))
d->m_editorList->setCurrentIndex(index.value());
// If we never added the toolbar from the editor, we will never change // 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. // the editor, so there's no need to update the toolbar either.
@@ -332,8 +334,11 @@ void EditorToolBar::setCurrentEditor(IEditor *editor)
void EditorToolBar::updateEditorListSelection(IEditor *newSelection) void EditorToolBar::updateEditorListSelection(IEditor *newSelection)
{ {
if (newSelection) if (newSelection) {
d->m_editorList->setCurrentIndex(DocumentModel::rowOfDocument(newSelection->document())); const Utils::optional<int> index = DocumentModel::rowOfDocument(newSelection->document());
if (QTC_GUARD(index))
d->m_editorList->setCurrentIndex(index.value());
}
} }
void EditorToolBar::changeActiveEditor(int row) void EditorToolBar::changeActiveEditor(int row)
@@ -403,7 +408,9 @@ void EditorToolBar::updateDocumentStatus(IDocument *document)
return; return;
} }
d->m_editorList->setCurrentIndex(DocumentModel::rowOfDocument(document)); const Utils::optional<int> index = DocumentModel::rowOfDocument(document);
if (QTC_GUARD(index))
d->m_editorList->setCurrentIndex(index.value());
if (document->filePath().isEmpty()) { if (document->filePath().isEmpty()) {
d->m_lockButton->setIcon(QIcon()); d->m_lockButton->setIcon(QIcon());

View File

@@ -73,6 +73,7 @@
#include <utils/hostosinfo.h> #include <utils/hostosinfo.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/pathchooser.h> #include <utils/pathchooser.h>
#include <utils/qtcassert.h>
#include <utils/savedaction.h> #include <utils/savedaction.h>
#include <utils/stylehelper.h> #include <utils/stylehelper.h>
@@ -2209,9 +2210,12 @@ void FakeVimPluginPrivate::highlightMatches(FakeVimHandler *, const QString &nee
int FakeVimPluginPrivate::currentFile() const int FakeVimPluginPrivate::currentFile() const
{ {
IEditor *editor = EditorManager::currentEditor(); IEditor *editor = EditorManager::currentEditor();
if (!editor) if (editor) {
const Utils::optional<int> index = DocumentModel::indexOfDocument(editor->document());
if (QTC_GUARD(index))
return index.value();
}
return -1; return -1;
return DocumentModel::indexOfDocument(editor->document());
} }
void FakeVimPluginPrivate::switchToFile(int n) void FakeVimPluginPrivate::switchToFile(int n)