+ fixes #0002209: Files that have a dot in name do not get the extension

This commit is contained in:
wmayer 2015-09-13 21:25:18 +02:00
parent d3281a66eb
commit b9865ed4f1
2 changed files with 22 additions and 2 deletions

View File

@ -69,6 +69,21 @@ void FileDialog::onSelectedFilter(const QString& filter)
}
}
bool FileDialog::hasSuffix(const QString& ext) const
{
QRegExp rx(QString::fromLatin1("\\*.(%1)\\W").arg(ext));
rx.setCaseSensitivity(Qt::CaseInsensitive);
QStringList filters = nameFilters();
for (QStringList::iterator it = filters.begin(); it != filters.end(); ++it) {
QString str = *it;
if (rx.indexIn(str) != -1) {
return true;
}
}
return false;
}
void FileDialog::accept()
{
// When saving to a file make sure that the entered filename ends with the selected
@ -78,8 +93,10 @@ void FileDialog::accept()
if (!files.isEmpty()) {
QString ext = this->defaultSuffix();
QString file = files.front();
QFileInfo fi(file);
if (!ext.isEmpty() && fi.suffix().isEmpty()) {
QString suffix = QFileInfo(file).suffix();
// #0001928: do not add a suffix if a file with suffix is entered
// #0002209: make sure that the entered suffix is part of one of the filters
if (!ext.isEmpty() && (suffix.isEmpty() || !hasSuffix(suffix))) {
file = QString::fromLatin1("%1.%2").arg(file).arg(ext);
// That's the built-in line edit
QLineEdit* fileNameEdit = this->findChild<QLineEdit*>(QString::fromLatin1("fileNameEdit"));

View File

@ -62,6 +62,9 @@ public:
void accept();
private:
bool hasSuffix(const QString&) const;
private Q_SLOTS:
void onSelectedFilter(const QString&);
};