11 #include "PointMeasureTreeModel.h"
16 #include <QFutureWatcher>
19 #include <QtConcurrentMap>
21 #include "ControlNet.h"
22 #include "ControlPoint.h"
25 #include "TreeViewContent.h"
26 #include "PointParentItem.h"
27 #include "MeasureLeafItem.h"
35 PointMeasureTreeModel::PointMeasureTreeModel(ControlNet *controlNet,
37 AbstractTreeModel(controlNet, v, parent) {
42 PointMeasureTreeModel::~PointMeasureTreeModel() {
46 PointMeasureTreeModel::CreateRootItemFunctor::CreateRootItemFunctor(
47 AbstractTreeModel *tm,
QThread *tt) {
50 m_avgCharWidth = QFontMetrics(
51 m_treeModel->getView()->getContentFont()).averageCharWidth();
55 PointMeasureTreeModel::CreateRootItemFunctor::CreateRootItemFunctor(
56 const CreateRootItemFunctor &other) {
57 m_treeModel = other.m_treeModel;
58 m_targetThread = other.m_targetThread;
59 m_avgCharWidth = other.m_avgCharWidth;
62 PointMeasureTreeModel::CreateRootItemFunctor::~CreateRootItemFunctor() {
67 PointParentItem *PointMeasureTreeModel::CreateRootItemFunctor::operator()(
68 ControlPoint *
const &point)
const {
69 PointParentItem *pointItem =
new PointParentItem(point, m_avgCharWidth);
70 pointItem->moveToThread(m_targetThread);
72 for (
int j = 0; j < point->GetNumMeasures(); j++) {
73 const ControlMeasure *measure = point->GetMeasure(j);
76 MeasureLeafItem *measureItem =
new MeasureLeafItem(
77 const_cast< ControlMeasure *
>(measure), m_avgCharWidth, pointItem);
78 measureItem->moveToThread(m_targetThread);
80 pointItem->addChild(measureItem);
87 void PointMeasureTreeModel::CreateRootItemFunctor::addToRootItem(
91 if (root.testAndSetOrdered(NULL,
new RootItem)) {
92 root.loadAcquire()->moveToThread(item->thread());
96 root.loadAcquire()->addChild(item);
100 PointMeasureTreeModel::CreateRootItemFunctor &
101 PointMeasureTreeModel::CreateRootItemFunctor::operator=(
102 const CreateRootItemFunctor &other) {
103 if (
this != &other) {
104 m_treeModel = other.m_treeModel;
105 m_avgCharWidth = other.m_avgCharWidth;
111 void PointMeasureTreeModel::rebuildItems() {
116 QFuture< QAtomicPointer< RootItem > > futureRoot;
117 if (getRebuildWatcher()->isStarted()) {
118 futureRoot = getRebuildWatcher()->future();
122 ASSERT(getControlNetwork());
123 futureRoot = QtConcurrent::mappedReduced(
124 getControlNetwork()->GetPoints(),
125 CreateRootItemFunctor(
this, QThread::currentThread()),
126 &CreateRootItemFunctor::addToRootItem,
127 QtConcurrent::OrderedReduce | QtConcurrent::SequentialReduce);
129 getRebuildWatcher()->setFuture(futureRoot);