CVirtualDirectoryModel: Mark GetIndexForDirectory as const

This function doesn't modify instance state.
This commit is contained in:
Lioncash 2020-07-05 10:33:32 -04:00
parent 05df1fcf89
commit 602c0cf1e3
4 changed files with 34 additions and 34 deletions

View File

@ -190,17 +190,17 @@ Qt::DropActions CVirtualDirectoryModel::supportedDropActions() const
return Qt::MoveAction; return Qt::MoveAction;
} }
QModelIndex CVirtualDirectoryModel::GetIndexForDirectory(CVirtualDirectory *pDir) QModelIndex CVirtualDirectoryModel::GetIndexForDirectory(const CVirtualDirectory *pDir) const
{ {
if (!pDir) if (pDir == nullptr)
return QModelIndex(); return QModelIndex();
QVector<int> Indices; QVector<int> Indices;
CVirtualDirectory *pOriginal = pDir; const CVirtualDirectory* pOriginal = pDir;
CVirtualDirectory *pParent = pDir->Parent(); const CVirtualDirectory* pParent = pDir->Parent();
// Get index list // Get index list
while (pParent) while (pParent != nullptr)
{ {
bool Found = false; bool Found = false;
@ -244,7 +244,7 @@ void CVirtualDirectoryModel::SetRoot(CVirtualDirectory *pDir)
endResetModel(); endResetModel();
} }
std::optional<std::pair<QModelIndex, int>> CVirtualDirectoryModel::GetProposedIndex(const QString& Path) std::optional<std::pair<QModelIndex, int>> CVirtualDirectoryModel::GetProposedIndex(const QString& Path) const
{ {
// Get parent path // Get parent path
TString FullPath = TO_TSTRING(Path); TString FullPath = TO_TSTRING(Path);
@ -256,7 +256,7 @@ std::optional<std::pair<QModelIndex, int>> CVirtualDirectoryModel::GetProposedIn
const 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)); const CVirtualDirectory* pParent = (ParentPath.IsEmpty() ? mpRoot : mpRoot->FindChildDirectory(ParentPath, false));
if (pParent == nullptr) if (pParent == nullptr)
return std::nullopt; return std::nullopt;
@ -281,7 +281,7 @@ std::optional<std::pair<QModelIndex, int>> CVirtualDirectoryModel::GetProposedIn
return std::make_pair(ParentIndex, RowIdx); return std::make_pair(ParentIndex, RowIdx);
} }
void CVirtualDirectoryModel::OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir, const QString& NewPath) void CVirtualDirectoryModel::OnDirectoryAboutToBeMoved(const CVirtualDirectory *pDir, const QString& NewPath)
{ {
const auto indexOptional = GetProposedIndex(NewPath); const auto indexOptional = GetProposedIndex(NewPath);
@ -317,7 +317,7 @@ void CVirtualDirectoryModel::OnDirectoryAboutToBeCreated(const QString& DirPath)
mInsertingRows = true; mInsertingRows = true;
} }
void CVirtualDirectoryModel::OnDirectoryAboutToBeDeleted(CVirtualDirectory *pDir) void CVirtualDirectoryModel::OnDirectoryAboutToBeDeleted(const CVirtualDirectory *pDir)
{ {
const QModelIndex Index = GetIndexForDirectory(pDir); const QModelIndex Index = GetIndexForDirectory(pDir);

View File

@ -35,17 +35,17 @@ public:
Qt::DropActions supportedDragActions() const override; Qt::DropActions supportedDragActions() const override;
Qt::DropActions supportedDropActions() const override; Qt::DropActions supportedDropActions() const override;
QModelIndex GetIndexForDirectory(CVirtualDirectory *pDir); QModelIndex GetIndexForDirectory(const CVirtualDirectory *pDir) const;
CVirtualDirectory* IndexDirectory(const QModelIndex& rkIndex) const; CVirtualDirectory* IndexDirectory(const QModelIndex& rkIndex) const;
void SetRoot(CVirtualDirectory *pDir); void SetRoot(CVirtualDirectory *pDir);
protected: protected:
std::optional<std::pair<QModelIndex, int>> GetProposedIndex(const QString& Path); std::optional<std::pair<QModelIndex, int>> GetProposedIndex(const QString& Path) const;
public slots: public slots:
void OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir, const QString& NewPath); void OnDirectoryAboutToBeMoved(const CVirtualDirectory *pDir, const QString& NewPath);
void OnDirectoryAboutToBeCreated(const QString& DirPath); void OnDirectoryAboutToBeCreated(const QString& DirPath);
void OnDirectoryAboutToBeDeleted(CVirtualDirectory *pDir); void OnDirectoryAboutToBeDeleted(const CVirtualDirectory *pDir);
void FinishModelChanges(); void FinishModelChanges();
}; };

View File

@ -47,25 +47,26 @@ void CVirtualDirectoryTreeView::setModel(QAbstractItemModel *pModel)
} }
} }
void CVirtualDirectoryTreeView::OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir) void CVirtualDirectoryTreeView::OnDirectoryAboutToBeMoved(const CVirtualDirectory *pDir)
{ {
if (mpModel) if (mpModel == nullptr)
{ return;
QModelIndex Index = mpModel->GetIndexForDirectory(pDir);
const QModelIndex Index = mpModel->GetIndexForDirectory(pDir);
if (selectionModel()->currentIndex() == Index) if (selectionModel()->currentIndex() == Index)
mTransferSelectionPostMove = true; mTransferSelectionPostMove = true;
}
} }
void CVirtualDirectoryTreeView::OnDirectoryMoved(CVirtualDirectory *pDir) void CVirtualDirectoryTreeView::OnDirectoryMoved(const CVirtualDirectory *pDir)
{ {
if (mTransferSelectionPostMove) if (!mTransferSelectionPostMove)
{ return;
// Make sure the model has updated first // Make sure the model has updated first
mpModel->FinishModelChanges(); mpModel->FinishModelChanges();
QModelIndex Index = mpModel->GetIndexForDirectory(pDir); const QModelIndex Index = mpModel->GetIndexForDirectory(pDir);
blockSignals(true); blockSignals(true);
expand(Index.parent()); expand(Index.parent());
@ -73,5 +74,4 @@ void CVirtualDirectoryTreeView::OnDirectoryMoved(CVirtualDirectory *pDir)
blockSignals(false); blockSignals(false);
mTransferSelectionPostMove = false; mTransferSelectionPostMove = false;
}
} }

View File

@ -19,8 +19,8 @@ public:
void setModel(QAbstractItemModel *pModel) override; void setModel(QAbstractItemModel *pModel) override;
public slots: public slots:
void OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir); void OnDirectoryAboutToBeMoved(const CVirtualDirectory *pDir);
void OnDirectoryMoved(CVirtualDirectory *pDir); void OnDirectoryMoved(const CVirtualDirectory *pDir);
}; };
#endif // CVIRTUALDIRECTORYTREEVIEW_H #endif // CVIRTUALDIRECTORYTREEVIEW_H