CVirtualDirectoryModel: Mark GetIndexForDirectory as const
This function doesn't modify instance state.
This commit is contained in:
parent
05df1fcf89
commit
602c0cf1e3
|
@ -190,17 +190,17 @@ Qt::DropActions CVirtualDirectoryModel::supportedDropActions() const
|
|||
return Qt::MoveAction;
|
||||
}
|
||||
|
||||
QModelIndex CVirtualDirectoryModel::GetIndexForDirectory(CVirtualDirectory *pDir)
|
||||
QModelIndex CVirtualDirectoryModel::GetIndexForDirectory(const CVirtualDirectory *pDir) const
|
||||
{
|
||||
if (!pDir)
|
||||
if (pDir == nullptr)
|
||||
return QModelIndex();
|
||||
|
||||
QVector<int> Indices;
|
||||
CVirtualDirectory *pOriginal = pDir;
|
||||
CVirtualDirectory *pParent = pDir->Parent();
|
||||
const CVirtualDirectory* pOriginal = pDir;
|
||||
const CVirtualDirectory* pParent = pDir->Parent();
|
||||
|
||||
// Get index list
|
||||
while (pParent)
|
||||
while (pParent != nullptr)
|
||||
{
|
||||
bool Found = false;
|
||||
|
||||
|
@ -244,7 +244,7 @@ void CVirtualDirectoryModel::SetRoot(CVirtualDirectory *pDir)
|
|||
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
|
||||
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 );
|
||||
|
||||
// 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)
|
||||
return std::nullopt;
|
||||
|
||||
|
@ -281,7 +281,7 @@ std::optional<std::pair<QModelIndex, int>> CVirtualDirectoryModel::GetProposedIn
|
|||
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);
|
||||
|
||||
|
@ -317,7 +317,7 @@ void CVirtualDirectoryModel::OnDirectoryAboutToBeCreated(const QString& DirPath)
|
|||
mInsertingRows = true;
|
||||
}
|
||||
|
||||
void CVirtualDirectoryModel::OnDirectoryAboutToBeDeleted(CVirtualDirectory *pDir)
|
||||
void CVirtualDirectoryModel::OnDirectoryAboutToBeDeleted(const CVirtualDirectory *pDir)
|
||||
{
|
||||
const QModelIndex Index = GetIndexForDirectory(pDir);
|
||||
|
||||
|
|
|
@ -35,17 +35,17 @@ public:
|
|||
Qt::DropActions supportedDragActions() const override;
|
||||
Qt::DropActions supportedDropActions() const override;
|
||||
|
||||
QModelIndex GetIndexForDirectory(CVirtualDirectory *pDir);
|
||||
QModelIndex GetIndexForDirectory(const CVirtualDirectory *pDir) const;
|
||||
CVirtualDirectory* IndexDirectory(const QModelIndex& rkIndex) const;
|
||||
void SetRoot(CVirtualDirectory *pDir);
|
||||
|
||||
protected:
|
||||
std::optional<std::pair<QModelIndex, int>> GetProposedIndex(const QString& Path);
|
||||
std::optional<std::pair<QModelIndex, int>> GetProposedIndex(const QString& Path) const;
|
||||
|
||||
public slots:
|
||||
void OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir, const QString& NewPath);
|
||||
void OnDirectoryAboutToBeMoved(const CVirtualDirectory *pDir, const QString& NewPath);
|
||||
void OnDirectoryAboutToBeCreated(const QString& DirPath);
|
||||
void OnDirectoryAboutToBeDeleted(CVirtualDirectory *pDir);
|
||||
void OnDirectoryAboutToBeDeleted(const CVirtualDirectory *pDir);
|
||||
void FinishModelChanges();
|
||||
};
|
||||
|
||||
|
|
|
@ -47,31 +47,31 @@ void CVirtualDirectoryTreeView::setModel(QAbstractItemModel *pModel)
|
|||
}
|
||||
}
|
||||
|
||||
void CVirtualDirectoryTreeView::OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir)
|
||||
void CVirtualDirectoryTreeView::OnDirectoryAboutToBeMoved(const CVirtualDirectory *pDir)
|
||||
{
|
||||
if (mpModel)
|
||||
{
|
||||
QModelIndex Index = mpModel->GetIndexForDirectory(pDir);
|
||||
if (mpModel == nullptr)
|
||||
return;
|
||||
|
||||
if (selectionModel()->currentIndex() == Index)
|
||||
mTransferSelectionPostMove = true;
|
||||
}
|
||||
const QModelIndex Index = mpModel->GetIndexForDirectory(pDir);
|
||||
|
||||
if (selectionModel()->currentIndex() == Index)
|
||||
mTransferSelectionPostMove = true;
|
||||
}
|
||||
|
||||
void CVirtualDirectoryTreeView::OnDirectoryMoved(CVirtualDirectory *pDir)
|
||||
void CVirtualDirectoryTreeView::OnDirectoryMoved(const CVirtualDirectory *pDir)
|
||||
{
|
||||
if (mTransferSelectionPostMove)
|
||||
{
|
||||
// Make sure the model has updated first
|
||||
mpModel->FinishModelChanges();
|
||||
if (!mTransferSelectionPostMove)
|
||||
return;
|
||||
|
||||
QModelIndex Index = mpModel->GetIndexForDirectory(pDir);
|
||||
// Make sure the model has updated first
|
||||
mpModel->FinishModelChanges();
|
||||
|
||||
blockSignals(true);
|
||||
expand(Index.parent());
|
||||
selectionModel()->setCurrentIndex(Index, QItemSelectionModel::ClearAndSelect);
|
||||
blockSignals(false);
|
||||
const QModelIndex Index = mpModel->GetIndexForDirectory(pDir);
|
||||
|
||||
mTransferSelectionPostMove = false;
|
||||
}
|
||||
blockSignals(true);
|
||||
expand(Index.parent());
|
||||
selectionModel()->setCurrentIndex(Index, QItemSelectionModel::ClearAndSelect);
|
||||
blockSignals(false);
|
||||
|
||||
mTransferSelectionPostMove = false;
|
||||
}
|
||||
|
|
|
@ -19,8 +19,8 @@ public:
|
|||
void setModel(QAbstractItemModel *pModel) override;
|
||||
|
||||
public slots:
|
||||
void OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir);
|
||||
void OnDirectoryMoved(CVirtualDirectory *pDir);
|
||||
void OnDirectoryAboutToBeMoved(const CVirtualDirectory *pDir);
|
||||
void OnDirectoryMoved(const CVirtualDirectory *pDir);
|
||||
};
|
||||
|
||||
#endif // CVIRTUALDIRECTORYTREEVIEW_H
|
||||
|
|
Loading…
Reference in New Issue