Isis 3 Programmer Reference
PointMeasureTreeModel.cpp
1
7/* SPDX-License-Identifier: CC0-1.0 */
8
9#include "PointMeasureTreeModel.h"
10
11#include <iostream>
12
13#include <QFuture>
14#include <QFutureWatcher>
15#include <QMutex>
16#include <QString>
17#include <QtConcurrentMap>
18
19#include "ControlNet.h"
20#include "ControlPoint.h"
21
22#include "TreeView.h"
23#include "TreeViewContent.h"
24#include "PointParentItem.h"
25#include "MeasureLeafItem.h"
26#include "RootItem.h"
27
28#include <QTime>
29#include <QVariant>
30
31
32namespace Isis {
33 PointMeasureTreeModel::PointMeasureTreeModel(ControlNet *controlNet,
34 TreeView *v, QObject *parent) :
35 AbstractTreeModel(controlNet, v, parent) {
36 rebuildItems();
37 }
38
39
40 PointMeasureTreeModel::~PointMeasureTreeModel() {
41 }
42
43
44 PointMeasureTreeModel::CreateRootItemFunctor::CreateRootItemFunctor(
45 AbstractTreeModel *tm, QThread *tt) {
46 m_treeModel = tm;
47 m_targetThread = tt;
48 m_avgCharWidth = QFontMetrics(
49 m_treeModel->getView()->getContentFont()).averageCharWidth();
50 }
51
52
53 PointMeasureTreeModel::CreateRootItemFunctor::CreateRootItemFunctor(
54 const CreateRootItemFunctor &other) {
55 m_treeModel = other.m_treeModel;
56 m_targetThread = other.m_targetThread;
57 m_avgCharWidth = other.m_avgCharWidth;
58 }
59
60 PointMeasureTreeModel::CreateRootItemFunctor::~CreateRootItemFunctor() {
61 m_treeModel = NULL;
62 }
63
64
65 PointParentItem *PointMeasureTreeModel::CreateRootItemFunctor::operator()(
66 ControlPoint *const &point) const {
67 PointParentItem *pointItem = new PointParentItem(point, m_avgCharWidth);
68 pointItem->moveToThread(m_targetThread);
69
70 for (int j = 0; j < point->GetNumMeasures(); j++) {
71 const ControlMeasure *measure = point->GetMeasure(j);
72
73 MeasureLeafItem *measureItem = new MeasureLeafItem(
74 const_cast< ControlMeasure * >(measure), m_avgCharWidth, pointItem);
75 measureItem->moveToThread(m_targetThread);
76
77 pointItem->addChild(measureItem);
78 }
79
80 return pointItem;
81 }
82
83
84 void PointMeasureTreeModel::CreateRootItemFunctor::addToRootItem(
85 QAtomicPointer< RootItem > & root, PointParentItem *const &item) {
86
87 // Allocate a new root item if our root is NULL
88 if (root.testAndSetOrdered(NULL, new RootItem)) {
89 root.loadAcquire()->moveToThread(item->thread());
90 }
91
92 if (item)
93 root.loadAcquire()->addChild(item);
94 }
95
96
97 PointMeasureTreeModel::CreateRootItemFunctor &
98 PointMeasureTreeModel::CreateRootItemFunctor::operator=(
99 const CreateRootItemFunctor &other) {
100 if (this != &other) {
101 m_treeModel = other.m_treeModel;
102 m_avgCharWidth = other.m_avgCharWidth;
103 }
104
105 return *this;
106 }
107
108 void PointMeasureTreeModel::rebuildItems() {
109 if (!isFrozen()) {
110 emit cancelSort();
111 setRebuilding(true);
112 emit filterCountsChanged(-1, getTopLevelItemCount());
113 QFuture< QAtomicPointer< RootItem > > futureRoot;
114 if (getRebuildWatcher()->isStarted()) {
115 futureRoot = getRebuildWatcher()->future();
116 futureRoot.cancel();
117 }
118
119 futureRoot = QtConcurrent::mappedReduced(
120 getControlNetwork()->GetPoints(),
121 CreateRootItemFunctor(this, QThread::currentThread()),
122 &CreateRootItemFunctor::addToRootItem,
123 QtConcurrent::OrderedReduce | QtConcurrent::SequentialReduce);
124
125 getRebuildWatcher()->setFuture(futureRoot);
126 }
127 else {
128 queueRebuild();
129 }
130 }
131}
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16