Optimize parts tree

Delay a little bit to reload the children of component to avoid the UI freeze issue.
master
Jeremy Hu 2020-01-14 23:29:40 +09:30
parent 8db7266158
commit 58e01ca349
2 changed files with 29 additions and 0 deletions

View File

@ -1035,6 +1035,32 @@ void PartTreeWidget::deleteItemChildren(QTreeWidgetItem *item)
}
void PartTreeWidget::componentChildrenChanged(QUuid componentId)
{
removeComponentDelayedTimer(componentId);
QTimer *delayedTimer = new QTimer(this);
delayedTimer->setSingleShot(true);
delayedTimer->setInterval(200);
connect(delayedTimer, &QTimer::timeout, this, [=]() {
removeComponentDelayedTimer(componentId);
reloadComponentChildren(componentId);
});
m_delayedComponentTimers.insert({componentId, delayedTimer});
delayedTimer->start();
}
void PartTreeWidget::removeComponentDelayedTimer(const QUuid &componentId)
{
auto findTimer = m_delayedComponentTimers.find(componentId);
if (findTimer != m_delayedComponentTimers.end()) {
delete findTimer->second;
m_delayedComponentTimers.erase(findTimer);
}
}
void PartTreeWidget::reloadComponentChildren(const QUuid &componentId)
{
QTreeWidgetItem *parentItem = findComponentItem(componentId);
if (nullptr == parentItem) {

View File

@ -102,6 +102,8 @@ private:
bool isComponentSelected(QUuid componentId);
std::vector<QUuid> collectSelectedComponentIds(const QPoint &pos);
void handleSingleClick(const QPoint &pos);
void reloadComponentChildren(const QUuid &componentId);
void removeComponentDelayedTimer(const QUuid &componentId);
private:
const Document *m_document = nullptr;
QTreeWidgetItem *m_rootItem = nullptr;
@ -115,6 +117,7 @@ private:
QBrush m_hightlightedPartBackground;
QUuid m_shiftStartComponentId;
std::set<QUuid> m_selectedComponentIds;
std::map<QUuid, QTimer *> m_delayedComponentTimers;
};
#endif