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 
40 namespace 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 
77  if (images) {
78  return (!images->isEmpty());
79  }
80  return false;
81  }
82 
83 
92  bool success = WorkOrder::setupExecution();
93 
94  if (success) {
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 
124  }
125 
126  return success;
127  }
128 
129 
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 
168  if (!m_warning.isEmpty()) {
169  project()->warn(m_warning);
170  m_warning.clear();
171  }
172  }
173 
174 
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 
206  }
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 }
QString name() const
Get the human-readable name of this image list.
Definition: ImageList.cpp:724
void setProgressValue(int)
Sets the current progress value for the WorkOrder.
Definition: WorkOrder.cpp:1382
Internalizes a list of images and allows for operations on the entire list.
Definition: ImageList.h:55
File name manipulation and expansion.
Definition: FileName.h:116
virtual bool setupExecution()
This sets up the state for the work order.
Definition: WorkOrder.cpp:1275
void append(const IException &exceptionSource)
Appends the given exception (and its list of previous exceptions) to this exception&#39;s causational exc...
Definition: IException.cpp:425
bool setupExecution()
Prompts the user for input.
void postExecution()
Display any warnings that occurred during the asynchronous computations.
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
void execute()
Use internalData() and write the images into the output directory.
Manipulate and parse attributes of output cube filenames.
A type of error that cannot be classified as any of the other error types.
Definition: IException.h:134
This represents a cube in a project-based GUI interface.
Definition: Image.h:107
bool m_isSynchronous
This is defaulted to true.
Definition: WorkOrder.h:541
Cube * copy(FileName newFile, const CubeAttributeOutput &newFileAttributes)
Copies the cube to the new fileName.
Definition: Cube.cpp:193
bool m_isUndoable
Set the workorder to be undoable/redoable This is defaulted to true - his will allow the workorder to...
Definition: WorkOrder.h:534
ProjectImageExportFunctor(QString destination)
Create an image export functor that will copy the image&#39;s cubes into the given destination directory...
void setProgressRange(int, int)
Sets the progress range of the WorkOrder.
Definition: WorkOrder.cpp:1372
bool isExecutable(ImageList *images)
Currently, this work order only works with either no data (file menu) or with any number of images...
QString toString() const
Returns a string representation of this exception.
Definition: IException.cpp:553
Isis exception class.
Definition: IException.h:107
This functor is meant for QtConcurrentMap.
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
Cube * cube()
Get the Cube pointer associated with this display property.
Definition: Image.cpp:287
Project * project() const
Returns the Project this WorkOrder is attached to.
Definition: WorkOrder.cpp:1314
ImageList * imageList()
Returns a pointer to the ImageList for this WorkOrder.
Definition: WorkOrder.cpp:645
IException errors() const
Get the accumulated error list from this functor&#39;s run.
QStringList internalData() const
Gets the internal data for this WorkOrder.
Definition: WorkOrder.cpp:1391
void setInternalData(QStringList data)
Sets the internal data for this WorkOrder.
Definition: WorkOrder.cpp:1332
void closeCube()
Cleans up the Cube pointer.
Definition: Image.cpp:307
QString fileName() const
Get the file name of the cube that this image represents.
Definition: Image.cpp:340
void * operator()(Image *const &imageToExport)
Write the given image&#39;s cube into the destination folder (preserves the base name).
ImageList * imageList(QString name)
Return an imagelist given its name.
Definition: Project.cpp:1520