Commit 0dafb940 authored by Elvis Angelaccio's avatar Elvis Angelaccio

Make sure we always set a default mime filter in save mode

KFileWidget::setMimeFilter() documentation says a default mime filter
should be set if the dialog is being open in save mode.

Without a default filter, the dialog uses a custom item (created by
concatenating all the mime type filters) as default. This is not useful
because it means an additional click for the user to select a filter that can
actually be useful.

If the initiallySelectedMimeTypeFilter() is empty, we use the first mime
filter as fallback.

Test Plan:
tests/qfiledialogtest --mimeTypeFilter application/json --mimeTypeFilter application/zip --acceptMode save

New unit test passes.

Reviewers: #plasma, dfaure

Differential Revision: https://phabricator.kde.org/D5956
parent 708ff0d0
......@@ -111,6 +111,16 @@ private Q_SLOTS:
QCOMPARE(dialog.selectedMimeTypeFilter(), targetMimeTypeFilter);
}
void testFallbackOnFirstFilterInSaveMode()
{
QFileDialog dialog;
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setMimeTypeFilters({QStringLiteral("application/json"), QStringLiteral("application/zip")});
dialog.show();
QCOMPARE(dialog.selectedMimeTypeFilter(), QStringLiteral("application/json"));
}
#endif
......
......@@ -298,7 +298,16 @@ void KDEPlatformFileDialogHelper::initializeDialog()
const QStringList mimeFilters = options()->mimeTypeFilters();
const QStringList nameFilters = options()->nameFilters();
if (!mimeFilters.isEmpty()) {
dialog->m_fileWidget->setMimeFilter(mimeFilters);
QString defaultMimeFilter;
if (options()->acceptMode() == QFileDialogOptions::AcceptSave) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
defaultMimeFilter = options()->initiallySelectedMimeTypeFilter();
#endif
if (defaultMimeFilter.isEmpty()) {
defaultMimeFilter = mimeFilters.at(0);
}
}
dialog->m_fileWidget->setMimeFilter(mimeFilters, defaultMimeFilter);
if ( mimeFilters.contains( QStringLiteral("inode/directory") ) )
dialog->m_fileWidget->setMode( dialog->m_fileWidget->mode() | KFile::Directory );
......
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