From dcfb34ca1c29a05857eafb1716796ac312c60f28 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 5 Jul 2020 10:12:31 -0400 Subject: [PATCH] CVirtualDirectoryModel: Eliminate string copies where applicable --- .../CVirtualDirectoryModel.cpp | 55 +++++++++---------- .../ResourceBrowser/CVirtualDirectoryModel.h | 6 +- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/Editor/ResourceBrowser/CVirtualDirectoryModel.cpp b/src/Editor/ResourceBrowser/CVirtualDirectoryModel.cpp index 60198078..fcd0b142 100644 --- a/src/Editor/ResourceBrowser/CVirtualDirectoryModel.cpp +++ b/src/Editor/ResourceBrowser/CVirtualDirectoryModel.cpp @@ -169,15 +169,12 @@ bool CVirtualDirectoryModel::dropMimeData(const QMimeData *pkData, Qt::DropActio QMimeData* CVirtualDirectoryModel::mimeData(const QModelIndexList& rkIndexes) const { - if (rkIndexes.size() == 1) - { - QModelIndex Index = rkIndexes.front(); - CVirtualDirectory *pDir = IndexDirectory(Index); - CResourceMimeData *pMimeData = new CResourceMimeData(pDir); - return pMimeData; - } - else + if (rkIndexes.size() != 1) return nullptr; + + const QModelIndex Index = rkIndexes.front(); + CVirtualDirectory *pDir = IndexDirectory(Index); + return new CResourceMimeData(pDir); } Qt::DropActions CVirtualDirectoryModel::supportedDragActions() const @@ -231,7 +228,9 @@ QModelIndex CVirtualDirectoryModel::GetIndexForDirectory(CVirtualDirectory *pDir CVirtualDirectory* CVirtualDirectoryModel::IndexDirectory(const QModelIndex& rkIndex) const { - if (!rkIndex.isValid()) return nullptr; + if (!rkIndex.isValid()) + return nullptr; + return static_cast(rkIndex.internalPointer()); } @@ -242,7 +241,7 @@ void CVirtualDirectoryModel::SetRoot(CVirtualDirectory *pDir) endResetModel(); } -bool CVirtualDirectoryModel::GetProposedIndex(QString Path, QModelIndex& rOutParent, int& rOutRow) +bool CVirtualDirectoryModel::GetProposedIndex(const QString& Path, QModelIndex& rOutParent, int& rOutRow) { // Get parent path TString FullPath = TO_TSTRING(Path); @@ -279,31 +278,31 @@ bool CVirtualDirectoryModel::GetProposedIndex(QString Path, QModelIndex& rOutPar return true; } -void CVirtualDirectoryModel::OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir, QString NewPath) +void CVirtualDirectoryModel::OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir, const QString& NewPath) { QModelIndex Parent; int Row; - if (GetProposedIndex(NewPath, Parent, Row)) - { - QModelIndex OldIndex = GetIndexForDirectory(pDir); - QModelIndex OldParent = OldIndex.parent(); - int OldRow = OldIndex.row(); + if (!GetProposedIndex(NewPath, Parent, Row)) + return; - if (OldParent == Parent && (Row == OldRow || Row == OldRow + 1)) - { - emit layoutAboutToBeChanged(); - mChangingLayout = true; - } - else - { - beginMoveRows(OldParent, OldRow, OldRow, Parent, Row); - mMovingRows = true; - } + const QModelIndex OldIndex = GetIndexForDirectory(pDir); + const QModelIndex OldParent = OldIndex.parent(); + const int OldRow = OldIndex.row(); + + if (OldParent == Parent && (Row == OldRow || Row == OldRow + 1)) + { + emit layoutAboutToBeChanged(); + mChangingLayout = true; + } + else + { + beginMoveRows(OldParent, OldRow, OldRow, Parent, Row); + mMovingRows = true; } } -void CVirtualDirectoryModel::OnDirectoryAboutToBeCreated(QString DirPath) +void CVirtualDirectoryModel::OnDirectoryAboutToBeCreated(const QString& DirPath) { QModelIndex Parent; int Row; @@ -317,7 +316,7 @@ void CVirtualDirectoryModel::OnDirectoryAboutToBeCreated(QString DirPath) void CVirtualDirectoryModel::OnDirectoryAboutToBeDeleted(CVirtualDirectory *pDir) { - QModelIndex Index = GetIndexForDirectory(pDir); + const QModelIndex Index = GetIndexForDirectory(pDir); if (Index.isValid()) { diff --git a/src/Editor/ResourceBrowser/CVirtualDirectoryModel.h b/src/Editor/ResourceBrowser/CVirtualDirectoryModel.h index 4fd04b23..19d2fbd3 100644 --- a/src/Editor/ResourceBrowser/CVirtualDirectoryModel.h +++ b/src/Editor/ResourceBrowser/CVirtualDirectoryModel.h @@ -37,11 +37,11 @@ public: void SetRoot(CVirtualDirectory *pDir); protected: - bool GetProposedIndex(QString Path, QModelIndex& rOutParent, int& rOutRow); + bool GetProposedIndex(const QString& Path, QModelIndex& rOutParent, int& rOutRow); public slots: - void OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir, QString NewPath); - void OnDirectoryAboutToBeCreated(QString DirPath); + void OnDirectoryAboutToBeMoved(CVirtualDirectory *pDir, const QString& NewPath); + void OnDirectoryAboutToBeCreated(const QString& DirPath); void OnDirectoryAboutToBeDeleted(CVirtualDirectory *pDir); void FinishModelChanges(); };