From 8ca4f909e8c4dd29ee1465b39605d71455a77ce6 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Wed, 24 May 2023 12:38:31 +0200 Subject: Examples: Move categorization to examplesparser For easier testing. Change-Id: I11f9de3f4fbcc2c85c196f1b59b2147e73ea8209 Reviewed-by: Christian Stenger Reviewed-by: Qt CI Bot --- src/plugins/qtsupport/exampleslistmodel.cpp | 52 --------------------------- src/plugins/qtsupport/examplesparser.cpp | 54 +++++++++++++++++++++++++++++ src/plugins/qtsupport/examplesparser.h | 3 ++ src/plugins/qtsupport/qtsupport_global.h | 12 +++++++ 4 files changed, 69 insertions(+), 52 deletions(-) diff --git a/src/plugins/qtsupport/exampleslistmodel.cpp b/src/plugins/qtsupport/exampleslistmodel.cpp index d3cf3535dd9..f18fefc34f2 100644 --- a/src/plugins/qtsupport/exampleslistmodel.cpp +++ b/src/plugins/qtsupport/exampleslistmodel.cpp @@ -4,7 +4,6 @@ #include "exampleslistmodel.h" #include "examplesparser.h" -#include "qtsupporttr.h" #include #include @@ -326,57 +325,6 @@ static bool isValidExampleOrDemo(ExampleItem *item) return ok || debugExamples(); } -static bool sortByHighlightedAndName(ExampleItem *first, ExampleItem *second) -{ - if (first->isHighlighted && !second->isHighlighted) - return true; - if (!first->isHighlighted && second->isHighlighted) - return false; - return first->name.compare(second->name, Qt::CaseInsensitive) < 0; -} - -static QList>> getCategories( - const QList &items, bool sortIntoCategories) -{ - static const QString otherDisplayName = Tr::tr("Other", "Category for all other examples"); - const bool useCategories = sortIntoCategories - || qtcEnvironmentVariableIsSet("QTC_USE_EXAMPLE_CATEGORIES"); - QList other; - QMap> categoryMap; - if (useCategories) { - for (ExampleItem *item : items) { - const QStringList itemCategories = item->metaData.value("category"); - for (const QString &category : itemCategories) - categoryMap[category].append(item); - if (itemCategories.isEmpty()) - other.append(item); - } - } - QList>> categories; - if (categoryMap.isEmpty()) { - // The example set doesn't define categories. Consider the "highlighted" ones as "featured" - QList featured; - QList allOther; - std::tie(featured, allOther) = Utils::partition(items, [](ExampleItem *i) { - return i->isHighlighted; - }); - if (!featured.isEmpty()) - categories.append({Tr::tr("Featured", "Category for highlighted examples"), featured}); - if (!allOther.isEmpty()) - categories.append({otherDisplayName, allOther}); - } else { - const auto end = categoryMap.constKeyValueEnd(); - for (auto it = categoryMap.constKeyValueBegin(); it != end; ++it) - categories.append(*it); - if (!other.isEmpty()) - categories.append({otherDisplayName, other}); - } - const auto end = categories.end(); - for (auto it = categories.begin(); it != end; ++it) - sort(it->second, sortByHighlightedAndName); - return categories; -} - void ExamplesViewController::updateExamples() { QString examplesInstallPath; diff --git a/src/plugins/qtsupport/examplesparser.cpp b/src/plugins/qtsupport/examplesparser.cpp index 5fd3161777c..bd6ff08918f 100644 --- a/src/plugins/qtsupport/examplesparser.cpp +++ b/src/plugins/qtsupport/examplesparser.cpp @@ -3,7 +3,10 @@ #include "examplesparser.h" +#include "qtsupporttr.h" + #include +#include #include #include @@ -298,4 +301,55 @@ expected_str> parseExamples(const QByteArray &manifestData, return items; } +static bool sortByHighlightedAndName(ExampleItem *first, ExampleItem *second) +{ + if (first->isHighlighted && !second->isHighlighted) + return true; + if (!first->isHighlighted && second->isHighlighted) + return false; + return first->name.compare(second->name, Qt::CaseInsensitive) < 0; +} + +QList>> getCategories(const QList &items, + bool sortIntoCategories) +{ + static const QString otherDisplayName = Tr::tr("Other", "Category for all other examples"); + const bool useCategories = sortIntoCategories + || qtcEnvironmentVariableIsSet("QTC_USE_EXAMPLE_CATEGORIES"); + QList other; + QMap> categoryMap; + if (useCategories) { + for (ExampleItem *item : items) { + const QStringList itemCategories = item->metaData.value("category"); + for (const QString &category : itemCategories) + categoryMap[category].append(item); + if (itemCategories.isEmpty()) + other.append(item); + } + } + QList>> categories; + if (categoryMap.isEmpty()) { + // The example set doesn't define categories. Consider the "highlighted" ones as "featured" + QList featured; + QList allOther; + std::tie(featured, allOther) = Utils::partition(items, [](ExampleItem *i) { + return i->isHighlighted; + }); + if (!featured.isEmpty()) + categories.append({Tr::tr("Featured", "Category for highlighted examples"), featured}); + if (!allOther.isEmpty()) + categories.append({otherDisplayName, allOther}); + } else { + const auto end = categoryMap.constKeyValueEnd(); + for (auto it = categoryMap.constKeyValueBegin(); it != end; ++it) + categories.append(*it); + if (!other.isEmpty()) + categories.append({otherDisplayName, other}); + } + const auto end = categories.end(); + for (auto it = categories.begin(); it != end; ++it) + sort(it->second, sortByHighlightedAndName); + return categories; +} + } // namespace QtSupport::Internal diff --git a/src/plugins/qtsupport/examplesparser.h b/src/plugins/qtsupport/examplesparser.h index 2d1afa54838..a7d5501bc51 100644 --- a/src/plugins/qtsupport/examplesparser.h +++ b/src/plugins/qtsupport/examplesparser.h @@ -44,6 +44,9 @@ QTSUPPORT_EXPORT Utils::expected_str> parseExamples( const Utils::FilePath &demosInstallPath, bool examples); +QTSUPPORT_TEST_EXPORT QList>> getCategories( + const QList &items, bool sortIntoCategories); + } // namespace QtSupport::Internal Q_DECLARE_METATYPE(QtSupport::Internal::ExampleItem *) diff --git a/src/plugins/qtsupport/qtsupport_global.h b/src/plugins/qtsupport/qtsupport_global.h index aca20f5aa27..60c9bf0c128 100644 --- a/src/plugins/qtsupport/qtsupport_global.h +++ b/src/plugins/qtsupport/qtsupport_global.h @@ -12,3 +12,15 @@ #else # define QTSUPPORT_EXPORT Q_DECL_IMPORT #endif + +#if defined(WITH_TESTS) +# if defined(QTSUPPORT_LIBRARY) +# define QTSUPPORT_TEST_EXPORT Q_DECL_EXPORT +# elif defined(QTSUPPORT_STATIC_LIBRARY) +# define QTSUPPORT_TEST_EXPORT +# else +# define QTSUPPORT_TEST_EXPORT Q_DECL_IMPORT +# endif +#else +# define QTSUPPORT_TEST_EXPORT +#endif -- cgit v1.2.3