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