Official websites use .gov
A .gov website belongs to an official government organization in the United States.

Secure .gov websites use HTTPS
A lock ( ) or https:// means you’ve safely connected to the .gov website. Share sensitive information only on official, secure websites.

Isis 3 Programmer Reference
ImageReader.cpp
1#include "ImageReader.h"
2
3#include <QAction>
4#include <QApplication>
5#include <QDebug>
6#include <QFuture>
7#include <QFutureWatcher>
8#include <QInputDialog>
9#include <QList>
10#include <QProgressBar>
11#include <QSettings>
12#include <QString>
13#include <QtConcurrentMap>
14#include <QVariant>
15
16#include "IException.h"
17#include "Image.h"
19#include "IString.h"
20#include "FileName.h"
21#include "ProgressBar.h"
22#include "PvlObject.h"
23
24namespace Isis {
25
35 ImageReader::ImageReader(QMutex *cameraMutex, bool requireFootprints, QObject *parent) :
36 QObject(parent) {
37 m_watcher = NULL;
38
39 m_cameraMutex = cameraMutex;
40 m_requireFootprints = requireFootprints;
41
42 m_progress = new ProgressBar("Reading Images");
43 m_watcher = new QFutureWatcher<Image *>;
44
45 initProgress();
46
47 connect(m_watcher, SIGNAL(resultReadyAt(int)),
48 this, SLOT(imageReady(int)));
49 connect(m_watcher, SIGNAL(finished()),
50 this, SLOT(mappedFinished()));
51
52 m_safeFileOpen = false;
53 m_openFilled = true;
54 m_defaultAlpha = 60;
55
56 m_mappedRunning = false;
57
58 readSettings();
59 }
60
61
66 writeSettings();
67
68 if (m_watcher) {
69 m_watcher->cancel();
70 m_watcher->waitForFinished();
71 }
72
73 delete m_watcher;
74 m_watcher = NULL;
75
76 m_backlog.clear();
77
78 m_cameraMutex = NULL;
79
80 delete m_progress;
81 }
82
83
84 QList<QAction *> ImageReader::actions(ImageDisplayProperties::Property relevantDispProperties) {
85 QList<QAction *> results;
86
87 if (!m_safeFileOpenAct) {
88 m_safeFileOpenAct = new QAction(tr("&Safe File Open"), this);
89 m_safeFileOpenAct->setCheckable(true);
90 m_safeFileOpenAct->setChecked(m_safeFileOpen);
91 m_safeFileOpenAct->setWhatsThis(tr("This lowers the number of "
92 "simulataneously open files drastically in order to stay under the "
93 "operating system limit. Only use this if you are having trouble "
94 "loading large numbers of images."));
95 connect(m_safeFileOpenAct, SIGNAL(toggled(bool)),
96 this, SLOT(setSafeFileOpen(bool)));
97 }
98
99 results.append(m_safeFileOpenAct);
100
101 if ((relevantDispProperties & ImageDisplayProperties::ShowFill) == ImageDisplayProperties::ShowFill) {
102 if (!m_openFilledAct) {
103 m_openFilledAct = new QAction(tr("Default Images &Filled"), this);
104 m_openFilledAct->setCheckable(true);
105 m_openFilledAct->setChecked(m_openFilled);
106 m_openFilledAct->setWhatsThis(tr("When this is enabled, images will be overlayed with "
107 "a color."));
108 connect(m_openFilledAct, SIGNAL(toggled(bool)),
109 this, SLOT(setOpenFilled(bool)));
110 }
111
112 results.append(m_openFilledAct);
113 }
114
115 if ((relevantDispProperties & ImageDisplayProperties::Color) == ImageDisplayProperties::Color) {
116 if (!m_askAlphaAct) {
117 m_askAlphaAct = new QAction(tr("Set Default &Transparency"), this);
118 connect(m_askAlphaAct, SIGNAL(triggered(bool)),
119 this, SLOT(askDefaultAlpha()));
120 }
121
122 results.append(m_askAlphaAct);
123 }
124
125 return results;
126 }
127
128
129 QProgressBar *ImageReader::progress() {
130 return m_progress;
131 }
132
133 void ImageReader::setQtVersion(int qtVersion) {
134 m_qtVersion = qtVersion;
135 }
136
137
138 void ImageReader::askDefaultAlpha() {
139 bool ok = false;
140 int alpha = QInputDialog::getInt(NULL, tr("Transparency Value"),
141 tr("Set the default transparency value\n"
142 "Values are 0 (invisible) to 255 (solid)"),
143 m_defaultAlpha, 0, 255, 1, &ok);
144
145 if(ok) {
146 m_defaultAlpha = alpha;
147 }
148 }
149
150
151 void ImageReader::read(PvlObject imagesObject) {
152 read(imagesObject.beginObject(), imagesObject.endObject());
153 }
154
155
159 void ImageReader::read(QStringList cubeNames) {
160 read(cubeNames.begin(), cubeNames.end());
161 }
162
163
164 void ImageReader::setOpenFilled(bool openFilled) {
165 m_openFilled = openFilled;
166 if (m_openFilledAct) {
167 m_openFilledAct->setChecked(m_openFilled);
168 }
169 }
170
171
172 void ImageReader::setSafeFileOpen(bool safeFileOpen) {
173 m_safeFileOpen = safeFileOpen;
174 if (m_safeFileOpen) {
175 m_safeFileOpenAct->setChecked(m_safeFileOpen);
176 }
177 }
178
179
180 void ImageReader::initProgress() {
181 m_progress->setVisible(false);
182 m_progress->setRange(0, 0);
183 m_progress->setValue(0);
184 }
185
186
187 void ImageReader::start() {
188 if (!m_backlog.isEmpty() && !m_mappedRunning) {
189 m_progress->setVisible(true);
190
191 int maxOpenImages = m_safeFileOpen? 20 : 400;
192 QList<QVariant> culledBacklog = m_backlog.mid(0, maxOpenImages);
193 m_backlog = m_backlog.mid(maxOpenImages);
194
195 QFuture< Image * > images = QtConcurrent::mapped(
196 culledBacklog,
197 VariantToImageFunctor(m_cameraMutex, m_requireFootprints, QThread::currentThread(),
198 m_openFilled, m_defaultAlpha, m_qtVersion));
199
200 m_watcher->setFuture(images);
201 m_mappedRunning = true;
202 }
203 }
204
205
206 void ImageReader::readSettings() {
207 QSettings settings(
208 FileName("$HOME/.Isis/" + QApplication::applicationName() + "/Image Reader.config")
209 .expanded(),
210 QSettings::NativeFormat);
211
212 m_safeFileOpen = settings.value("safeFileOpen", m_safeFileOpen).toBool();
213 m_openFilled = settings.value("openFilled", m_openFilled).toBool();
214 m_defaultAlpha = settings.value("defaultAlpha", m_defaultAlpha).toInt();
215 }
216
217
218 void ImageReader::writeSettings() {
219 QSettings settings(
220 FileName("$HOME/.Isis/" + QApplication::applicationName() + "/Image Reader.config")
221 .expanded(),
222 QSettings::NativeFormat);
223
224 settings.setValue("safeFileOpen", m_safeFileOpen);
225 settings.setValue("openFilled", m_openFilled);
226 settings.setValue("defaultAlpha", m_defaultAlpha);
227 }
228
229
230 void ImageReader::imageReady(int index) {
231 m_progress->setValue(m_progress->value() + 1);
232 }
233
234
235 void ImageReader::mappedFinished() {
236 ImageList images(m_watcher->future().results());
237
238 // Tracie Sucharski: Go through list & get rid of NULLs. This is a temporary fix to get rid
239 // of seg faulting when something goes wrong. This is not a good solution & needs to be
240 // properly fixed
241 foreach (Image *image, images) {
242 if (!image) {
243 images.removeAll(image);
244 }
245 }
246
247 emit imagesReady(images);
248
249 m_mappedRunning = false;
250 if (!m_backlog.isEmpty()) {
251 start();
252 }
253 else {
254 initProgress();
255 }
256 }
257
258
265 QMutex *cameraMutex, bool requireFootprints, QThread *targetThread, bool openFilled,
266 int defaultAlpha, int qtVersion) {
267 m_mutex = cameraMutex;
268 m_targetThread = targetThread;
269 m_openFilled = openFilled;
270 m_defaultAlpha = defaultAlpha;
271 m_requireFootprints = requireFootprints;
272 m_qtVersion = qtVersion;
273 }
274
275
284 const QVariant &imageData) {
285 Image *result = NULL;
286
287 try {
288 QString fileName;
289 if (imageData.canConvert<QString>()) {
290 fileName = imageData.value<QString>();
291
292 result = new Image(FileName(fileName).expanded());
293 ImageDisplayProperties *prop = result->displayProperties();
294 prop->setShowFill(m_openFilled);
295
296 QColor newColor = prop->getValue(ImageDisplayProperties::Color).value<QColor>();
297 newColor.setAlpha(m_defaultAlpha);
298
299 prop->setColor(newColor);
300 }
301 else if (imageData.canConvert<PvlObject>()) {
302 PvlObject imageObj = imageData.value<PvlObject>();
303 fileName = ((IString)imageObj["FileName"][0]).ToQt();
304 result = new Image(FileName(fileName).expanded());
305 PvlObject &displayProps = imageObj.findObject("DisplayProperties");
306 displayProps += PvlKeyword("QtVersion", QString::number(m_qtVersion));
307 result->fromPvl(imageObj);
308 }
309
310 if (m_requireFootprints) {
311 try {
312 result->initFootprint(m_mutex);
313 }
314 catch (IException &) {
315 throw;
316 }
317 }
318
319 result->moveToThread(m_targetThread);
320 }
321 catch(IException &e) {
322 e.print();
323 result = NULL;
324 }
325
326 return result;
327 }
328}
QVariant getValue(int property) const
Get a property's associated data.
This is the GUI communication mechanism for cubes.
void setShowFill(bool)
Change the visibility of the fill area associated with this cube.
Property
This is a list of properties and actions that are possible.
@ ShowFill
True if the cube should show a fill area if possible (bool)
@ Color
The color of the cube, default randomized (QColor)
void setColor(QColor newColor)
Change the color associated with this cube.
This represents a cube in a project-based GUI interface.
Definition Image.h:105
void fromPvl(const PvlObject &pvl)
Read the image settings from a Pvl.
Definition Image.cpp:170
bool initFootprint(QMutex *cameraMutex)
Calculate a footprint for this image.
Definition Image.cpp:385
Converts from file name or project representation to Image *.
VariantToImageFunctor(QMutex *cameraMutex, bool requireFootprints, QThread *targetThread, bool openFilled, int defaultAlpha, int qtVersion)
Create a functor for converting from QVariant to an Image *.
Image * operator()(const QVariant &)
Read the QString filename and make an Image from it.
QPointer< QAction > m_askAlphaAct
Variant Internal Format: (QString|PvlObject).
Definition ImageReader.h:97
ImageReader(QMutex *cameraMutex, bool requireFootprints=true, QObject *parent=NULL)
MosaicWidget constructor.
virtual ~ImageReader()
Free the allocated memory by this object.
This is free and unencumbered software released into the public domain.
This is free and unencumbered software released into the public domain.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16