From 97fa0c7f2d5b46c434d09028c05343b4092636b8 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 5 Jul 2020 10:19:49 -0400 Subject: [PATCH] CVirtualDirectoryModel: Return std::optional from GetProposedIndex() Same behavior, minus out parameters. --- .../CVirtualDirectoryModel.cpp | 48 +++++++++---------- .../ResourceBrowser/CVirtualDirectoryModel.h | 5 +- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/Editor/ResourceBrowser/CVirtualDirectoryModel.cpp b/src/Editor/ResourceBrowser/CVirtualDirectoryModel.cpp index fcd0b142..5ab8ed90 100644 --- a/src/Editor/ResourceBrowser/CVirtualDirectoryModel.cpp +++ b/src/Editor/ResourceBrowser/CVirtualDirectoryModel.cpp @@ -241,7 +241,7 @@ void CVirtualDirectoryModel::SetRoot(CVirtualDirectory *pDir) endResetModel(); } -bool CVirtualDirectoryModel::GetProposedIndex(const QString& Path, QModelIndex& rOutParent, int& rOutRow) +std::optional> CVirtualDirectoryModel::GetProposedIndex(const QString& Path) { // Get parent path TString FullPath = TO_TSTRING(Path); @@ -249,43 +249,43 @@ bool CVirtualDirectoryModel::GetProposedIndex(const QString& Path, QModelIndex& if (FullPath.EndsWith('/') || FullPath.EndsWith('\\')) FullPath = FullPath.ChopBack(1); - uint32 LastSlash = FullPath.LastIndexOf("\\/"); - TString ParentPath = FullPath.ChopBack( FullPath.Size() - LastSlash ); + const uint32 LastSlash = FullPath.LastIndexOf("\\/"); + const TString ParentPath = FullPath.ChopBack( FullPath.Size() - LastSlash ); // Find parent index - CVirtualDirectory *pParent = (ParentPath.IsEmpty() ? mpRoot : mpRoot->FindChildDirectory(ParentPath, false)); - if (!pParent) return false; + CVirtualDirectory* pParent = (ParentPath.IsEmpty() ? mpRoot : mpRoot->FindChildDirectory(ParentPath, false)); + if (pParent == nullptr) + return std::nullopt; - QModelIndex ParentIndex = GetIndexForDirectory(pParent); - if (!ParentIndex.isValid()) return false; + const QModelIndex ParentIndex = GetIndexForDirectory(pParent); + if (!ParentIndex.isValid()) + return std::nullopt; // Determine the row number that the new directory will be inserted at - QString DirName = TO_QSTRING(FullPath.ChopFront( LastSlash + 1 )); - int NumRows = rowCount(ParentIndex); + const QString DirName = TO_QSTRING(FullPath.ChopFront( LastSlash + 1 )); + const int NumRows = rowCount(ParentIndex); int RowIdx = 0; for (; RowIdx < NumRows; RowIdx++) { - QModelIndex Index = index(RowIdx, 0, ParentIndex); - QString OtherName = data(Index, Qt::DisplayRole).toString(); + const QModelIndex Index = index(RowIdx, 0, ParentIndex); + const QString OtherName = data(Index, Qt::DisplayRole).toString(); if (QString::compare(DirName, OtherName, Qt::CaseInsensitive) < 0) break; } - rOutParent = ParentIndex; - rOutRow = RowIdx; - return true; + return std::make_pair(ParentIndex, RowIdx); } void CVirtualDirectoryModel::OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir, const QString& NewPath) { - QModelIndex Parent; - int Row; + const auto indexOptional = GetProposedIndex(NewPath); - if (!GetProposedIndex(NewPath, Parent, Row)) + if (!indexOptional) return; + const auto [Parent, Row] = *indexOptional; const QModelIndex OldIndex = GetIndexForDirectory(pDir); const QModelIndex OldParent = OldIndex.parent(); const int OldRow = OldIndex.row(); @@ -304,14 +304,14 @@ void CVirtualDirectoryModel::OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir, void CVirtualDirectoryModel::OnDirectoryAboutToBeCreated(const QString& DirPath) { - QModelIndex Parent; - int Row; + const auto indexOptional = GetProposedIndex(DirPath); - if (GetProposedIndex(DirPath, Parent, Row)) - { - beginInsertRows(Parent, Row, Row); - mInsertingRows = true; - } + if (!indexOptional) + return; + + const auto [Parent, Row] = *indexOptional; + beginInsertRows(Parent, Row, Row); + mInsertingRows = true; } void CVirtualDirectoryModel::OnDirectoryAboutToBeDeleted(CVirtualDirectory *pDir) diff --git a/src/Editor/ResourceBrowser/CVirtualDirectoryModel.h b/src/Editor/ResourceBrowser/CVirtualDirectoryModel.h index 19d2fbd3..9e2178fe 100644 --- a/src/Editor/ResourceBrowser/CVirtualDirectoryModel.h +++ b/src/Editor/ResourceBrowser/CVirtualDirectoryModel.h @@ -6,6 +6,9 @@ #include #include +#include +#include + class CVirtualDirectoryModel : public QAbstractItemModel { Q_OBJECT @@ -37,7 +40,7 @@ public: void SetRoot(CVirtualDirectory *pDir); protected: - bool GetProposedIndex(const QString& Path, QModelIndex& rOutParent, int& rOutRow); + std::optional> GetProposedIndex(const QString& Path); public slots: void OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir, const QString& NewPath);