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