Utils: Promote a local conversion function to FilePath::fromUrl()

Change-Id: I748cceb50084a29b971f31a8bd2ad7159738d7fd
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
hjk
2024-01-26 16:29:18 +01:00
parent f1787812e3
commit bb7a50e16e
3 changed files with 17 additions and 10 deletions

View File

@@ -182,6 +182,19 @@ FilePath FilePath::fromVariant(const QVariant &variant)
return fromSettings(variant); // FIXME: Use variant.value<FilePath>()
}
/*!
Constructs a FilePath from \a url.
\sa toVariant()
*/
FilePath FilePath::fromUrl(const QUrl &url)
{
FilePath result;
if (url.isLocalFile())
return FilePath::fromString(url.toLocalFile());
return FilePath::fromParts(url.scheme(), url.host(), url.path());
}
FilePath FilePath::fromParts(const QStringView scheme, const QStringView host, const QStringView path)
{
FilePath result;

View File

@@ -68,6 +68,7 @@ public:
[[nodiscard]] static FilePath fromUtf8(const char *filepath, int filepathSize = -1);
[[nodiscard]] static FilePath fromSettings(const QVariant &variant);
[[nodiscard]] static FilePath fromVariant(const QVariant &variant);
[[nodiscard]] static FilePath fromUrl(const QUrl &url);
[[nodiscard]] static FilePath fromParts(const QStringView scheme, const QStringView host, const QStringView path);
[[nodiscard]] static FilePath fromPathPart(const QStringView path);

View File

@@ -397,19 +397,12 @@ static QWidget *dialogParent(QWidget *parent)
return parent ? parent : s_dialogParentGetter ? s_dialogParentGetter() : nullptr;
}
static FilePath qUrlToFilePath(const QUrl &url)
{
if (url.isLocalFile())
return FilePath::fromString(url.toLocalFile());
return FilePath::fromParts(url.scheme(), url.host(), url.path());
}
static QUrl filePathToQUrl(const FilePath &filePath)
{
return QUrl::fromLocalFile(filePath.toFSPathString());
}
void prepareNonNativeDialog(QFileDialog &dialog)
static void prepareNonNativeDialog(QFileDialog &dialog)
{
const auto isValidSideBarPath = [](const FilePath &fp) {
return !fp.needsDevice() || fp.hasFileAccess();
@@ -422,7 +415,7 @@ void prepareNonNativeDialog(QFileDialog &dialog)
// Check existing urls, remove paths that need a device and are no longer valid.
for (const QUrl &url : dialog.sidebarUrls()) {
FilePath path = qUrlToFilePath(url);
FilePath path = FilePath::fromUrl(url);
if (isValidSideBarPath(path))
sideBarPaths.append(path);
}
@@ -467,7 +460,7 @@ FilePaths getFilePaths(QWidget *parent,
if (dialog.exec() == QDialog::Accepted) {
if (selectedFilter)
*selectedFilter = dialog.selectedNameFilter();
return Utils::transform(dialog.selectedUrls(), &qUrlToFilePath);
return Utils::transform(dialog.selectedUrls(), &FilePath::fromUrl);
}
return {};
}