diff --git a/autotests/kfiledialog_unittest.cpp b/autotests/kfiledialog_unittest.cpp --- a/autotests/kfiledialog_unittest.cpp +++ b/autotests/kfiledialog_unittest.cpp @@ -74,6 +74,23 @@ QCOMPARE(dialog.selectedNameFilter(), selectNameFilter); } + void testSelectNameFilterMultipleMatching() + { + QFileDialog dialog; + + QStringList nameFilterList = QStringList() << QStringLiteral("c (*.cpp)") << QStringLiteral("h1 (*.h)") << QStringLiteral("h2 (*.h)"); + dialog.setNameFilters(nameFilterList); + QCOMPARE(dialog.nameFilters(), nameFilterList); + + QString selectNameFilter(QStringLiteral("h2 (*.h)")); + dialog.selectNameFilter(selectNameFilter); + QEXPECT_FAIL("", "Does currently not work. Works, once the dialog gets shown, though.", Continue); + QCOMPARE(dialog.selectedNameFilter(), selectNameFilter); + + dialog.show(); + QCOMPARE(dialog.selectedNameFilter(), selectNameFilter); + } + void testSelectedMimeTypeFilter_data() { QTest::addColumn("mimeTypeFilters"); diff --git a/src/platformtheme/kdeplatformfiledialogbase_p.h b/src/platformtheme/kdeplatformfiledialogbase_p.h --- a/src/platformtheme/kdeplatformfiledialogbase_p.h +++ b/src/platformtheme/kdeplatformfiledialogbase_p.h @@ -40,6 +40,7 @@ virtual void selectFile(const QUrl &filename) = 0; virtual QString selectedMimeTypeFilter() = 0; virtual QString selectedNameFilter() = 0; + virtual QString currentFilterText() = 0; virtual QList selectedFiles() = 0; void delayedShow(); diff --git a/src/platformtheme/kdeplatformfiledialoghelper.h b/src/platformtheme/kdeplatformfiledialoghelper.h --- a/src/platformtheme/kdeplatformfiledialoghelper.h +++ b/src/platformtheme/kdeplatformfiledialoghelper.h @@ -42,6 +42,7 @@ void setCustomLabel(QFileDialogOptions::DialogLabel label, const QString & text); QString selectedMimeTypeFilter() override; QString selectedNameFilter() override; + QString currentFilterText() override; QList selectedFiles() override; protected: diff --git a/src/platformtheme/kdeplatformfiledialoghelper.cpp b/src/platformtheme/kdeplatformfiledialoghelper.cpp --- a/src/platformtheme/kdeplatformfiledialoghelper.cpp +++ b/src/platformtheme/kdeplatformfiledialoghelper.cpp @@ -77,16 +77,17 @@ /* * Map a KDE filter string into a Qt one. */ -static QString kde2QtFilter(const QStringList &list, const QString &kde) +static QString kde2QtFilter(const QStringList &list, const QString &kde, const QString &filterText) { QStringList::const_iterator it(list.constBegin()), end(list.constEnd()); int pos; for (; it != end; ++it) { if (-1 != (pos = it->indexOf(kde)) && pos > 0 && (QLatin1Char('(') == (*it)[pos - 1] || QLatin1Char(' ') == (*it)[pos - 1]) && it->length() >= kde.length() + pos && - (QLatin1Char(')') == (*it)[pos + kde.length()] || QLatin1Char(' ') == (*it)[pos + kde.length()])) { + (QLatin1Char(')') == (*it)[pos + kde.length()] || QLatin1Char(' ') == (*it)[pos + kde.length()]) && + (filterText.isEmpty() || it->startsWith(filterText))) { return *it; } } @@ -206,6 +207,11 @@ return m_fileWidget->filterWidget()->currentFilter(); } +QString KDEPlatformFileDialog::currentFilterText() +{ + return m_fileWidget->filterWidget()->currentText(); +} + void KDEPlatformFileDialog::selectMimeTypeFilter(const QString &filter) { m_fileWidget->filterWidget()->setCurrentFilter(filter); @@ -404,7 +410,7 @@ QString KDEPlatformFileDialogHelper::selectedNameFilter() const { - return kde2QtFilter(options()->nameFilters(), m_dialog->selectedNameFilter()); + return kde2QtFilter(options()->nameFilters(), m_dialog->selectedNameFilter(), m_dialog->currentFilterText()); } QUrl KDEPlatformFileDialogHelper::directory() const diff --git a/src/platformtheme/kdirselectdialog.cpp b/src/platformtheme/kdirselectdialog.cpp --- a/src/platformtheme/kdirselectdialog.cpp +++ b/src/platformtheme/kdirselectdialog.cpp @@ -568,6 +568,11 @@ return QString(); } +QString KDirSelectDialog::currentFilterText() +{ + return QString(); +} + void KDirSelectDialog::selectFile(const QUrl &filename) { Q_UNUSED(filename) diff --git a/src/platformtheme/kdirselectdialog_p.h b/src/platformtheme/kdirselectdialog_p.h --- a/src/platformtheme/kdirselectdialog_p.h +++ b/src/platformtheme/kdirselectdialog_p.h @@ -104,6 +104,7 @@ void selectFile(const QUrl &filename) override; QString selectedMimeTypeFilter() override; QString selectedNameFilter() override; + QString currentFilterText() override; QList selectedFiles() override; public Q_SLOTS: