Correctly count the number of torrents in subcategories

PR #18261.
Closes #18137.
This commit is contained in:
Vladimir Golovnev 2022-12-22 08:19:33 +03:00 committed by GitHub
parent b12fdcf018
commit aeae065007
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -56,7 +56,7 @@ public:
clear(); clear();
if (m_parent) if (m_parent)
{ {
m_parent->m_torrentsCount -= m_torrentsCount; m_parent->decreaseTorrentsCount(m_torrentsCount);
const QString uid = m_parent->m_children.key(this); const QString uid = m_parent->m_children.key(this);
m_parent->m_children.remove(uid); m_parent->m_children.remove(uid);
m_parent->m_childUids.removeOne(uid); m_parent->m_childUids.removeOne(uid);
@ -86,18 +86,18 @@ public:
return m_torrentsCount; return m_torrentsCount;
} }
void increaseTorrentsCount() void increaseTorrentsCount(const int delta = 1)
{ {
++m_torrentsCount; m_torrentsCount += delta;
if (m_parent) if (m_parent)
m_parent->increaseTorrentsCount(); m_parent->increaseTorrentsCount(delta);
} }
void decreaseTorrentsCount() void decreaseTorrentsCount(const int delta = 1)
{ {
--m_torrentsCount; m_torrentsCount -= delta;
if (m_parent) if (m_parent)
m_parent->decreaseTorrentsCount(); m_parent->decreaseTorrentsCount(delta);
} }
int pos() const int pos() const
@ -139,7 +139,7 @@ public:
item->m_parent = this; item->m_parent = this;
m_children[uid] = item; m_children[uid] = item;
m_childUids.append(uid); m_childUids.append(uid);
m_torrentsCount += item->torrentsCount(); increaseTorrentsCount(item->torrentsCount());
} }
void clear() void clear()
@ -408,9 +408,9 @@ void CategoryFilterModel::populate()
m_rootItem->addChild(UID_UNCATEGORIZED, new CategoryModelItem(nullptr, tr("Uncategorized"), torrentsCount)); m_rootItem->addChild(UID_UNCATEGORIZED, new CategoryModelItem(nullptr, tr("Uncategorized"), torrentsCount));
using BitTorrent::Torrent; using BitTorrent::Torrent;
for (const QString &categoryName : asConst(session->categories()))
{
if (m_isSubcategoriesEnabled) if (m_isSubcategoriesEnabled)
{
for (const QString &categoryName : asConst(session->categories()))
{ {
CategoryModelItem *parent = m_rootItem; CategoryModelItem *parent = m_rootItem;
for (const QString &subcat : asConst(session->expandCategory(categoryName))) for (const QString &subcat : asConst(session->expandCategory(categoryName)))
@ -425,7 +425,10 @@ void CategoryFilterModel::populate()
parent = parent->child(subcatName); parent = parent->child(subcatName);
} }
} }
}
else else
{
for (const QString &categoryName : asConst(session->categories()))
{ {
const int torrentsCount = std::count_if(torrents.begin(), torrents.end() const int torrentsCount = std::count_if(torrents.begin(), torrents.end()
, [categoryName](Torrent *torrent) { return torrent->belongsToCategory(categoryName); }); , [categoryName](Torrent *torrent) { return torrent->belongsToCategory(categoryName); });