Examples: Move categorization to examplesparser

For easier testing.

Change-Id: I11f9de3f4fbcc2c85c196f1b59b2147e73ea8209
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Eike Ziller
2023-05-24 12:38:31 +02:00
parent 0d3d4892c6
commit 8ca4f909e8
4 changed files with 69 additions and 52 deletions

View File

@@ -4,7 +4,6 @@
#include "exampleslistmodel.h"
#include "examplesparser.h"
#include "qtsupporttr.h"
#include <QBuffer>
#include <QApplication>
@@ -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<std::pair<QString, QList<ExampleItem *>>> getCategories(
const QList<ExampleItem *> &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<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;