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;
|
||||
|
||||
@@ -35,6 +35,8 @@
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
#include <functional>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QAbstractButton;
|
||||
class QLineEdit;
|
||||
@@ -108,7 +110,7 @@ public:
|
||||
/** Returns the suggested label title when used in a form layout. */
|
||||
static QString label();
|
||||
|
||||
virtual bool validatePath(const QString &path, QString *errorMessage = 0);
|
||||
bool validatePath(const QString &path, QString *errorMessage = 0);
|
||||
|
||||
/** Return the home directory, which needs some fixing under Windows. */
|
||||
static QString homePath();
|
||||
@@ -137,6 +139,10 @@ public:
|
||||
void setReadOnly(bool b);
|
||||
|
||||
void triggerChanged();
|
||||
|
||||
typedef std::function<bool(const QString &, QString *)> PathValidator;
|
||||
void setAdditionalPathValidator(const PathValidator &pathValidator);
|
||||
|
||||
private:
|
||||
// Returns overridden title or the one from <title>
|
||||
QString makeDialogTitle(const QString &title);
|
||||
|
||||
@@ -59,24 +59,17 @@ QStringList qt_clean_filter_list(const QString &filter)
|
||||
return f.split(QLatin1Char(' '), QString::SkipEmptyParts);
|
||||
}
|
||||
|
||||
LibraryPathChooser::LibraryPathChooser(QWidget *parent)
|
||||
: Utils::PathChooser(parent)
|
||||
static bool validateLibraryPath(const QString &path, const Utils::PathChooser *pathChooser,
|
||||
QString *errorMessage)
|
||||
{
|
||||
}
|
||||
|
||||
bool LibraryPathChooser::validatePath(const QString &path, QString *errorMessage)
|
||||
{
|
||||
bool result = PathChooser::validatePath(path, errorMessage);
|
||||
if (!result)
|
||||
return false;
|
||||
|
||||
Q_UNUSED(errorMessage);
|
||||
QFileInfo fi(path);
|
||||
if (!fi.exists())
|
||||
return false;
|
||||
|
||||
const QString fileName = fi.fileName();
|
||||
|
||||
QStringList filters = qt_clean_filter_list(promptDialogFilter());
|
||||
QStringList filters = qt_clean_filter_list(pathChooser->promptDialogFilter());
|
||||
for (int i = 0; i < filters.count(); i++) {
|
||||
QRegExp regExp(filters.at(i));
|
||||
regExp.setPatternSyntax(QRegExp::Wildcard);
|
||||
@@ -86,6 +79,15 @@ bool LibraryPathChooser::validatePath(const QString &path, QString *errorMessage
|
||||
return false;
|
||||
}
|
||||
|
||||
LibraryPathChooser::LibraryPathChooser(QWidget *parent)
|
||||
: Utils::PathChooser(parent)
|
||||
{
|
||||
setAdditionalPathValidator([this](const QString &path, QString *errorMessage) {
|
||||
return validateLibraryPath(path, this, errorMessage);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
AddLibraryWizard::AddLibraryWizard(const QString &fileName, QWidget *parent) :
|
||||
Utils::Wizard(parent), m_proFile(fileName)
|
||||
{
|
||||
|
||||
@@ -149,7 +149,6 @@ class LibraryPathChooser : public Utils::PathChooser
|
||||
Q_OBJECT
|
||||
public:
|
||||
LibraryPathChooser(QWidget *parent);
|
||||
virtual bool validatePath(const QString &path, QString *errorMessage);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -72,15 +72,15 @@ NdkPathChooser::NdkPathChooser(Mode mode, QWidget *parent)
|
||||
setPromptDialogFilter(Utils::HostOsInfo::isWindowsHost() ? QLatin1String("*.bat") :
|
||||
QLatin1String("*.sh"));
|
||||
}
|
||||
setAdditionalPathValidator([this](const QString &path, QString *errorMessage) {
|
||||
return validateNdkPath(path, errorMessage);
|
||||
});
|
||||
}
|
||||
|
||||
bool NdkPathChooser::validatePath(const QString &path, QString *errorMessage)
|
||||
bool NdkPathChooser::validateNdkPath(const QString &path, QString *errorMessage) const
|
||||
{
|
||||
bool result = PathChooser::validatePath(path, errorMessage);
|
||||
if (!result)
|
||||
return false;
|
||||
|
||||
if (m_mode == NdkPathChooser::InstallMode)
|
||||
Q_UNUSED(errorMessage);
|
||||
if (m_mode == InstallMode)
|
||||
return !(QnxUtils::sdkInstallerPath(path).isEmpty());
|
||||
|
||||
QFileInfo fi(path);
|
||||
|
||||
@@ -63,9 +63,10 @@ public:
|
||||
};
|
||||
|
||||
NdkPathChooser(Mode mode, QWidget *parent = 0);
|
||||
virtual bool validatePath(const QString &path, QString *errorMessage);
|
||||
|
||||
private:
|
||||
bool validateNdkPath(const QString &path, QString *errorMessage) const;
|
||||
|
||||
Mode m_mode;
|
||||
};
|
||||
|
||||
|
||||
@@ -36,22 +36,8 @@
|
||||
namespace Qnx {
|
||||
namespace Internal {
|
||||
|
||||
SrcProjectPathChooser::SrcProjectPathChooser(QWidget *parent) :
|
||||
Utils::PathChooser(parent)
|
||||
static bool validateProjectPath(const QString &path, QString *errorMessage)
|
||||
{
|
||||
setPromptDialogTitle(tr("Choose imported Cascades project directory"));
|
||||
setExpectedKind(Utils::PathChooser::ExistingDirectory);
|
||||
}
|
||||
|
||||
SrcProjectPathChooser::~SrcProjectPathChooser()
|
||||
{
|
||||
}
|
||||
|
||||
bool SrcProjectPathChooser::validatePath(const QString &path, QString *errorMessage)
|
||||
{
|
||||
if (!Utils::PathChooser::validatePath(path, errorMessage))
|
||||
return false;
|
||||
|
||||
bool proFound = false;
|
||||
bool barDescriptorFound = false;
|
||||
QDirIterator di(path);
|
||||
@@ -68,11 +54,21 @@ bool SrcProjectPathChooser::validatePath(const QString &path, QString *errorMess
|
||||
break;
|
||||
}
|
||||
const bool ret = barDescriptorFound && proFound;
|
||||
if (!ret && errorMessage)
|
||||
*errorMessage = tr("Directory does not seem to be a valid Cascades project.");
|
||||
if (!ret && errorMessage) {
|
||||
*errorMessage = QCoreApplication::translate("Qnx",
|
||||
"Directory does not seem to be a valid Cascades project.");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
SrcProjectPathChooser::SrcProjectPathChooser(QWidget *parent) :
|
||||
Utils::PathChooser(parent)
|
||||
{
|
||||
setPromptDialogTitle(tr("Choose imported Cascades project directory"));
|
||||
setExpectedKind(Utils::PathChooser::ExistingDirectory);
|
||||
setAdditionalPathValidator(validateProjectPath);
|
||||
}
|
||||
|
||||
} // namespace Internal
|
||||
} // namespace Qnx
|
||||
|
||||
@@ -42,9 +42,6 @@ class SrcProjectPathChooser : public Utils::PathChooser
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit SrcProjectPathChooser(QWidget *parent = 0);
|
||||
virtual ~SrcProjectPathChooser();
|
||||
|
||||
virtual bool validatePath(const QString &path, QString *errorMessage = 0);
|
||||
};
|
||||
|
||||
} // namespace Internal
|
||||
|
||||
Reference in New Issue
Block a user