Commit 2359fe4a authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

Make sure we don't treat a non-sorted vector as sorted

BUG: 388313
parent 98ec68d3
......@@ -154,8 +154,30 @@ bool Category::categoryLessThan(Category *c1, const Category *c2)
return (!c1->isAddons() && c2->isAddons()) || (c1->isAddons()==c2->isAddons() && QString::localeAwareCompare(c1->name(), c2->name()) < 0);
}
static bool isSorted(const QVector<Category*>& vector)
{
Category *last = nullptr;
for(auto a: vector) {
if (last && !Category::categoryLessThan(last, a))
return false;
last = a;
}
return true;
}
void Category::sortCategories(QVector<Category *>& cats)
{
std::sort(cats.begin(), cats.end(), &categoryLessThan);
for(auto cat: cats) {
sortCategories(cat->m_subCategories);
}
Q_ASSERT(isSorted(cats));
}
void Category::addSubcategory(QVector< Category* >& list, Category* newcat)
{
Q_ASSERT(isSorted(list));
auto it = std::lower_bound(list.begin(), list.end(), newcat, &categoryLessThan);
if (it == list.end()) {
list << newcat;
......@@ -185,6 +207,7 @@ void Category::addSubcategory(QVector< Category* >& list, Category* newcat)
}
list.insert(it, newcat);
Q_ASSERT(isSorted(list));
}
bool Category::blacklistPluginsInVector(const QSet<QString>& pluginNames, QVector<Category *>& subCategories)
......
......@@ -62,7 +62,8 @@ public:
QVector<Category *> subCategories() const;
QVariantList subCategoriesVariant() const;
static void addSubcategory(QVector<Category*>& list, Category* cat);
static void sortCategories(QVector<Category*>& cats);
static void addSubcategory(QVector<Category*>& cats, Category* cat);
void parseData(const QString& path, const QDomNode& data);
bool blacklistPlugins(const QSet<QString>& pluginName);
bool isAddons() const { return m_isAddons; }
......
......@@ -53,6 +53,7 @@ void CategoryModel::populateCategories()
if(ret.isEmpty()) {
ret = cats;
Category::sortCategories(ret);
} else {
Q_FOREACH (Category* c, cats)
Category::addSubcategory(ret, c);
......
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