CVirtualDirectoryModel: Eliminate string copies where applicable

This commit is contained in:
Lioncash 2020-07-05 10:12:31 -04:00
parent fdcb0b770c
commit dcfb34ca1c
2 changed files with 30 additions and 31 deletions

View File

@ -169,15 +169,12 @@ bool CVirtualDirectoryModel::dropMimeData(const QMimeData *pkData, Qt::DropActio
QMimeData* CVirtualDirectoryModel::mimeData(const QModelIndexList& rkIndexes) const QMimeData* CVirtualDirectoryModel::mimeData(const QModelIndexList& rkIndexes) const
{ {
if (rkIndexes.size() == 1) if (rkIndexes.size() != 1)
{
QModelIndex Index = rkIndexes.front();
CVirtualDirectory *pDir = IndexDirectory(Index);
CResourceMimeData *pMimeData = new CResourceMimeData(pDir);
return pMimeData;
}
else
return nullptr; return nullptr;
const QModelIndex Index = rkIndexes.front();
CVirtualDirectory *pDir = IndexDirectory(Index);
return new CResourceMimeData(pDir);
} }
Qt::DropActions CVirtualDirectoryModel::supportedDragActions() const Qt::DropActions CVirtualDirectoryModel::supportedDragActions() const
@ -231,7 +228,9 @@ QModelIndex CVirtualDirectoryModel::GetIndexForDirectory(CVirtualDirectory *pDir
CVirtualDirectory* CVirtualDirectoryModel::IndexDirectory(const QModelIndex& rkIndex) const CVirtualDirectory* CVirtualDirectoryModel::IndexDirectory(const QModelIndex& rkIndex) const
{ {
if (!rkIndex.isValid()) return nullptr; if (!rkIndex.isValid())
return nullptr;
return static_cast<CVirtualDirectory*>(rkIndex.internalPointer()); return static_cast<CVirtualDirectory*>(rkIndex.internalPointer());
} }
@ -242,7 +241,7 @@ void CVirtualDirectoryModel::SetRoot(CVirtualDirectory *pDir)
endResetModel(); endResetModel();
} }
bool CVirtualDirectoryModel::GetProposedIndex(QString Path, QModelIndex& rOutParent, int& rOutRow) bool CVirtualDirectoryModel::GetProposedIndex(const QString& Path, QModelIndex& rOutParent, int& rOutRow)
{ {
// Get parent path // Get parent path
TString FullPath = TO_TSTRING(Path); TString FullPath = TO_TSTRING(Path);
@ -279,31 +278,31 @@ bool CVirtualDirectoryModel::GetProposedIndex(QString Path, QModelIndex& rOutPar
return true; return true;
} }
void CVirtualDirectoryModel::OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir, QString NewPath) void CVirtualDirectoryModel::OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir, const QString& NewPath)
{ {
QModelIndex Parent; QModelIndex Parent;
int Row; int Row;
if (GetProposedIndex(NewPath, Parent, Row)) if (!GetProposedIndex(NewPath, Parent, Row))
{ return;
QModelIndex OldIndex = GetIndexForDirectory(pDir);
QModelIndex OldParent = OldIndex.parent();
int OldRow = OldIndex.row();
if (OldParent == Parent && (Row == OldRow || Row == OldRow + 1)) const QModelIndex OldIndex = GetIndexForDirectory(pDir);
{ const QModelIndex OldParent = OldIndex.parent();
emit layoutAboutToBeChanged(); const int OldRow = OldIndex.row();
mChangingLayout = true;
} if (OldParent == Parent && (Row == OldRow || Row == OldRow + 1))
else {
{ emit layoutAboutToBeChanged();
beginMoveRows(OldParent, OldRow, OldRow, Parent, Row); mChangingLayout = true;
mMovingRows = true; }
} else
{
beginMoveRows(OldParent, OldRow, OldRow, Parent, Row);
mMovingRows = true;
} }
} }
void CVirtualDirectoryModel::OnDirectoryAboutToBeCreated(QString DirPath) void CVirtualDirectoryModel::OnDirectoryAboutToBeCreated(const QString& DirPath)
{ {
QModelIndex Parent; QModelIndex Parent;
int Row; int Row;
@ -317,7 +316,7 @@ void CVirtualDirectoryModel::OnDirectoryAboutToBeCreated(QString DirPath)
void CVirtualDirectoryModel::OnDirectoryAboutToBeDeleted(CVirtualDirectory *pDir) void CVirtualDirectoryModel::OnDirectoryAboutToBeDeleted(CVirtualDirectory *pDir)
{ {
QModelIndex Index = GetIndexForDirectory(pDir); const QModelIndex Index = GetIndexForDirectory(pDir);
if (Index.isValid()) if (Index.isValid())
{ {

View File

@ -37,11 +37,11 @@ public:
void SetRoot(CVirtualDirectory *pDir); void SetRoot(CVirtualDirectory *pDir);
protected: protected:
bool GetProposedIndex(QString Path, QModelIndex& rOutParent, int& rOutRow); bool GetProposedIndex(const QString& Path, QModelIndex& rOutParent, int& rOutRow);
public slots: public slots:
void OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir, QString NewPath); void OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir, const QString& NewPath);
void OnDirectoryAboutToBeCreated(QString DirPath); void OnDirectoryAboutToBeCreated(const QString& DirPath);
void OnDirectoryAboutToBeDeleted(CVirtualDirectory *pDir); void OnDirectoryAboutToBeDeleted(CVirtualDirectory *pDir);
void FinishModelChanges(); void FinishModelChanges();
}; };