diff options
author | Marco Bubke <marco.bubke@nokia.com> | 2011-09-20 12:54:18 +0200 |
---|---|---|
committer | Alessandro Portale <alessandro.portale@nokia.com> | 2011-09-20 13:29:31 +0200 |
commit | dfbdf287034fca3cc9c59c546d06fda2af3e67ba (patch) | |
tree | 1119541029f435331ab3328f052e6f2f4bf6901e /src/libs/utils/filenamevalidatinglineedit.cpp | |
parent | 71cbd765b2f887a68495b947e202ee4ca4295fd8 (diff) | |
download | qt-creator-dfbdf287034fca3cc9c59c546d06fda2af3e67ba.tar.gz |
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/5163
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Alessandro Portale <alessandro.portale@nokia.com>
Diffstat (limited to 'src/libs/utils/filenamevalidatinglineedit.cpp')
-rw-r--r-- | src/libs/utils/filenamevalidatinglineedit.cpp | 44 |
1 files changed, 41 insertions, 3 deletions
diff --git a/src/libs/utils/filenamevalidatinglineedit.cpp b/src/libs/utils/filenamevalidatinglineedit.cpp index 9a428eaf0e..a3e83a0594 100644 --- a/src/libs/utils/filenamevalidatinglineedit.cpp +++ b/src/libs/utils/filenamevalidatinglineedit.cpp @@ -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 |