aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qtsupport/exampleslistmodel.cpp
diff options
context:
space:
mode:
authorEike Ziller <eike.ziller@qt.io>2023-03-09 17:02:58 +0100
committerEike Ziller <eike.ziller@qt.io>2023-03-16 13:50:06 +0000
commita2de016f64f91c154a222a2216c50f59e9350459 (patch)
tree2d847162784aa3b34d8f7e8b6423f5729249518c /src/plugins/qtsupport/exampleslistmodel.cpp
parentd5d7b1d19262780c5350a234ef27abbc3378335a (diff)
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 <christian.stenger@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Diffstat (limited to 'src/plugins/qtsupport/exampleslistmodel.cpp')
-rw-r--r--src/plugins/qtsupport/exampleslistmodel.cpp67
1 files changed, 54 insertions, 13 deletions
diff --git a/src/plugins/qtsupport/exampleslistmodel.cpp b/src/plugins/qtsupport/exampleslistmodel.cpp
index 40196934d4..c567f992e4 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<std::pair<QString, QList<ExampleItem *>>> getCategories(
+ const QList<ExampleItem *> &items)
+{
+ static const QString otherDisplayName = Tr::tr("Other", "Category for all other examples");
+ const bool useCategories = qtcEnvironmentVariableIsSet("QTC_USE_EXAMPLE_CATEGORIES");
+ QList<ExampleItem *> other;
+ QMap<QString, QList<ExampleItem *>> 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<std::pair<QString, QList<ExampleItem *>>> categories;
+ if (categoryMap.isEmpty()) {
+ // The example set doesn't define categories. Consider the "highlighted" ones as "featured"
+ QList<ExampleItem *> featured;
+ QList<ExampleItem *> 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<ExampleItem *> featured;
- QList<ExampleItem *> 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<ListItem *>(featured));
+ const QList<std::pair<QString, QList<ExampleItem *>>> sections = getCategories(items);
+ for (int i = 0; i < sections.size(); ++i) {
+ m_view->addSection({sections.at(i).first, i},
+ static_container_cast<ListItem *>(sections.at(i).second));
}
- m_view->addSection({Tr::tr("Other", "Category for all other examples"), 1},
- static_container_cast<ListItem *>(other));
}
void ExampleSetModel::updateQtVersionList()