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