CVirtualDirectoryModel: Return std::optional from GetProposedIndex()

Same behavior, minus out parameters.
This commit is contained in:
Lioncash 2020-07-05 10:19:49 -04:00
parent dcfb34ca1c
commit 97fa0c7f2d
2 changed files with 28 additions and 25 deletions

View File

@ -241,7 +241,7 @@ void CVirtualDirectoryModel::SetRoot(CVirtualDirectory *pDir)
endResetModel(); endResetModel();
} }
bool CVirtualDirectoryModel::GetProposedIndex(const QString& Path, QModelIndex& rOutParent, int& rOutRow) std::optional<std::pair<QModelIndex, int>> CVirtualDirectoryModel::GetProposedIndex(const QString& Path)
{ {
// Get parent path // Get parent path
TString FullPath = TO_TSTRING(Path); TString FullPath = TO_TSTRING(Path);
@ -249,43 +249,43 @@ bool CVirtualDirectoryModel::GetProposedIndex(const QString& Path, QModelIndex&
if (FullPath.EndsWith('/') || FullPath.EndsWith('\\')) if (FullPath.EndsWith('/') || FullPath.EndsWith('\\'))
FullPath = FullPath.ChopBack(1); FullPath = FullPath.ChopBack(1);
uint32 LastSlash = FullPath.LastIndexOf("\\/"); const uint32 LastSlash = FullPath.LastIndexOf("\\/");
TString ParentPath = FullPath.ChopBack( FullPath.Size() - LastSlash ); const TString ParentPath = FullPath.ChopBack( FullPath.Size() - LastSlash );
// Find parent index // Find parent index
CVirtualDirectory *pParent = (ParentPath.IsEmpty() ? mpRoot : mpRoot->FindChildDirectory(ParentPath, false)); CVirtualDirectory* pParent = (ParentPath.IsEmpty() ? mpRoot : mpRoot->FindChildDirectory(ParentPath, false));
if (!pParent) return false; if (pParent == nullptr)
return std::nullopt;
QModelIndex ParentIndex = GetIndexForDirectory(pParent); const QModelIndex ParentIndex = GetIndexForDirectory(pParent);
if (!ParentIndex.isValid()) return false; if (!ParentIndex.isValid())
return std::nullopt;
// Determine the row number that the new directory will be inserted at // Determine the row number that the new directory will be inserted at
QString DirName = TO_QSTRING(FullPath.ChopFront( LastSlash + 1 )); const QString DirName = TO_QSTRING(FullPath.ChopFront( LastSlash + 1 ));
int NumRows = rowCount(ParentIndex); const int NumRows = rowCount(ParentIndex);
int RowIdx = 0; int RowIdx = 0;
for (; RowIdx < NumRows; RowIdx++) for (; RowIdx < NumRows; RowIdx++)
{ {
QModelIndex Index = index(RowIdx, 0, ParentIndex); const QModelIndex Index = index(RowIdx, 0, ParentIndex);
QString OtherName = data(Index, Qt::DisplayRole).toString(); const QString OtherName = data(Index, Qt::DisplayRole).toString();
if (QString::compare(DirName, OtherName, Qt::CaseInsensitive) < 0) if (QString::compare(DirName, OtherName, Qt::CaseInsensitive) < 0)
break; break;
} }
rOutParent = ParentIndex; return std::make_pair(ParentIndex, RowIdx);
rOutRow = RowIdx;
return true;
} }
void CVirtualDirectoryModel::OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir, const QString& NewPath) void CVirtualDirectoryModel::OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir, const QString& NewPath)
{ {
QModelIndex Parent; const auto indexOptional = GetProposedIndex(NewPath);
int Row;
if (!GetProposedIndex(NewPath, Parent, Row)) if (!indexOptional)
return; return;
const auto [Parent, Row] = *indexOptional;
const QModelIndex OldIndex = GetIndexForDirectory(pDir); const QModelIndex OldIndex = GetIndexForDirectory(pDir);
const QModelIndex OldParent = OldIndex.parent(); const QModelIndex OldParent = OldIndex.parent();
const int OldRow = OldIndex.row(); const int OldRow = OldIndex.row();
@ -304,14 +304,14 @@ void CVirtualDirectoryModel::OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir,
void CVirtualDirectoryModel::OnDirectoryAboutToBeCreated(const QString& DirPath) void CVirtualDirectoryModel::OnDirectoryAboutToBeCreated(const QString& DirPath)
{ {
QModelIndex Parent; const auto indexOptional = GetProposedIndex(DirPath);
int Row;
if (GetProposedIndex(DirPath, Parent, Row)) if (!indexOptional)
{ return;
beginInsertRows(Parent, Row, Row);
mInsertingRows = true; const auto [Parent, Row] = *indexOptional;
} beginInsertRows(Parent, Row, Row);
mInsertingRows = true;
} }
void CVirtualDirectoryModel::OnDirectoryAboutToBeDeleted(CVirtualDirectory *pDir) void CVirtualDirectoryModel::OnDirectoryAboutToBeDeleted(CVirtualDirectory *pDir)

View File

@ -6,6 +6,9 @@
#include <QAbstractItemModel> #include <QAbstractItemModel>
#include <QIcon> #include <QIcon>
#include <optional>
#include <utility>
class CVirtualDirectoryModel : public QAbstractItemModel class CVirtualDirectoryModel : public QAbstractItemModel
{ {
Q_OBJECT Q_OBJECT
@ -37,7 +40,7 @@ public:
void SetRoot(CVirtualDirectory *pDir); void SetRoot(CVirtualDirectory *pDir);
protected: protected:
bool GetProposedIndex(const QString& Path, QModelIndex& rOutParent, int& rOutRow); std::optional<std::pair<QModelIndex, int>> GetProposedIndex(const QString& Path);
public slots: public slots:
void OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir, const QString& NewPath); void OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir, const QString& NewPath);