Isis 3 Programmer Reference
ExportImagesWorkOrder.cpp
Go to the documentation of this file.
1
23#include "ExportImagesWorkOrder.h"
24
25#include <QDebug>
26#include <QFileDialog>
27#include <QFuture>
28#include <QInputDialog>
29#include <QtConcurrentMap>
30
31#include "Cube.h"
32#include "CubeAttribute.h"
33#include "Image.h"
34#include "ImageList.h"
35#include "FileName.h"
36#include "IException.h"
37#include "IString.h"
38#include "Project.h"
39
40namespace Isis {
41
42 ExportImagesWorkOrder::ExportImagesWorkOrder(Project *project) :
43 WorkOrder(project) {
44
45 // This is an asynchronous workorder and not undoable
46 m_isSynchronous = false;
47 m_isUndoable = false;
48
49 QAction::setText(tr("Export I&mages..."));
50 }
51
52
53 ExportImagesWorkOrder::ExportImagesWorkOrder(const ExportImagesWorkOrder &other) :
54 WorkOrder(other) {
55 }
56
57
58 ExportImagesWorkOrder::~ExportImagesWorkOrder() {
59
60 }
61
62
63 ExportImagesWorkOrder *ExportImagesWorkOrder::clone() const {
64 return new ExportImagesWorkOrder(*this);
65 }
66
67
76 bool ExportImagesWorkOrder::isExecutable(ImageList *images) {
77 if (images) {
78 return (!images->isEmpty());
79 }
80 return false;
81 }
82
83
91 bool ExportImagesWorkOrder::setupExecution() {
92 bool success = WorkOrder::setupExecution();
93
94 if (success) {
95 QStringList internalData;
96
97 if (imageList()->isEmpty()) {
98 QStringList imageListNames;
99 foreach (ImageList *list, project()->images()) {
100 imageListNames.append(list->name());
101 }
102
103 qSort(imageListNames);
104
105 QString choice = QInputDialog::getItem(NULL, tr("Select Image List"),
106 tr("Please choose a list of images to export."), imageListNames, 0, false, &success);
107
108 internalData.append(choice);
109
110 QUndoCommand::setText(tr("Export image list [%1]").arg(choice));
111 }
112 else {
113 QUndoCommand::setText(tr("Export [%1] images").arg(imageList()->count()));
114 }
115
116 QString destination = QFileDialog::getExistingDirectory(NULL, tr("Export Images"), ".");
117
118 if (destination.isEmpty()) {
119 success = false;
120 }
121 internalData.append(destination);
122
123 setInternalData(internalData);
124 }
125
126 return success;
127 }
128
129
134 void ExportImagesWorkOrder::execute() {
135 ImageList *list = imageList();
136
137 if (list->isEmpty()) {
138 list = project()->imageList(internalData()[0]);
139 }
140
141
142 QString destination = internalData().last();
143 ProjectImageExportFunctor functor(destination);
144
145 QFuture<void *> future = QtConcurrent::mapped(*list, functor);
146
147 setProgressRange(0, list->count());
148
149 QThreadPool::globalInstance()->releaseThread();
150 for (int i = 0; i < list->count(); i++) {
151 setProgressValue(i);
152
153 // Wait for the result before updating the progress to the next value... it's still
154 // working on writing N images, just progress is going 1 by 1.
155 future.resultAt(i);
156 }
157 QThreadPool::globalInstance()->reserveThread();
158
159 // Get the errors that occurred during the mapped in order to report them in postSyncRedo
160 m_warning = functor.errors().toString();
161 }
162
163
167 void ExportImagesWorkOrder::postExecution() {
168 if (!m_warning.isEmpty()) {
169 project()->warn(m_warning);
170 m_warning.clear();
171 }
172 }
173
174
181 ExportImagesWorkOrder::ProjectImageExportFunctor::ProjectImageExportFunctor(
182 QString destination) : m_errors(new IException()), m_numErrors(new int(0)) {
183
184 m_destination = destination;
185 }
186
187
195 const ProjectImageExportFunctor &other) : m_errors(other.m_errors),
196 m_numErrors(other.m_numErrors) {
197
198 m_destination = other.m_destination;
199 }
200
201
207
208
217 try {
218 FileName outputFileName =
219 m_destination + "/" + FileName(imageToExport->fileName()).baseName();
220
221 // Copy the image, free the copy from memory because we don't need it.
222 delete imageToExport->cube()->copy(outputFileName, CubeAttributeOutput());
223 // Avoid opening too many cubes
224 imageToExport->closeCube();
225 }
226 catch (IException &e) {
227 QMutexLocker locker(&m_errorsLock);
228 m_errors->append(e);
229 (*m_numErrors)++;
230 }
231
232 return NULL;
233 }
234
235
243 IException result;
244
245 result.append(*m_errors);
246
247 if (*m_numErrors != 0) {
248 result.append(
250 tr("Failed to export [%1] images").arg(*m_numErrors),
251 _FILEINFO_));
252 }
253
254 return result;
255 }
256}
Manipulate and parse attributes of output cube filenames.
IException errors() const
Get the accumulated error list from this functor's run.
void * operator()(Image *const &imageToExport)
Write the given image's cube into the destination folder (preserves the base name).
ProjectImageExportFunctor(QString destination)
Create an image export functor that will copy the image's cubes into the given destination directory.
File name manipulation and expansion.
Definition FileName.h:100
QString baseName() const
Returns the name of the file without the path and without extensions.
Definition FileName.cpp:145
Isis exception class.
Definition IException.h:91
@ Unknown
A type of error that cannot be classified as any of the other error types.
Definition IException.h:118
void append(const IException &exceptionSource)
Appends the given exception (and its list of previous exceptions) to this exception's causational exc...
This represents a cube in a project-based GUI interface.
Definition Image.h:107
Internalizes a list of images and allows for operations on the entire list.
Definition ImageList.h:55
QString name() const
Get the human-readable name of this image list.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16