|
| 1 | +/* |
| 2 | + * Copyright 2017 Kai Pastor |
| 3 | + * |
| 4 | + * This file is part of OpenOrienteering. |
| 5 | + * |
| 6 | + * OpenOrienteering is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * OpenOrienteering is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with OpenOrienteering. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | + |
| 21 | +#include "file_dialog.h" |
| 22 | + |
| 23 | +#include <algorithm> |
| 24 | +#include <iterator> |
| 25 | + |
| 26 | +#include <QtGlobal> |
| 27 | +#include <QStringRef> // IWYU pragma: keep |
| 28 | +#include <QVector> |
| 29 | + |
| 30 | +#ifndef QTBUG_51712_QUIRK_ENABLED |
| 31 | +# if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID) && !defined(QT_TESTLIB_LIB) |
| 32 | +# define QTBUG_51712_QUIRK_ENABLED 1 |
| 33 | +# else |
| 34 | +# define QTBUG_51712_QUIRK_ENABLED 0 |
| 35 | +# endif |
| 36 | +#endif |
| 37 | + |
| 38 | +#if QTBUG_51712_QUIRK_ENABLED |
| 39 | +# include <memory> |
| 40 | +# include <private/qguiapplication_p.h> |
| 41 | +# include <qpa/qplatformdialoghelper.h> |
| 42 | +# include <qpa/qplatformtheme.h> |
| 43 | +# include <QByteArray> |
| 44 | +# include <QLatin1Char> |
| 45 | +# include <QMetaObject> |
| 46 | +# include <QObject> |
| 47 | +# include <QStringList> |
| 48 | +#endif |
| 49 | + |
| 50 | + |
| 51 | +bool FileDialog::needUpperCaseExtensions() |
| 52 | +{ |
| 53 | +#if QTBUG_51712_QUIRK_ENABLED |
| 54 | + auto platform_theme = QGuiApplicationPrivate::platformTheme(); |
| 55 | + if (platform_theme |
| 56 | + && platform_theme->usePlatformNativeDialog(QPlatformTheme::DialogType::FileDialog)) |
| 57 | + { |
| 58 | + std::unique_ptr<QPlatformDialogHelper> helper { |
| 59 | + platform_theme->createPlatformDialogHelper(QPlatformTheme::DialogType::FileDialog) |
| 60 | + }; |
| 61 | + if (helper) |
| 62 | + return qstrncmp(helper->metaObject()->className(), "QGtk", 4) == 0; |
| 63 | + } |
| 64 | +#endif |
| 65 | + return false; |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +void FileDialog::adjustParameters(QString& filter, QFileDialog::Options& options) |
| 70 | +{ |
| 71 | + using std::begin; |
| 72 | + using std::end; |
| 73 | + |
| 74 | + constexpr int max_filter_length = 100; |
| 75 | + static const auto separator = QString::fromLatin1(";;"); |
| 76 | +#if QT_VERSION >= 0x50400 |
| 77 | + const auto filters = filter.splitRef(separator); |
| 78 | +#else |
| 79 | + const auto filters = filter.split(separator); |
| 80 | +#endif |
| 81 | + |
| 82 | + bool has_long_filters = std::any_of(begin(filters), end(filters), [](auto&& item) { |
| 83 | + return item.length() > max_filter_length; |
| 84 | + }); |
| 85 | + |
| 86 | +#if QTBUG_51712_QUIRK_ENABLED |
| 87 | + static auto need_upper_case = needUpperCaseExtensions(); |
| 88 | + if (need_upper_case) |
| 89 | + { |
| 90 | + QStringList new_filters; |
| 91 | + new_filters.reserve(filter.size()); |
| 92 | + for (auto&& item : filters) |
| 93 | + { |
| 94 | + QString new_item; |
| 95 | + new_item.reserve(2 * item.length()); |
| 96 | + auto split_0 = item.indexOf(QLatin1Char('(')); |
| 97 | + auto split_1 = item.lastIndexOf(QLatin1Char(')')); |
| 98 | + new_item.append(item.left(split_1)); |
| 99 | + new_item.append(QLatin1Char(' ')); |
| 100 | +#if QT_VERSION >= 0x50400 |
| 101 | + new_item.append(item.mid(split_0+1).toString().toUpper()); |
| 102 | +#else |
| 103 | + new_item.append(item.mid(split_0+1).toUpper()); |
| 104 | +#endif |
| 105 | + new_filters.append(new_item); |
| 106 | + if (new_item.length() > max_filter_length) |
| 107 | + has_long_filters = true; |
| 108 | + } |
| 109 | + filter = new_filters.join(separator); |
| 110 | + } |
| 111 | +#endif |
| 112 | + |
| 113 | + if (has_long_filters) |
| 114 | + options |= QFileDialog::HideNameFilterDetails; |
| 115 | +} |
0 commit comments