Commit dfbdf287 authored by Marco Bubke's avatar Marco Bubke Committed by Alessandro Portale

Add extension property to FileNameValidatingLineEdit

Does nothing if extension is empty. The extension check is case
sensitive.

Change-Id: I58f89eb57d53146dae95e58d77cd8b47826b8f42
Reviewed-on: http://codereview.qt-project.org/5163Reviewed-by: default avatarQt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: default avatarAlessandro Portale <alessandro.portale@nokia.com>
parent 71cbd765
......@@ -68,8 +68,7 @@ static const QRegExp &windowsDeviceSubDirPattern()
// ----------- FileNameValidatingLineEdit
FileNameValidatingLineEdit::FileNameValidatingLineEdit(QWidget *parent) :
BaseValidatingLineEdit(parent),
m_allowDirectories(false),
m_unused(0)
m_allowDirectories(false)
{
}
......@@ -143,7 +142,46 @@ bool FileNameValidatingLineEdit::validateFileName(const QString &name,
bool FileNameValidatingLineEdit::validate(const QString &value, QString *errorMessage) const
{
return validateFileName(value, m_allowDirectories, errorMessage);
return validateFileNameExtension(value, requiredExtensions(), errorMessage)
&& validateFileName(value, allowDirectories(), errorMessage);
}
bool FileNameValidatingLineEdit::validateFileNameExtension(const QString &fileName,
const QStringList &requiredExtensions,
QString *errorMessage)
{
// file extension
if (!requiredExtensions.isEmpty()) {
foreach (const QString &requiredExtension, requiredExtensions) {
QString extension = QLatin1String(".") + requiredExtension;
if (fileName.endsWith(extension, Qt::CaseSensitive) && extension.count() < fileName.count()) {
return true;
}
}
if (errorMessage) {
if (requiredExtensions.count() == 1) {
*errorMessage = tr("File extension %1 is required:").arg(requiredExtensions.first());
} else {
*errorMessage = tr("File extensions %1 are required:").arg(requiredExtensions.join(", "));
}
}
return false;
}
return true;
}
QStringList FileNameValidatingLineEdit::requiredExtensions() const
{
return m_requiredExtensionList;
}
void FileNameValidatingLineEdit::setRequiredExtensions(const QStringList &extensions)
{
m_requiredExtensionList = extensions;
}
} // namespace Utils
......@@ -41,6 +41,7 @@ class QTCREATOR_UTILS_EXPORT FileNameValidatingLineEdit : public BaseValidatingL
{
Q_OBJECT
Q_PROPERTY(bool allowDirectories READ allowDirectories WRITE setAllowDirectories)
Q_PROPERTY(QStringList requiredExtensions READ requiredExtensions WRITE setRequiredExtensions)
public:
explicit FileNameValidatingLineEdit(QWidget *parent = 0);
......@@ -49,6 +50,10 @@ public:
bool allowDirectories = false,
QString *errorMessage = 0);
static bool validateFileNameExtension(const QString &name,
const QStringList &requiredExtensions = QStringList(),
QString *errorMessage = 0);
/**
* Sets whether entering directories is allowed. This will enable the user
* to enter slashes in the filename. Default is off.
......@@ -56,12 +61,19 @@ public:
bool allowDirectories() const;
void setAllowDirectories(bool v);
/**
* Sets a requred extension. If the extension is empty no extension is required.
* Default is empty.
*/
QStringList requiredExtensions() const;
void setRequiredExtensions(const QStringList &extensionList);
protected:
virtual bool validate(const QString &value, QString *errorMessage) const;
private:
bool m_allowDirectories;
void *m_unused;
QStringList m_requiredExtensionList;
};
} // namespace Utils
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment