Isis 3.0 Programmer Reference
Back | Home
ConcurrentControlNetReader.cpp
Go to the documentation of this file.
1 
25 
26 #include <algorithm>
27 #include <iostream>
28 
29 #include <QDebug>
30 #include <QFuture>
31 #include <QFutureWatcher>
32 #include <QString>
33 #include <QtConcurrentRun>
34 #include <QtConcurrentMap>
35 #include <QtCore>
36 #include <QTimer>
37 
38 #include "Control.h"
39 #include "ControlNet.h"
40 #include "FileName.h"
41 #include "IString.h"
42 #include "Progress.h"
43 #include "IException.h"
44 
45 using std::cerr;
46 using std::cout;
47 using std::swap;
48 
49 namespace Isis {
50 
55  nullify();
56 
57  m_mappedRunning = false;
58 
59  m_progressBar = new ProgressBar("Reading Control Nets");
61 
62  initProgress();
63 
64  connect(m_watcher, SIGNAL(finished()), this, SLOT(mappedFinished()));
65  }
66 
67 
72 
73  if (m_watcher)
74  {
75  m_watcher->cancel();
76  m_watcher->waitForFinished();
77  }
78 
79  delete m_watcher;
80  m_watcher = NULL;
81 
82  delete m_progressBar;
83  }
84 
85 
86  ProgressBar *ConcurrentControlNetReader::progressBar() {
87  return m_progressBar;
88  }
89 
90 
94  void ConcurrentControlNetReader::read(QString filename) {
95 
96  m_backlog.append(filename);
97 
98  if (!m_progressBar.isNull()) {
99  m_progressBar->setRange(0, m_progressBar->maximum() + 1);
100  }
101 
102  start();
103  }
104 
105 
110 
111  m_backlog.append(filenames);
112 
113  if (!m_progressBar.isNull()) {
114  m_progressBar->setRange(0, m_progressBar->maximum() + filenames.size());
115  }
116 
117  start();
118  }
119 
120 
125  m_watcher = NULL;
126  }
127 
128 
129  void ConcurrentControlNetReader::initProgress() {
130  if (m_progressBar) {
131  m_progressBar->setVisible(false);
132  m_progressBar->setRange(0, 100);
133  m_progressBar->setValue(0);
134  }
135  }
136 
137 
138  void ConcurrentControlNetReader::start() {
139 
140  if (!m_backlog.isEmpty() && !m_mappedRunning) {
141 
142  QList< QPair<FileName, Progress *> > functorInput;
143  foreach (QString backlogFileName, m_backlog) {
144  Progress *progress = new Progress;
145  progress->DisableAutomaticDisplay();
146  m_progress.append(progress);
147 
148  functorInput.append(qMakePair(FileName(backlogFileName), progress));
149  }
150 
151  QFuture<Control *> networks = QtConcurrent::mapped(functorInput,
152  FileNameToControlFunctor(QThread::currentThread()));
153 
154  Control * control = networks.result();
155  ControlNet * cnet = control->controlNet();
156  if (!cnet->IsValid()) {
157  throw IException();
158  }
159 
160  if (!m_progressBar.isNull()) {
161  m_progressBar->setVisible(true);
162  }
163 
164  delete m_progressUpdateTimer;
165 
166  m_watcher->setFuture(networks);
167  m_mappedRunning = true;
168  m_backlog.clear();
169 
170  m_progressUpdateTimer = new QTimer;
171  connect(m_progressUpdateTimer, SIGNAL(timeout()),
172  this, SLOT(updateProgressValue()));
173  m_progressUpdateTimer->start(100);
174  }
175  }
176 
177 
178  void ConcurrentControlNetReader::updateProgressValue() {
179  if (!m_mappedRunning) {
180  foreach (Progress *progress, m_progress) {
181  delete progress;
182  }
183 
184  m_progress.clear();
185  }
186 
187  int progressMin = 0;
188  int progressMax = (m_progress.count() + m_backlog.count()) * 1000;
189  int progressCurrent = 0;
190 
191  foreach (Progress *progress, m_progress) {
192  if (progress->MaximumSteps()) {
193  if (progress->CurrentStep() < progress->MaximumSteps()) {
194  double progressPercent = progress->CurrentStep() / (double)progress->MaximumSteps();
195 
196  progressCurrent += qFloor(progressPercent * 1000);
197  }
198  else {
199  progressCurrent += 1000;
200  }
201  }
202  }
203 
204  if (m_progressBar) {
205  if (progressMax > 0) {
206  m_progressBar->setRange(progressMin, progressMax);
207  m_progressBar->setValue(progressCurrent);
208  }
209  else {
210  m_progressBar->setRange(0, 100);
211  m_progressBar->setValue(100);
212  }
213  }
214  }
215 
216 
217  void ConcurrentControlNetReader::mappedFinished() {
218  m_mappedRunning = false;
219 
220  delete m_progressUpdateTimer;
221  updateProgressValue();
222 
223  QList<Control *> networks(m_watcher->future().results());
224  emit networksReady(networks);
225 
226  if (!m_backlog.isEmpty()) {
227  start();
228  }
229  else {
230  initProgress();
231  }
232  }
233 
234 
235  ConcurrentControlNetReader::FileNameToControlFunctor::FileNameToControlFunctor(
236  QThread *targetThread) {
237  m_targetThread = targetThread;
238  }
239 
240 
241  ConcurrentControlNetReader::FileNameToControlFunctor::FileNameToControlFunctor(
242  const FileNameToControlFunctor & other) {
243  m_targetThread = other.m_targetThread;
244  }
245 
246 
247  ConcurrentControlNetReader::FileNameToControlFunctor::~FileNameToControlFunctor() {
248  m_targetThread = NULL;
249  }
250 
251 
252  Control * ConcurrentControlNetReader::FileNameToControlFunctor::operator()(
253  const QPair<FileName, Progress *> &fileNameAndProgress) const {
254 
255  QString fileNameString = fileNameAndProgress.first.expanded();
256  Progress *progress = fileNameAndProgress.second;
257 
258  ControlNet *newCnet = new ControlNet(fileNameString, progress);
259  Control *result = new Control(newCnet, fileNameString);
260 
261  newCnet->setParent(result);
262  result->moveToThread(m_targetThread);
263 
264  return result;
265  }
266 
267 
268  ConcurrentControlNetReader::FileNameToControlFunctor &
269  ConcurrentControlNetReader::FileNameToControlFunctor::operator=(
270  const FileNameToControlFunctor &rhs) {
271  m_targetThread = rhs.m_targetThread;
272  return *this;
273  }
274 }
275 
void nullify()
Initializes members to NULL.
QFutureWatcher< Control * > * m_watcher
provides SIGNALS / SLOTS for FileNameToControlFunctor
ConcurrentControlNetReader()
Allocates memory at construction instead of as needed.
~ConcurrentControlNetReader()
This destructor will cancel all running threads and block.
QProgressBar with customizable text.
Definition: ProgressBar.h:15

U.S. Department of the Interior | U.S. Geological Survey
ISIS | Privacy & Disclaimers | Astrogeology Research Program
To contact us, please post comments and questions on the ISIS Support Center
File Modified: 07/12/2023 23:16:09