Isis 3 Programmer Reference
ImagePointTreeModel.cpp
1 #include "IsisDebug.h"
2 
3 #include "ImagePointTreeModel.h"
4 
5 #include <iostream>
6 
7 #include <QFuture>
8 #include <QFutureWatcher>
9 #include <QList>
10 #include <QModelIndex>
11 #include <QString>
12 #include <QtConcurrentMap>
13 
14 #include "ControlMeasure.h"
15 #include "ControlNet.h"
16 #include "ControlPoint.h"
17 
18 #include "TreeView.h"
19 #include "TreeViewContent.h"
20 #include "PointLeafItem.h"
21 #include "RootItem.h"
22 #include "ImageParentItem.h"
23 
24 #include <QTime>
25 
26 
27 using std::cerr;
28 
29 
30 namespace Isis {
31  ImagePointTreeModel::ImagePointTreeModel(ControlNet *controlNet,
32  TreeView *v, QObject *parent) :
33  AbstractTreeModel(controlNet, v, parent) {
34  rebuildItems();
35  }
36 
37 
38  ImagePointTreeModel::~ImagePointTreeModel() {
39  }
40 
41 
42  ImagePointTreeModel::CreateRootItemFunctor::CreateRootItemFunctor(
43  AbstractTreeModel *tm, ControlNet *net, QThread *tt) {
44  m_treeModel = tm;
45  m_controlNet = net;
46  m_targetThread = tt;
47  m_avgCharWidth = QFontMetrics(
48  m_treeModel->getView()->getContentFont()).averageCharWidth();
49  }
50 
51 
52  ImagePointTreeModel::CreateRootItemFunctor::CreateRootItemFunctor(
53  const CreateRootItemFunctor &other) {
54  m_treeModel = other.m_treeModel;
55  m_controlNet = other.m_controlNet;
56  m_targetThread = other.m_targetThread;
57  m_avgCharWidth = other.m_avgCharWidth;
58  }
59 
60 
61  ImagePointTreeModel::CreateRootItemFunctor::~CreateRootItemFunctor() {
62  m_targetThread = NULL;
63  m_controlNet = NULL;
64  m_treeModel = NULL;
65  }
66 
67 
68  ImageParentItem *ImagePointTreeModel::CreateRootItemFunctor::operator()(
69  QString imageSerial) const {
70  ImageParentItem *imageItem = NULL;
71 
72  // TODO connect parent item destroy to image removed from network
73 
74  imageItem = new ImageParentItem(imageSerial, m_controlNet, m_avgCharWidth);
75  imageItem->setSelectable(false);
76  imageItem->moveToThread(m_targetThread);
77  QList< ControlMeasure * > measures = m_controlNet->GetMeasuresInCube(imageSerial);
78  for (int j = 0; j < measures.size(); j++) {
79  ASSERT(measures[j]);
80  ControlPoint *point = measures[j]->Parent();
81 
82  ASSERT(point);
83  PointLeafItem *pointItem = new PointLeafItem(
84  point, m_avgCharWidth, imageItem);
85  pointItem->setSelectable(false);
86  pointItem->moveToThread(m_targetThread);
87 
88  imageItem->addChild(pointItem);
89  }
90 
91  return imageItem;
92  }
93 
94 
95  void ImagePointTreeModel::CreateRootItemFunctor::addToRootItem(
96  QAtomicPointer< RootItem > & root, ImageParentItem *const &item) {
97 
98  // Allocate a new root item if our root is NULL
99  if (root.testAndSetOrdered(NULL, new RootItem)) {
100  root.loadAcquire()->moveToThread(item->thread());
101  }
102 
103  if (item)
104  root.loadAcquire()->addChild(item);
105  }
106 
107 
108  ImagePointTreeModel::CreateRootItemFunctor &
109  ImagePointTreeModel::CreateRootItemFunctor::operator=(
110  const CreateRootItemFunctor &other) {
111  if (this != &other) {
112  m_treeModel = other.m_treeModel;
113  m_avgCharWidth = other.m_avgCharWidth;
114  }
115 
116  return *this;
117  }
118 
119 
120  void ImagePointTreeModel::rebuildItems() {
121  // cerr << "ImagePointTreeModel::rebuildItems\n";
122  if (!isFrozen()) {
123  emit cancelSort();
124  setRebuilding(true);
125  emit filterCountsChanged(-1, getTopLevelItemCount());
126  QFuture< QAtomicPointer< RootItem > > futureRoot;
127  if (getRebuildWatcher()->isStarted()) {
128  futureRoot = getRebuildWatcher()->future();
129  futureRoot.cancel();
130  // futureRoot.waitForFinished();
131  // if (futureRoot.result())
132  // delete futureRoot.result();
133  }
134 
135  futureRoot = QtConcurrent::mappedReduced(
136  getControlNetwork()->GetCubeSerials(),
137  CreateRootItemFunctor(this, getControlNetwork(), QThread::currentThread()),
138  &CreateRootItemFunctor::addToRootItem,
139  QtConcurrent::OrderedReduce | QtConcurrent::SequentialReduce);
140 
141  getRebuildWatcher()->setFuture(futureRoot);
142  }
143  else {
144  queueRebuild();
145  }
146  // cerr << "/ImagePointTreeModel::rebuildItems\n";
147  }
148 }
void filterCountsChanged(int visibleTopLevelItemCount, int topLevelItemCount)
This signal is emitted after filtering to provide the number of visible top-level items remaining aft...
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31