11 #include "CnetDisplayProperties.h"
19 #include <QFutureWatcher>
21 #include <QReadWriteLock>
23 #include <QStringList>
24 #include <QTextStream>
26 #include <QtConcurrentRun>
28 #include "ControlNet.h"
30 #include "IException.h"
32 #include "SerialNumber.h"
36 CnetDisplayProperties *CnetDisplayProperties::m_instance = NULL;
39 CnetDisplayProperties *CnetDisplayProperties::getInstance() {
40 return m_instance ? m_instance : m_instance =
new CnetDisplayProperties;
44 CnetDisplayProperties::CnetDisplayProperties() {
48 m_curComposing =
false;
49 m_showFullPath =
false;
51 m_readWriteLock =
new QReadWriteLock;
55 m_composedCount =
new QAtomicInt;
56 m_interruptFlag =
new QAtomicInt;
57 m_interruptFlag->fetchAndStoreRelaxed(0);
59 m_composeStatusPoller =
new QTimer;
60 connect(m_composeStatusPoller, SIGNAL(timeout()),
61 this, SLOT(composeStatusUpdated()));
64 connect(m_composeWatcher, SIGNAL(finished()),
65 this, SLOT(serialNumbersComposed()));
86 CnetDisplayProperties::~CnetDisplayProperties() {
87 delete m_serialNumberToFileNameMap;
88 m_serialNumberToFileNameMap = NULL;
90 delete m_composeWatcher;
91 m_composeWatcher = NULL;
93 delete m_composedCount;
94 m_composedCount = NULL;
96 delete m_interruptFlag;
97 m_interruptFlag = NULL;
101 bool CnetDisplayProperties::currentlyComposing()
const {
102 return m_curComposing;
117 if (!currentlyComposing()) {
119 QString possibleFileName =
getImageName(serialNumber,
true);
121 if (possibleFileName != serialNumber)
122 results.append(possibleFileName);
130 QString CnetDisplayProperties::getFileName(QString fileName,
131 bool forceFullPaths)
const {
134 if (forceFullPaths || getShowsFullPaths())
137 result = QFileInfo(fileName).fileName();
155 bool forceFullPaths)
const {
156 ASSERT(m_serialNumberToFileNameMap);
158 QString imageName = cubeSerialNumber;
160 m_readWriteLock->lockForRead();
162 if (m_serialNumberToFileNameMap && useFileNames &&
163 m_serialNumberToFileNameMap->contains(cubeSerialNumber)) {
164 QString value = m_serialNumberToFileNameMap->value(cubeSerialNumber);
165 m_readWriteLock->unlock();
167 if (value.toLower() !=
"unknown")
168 imageName = getFileName(value, forceFullPaths);
171 m_readWriteLock->unlock();
178 QString CnetDisplayProperties::getSerialNumber(QString imageId) {
179 ASSERT(m_serialNumberToFileNameMap);
181 QString sn = imageId;
183 if (m_serialNumberToFileNameMap && useFileNames) {
184 m_readWriteLock->lockForRead();
185 QMapIterator< QString, QString > i(*m_serialNumberToFileNameMap);
188 while (!found && i.hasNext()) {
190 if (i.value() == imageId) {
196 m_readWriteLock->unlock();
203 bool CnetDisplayProperties::getShowsFullPaths()
const {
204 return m_showFullPath;
208 void CnetDisplayProperties::setCubeList(QString fileName) {
209 QFile imageListFile(fileName);
211 if (!imageListFile.exists()) {
212 IString msg =
"The file [";
213 msg += (IString) fileName;
214 msg +=
"] does not exist.\n";
218 if (!imageListFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
219 IString msg =
"The file [";
220 msg += (IString) fileName;
221 msg +=
"] failed to open.\n";
226 QTextStream ts(&imageListFile);
229 imageFileNames << ts.readLine();
231 imageListFile.close();
233 m_curComposing =
true;
235 QFuture< QMap< QString, QString > > future = QtConcurrent::run(
236 this, &CnetDisplayProperties::composeSerialNumbers, imageFileNames);
238 m_composeWatcher->setFuture(future);
242 void CnetDisplayProperties::setFileNameUsage(
bool preferFileNames) {
243 useFileNames = preferFileNames;
260 void CnetDisplayProperties::setShowsFullPaths(
bool newState) {
261 m_showFullPath = newState;
267 emit composeProgressRangeChanged(0, fileNames.size() - 1);
268 m_composedCount->fetchAndStoreRelaxed(0);
272 for (
int i = 0; *m_interruptFlag == 0 && i < fileNames.size(); i++) {
273 QString fileName = fileNames[i];
280 m_composedCount->fetchAndAddRelaxed(1);
287 void CnetDisplayProperties::nullify() {
288 m_serialNumberToFileNameMap = NULL;
289 m_composeWatcher = NULL;
291 m_composedCount = NULL;
292 m_interruptFlag = NULL;
294 m_readWriteLock = NULL;
298 void CnetDisplayProperties::composeStatusUpdated() {
299 emit composeProgressChanged(*m_composedCount);
303 void CnetDisplayProperties::serialNumbersComposed() {
304 if (*m_interruptFlag) {
305 m_interruptFlag->fetchAndStoreRelaxed(0);
308 m_readWriteLock->lockForWrite();
309 *m_serialNumberToFileNameMap = m_composeWatcher->result();
311 m_readWriteLock->unlock();
314 m_curComposing =
false;
315 m_composeStatusPoller->stop();
316 emit composeProgressRangeChanged(0, 0);
317 emit composeProgressChanged(0);
318 emit compositionFinished();