From 2d5d62fdbc41be6535655f0add2cf4eb1cd35986 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Wed, 1 Nov 2017 15:46:00 +0100 Subject: [PATCH] Restrict suggested file filters to registered supported mime types When using File > Open File or Project, and with Save As. It was containing all mime types registered in Qt Creator, including the default one from freedesktop.org, most of them being not interesting. Task-number: QTCREATORBUG-17913 Change-Id: Ic5e49b27b566eb41419240b78a6cd99b233aeb07 Reviewed-by: Tobias Hunger --- src/plugins/coreplugin/documentmanager.cpp | 30 ++++++++++++++++++- src/plugins/coreplugin/documentmanager.h | 2 ++ .../editormanager/editormanager.cpp | 2 +- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/plugins/coreplugin/documentmanager.cpp b/src/plugins/coreplugin/documentmanager.cpp index 8cc8b812469..7d7309f8ab2 100644 --- a/src/plugins/coreplugin/documentmanager.cpp +++ b/src/plugins/coreplugin/documentmanager.cpp @@ -27,6 +27,7 @@ #include "icore.h" #include "idocument.h" +#include "idocumentfactory.h" #include "coreconstants.h" #include @@ -710,6 +711,33 @@ bool DocumentManager::saveDocument(IDocument *document, const QString &fileName, return ret; } +template +QSet filterStrings() +{ + QSet filters; + for (FactoryType *factory : ExtensionSystem::PluginManager::getObjects()) { + for (const QString &mt : factory->mimeTypes()) { + const QString filter = mimeTypeForName(mt).filterString(); + if (!filter.isEmpty()) + filters.insert(filter); + } + } + return filters; +} + +QString DocumentManager::allDocumentFactoryFiltersString(QString *allFilesFilter = 0) +{ + const QSet uniqueFilters = filterStrings() + + filterStrings(); + QStringList filters = uniqueFilters.toList(); + filters.sort(); + const QString allFiles = Utils::allFilesFilterString(); + if (allFilesFilter) + *allFilesFilter = allFiles; + filters.prepend(allFiles); + return filters.join(QLatin1String(";;")); +} + QString DocumentManager::getSaveFileName(const QString &title, const QString &pathIn, const QString &filter, QString *selectedFilter) { @@ -771,7 +799,7 @@ QString DocumentManager::getSaveFileNameWithExtension(const QString &title, cons QString DocumentManager::getSaveAsFileName(const IDocument *document) { QTC_ASSERT(document, return QString()); - const QString filter = Utils::allFiltersString(); + const QString filter = allDocumentFactoryFiltersString(); const QString filePath = document->filePath().toString(); QString selectedFilter; QString fileDialogPath = filePath; diff --git a/src/plugins/coreplugin/documentmanager.h b/src/plugins/coreplugin/documentmanager.h index 0714cdf7bf3..5e3eda755fb 100644 --- a/src/plugins/coreplugin/documentmanager.h +++ b/src/plugins/coreplugin/documentmanager.h @@ -84,6 +84,8 @@ public: const QString &fileName = QString(), bool *isReadOnly = nullptr); + static QString allDocumentFactoryFiltersString(QString *allFilesFilter); + static QStringList getOpenFileNames(const QString &filters, const QString &path = QString(), QString *selectedFilter = nullptr); diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp index 261e3560d69..46c4dbd6b60 100644 --- a/src/plugins/coreplugin/editormanager/editormanager.cpp +++ b/src/plugins/coreplugin/editormanager/editormanager.cpp @@ -2726,7 +2726,7 @@ void EditorManager::addCloseEditorListener(const std::function QStringList EditorManager::getOpenFileNames() { QString selectedFilter; - const QString &fileFilters = Utils::allFiltersString(&selectedFilter); + const QString &fileFilters = DocumentManager::allDocumentFactoryFiltersString(&selectedFilter); return DocumentManager::getOpenFileNames(fileFilters, QString(), &selectedFilter); }