From 58e01ca34946be8774cd68088ef2847eb898c998 Mon Sep 17 00:00:00 2001 From: Jeremy Hu Date: Tue, 14 Jan 2020 23:29:40 +0930 Subject: [PATCH] Optimize parts tree Delay a little bit to reload the children of component to avoid the UI freeze issue. --- src/parttreewidget.cpp | 26 ++++++++++++++++++++++++++ src/parttreewidget.h | 3 +++ 2 files changed, 29 insertions(+) diff --git a/src/parttreewidget.cpp b/src/parttreewidget.cpp index 8578cb35..8ed80acb 100644 --- a/src/parttreewidget.cpp +++ b/src/parttreewidget.cpp @@ -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) { diff --git a/src/parttreewidget.h b/src/parttreewidget.h index facbaf8a..bd156f42 100644 --- a/src/parttreewidget.h +++ b/src/parttreewidget.h @@ -102,6 +102,8 @@ private: bool isComponentSelected(QUuid componentId); std::vector 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 m_selectedComponentIds; + std::map m_delayedComponentTimers; }; #endif