1 #include "ImageReader.h"
4 #include <QApplication>
7 #include <QFutureWatcher>
8 #include <QInputDialog>
10 #include <QProgressBar>
13 #include <QtConcurrentMap>
16 #include "IException.h"
21 #include "ProgressBar.h"
22 #include "PvlObject.h"
39 m_cameraMutex = cameraMutex;
40 m_requireFootprints = requireFootprints;
47 connect(m_watcher, SIGNAL(resultReadyAt(
int)),
48 this, SLOT(imageReady(
int)));
49 connect(m_watcher, SIGNAL(finished()),
50 this, SLOT(mappedFinished()));
52 m_safeFileOpen =
false;
56 m_mappedRunning =
false;
70 m_watcher->waitForFinished();
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)));
99 results.append(m_safeFileOpenAct);
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 "
108 connect(m_openFilledAct, SIGNAL(toggled(
bool)),
109 this, SLOT(setOpenFilled(
bool)));
112 results.append(m_openFilledAct);
119 this, SLOT(askDefaultAlpha()));
134 void ImageReader::askDefaultAlpha() {
136 int alpha = QInputDialog::getInt(NULL, tr(
"Transparency Value"),
137 tr(
"Set the default transparency value\n"
138 "Values are 0 (invisible) to 255 (solid)"),
139 m_defaultAlpha, 0, 255, 1, &ok);
142 m_defaultAlpha = alpha;
147 void ImageReader::read(PvlObject imagesObject) {
148 read(imagesObject.beginObject(), imagesObject.endObject());
156 read(cubeNames.begin(), cubeNames.end());
160 void ImageReader::setOpenFilled(
bool openFilled) {
161 m_openFilled = openFilled;
162 if (m_openFilledAct) {
163 m_openFilledAct->setChecked(m_openFilled);
168 void ImageReader::setSafeFileOpen(
bool safeFileOpen) {
169 m_safeFileOpen = safeFileOpen;
170 if (m_safeFileOpen) {
171 m_safeFileOpenAct->setChecked(m_safeFileOpen);
176 void ImageReader::initProgress() {
177 m_progress->setVisible(
false);
178 m_progress->setRange(0, 0);
179 m_progress->setValue(0);
183 void ImageReader::start() {
184 if (!m_backlog.isEmpty() && !m_mappedRunning) {
185 m_progress->setVisible(
true);
187 int maxOpenImages = m_safeFileOpen? 20 : 400;
189 m_backlog = m_backlog.mid(maxOpenImages);
191 QFuture< Image * > images = QtConcurrent::mapped(
193 VariantToImageFunctor(m_cameraMutex, m_requireFootprints, QThread::currentThread(),
194 m_openFilled, m_defaultAlpha));
196 m_watcher->setFuture(images);
197 m_mappedRunning =
true;
202 void ImageReader::readSettings() {
204 FileName(
"$HOME/.Isis/" + QApplication::applicationName() +
"/Image Reader.config")
206 QSettings::NativeFormat);
208 m_safeFileOpen = settings.value(
"safeFileOpen", m_safeFileOpen).toBool();
209 m_openFilled = settings.value(
"openFilled", m_openFilled).toBool();
210 m_defaultAlpha = settings.value(
"defaultAlpha", m_defaultAlpha).toInt();
214 void ImageReader::writeSettings() {
216 FileName(
"$HOME/.Isis/" + QApplication::applicationName() +
"/Image Reader.config")
218 QSettings::NativeFormat);
220 settings.setValue(
"safeFileOpen", m_safeFileOpen);
221 settings.setValue(
"openFilled", m_openFilled);
222 settings.setValue(
"defaultAlpha", m_defaultAlpha);
226 void ImageReader::imageReady(
int index) {
227 m_progress->setValue(m_progress->value() + 1);
231 void ImageReader::mappedFinished() {
232 ImageList images(m_watcher->future().results());
237 foreach (Image *image, images) {
239 images.removeAll(image);
243 emit imagesReady(images);
245 m_mappedRunning =
false;
246 if (!m_backlog.isEmpty()) {
261 QMutex *cameraMutex,
bool requireFootprints,
QThread *targetThread,
bool openFilled,
263 m_mutex = cameraMutex;
264 m_targetThread = targetThread;
265 m_openFilled = openFilled;
266 m_defaultAlpha = defaultAlpha;
267 m_requireFootprints = requireFootprints;
279 const QVariant &imageData) {
280 Image *result = NULL;
284 if (imageData.canConvert<QString>()) {
285 fileName = imageData.value<QString>();
292 newColor.setAlpha(m_defaultAlpha);
296 else if (imageData.canConvert<
PvlObject>()) {
298 fileName = ((
IString)imageObj[
"FileName"][0]).ToQt();
303 if (m_requireFootprints) {
312 result->moveToThread(m_targetThread);