forked from qt-creator/qt-creator
Utils: Simplify pathchooser validation
Allow to set a default value for the PathChooser which will be used on the one hand as placeHolderText and on the other hand for the default validation if the underlying line edit has no text set. Change-Id: I49d2f773cf3933cb30b54ee6de82290b480e743d Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -189,6 +189,7 @@ public:
|
|||||||
QString m_dialogTitleOverride;
|
QString m_dialogTitleOverride;
|
||||||
QString m_dialogFilter;
|
QString m_dialogFilter;
|
||||||
QString m_initialBrowsePathOverride;
|
QString m_initialBrowsePathOverride;
|
||||||
|
QString m_defaultValue;
|
||||||
FilePath m_baseDirectory;
|
FilePath m_baseDirectory;
|
||||||
Environment m_environment;
|
Environment m_environment;
|
||||||
BinaryVersionToolTipEventFilter *m_binaryVersionToolTipEventFilter = nullptr;
|
BinaryVersionToolTipEventFilter *m_binaryVersionToolTipEventFilter = nullptr;
|
||||||
@@ -521,6 +522,12 @@ void PathChooser::setAboutToShowContextMenuHandler(PathChooser::AboutToShowConte
|
|||||||
s_aboutToShowContextMenuHandler = handler;
|
s_aboutToShowContextMenuHandler = handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PathChooser::setDefaultValue(const QString &defaultValue)
|
||||||
|
{
|
||||||
|
d->m_defaultValue = defaultValue;
|
||||||
|
d->m_lineEdit->setPlaceholderText(defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
FancyLineEdit::ValidationFunction PathChooser::defaultValidationFunction() const
|
FancyLineEdit::ValidationFunction PathChooser::defaultValidationFunction() const
|
||||||
{
|
{
|
||||||
return std::bind(&PathChooser::validatePath, this, std::placeholders::_1, std::placeholders::_2);
|
return std::bind(&PathChooser::validatePath, this, std::placeholders::_1, std::placeholders::_2);
|
||||||
@@ -528,15 +535,19 @@ FancyLineEdit::ValidationFunction PathChooser::defaultValidationFunction() const
|
|||||||
|
|
||||||
bool PathChooser::validatePath(FancyLineEdit *edit, QString *errorMessage) const
|
bool PathChooser::validatePath(FancyLineEdit *edit, QString *errorMessage) const
|
||||||
{
|
{
|
||||||
const QString path = edit->text();
|
QString path = edit->text();
|
||||||
QString expandedPath = d->expandedPath(path);
|
|
||||||
|
|
||||||
if (path.isEmpty()) {
|
if (path.isEmpty()) {
|
||||||
|
if (!d->m_defaultValue.isEmpty()) {
|
||||||
|
path = d->m_defaultValue;
|
||||||
|
} else {
|
||||||
if (errorMessage)
|
if (errorMessage)
|
||||||
*errorMessage = tr("The path must not be empty.");
|
*errorMessage = tr("The path must not be empty.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const QString expandedPath = d->expandedPath(path);
|
||||||
if (expandedPath.isEmpty()) {
|
if (expandedPath.isEmpty()) {
|
||||||
if (errorMessage)
|
if (errorMessage)
|
||||||
*errorMessage = tr("The path \"%1\" expanded to an empty string.").arg(QDir::toNativeSeparators(path));
|
*errorMessage = tr("The path \"%1\" expanded to an empty string.").arg(QDir::toNativeSeparators(path));
|
||||||
|
@@ -153,6 +153,10 @@ public:
|
|||||||
// Deprecated. Use filePath()
|
// Deprecated. Use filePath()
|
||||||
FilePath fileName() const { return filePath(); }
|
FilePath fileName() const { return filePath(); }
|
||||||
|
|
||||||
|
// this sets the placeHolderText to defaultValue and enables to use this as
|
||||||
|
// input value during validation if the real value is empty
|
||||||
|
// setting an empty QString will disable this and clear the placeHolderText
|
||||||
|
void setDefaultValue(const QString &defaultValue);
|
||||||
private:
|
private:
|
||||||
bool validatePath(FancyLineEdit *edit, QString *errorMessage) const;
|
bool validatePath(FancyLineEdit *edit, QString *errorMessage) const;
|
||||||
// Returns overridden title or the one from <title>
|
// Returns overridden title or the one from <title>
|
||||||
|
@@ -49,24 +49,10 @@ static void setupPathChooser(Utils::PathChooser *const chooser,
|
|||||||
const QString &historyCompleterId)
|
const QString &historyCompleterId)
|
||||||
{
|
{
|
||||||
chooser->setPromptDialogTitle(promptDiaglogTitle);
|
chooser->setPromptDialogTitle(promptDiaglogTitle);
|
||||||
chooser->lineEdit()->setPlaceholderText(placeHolderText);
|
chooser->setDefaultValue(placeHolderText);
|
||||||
chooser->setPath(pathFromSettings);
|
chooser->setPath(pathFromSettings);
|
||||||
chooser->setExpectedKind(Utils::PathChooser::ExistingCommand);
|
chooser->setExpectedKind(Utils::PathChooser::ExistingCommand);
|
||||||
chooser->setHistoryCompleter(historyCompleterId);
|
chooser->setHistoryCompleter(historyCompleterId);
|
||||||
chooser->setValidationFunction([chooser](Utils::FancyLineEdit *edit, QString *errorMessage) {
|
|
||||||
const QString currentFilePath = chooser->filePath().toString();
|
|
||||||
Utils::PathChooser pc;
|
|
||||||
Utils::PathChooser *helperPathChooser;
|
|
||||||
if (currentFilePath.isEmpty()) {
|
|
||||||
pc.setExpectedKind(chooser->expectedKind());
|
|
||||||
pc.setPath(edit->placeholderText());
|
|
||||||
helperPathChooser = &pc;
|
|
||||||
} else {
|
|
||||||
helperPathChooser = chooser;
|
|
||||||
}
|
|
||||||
|
|
||||||
return chooser->defaultValidationFunction()(helperPathChooser->lineEdit(), errorMessage);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SettingsWidget *SettingsWidget::instance()
|
SettingsWidget *SettingsWidget::instance()
|
||||||
|
Reference in New Issue
Block a user