From e6414d99414a7ee943ce43a6f361f720a6f0d455 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Thu, 9 Mar 2023 17:02:58 +0100 Subject: [PATCH] Examples: Optionally parse categories from meta data Optionally parses example categories from the examples manifests, when setting the environment variable QTC_USE_EXAMPLE_CATEGORIES (can be done in Qt Creator's Environment > System > Environment settings). It doesn't make sense to unconditionally enable that yet, because only few examples actually have categories so far, so we will need to wait until some Qt version is suited for enabling this. If an example set does not provide categories, the "highlighted" property is used to provide a "Featured" category, as before. If an example set does provide categories, these are shown instead, sorted alphabetically, and examples with the "highlighted" property are put at the front of the category, overriding the otherwise alphabetical listing inside the categories. Examples without a category are put into a separate "Other" category at the end. Task-number: QTCREATORBUG-28546 Change-Id: I7ca312686eae13e16961def1b4b36ffd7050a447 Reviewed-by: Christian Stenger Reviewed-by: (cherry picked from commit a2de016f64f91c154a222a2216c50f59e9350459) --- src/plugins/qtsupport/exampleslistmodel.cpp | 67 +++++++++++++++++---- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/src/plugins/qtsupport/exampleslistmodel.cpp b/src/plugins/qtsupport/exampleslistmodel.cpp index 40196934d46..c567f992e41 100644 --- a/src/plugins/qtsupport/exampleslistmodel.cpp +++ b/src/plugins/qtsupport/exampleslistmodel.cpp @@ -320,6 +320,56 @@ 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) +{ + static const QString otherDisplayName = Tr::tr("Other", "Category for all other examples"); + const bool useCategories = 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; @@ -363,21 +413,12 @@ void ExamplesViewController::updateExamples() [](ExampleItem *item) { return item->tags.contains("ios"); }); } } - Utils::sort(items, [](ExampleItem *first, ExampleItem *second) { - return first->name.compare(second->name, Qt::CaseInsensitive) < 0; - }); - QList featured; - QList other; - std::tie(featured, other) = Utils::partition(items, - [](ExampleItem *i) { return i->isHighlighted; }); - - if (!featured.isEmpty()) { - m_view->addSection({Tr::tr("Featured", "Category for highlighted examples"), 0}, - static_container_cast(featured)); + const QList>> sections = getCategories(items); + for (int i = 0; i < sections.size(); ++i) { + m_view->addSection({sections.at(i).first, i}, + static_container_cast(sections.at(i).second)); } - m_view->addSection({Tr::tr("Other", "Category for all other examples"), 1}, - static_container_cast(other)); } void ExampleSetModel::updateQtVersionList()