diff --git a/src/lib/utility/file/FileManager.cpp b/src/lib/utility/file/FileManager.cpp index 91534d81..7cb67049 100644 --- a/src/lib/utility/file/FileManager.cpp +++ b/src/lib/utility/file/FileManager.cpp @@ -57,33 +57,6 @@ std::set FileManager::getAllSourceFilePaths() const return m_allSourceFilePaths; } -std::set FileManager::getAllSourceFilePathsRelative(const FilePath& baseDirectory) const -{ - std::set absolutePaths; - for (const FilePath& path: getAllSourceFilePaths()) - { - if (baseDirectory.exists()) - { - absolutePaths.insert(path.getRelativeTo(baseDirectory)); - } - else - { - absolutePaths.insert(path); - } - } - return absolutePaths; -} - -std::vector FileManager::makeCanonical(const std::vector& filePaths) -{ - std::vector ret; - for (const FilePath& filePath: filePaths) - { - ret.push_back(filePath.getCanonical()); - } - return ret; -} - bool FileManager::isExcluded(const FilePath& filePath) const { for (const FilePathFilter& filter : m_excludeFilters) diff --git a/src/lib/utility/file/FileManager.h b/src/lib/utility/file/FileManager.h index b8cc3ec9..7d512c9d 100644 --- a/src/lib/utility/file/FileManager.h +++ b/src/lib/utility/file/FileManager.h @@ -29,10 +29,8 @@ public: // returns a list of paths to all files that reside in the non-excluded source paths std::set getAllSourceFilePaths() const; - std::set getAllSourceFilePathsRelative(const FilePath& baseDirectory) const; private: - std::vector makeCanonical(const std::vector& filePaths); bool isExcluded(const FilePath& filePath) const; std::vector m_sourcePaths; diff --git a/src/lib/utility/utilityFile.cpp b/src/lib/utility/utilityFile.cpp index 3775f9fc..11be404b 100644 --- a/src/lib/utility/utilityFile.cpp +++ b/src/lib/utility/utilityFile.cpp @@ -26,3 +26,22 @@ std::vector utility::getTopLevelPaths(const std::set& paths) return topLevelPaths; } + +FilePath utility::getAsRelativeIfShorter(const FilePath& absolutePath, const FilePath& baseDirectory) +{ + if (!baseDirectory.empty()) + { + const FilePath relativePath = absolutePath.getRelativeTo(baseDirectory); + if (relativePath.wstr().size() < absolutePath.wstr().size()) + { + return relativePath; + } + } + return absolutePath; +} + +std::vector utility::getAsRelativeIfShorter(const std::vector& absolutePaths, const FilePath& baseDirectory) +{ + return utility::convert(absolutePaths, [&](const FilePath& path) { return getAsRelativeIfShorter(path, baseDirectory); }); +} + diff --git a/src/lib/utility/utilityFile.h b/src/lib/utility/utilityFile.h index 162fa29f..d16828d8 100644 --- a/src/lib/utility/utilityFile.h +++ b/src/lib/utility/utilityFile.h @@ -10,6 +10,9 @@ namespace utility { std::vector getTopLevelPaths(const std::vector& paths); std::vector getTopLevelPaths(const std::set& paths); + + FilePath getAsRelativeIfShorter(const FilePath& absolutePath, const FilePath& baseDirectory); + std::vector getAsRelativeIfShorter(const std::vector& absolutePaths, const FilePath& baseDirectory); } #endif // UTILITY_FILE_H diff --git a/src/lib_gui/qt/element/QtLocationPicker.cpp b/src/lib_gui/qt/element/QtLocationPicker.cpp index 0b1d1a17..9db0b0b9 100644 --- a/src/lib_gui/qt/element/QtLocationPicker.cpp +++ b/src/lib_gui/qt/element/QtLocationPicker.cpp @@ -6,10 +6,10 @@ #include #include -#include "utility/ResourcePaths.h" - #include "qt/element/QtIconButton.h" #include "qt/utility/QtFileDialog.h" +#include "utility/ResourcePaths.h" +#include "utility/utilityFile.h" QtLocationPicker::QtLocationPicker(QWidget *parent) : QWidget(parent) @@ -118,17 +118,9 @@ void QtLocationPicker::handleButtonPress() if (!fileName.isEmpty()) { - if (!m_relativeRootDirectory.empty()) - { - const FilePath path(fileName.toStdWString()); - const FilePath relPath = path.getRelativeTo(m_relativeRootDirectory); - if (relPath.wstr().size() < path.wstr().size()) - { - fileName = QString::fromStdWString(relPath.wstr()); - } - } - - m_data->setText(fileName); + m_data->setText(QString::fromStdWString(utility::getAsRelativeIfShorter( + FilePath(fileName.toStdWString()), m_relativeRootDirectory + ).wstr())); emit locationPicked(); } } diff --git a/src/lib_gui/qt/element/QtPathListBox.cpp b/src/lib_gui/qt/element/QtPathListBox.cpp index 5526ceaf..8351aac6 100644 --- a/src/lib_gui/qt/element/QtPathListBox.cpp +++ b/src/lib_gui/qt/element/QtPathListBox.cpp @@ -6,6 +6,7 @@ #include #include "qt/element/QtPathListBoxItem.h" +#include "utility/utilityFile.h" QtPathListBox::QtPathListBox(QWidget *parent, const QString& listName, SelectionPolicyType selectionPolicy) : QtListBox(parent, listName) @@ -74,16 +75,9 @@ void QtPathListBox::makeAbsolute(FilePath& path) const } } -void QtPathListBox::makeRelative(FilePath& path) const +void QtPathListBox::makeRelativeIfShorter(FilePath& path) const { - if (!m_relativeRootDirectory.empty()) - { - const FilePath relPath = path.getRelativeTo(m_relativeRootDirectory); - if (relPath.wstr().size() < path.wstr().size()) - { - path = relPath; - } - } + path = utility::getAsRelativeIfShorter(path, m_relativeRootDirectory); } void QtPathListBox::dropEvent(QDropEvent *event) @@ -91,7 +85,7 @@ void QtPathListBox::dropEvent(QDropEvent *event) foreach(QUrl url, event->mimeData()->urls()) { FilePath path(url.toLocalFile().toStdWString()); - makeRelative(path); + makeRelativeIfShorter(path); addListBoxItemWithText(QString::fromStdWString(path.wstr())); } } diff --git a/src/lib_gui/qt/element/QtPathListBox.h b/src/lib_gui/qt/element/QtPathListBox.h index db817970..17f2b968 100644 --- a/src/lib_gui/qt/element/QtPathListBox.h +++ b/src/lib_gui/qt/element/QtPathListBox.h @@ -26,7 +26,7 @@ public: void setPaths(const std::vector& list, bool readOnly = false); void makeAbsolute(FilePath& path) const; - void makeRelative(FilePath& path) const; + void makeRelativeIfShorter(FilePath& path) const; protected: void dropEvent(QDropEvent *event) override; diff --git a/src/lib_gui/qt/element/QtPathListBoxItem.cpp b/src/lib_gui/qt/element/QtPathListBoxItem.cpp index c99b4627..124ed40e 100644 --- a/src/lib_gui/qt/element/QtPathListBoxItem.cpp +++ b/src/lib_gui/qt/element/QtPathListBoxItem.cpp @@ -52,14 +52,14 @@ void QtPathListBoxItem::handleButtonPress() if (!list.isEmpty()) { FilePath path(list.at(0).toStdWString()); - m_listBox->makeRelative(path); + m_listBox->makeRelativeIfShorter(path); setText(QString::fromStdWString(path.wstr())); } for (int i = 1; i < list.size(); i++) { FilePath path(list.at(i).toStdWString()); - m_listBox->makeRelative(path); + m_listBox->makeRelativeIfShorter(path); getListBox()->addListBoxItemWithText(QString::fromStdWString(path.wstr())); } diff --git a/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPath.cpp b/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPath.cpp index 1eebf02c..35be2731 100644 --- a/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPath.cpp +++ b/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPath.cpp @@ -29,6 +29,7 @@ #include "utility/sonargraph/SonargraphProject.h" #include "utility/ScopedFunctor.h" #include "utility/utility.h" +#include "utility/utilityFile.h" #include "utility/utilityGradle.h" #include "utility/utilityMaven.h" #include "Application.h" @@ -171,18 +172,10 @@ void QtProjectWizzardContentPathCDB::load() { m_picker->setText(QString::fromStdWString(m_settings->getCompilationDatabasePath().wstr())); - m_filePaths.clear(); - - const FilePath projectPath = m_settings->getProjectDirectoryPath(); - for (FilePath path : SourceGroupCxxCdb(m_settings).getAllSourceFilePaths()) - { - if (projectPath.exists()) - { - path.makeRelativeTo(projectPath); - } - - m_filePaths.push_back(path); - } + m_filePaths = utility::getAsRelativeIfShorter( + utility::toVector(SourceGroupCxxCdb(m_settings).getAllSourceFilePaths()), + m_settings->getProjectDirectoryPath() + ); if (m_fileCountLabel) { @@ -223,7 +216,7 @@ void QtProjectWizzardContentPathCDB::pickedPath() { if (projectPath.contains(path)) { - indexedHeaderPaths.insert(path.getRelativeTo(projectPath)); + indexedHeaderPaths.insert(path.getRelativeTo(projectPath)); // the relative path is always shorter than the absolute path } } m_settings->setIndexedHeaderPaths(utility::toVector(indexedHeaderPaths)); @@ -285,18 +278,10 @@ void QtProjectWizzardContentCodeblocksProjectPath::load() { m_picker->setText(QString::fromStdWString(m_settings->getCodeblocksProjectPath().wstr())); - m_filePaths.clear(); - - const FilePath projectPath = m_settings->getProjectDirectoryPath(); - for (FilePath path : SourceGroupCxxCodeblocks(m_settings).getAllSourceFilePaths()) - { - if (projectPath.exists()) - { - path.makeRelativeTo(projectPath); - } - - m_filePaths.push_back(path); - } + m_filePaths = utility::getAsRelativeIfShorter( + utility::toVector(SourceGroupCxxCodeblocks(m_settings).getAllSourceFilePaths()), + m_settings->getProjectDirectoryPath() + ); if (m_fileCountLabel) { @@ -342,7 +327,7 @@ void QtProjectWizzardContentCodeblocksProjectPath::pickedPath() { if (projectPath.contains(path)) { - indexedHeaderPaths.insert(path.getRelativeTo(projectPath)); + indexedHeaderPaths.insert(path.getRelativeTo(projectPath)); // the relative path is always shorter than the absolute path } } m_settings->setIndexedHeaderPaths(utility::toVector(indexedHeaderPaths)); @@ -409,8 +394,6 @@ void QtProjectWizzardContentSonargraphProjectPath::load() { m_picker->setText(QString::fromStdWString(m_settingsWithSonargraphProjectPath->getSonargraphProjectPath().wstr())); - m_filePaths.clear(); - std::set allSourceFilePaths; if (std::shared_ptr settings = std::dynamic_pointer_cast(m_settings)) { @@ -420,16 +403,11 @@ void QtProjectWizzardContentSonargraphProjectPath::load() { allSourceFilePaths = SourceGroupJavaSonargraph(settings).getAllSourceFilePaths(); } - - const FilePath projectPath = m_settings->getProjectDirectoryPath(); - for (FilePath path : allSourceFilePaths) - { - if (projectPath.exists()) - { - path.makeRelativeTo(projectPath); - } - m_filePaths.push_back(path); - } + + m_filePaths = utility::getAsRelativeIfShorter( + utility::toVector(allSourceFilePaths), + m_settings->getProjectDirectoryPath() + ); if (m_fileCountLabel) { @@ -495,7 +473,7 @@ void QtProjectWizzardContentSonargraphProjectPath::pickedPath() { if (projectPath.contains(path)) { - indexedHeaderPaths.insert(path.getRelativeTo(projectPath)); + indexedHeaderPaths.insert(path.getRelativeTo(projectPath)); // the relative path is always shorter than the absolute path } } m_settingsCxxSonargraph->setIndexedHeaderPaths(utility::toVector(indexedHeaderPaths)); @@ -590,20 +568,11 @@ std::vector QtProjectWizzardContentPathSourceMaven::getFilePaths() con } } - std::vector list; - const FilePath projectPath = m_settings->getProjectDirectoryPath(); - for (FilePath path : SourceGroupJavaMaven(m_settings).getAllSourceFilePaths()) - { - if (projectPath.exists()) - { - path.makeRelativeTo(projectPath); - } - - list.push_back(path); - } - - return list; -} + return utility::getAsRelativeIfShorter( + utility::toVector(SourceGroupJavaMaven(m_settings).getAllSourceFilePaths()), + m_settings->getProjectDirectoryPath() + ); +} std::shared_ptr QtProjectWizzardContentPathSourceMaven::getSourceGroupSettings() { @@ -689,18 +658,10 @@ void QtProjectWizzardContentPathSourceGradle::save() std::vector QtProjectWizzardContentPathSourceGradle::getFilePaths() const { - std::vector list; - const FilePath projectPath = m_settings->getProjectDirectoryPath(); - for (FilePath path : SourceGroupJavaGradle(m_settings).getAllSourceFilePaths()) - { - if (projectPath.exists()) - { - path.makeRelativeTo(projectPath); - } - - list.push_back(path); - } - return list; + return utility::getAsRelativeIfShorter( + utility::toVector(SourceGroupJavaGradle(m_settings).getAllSourceFilePaths()), + m_settings->getProjectDirectoryPath() + ); } std::shared_ptr QtProjectWizzardContentPathSourceGradle::getSourceGroupSettings() diff --git a/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPaths.cpp b/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPaths.cpp index 8075a55c..40988f11 100644 --- a/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPaths.cpp +++ b/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPaths.cpp @@ -249,19 +249,10 @@ std::vector QtProjectWizzardContentPathsSource::getFilePaths() const allSourceFilePaths = SourceGroupJavaEmpty(settings).getAllSourceFilePaths(); } - std::vector filePaths; - - const FilePath projectPath = m_settings->getProjectDirectoryPath(); - for (FilePath path : allSourceFilePaths) - { - if (projectPath.exists()) - { - path.makeRelativeTo(projectPath); - } - filePaths.push_back(path); - } - - return filePaths; + return utility::getAsRelativeIfShorter( + utility::toVector(allSourceFilePaths), + m_settings->getProjectDirectoryPath() + ); } QString QtProjectWizzardContentPathsSource::getFileNamesTitle() const @@ -466,7 +457,7 @@ void QtProjectWizzardContentIndexedHeaderPaths::buttonClicked() dynamic_cast(m_filesDialog.get())->setPathsList( utility::convert( getIndexedPathsDerivedFromSonargraphProject(sonargraphSettings), - [&](const FilePath& path) { return path.getRelativeTo(projectPath); } + [&](const FilePath& path) { return utility::getAsRelativeIfShorter(path, projectPath); } ), sonargraphSettings->getIndexedHeaderPaths(), m_settings->getProjectDirectoryPath() @@ -498,7 +489,7 @@ void QtProjectWizzardContentIndexedHeaderPaths::buttonClicked() dynamic_cast(m_filesDialog.get())->setPathsList( utility::convert( getIndexedPathsDerivedFromCodeblocksProject(codeblocksSettings), - [&](const FilePath& path) { return path.getRelativeTo(projectPath); } + [&](const FilePath& path) { return utility::getAsRelativeIfShorter(path, projectPath); } ), codeblocksSettings->getIndexedHeaderPaths(), m_settings->getProjectDirectoryPath() @@ -530,7 +521,7 @@ void QtProjectWizzardContentIndexedHeaderPaths::buttonClicked() dynamic_cast(m_filesDialog.get())->setPathsList( utility::convert( getIndexedPathsDerivedFromCDB(cdbSettings), - [&](const FilePath& path) { return path.getRelativeTo(projectPath); } + [&](const FilePath& path) { return utility::getAsRelativeIfShorter(path, projectPath); } ), cdbSettings->getIndexedHeaderPaths(), m_settings->getProjectDirectoryPath() @@ -861,11 +852,14 @@ void QtProjectWizzardContentPathsHeaderSearch::showDetectedIncludesResult(const const std::set headerSearchPaths = utility::toSet(m_settings->makePathsExpandedAndAbsolute(m_list->getPathsAsDisplayed())); std::vector additionalHeaderSearchPaths; - for (const FilePath& detectedHeaderSearchPath : detectedHeaderSearchPaths) { - if (headerSearchPaths.find(detectedHeaderSearchPath) == headerSearchPaths.end()) + const FilePath relativeRoot = m_list->getRelativeRootDirectory(); + for (const FilePath& detectedHeaderSearchPath : detectedHeaderSearchPaths) { - additionalHeaderSearchPaths.push_back(detectedHeaderSearchPath); + if (headerSearchPaths.find(detectedHeaderSearchPath) == headerSearchPaths.end()) + { + additionalHeaderSearchPaths.push_back(utility::getAsRelativeIfShorter(detectedHeaderSearchPath, relativeRoot)); + } } } @@ -878,21 +872,9 @@ void QtProjectWizzardContentPathsHeaderSearch::showDetectedIncludesResult(const else { std::wstring detailedText = L""; - const FilePath relativeRoot = m_list->getRelativeRootDirectory(); for (const FilePath& path : additionalHeaderSearchPaths) { - if (!relativeRoot.empty()) - { - const FilePath relPath = path.getRelativeTo(relativeRoot); - if (relPath.wstr().size() < path.wstr().size()) - { - detailedText += relPath.wstr() + L"\n"; - } - else - { - detailedText += path.wstr() + L"\n"; - } - } + detailedText += path.wstr() + L"\n"; } m_filesDialog = std::make_shared(