Isis 3 Programmer Reference
CnetDisplayProperties.cpp
1 
7 /* SPDX-License-Identifier: CC0-1.0 */
8 
9 #include "IsisDebug.h"
10 
11 #include "CnetDisplayProperties.h"
12 
13 #include <iostream>
14 
15 #include <QAtomicInt>
16 #include <QFile>
17 #include <QFileInfo>
18 #include <QFuture>
19 #include <QFutureWatcher>
20 #include <QMap>
21 #include <QReadWriteLock>
22 #include <QString>
23 #include <QStringList>
24 #include <QTextStream>
25 #include <QTimer>
26 #include <QtConcurrentRun>
27 
28 #include "ControlNet.h"
29 #include "Cube.h"
30 #include "IException.h"
31 #include "IString.h"
32 #include "SerialNumber.h"
33 
34 
35 namespace Isis {
36  CnetDisplayProperties *CnetDisplayProperties::m_instance = NULL;
37 
38 
39  CnetDisplayProperties *CnetDisplayProperties::getInstance() {
40  return m_instance ? m_instance : m_instance = new CnetDisplayProperties;
41  }
42 
43 
44  CnetDisplayProperties::CnetDisplayProperties() {
45  nullify();
46 
47  useFileNames = true;
48  m_curComposing = false;
49  m_showFullPath = false;
50 
51  m_readWriteLock = new QReadWriteLock;
52 
53  m_serialNumberToFileNameMap = new QMap< QString, QString >;
54 
55  m_composedCount = new QAtomicInt;
56  m_interruptFlag = new QAtomicInt;
57  m_interruptFlag->fetchAndStoreRelaxed(0);
58 
59  m_composeStatusPoller = new QTimer;
60  connect(m_composeStatusPoller, SIGNAL(timeout()),
61  this, SLOT(composeStatusUpdated()));
62 
63  m_composeWatcher = new QFutureWatcher< QMap< QString, QString > >;
64  connect(m_composeWatcher, SIGNAL(finished()),
65  this, SLOT(serialNumbersComposed()));
66  }
67 
68 
69 // CnetDisplayProperties::CnetDisplayProperties(
70 // CnetDisplayProperties const & other)
71 // {
72 // nullify();
73 //
74 // useFileNames = other.useFileNames;
75 // m_serialNumberToFileNameMap = new QMap< QString, QString >(
76 // *other.m_serialNumberToFileNameMap);
77 //
78 // m_composeStatusPoller = new QTimer;
79 //
80 // m_composeWatcher = new QFutureWatcher< QMap< QString, QString > >;
81 // connect(m_composeWatcher, SIGNAL(finished()),
82 // this, SLOT(serialNumbersComposed()));
83 // }
84 
85 
86  CnetDisplayProperties::~CnetDisplayProperties() {
87  delete m_serialNumberToFileNameMap;
88  m_serialNumberToFileNameMap = NULL;
89 
90  delete m_composeWatcher;
91  m_composeWatcher = NULL;
92 
93  delete m_composedCount;
94  m_composedCount = NULL;
95 
96  delete m_interruptFlag;
97  m_interruptFlag = NULL;
98  }
99 
100 
101  bool CnetDisplayProperties::currentlyComposing() const {
102  return m_curComposing;
103  }
104 
105 
115  QList<QString> results;
116 
117  if (!currentlyComposing()) {
118  foreach (QString serialNumber, cnet->GetCubeSerials()) {
119  QString possibleFileName = getImageName(serialNumber, true);
120 
121  if (possibleFileName != serialNumber)
122  results.append(possibleFileName);
123  }
124  }
125 
126  return results;
127  }
128 
129 
130  QString CnetDisplayProperties::getFileName(QString fileName,
131  bool forceFullPaths) const {
132  QString result;
133 
134  if (forceFullPaths || getShowsFullPaths())
135  result = fileName;
136  else
137  result = QFileInfo(fileName).fileName();
138 
139  return result;
140  }
141 
142 
154  QString CnetDisplayProperties::getImageName(QString cubeSerialNumber,
155  bool forceFullPaths) const {
156  ASSERT(m_serialNumberToFileNameMap);
157 
158  QString imageName = cubeSerialNumber;
159 
160  m_readWriteLock->lockForRead();
161 
162  if (m_serialNumberToFileNameMap && useFileNames &&
163  m_serialNumberToFileNameMap->contains(cubeSerialNumber)) {
164  QString value = m_serialNumberToFileNameMap->value(cubeSerialNumber);
165  m_readWriteLock->unlock();
166 
167  if (value.toLower() != "unknown")
168  imageName = getFileName(value, forceFullPaths);
169  }
170  else {
171  m_readWriteLock->unlock();
172  }
173 
174  return imageName;
175  }
176 
177 
178  QString CnetDisplayProperties::getSerialNumber(QString imageId) {
179  ASSERT(m_serialNumberToFileNameMap);
180 
181  QString sn = imageId;
182 
183  if (m_serialNumberToFileNameMap && useFileNames) {
184  m_readWriteLock->lockForRead();
185  QMapIterator< QString, QString > i(*m_serialNumberToFileNameMap);
186 
187  bool found = false;
188  while (!found && i.hasNext()) {
189  i.next();
190  if (i.value() == imageId) {
191  found = true;
192  sn = i.key();
193  }
194  }
195 
196  m_readWriteLock->unlock();
197  }
198 
199  return sn;
200  }
201 
202 
203  bool CnetDisplayProperties::getShowsFullPaths() const {
204  return m_showFullPath;
205  }
206 
207 
208  void CnetDisplayProperties::setCubeList(QString fileName) {
209  QFile imageListFile(fileName);
210 
211  if (!imageListFile.exists()) {
212  IString msg = "The file [";
213  msg += (IString) fileName;
214  msg += "] does not exist.\n";
215  throw IException(IException::Programmer, msg, _FILEINFO_);
216  }
217 
218  if (!imageListFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
219  IString msg = "The file [";
220  msg += (IString) fileName;
221  msg += "] failed to open.\n";
222  throw IException(IException::Programmer, msg, _FILEINFO_);
223  }
224 
225  QStringList imageFileNames;
226  QTextStream ts(&imageListFile);
227 
228  while (!ts.atEnd())
229  imageFileNames << ts.readLine();
230 
231  imageListFile.close();
232 
233  m_curComposing = true;
234 
235  QFuture< QMap< QString, QString > > future = QtConcurrent::run(
236  this, &CnetDisplayProperties::composeSerialNumbers, imageFileNames);
237 
238  m_composeWatcher->setFuture(future);
239  }
240 
241 
242  void CnetDisplayProperties::setFileNameUsage(bool preferFileNames) {
243  useFileNames = preferFileNames;
244  }
245 
246 
247 // CnetDisplayProperties & CnetDisplayProperties::operator=(
248 // CnetDisplayProperties const & other)
249 // {
250 // if (this != &other)
251 // {
252 // m_composeWatcher->waitForFinished();
253 // useFileNames = other.useFileNames;
254 // }
255 //
256 // return *this;
257 // }
258 
259 
260  void CnetDisplayProperties::setShowsFullPaths(bool newState) {
261  m_showFullPath = newState;
262  }
263 
264 
265  QMap< QString, QString > CnetDisplayProperties::composeSerialNumbers(
266  QStringList fileNames) {
267  emit composeProgressRangeChanged(0, fileNames.size() - 1);
268  m_composedCount->fetchAndStoreRelaxed(0);
269 
271 
272  for (int i = 0; *m_interruptFlag == 0 && i < fileNames.size(); i++) {
273  QString fileName = fileNames[i];
274 
275  Cube cube;
276  cube.open(fileName);
277 
278  newMap.insert(SerialNumber::Compose(fileName), fileNames[i]);
279 
280  m_composedCount->fetchAndAddRelaxed(1);
281  }
282 
283  return newMap;
284  }
285 
286 
287  void CnetDisplayProperties::nullify() {
288  m_serialNumberToFileNameMap = NULL;
289  m_composeWatcher = NULL;
290 
291  m_composedCount = NULL;
292  m_interruptFlag = NULL;
293 
294  m_readWriteLock = NULL;
295  }
296 
297 
298  void CnetDisplayProperties::composeStatusUpdated() {
299  emit composeProgressChanged(*m_composedCount);
300  }
301 
302 
303  void CnetDisplayProperties::serialNumbersComposed() {
304  if (*m_interruptFlag) {
305  m_interruptFlag->fetchAndStoreRelaxed(0);
306  }
307  else {
308  m_readWriteLock->lockForWrite();
309  *m_serialNumberToFileNameMap = m_composeWatcher->result();
310 
311  m_readWriteLock->unlock();
312  }
313 
314  m_curComposing = false;
315  m_composeStatusPoller->stop();
316  emit composeProgressRangeChanged(0, 0);
317  emit composeProgressChanged(0);
318  emit compositionFinished();
319  }
320 }
QList< QString >
Isis::SerialNumber::Compose
static QString Compose(Pvl &label, bool def2filename=false)
Compose a SerialNumber from a PVL.
Definition: SerialNumber.cpp:38
QStringList
Isis::ControlNet::GetCubeSerials
QList< QString > GetCubeSerials() const
Use this method to get a complete list of all the cube serial numbers in the network.
Definition: ControlNet.cpp:1016
Isis::CnetDisplayProperties::getCubeList
QList< QString > getCubeList(ControlNet *cnet) const
TODO comment me.
Definition: CnetDisplayProperties.cpp:114
Isis::ControlNet
a control network
Definition: ControlNet.h:257
Isis::CnetDisplayProperties::getImageName
QString getImageName(QString cubeSerialNumber, bool forceFullPaths=false) const
Definition: CnetDisplayProperties.cpp:154
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
QFutureWatcher
This is free and unencumbered software released into the public domain.
Definition: AbstractTableModel.h:24
QMap< QString, QString >
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16