fixed quadratic cost in IdDialog::loadCircles().

This commit is contained in:
csoler 2020-11-17 23:54:43 +01:00
parent 7db8400233
commit 883dfd9c99

View File

@ -597,6 +597,10 @@ void IdDialog::loadCircles(const std::list<RsGroupMetaData>& groupInfo)
mStateHelper->setActive(CIRCLESDIALOG_GROUPMETA, true);
// Disable sorting while updating which avoids calling sortChildren() in child(i), causing heavy loads when adding
// many items to a tree.
ui->treeWidget_membership->setSortingEnabled(false);
std::vector<bool> expanded_top_level_items;
std::set<RsGxsCircleId> expanded_circle_items;
saveExpandedCircleItems(expanded_top_level_items,expanded_circle_items);
@ -730,6 +734,15 @@ void IdDialog::loadCircles(const std::list<RsGroupMetaData>& groupInfo)
for(auto index:to_delete)
delete item->takeChild(index); // delete items starting from the largest index, because otherwise the count changes while deleting...
// Now make a list of items to add, but only add them at once at the end of the loop, to avoid a quadratic cost.
QList<QTreeWidgetItem*> new_sub_items;
// ...and make a map of which index each item has, to make the search logarithmic
std::map<QString,uint32_t> subitem_indices;
for(uint32_t k=0; k < (uint32_t)item->childCount(); ++k)
subitem_indices[item->child(k)->data(CIRCLEGROUP_CIRCLE_COL_GROUPID,Qt::UserRole).toString()] = k;
for(std::map<RsGxsId,uint32_t>::const_iterator it(details.mSubscriptionFlags.begin());it!=details.mSubscriptionFlags.end();++it)
{
#ifdef ID_DEBUG
@ -745,15 +758,11 @@ void IdDialog::loadCircles(const std::list<RsGroupMetaData>& groupInfo)
int subitem_index = -1;
// see if the item already exists
for(uint32_t k=0; k < (uint32_t)item->childCount(); ++k)
if(item->child(k)->data(CIRCLEGROUP_CIRCLE_COL_GROUPID,Qt::UserRole).toString().toStdString() == it->first.toStdString())
{
subitem_index = k;
#ifdef ID_DEBUG
std::cerr << " found existing sub item." << std::endl;
#endif
break ;
}
auto itt = subitem_indices.find(QString::fromStdString(it->first.toStdString()));
if(itt != subitem_indices.end())
subitem_index = itt->second;
if(!(invited || subscrb))
{
@ -819,7 +828,7 @@ void IdDialog::loadCircles(const std::list<RsGroupMetaData>& groupInfo)
//subitem->setIcon(RSID_COL_NICKNAME, QIcon(pixmap));
item->addChild(subitem) ;
new_sub_items.push_back(subitem);
}
else
subitem = item->child(subitem_index);
@ -851,6 +860,9 @@ void IdDialog::loadCircles(const std::list<RsGroupMetaData>& groupInfo)
}
}
// add all items
item->addChildren(new_sub_items);
// The bullet colors below are for the *Membership*. This is independent from admin rights, which cannot be shown as a color.
// Admin/non admin is shows using Bold font.
@ -861,6 +873,8 @@ void IdDialog::loadCircles(const std::list<RsGroupMetaData>& groupInfo)
else
item->setIcon(CIRCLEGROUP_CIRCLE_COL_GROUPNAME,FilesDefs::getIconFromQtResourcePath(IMAGE_UNKNOWN)) ;
}
ui->treeWidget_membership->setSortingEnabled(true);
restoreExpandedCircleItems(expanded_top_level_items,expanded_circle_items);
}