* This is a convenience static function that will return one or more existing files selected by the * user. */
| 769 | * user. |
| 770 | */ |
| 771 | QStringList FileDialog::getOpenFileNames( |
| 772 | QWidget* parent, |
| 773 | const QString& caption, |
| 774 | const QString& dir, |
| 775 | const FilterList& filters, |
| 776 | qsizetype* selectedFilterIndex, |
| 777 | Options options |
| 778 | ) |
| 779 | { |
| 780 | ActionDisabler actionDisabler {}; |
| 781 | QString dirName = dir; |
| 782 | if (dirName.isEmpty()) { |
| 783 | dirName = getPreferredDialogDirectory(); |
| 784 | } |
| 785 | |
| 786 | QString windowTitle = caption; |
| 787 | if (windowTitle.isEmpty()) { |
| 788 | windowTitle = FileDialog::tr("Open"); |
| 789 | } |
| 790 | |
| 791 | options |= QFileDialog::HideNameFilterDetails; |
| 792 | |
| 793 | qsizetype actuallySelectedFilterIndex = selectedFilterIndex != nullptr ? *selectedFilterIndex |
| 794 | : -1; |
| 795 | |
| 796 | QStringList files; |
| 797 | if (DialogOptions::dontUseNativeFileDialog()) { |
| 798 | const bool showPatterns = FileDialogInternal::getPreferShowFilterPatterns(); |
| 799 | const auto qtFilterList = toQtFilterList(filters, showPatterns); |
| 800 | QList<QUrl> urls = fetchSidebarUrls(); |
| 801 | |
| 802 | options |= QFileDialog::DontUseNativeDialog; |
| 803 | |
| 804 | FileDialog dlg(parent); |
| 805 | dlg.setOptions(options); |
| 806 | dlg.setWindowTitle(windowTitle); |
| 807 | dlg.setSidebarUrls(urls); |
| 808 | auto iconprov = std::make_unique<FileIconProvider>(); |
| 809 | dlg.setIconProvider(iconprov.get()); |
| 810 | dlg.setFileMode(QFileDialog::ExistingFiles); |
| 811 | dlg.setAcceptMode(QFileDialog::AcceptOpen); |
| 812 | dlg.setDirectory(dirName); |
| 813 | dlg.setNameFilters(qtFilterList); |
| 814 | if (actuallySelectedFilterIndex >= 0) { |
| 815 | dlg.selectNameFilter(toQtFilter(filters[actuallySelectedFilterIndex], showPatterns)); |
| 816 | } |
| 817 | if (dlg.exec() == QDialog::Accepted) { |
| 818 | files = dlg.selectedFiles(); |
| 819 | } |
| 820 | // Non-native QFileDialog::selectedNameFilter() always returns a filter, even if |
| 821 | // the user cancelled or the dialog wasn't shown at all. |
| 822 | actuallySelectedFilterIndex = qtFilterList.indexOf(dlg.selectedNameFilter()); |
| 823 | if (actuallySelectedFilterIndex < 0) { |
| 824 | // Log an error since this happening means the code is incorrect |
| 825 | Base::Console().error( |
| 826 | "Non-native FileDialog returned a selected filter that wasn't in the original " |
| 827 | "list, defaulting to index 0\nProblem cause filter: \"%s\"\n", |
| 828 | dlg.selectedNameFilter().toStdString() |
no test coverage detected