logic: paths that are automatically filled by the project setup wizard will be made relative if they are shorter if the absolute paths

This commit is contained in:
mlangkabel
2018-06-15 18:39:03 +02:00
parent b6588cd6ba
commit bbf462d1fb
10 changed files with 74 additions and 152 deletions
-27
View File
@@ -57,33 +57,6 @@ std::set<FilePath> FileManager::getAllSourceFilePaths() const
return m_allSourceFilePaths;
}
std::set<FilePath> FileManager::getAllSourceFilePathsRelative(const FilePath& baseDirectory) const
{
std::set<FilePath> absolutePaths;
for (const FilePath& path: getAllSourceFilePaths())
{
if (baseDirectory.exists())
{
absolutePaths.insert(path.getRelativeTo(baseDirectory));
}
else
{
absolutePaths.insert(path);
}
}
return absolutePaths;
}
std::vector<FilePath> FileManager::makeCanonical(const std::vector<FilePath>& filePaths)
{
std::vector<FilePath> 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)
-2
View File
@@ -29,10 +29,8 @@ public:
// returns a list of paths to all files that reside in the non-excluded source paths
std::set<FilePath> getAllSourceFilePaths() const;
std::set<FilePath> getAllSourceFilePathsRelative(const FilePath& baseDirectory) const;
private:
std::vector<FilePath> makeCanonical(const std::vector<FilePath>& filePaths);
bool isExcluded(const FilePath& filePath) const;
std::vector<FilePath> m_sourcePaths;
+19
View File
@@ -26,3 +26,22 @@ std::vector<FilePath> utility::getTopLevelPaths(const std::set<FilePath>& 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<FilePath> utility::getAsRelativeIfShorter(const std::vector<FilePath>& absolutePaths, const FilePath& baseDirectory)
{
return utility::convert<FilePath, FilePath>(absolutePaths, [&](const FilePath& path) { return getAsRelativeIfShorter(path, baseDirectory); });
}
+3
View File
@@ -10,6 +10,9 @@ namespace utility
{
std::vector<FilePath> getTopLevelPaths(const std::vector<FilePath>& paths);
std::vector<FilePath> getTopLevelPaths(const std::set<FilePath>& paths);
FilePath getAsRelativeIfShorter(const FilePath& absolutePath, const FilePath& baseDirectory);
std::vector<FilePath> getAsRelativeIfShorter(const std::vector<FilePath>& absolutePaths, const FilePath& baseDirectory);
}
#endif // UTILITY_FILE_H
+5 -13
View File
@@ -6,10 +6,10 @@
#include <QPainter>
#include <QStyleOption>
#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();
}
}
+4 -10
View File
@@ -6,6 +6,7 @@
#include <QMimeData>
#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()));
}
}
+1 -1
View File
@@ -26,7 +26,7 @@ public:
void setPaths(const std::vector<FilePath>& 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;
+2 -2
View File
@@ -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()));
}
@@ -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<FilePath> allSourceFilePaths;
if (std::shared_ptr<SourceGroupSettingsCxxSonargraph> settings = std::dynamic_pointer_cast<SourceGroupSettingsCxxSonargraph>(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<FilePath> QtProjectWizzardContentPathSourceMaven::getFilePaths() con
}
}
std::vector<FilePath> 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<SourceGroupSettings> QtProjectWizzardContentPathSourceMaven::getSourceGroupSettings()
{
@@ -689,18 +658,10 @@ void QtProjectWizzardContentPathSourceGradle::save()
std::vector<FilePath> QtProjectWizzardContentPathSourceGradle::getFilePaths() const
{
std::vector<FilePath> 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<SourceGroupSettings> QtProjectWizzardContentPathSourceGradle::getSourceGroupSettings()
@@ -249,19 +249,10 @@ std::vector<FilePath> QtProjectWizzardContentPathsSource::getFilePaths() const
allSourceFilePaths = SourceGroupJavaEmpty(settings).getAllSourceFilePaths();
}
std::vector<FilePath> 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<QtSelectPathsDialog*>(m_filesDialog.get())->setPathsList(
utility::convert<FilePath, FilePath>(
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<QtSelectPathsDialog*>(m_filesDialog.get())->setPathsList(
utility::convert<FilePath, FilePath>(
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<QtSelectPathsDialog*>(m_filesDialog.get())->setPathsList(
utility::convert<FilePath, FilePath>(
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<FilePath> headerSearchPaths = utility::toSet(m_settings->makePathsExpandedAndAbsolute(m_list->getPathsAsDisplayed()));
std::vector<FilePath> 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<QtTextEditDialog>(