forked from qt-creator/qt-creator
PathChooser: Replace virtual function with function object.
This allows calling code to add its own path validation without creating
a derived class.
Some notes on API decisions:
- Since all current users of this functionality call the base class
implementation in their derived function, no functionality is
provided to completely replace the path validation function (as
opposed to merely add checks). In the unlikely case that this is
ever needed, we can easily add it.
- The member function is called "setAdditionalValidator" rather than
the shorter "addValidator" because the latter might suggest that
repeated calls will chain the provided functions.
- There is also no functionality to conveniently remove the
additional validator, because such dynamic behavior was not needed
so far.
This patch only does the minimum changes to the calling sites that are
required for them to continue compiling and working. Removal of the
derived classes that are no longer needed happens in a follow-up patch.
Change-Id: I5282835b5dd227149748a7f567006beb288d8aa3
Reviewed-by: hjk <hjk@theqtcompany.com>
Reviewed-by: Eike Ziller <eike.ziller@theqtcompany.com>
This commit is contained in:
@@ -191,13 +191,15 @@ public:
|
||||
Environment m_environment;
|
||||
BinaryVersionToolTipEventFilter *m_binaryVersionToolTipEventFilter;
|
||||
QList<QAbstractButton *> m_buttons;
|
||||
PathChooser::PathValidator m_additionalValidator;
|
||||
};
|
||||
|
||||
PathChooserPrivate::PathChooserPrivate(PathChooser *chooser) :
|
||||
m_hLayout(new QHBoxLayout),
|
||||
m_lineEdit(new PathValidatingLineEdit(chooser)),
|
||||
m_acceptingKind(PathChooser::ExistingDirectory),
|
||||
m_binaryVersionToolTipEventFilter(0)
|
||||
m_binaryVersionToolTipEventFilter(0),
|
||||
m_additionalValidator([](const QString &, QString *) { return true; })
|
||||
{
|
||||
}
|
||||
|
||||
@@ -461,6 +463,11 @@ void PathChooser::triggerChanged()
|
||||
d->m_lineEdit->triggerChanged();
|
||||
}
|
||||
|
||||
void PathChooser::setAdditionalPathValidator(const PathChooser::PathValidator &pathValidator)
|
||||
{
|
||||
d->m_additionalValidator = pathValidator;
|
||||
}
|
||||
|
||||
bool PathChooser::validatePath(const QString &path, QString *errorMessage)
|
||||
{
|
||||
QString expandedPath = d->expandedPath(path);
|
||||
@@ -579,6 +586,10 @@ bool PathChooser::validatePath(const QString &path, QString *errorMessage)
|
||||
default:
|
||||
;
|
||||
}
|
||||
|
||||
if (!d->m_additionalValidator(path, errorMessage))
|
||||
return false;
|
||||
|
||||
if (errorMessage)
|
||||
*errorMessage = tr("Full path: <b>%1</b>").arg(QDir::toNativeSeparators(expandedPath));
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user