Split up subcategories for KNS categories

This adds functionality to programmatically add subcategories (with a
suitable warning not to do so after the initialisation stage), and
uses that to perform a splitting of the subcategories supplied the
KNewStuff backend.

TEST PLAN
Previously: Many categories, with annoying and bad readability
With patch: A nice dig-down tree of categories in the KDE Store
applications category

Differential Revision: https://phabricator.kde.org/D19925
parent 7765acd5
......@@ -222,6 +222,19 @@ void Category::addSubcategory(QVector< Category* >& list, Category* newcat)
Q_ASSERT(isSorted(list));
}
void Category::addSubcategory(Category* cat)
{
int i = 0;
for(Category* subCat : m_subCategories) {
if(!categoryLessThan(subCat, cat)) {
break;
}
++i;
}
m_subCategories.insert(i, cat);
Q_ASSERT(isSorted(m_subCategories));
}
bool Category::blacklistPluginsInVector(const QSet<QString>& pluginNames, QVector<Category *>& subCategories)
{
bool ret = false;
......
......@@ -68,6 +68,12 @@ public:
static void sortCategories(QVector<Category*>& cats);
static void addSubcategory(QVector<Category*>& cats, Category* cat);
/**
* Add a subcategory to this category. This function should only
* be used during the initialisation stage, before adding the local
* root category to the global root category model.
*/
void addSubcategory(Category* cat);
void parseData(const QString& path, const QDomNode& data);
bool blacklistPlugins(const QSet<QString>& pluginName);
bool isAddons() const { return m_isAddons; }
......
......@@ -143,6 +143,17 @@ KNSBackend::KNSBackend(QObject* parent, const QString& iconName, const QString &
categories << new Category(cat, {}, { {CategoryFilter, cat } }, backendName, {}, {}, true);
}
QVector<Category*> topCategories{categories};
for (const auto &cat: categories) {
const QString catName = cat->name().append(QLatin1Char('/'));
for (const auto& potentialSubCat: categories) {
if(potentialSubCat->name().startsWith(catName)) {
cat->addSubcategory(potentialSubCat);
topCategories.removeOne(potentialSubCat);
}
}
}
m_engine = new KNSCore::Engine(this);
connect(m_engine, &KNSCore::Engine::signalErrorCode, this, &KNSBackend::signalErrorCode);
connect(m_engine, &KNSCore::Engine::signalEntriesLoaded, this, &KNSBackend::receivedEntries, Qt::QueuedConnection);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment