Utils: Handle resolving paths and validation more appropriate

Manually editing the path inside a PathChooser will trigger some
validation and may end up in constructing some invalid paths
but still marking them as valid.
Beside this it will make the cursor jump to different locations
while typing which ends up in creating even more incorrect paths.
So, avoid creating invalid paths and mark the resulting path
as invalid if it is.

Change-Id: I27f55ae03745474fc6b91c44cdbc307c581fb2f8
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Christian Stenger
2021-08-05 07:40:41 +02:00
parent c63d72a8e8
commit 400426ed9f

View File

@@ -231,8 +231,19 @@ FilePath PathChooserPrivate::expandedPath(const QString &input) const
case PathChooser::ExistingDirectory:
case PathChooser::File:
case PathChooser::SaveFile:
if (!m_baseDirectory.isEmpty())
return m_baseDirectory.resolvePath(path.path()).absoluteFilePath();
if (!m_baseDirectory.isEmpty()) {
Utils::FilePath fp = m_baseDirectory.resolvePath(path.path()).absoluteFilePath();
// FIXME bad hotfix for manually editing PathChooser (invalid paths, jumping cursor)
// examples: have an absolute path and try to change the device letter by typing the new
// letter and removing the original afterwards ends up in
// D:\\dev\\project\\cD:\\dev\\build-project (before trying to remove the original)
// as 'cD:\\dev\\build-project' is considered is handled as being relative
// input = "cD:\\dev\build-project"; // prepended 'c' to change the device letter
// m_baseDirectory = "D:\\dev\\project"
if (!fp.needsDevice() && HostOsInfo::isWindowsHost() && fp.toString().count(':') > 1)
return path;
return fp;
}
break;
}
return path;
@@ -588,6 +599,12 @@ bool PathChooser::validatePath(FancyLineEdit *edit, QString *errorMessage) const
*errorMessage = tr("The path \"%1\" is not a directory.").arg(filePath.toUserOutput());
return false;
}
if (HostOsInfo::isWindowsHost() && !filePath.startsWithDriveLetter()
&& !filePath.startsWith("\\\\") && !filePath.startsWith("//")) {
if (errorMessage)
*errorMessage = tr("Invalid path \"%1\".").arg(filePath.toUserOutput());
return false;
}
break;
case PathChooser::Command:
if (filePath.exists() && !filePath.isExecutableFile()) {