Isis 3.0 Programmer Reference
Back | Home
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  QAction::setText(tr("Export I&mages..."));
45  }
46 
47 
48  ExportImagesWorkOrder::ExportImagesWorkOrder(const ExportImagesWorkOrder &other) :
49  WorkOrder(other) {
50  }
51 
52 
53  ExportImagesWorkOrder::~ExportImagesWorkOrder() {
54 
55  }
56 
57 
58  ExportImagesWorkOrder *ExportImagesWorkOrder::clone() const {
59  return new ExportImagesWorkOrder(*this);
60  }
61 
62 
72  return (!images->isEmpty());
73  }
74 
75 
84  bool success = WorkOrder::execute();
85 
86  if (success) {
88 
89  if (imageList()->isEmpty()) {
90  QStringList imageListNames;
91  foreach (ImageList *list, project()->images()) {
92  imageListNames.append(list->name());
93  }
94 
95  qSort(imageListNames);
96 
97  QString choice = QInputDialog::getItem(NULL, tr("Select Image List"),
98  tr("Please choose a list of images to export."), imageListNames, 0, false, &success);
99 
100  internalData.append(choice);
101 
102  QUndoCommand::setText(tr("Export image list [%1]").arg(choice));
103  }
104  else {
105  QUndoCommand::setText(tr("Export [%1] images").arg(imageList()->count()));
106  }
107 
108  QString destination = QFileDialog::getExistingDirectory(NULL, tr("Export Images"), ".");
109 
110  if (destination.isEmpty()) {
111  success = false;
112  }
113  internalData.append(destination);
114 
115  setInternalData(internalData);
116  }
117 
118  return success;
119  }
120 
121 
127  ImageList *list = imageList();
128 
129  if (list->isEmpty()) {
130  list = project()->imageList(internalData()[0]);
131  }
132 
133 
134  QString destination = internalData().last();
135  ProjectImageExportFunctor functor(destination);
136 
137  QFuture<void *> future = QtConcurrent::mapped(*list, functor);
138 
139  setProgressRange(0, list->count());
140 
141  QThreadPool::globalInstance()->releaseThread();
142  for (int i = 0; i < list->count(); i++) {
143  setProgressValue(i);
144 
145  // Wait for the result before updating the progress to the next value... it's still
146  // working on writing N images, just progress is going 1 by 1.
147  future.resultAt(i);
148  }
149  QThreadPool::globalInstance()->reserveThread();
150 
151  // Get the errors that occurred during the mapped in order to report them in postSyncRedo
152  m_warning = functor.errors().toString();
153  }
154 
155 
160  if (!m_warning.isEmpty()) {
161  project()->warn(m_warning);
162  m_warning.clear();
163  }
164  }
165 
166 
174  QString destination) : m_errors(new IException()), m_numErrors(new int(0)) {
175 
176  m_destination = destination;
177  }
178 
179 
187  const ProjectImageExportFunctor &other) : m_errors(other.m_errors),
188  m_numErrors(other.m_numErrors) {
189 
190  m_destination = other.m_destination;
191  }
192 
193 
198  }
199 
200 
209  try {
210  FileName outputFileName =
211  m_destination + "/" + FileName(imageToExport->fileName()).baseName();
212 
213  // Copy the image, free the copy from memory because we don't need it.
214  delete imageToExport->cube()->copy(outputFileName, CubeAttributeOutput());
215  // Avoid opening too many cubes
216  imageToExport->closeCube();
217  }
218  catch (IException &e) {
219  QMutexLocker locker(&m_errorsLock);
220  m_errors->append(e);
221  (*m_numErrors)++;
222  }
223 
224  return NULL;
225  }
226 
227 
235  IException result;
236 
237  result.append(*m_errors);
238 
239  if (*m_numErrors != 0) {
240  result.append(
242  tr("Failed to export [%1] images").arg(*m_numErrors),
243  _FILEINFO_));
244  }
245 
246  return result;
247  }
248 }
void setProgressValue(int)
Sets the current progress value for the WorkOrder.
Definition: WorkOrder.cpp:1177
Internalizes a list of images and allows for operations on the entire list.
Definition: ImageList.h:44
File name manipulation and expansion.
Definition: FileName.h:111
void postSyncRedo()
Display any warnings that occurred during the asynchronous computations.
QString fileName() const
Get the file name of the cube that this image represents.
Definition: Image.cpp:305
void asyncRedo()
Use internalData() and write the images into the output directory.
QString name() const
Get the human-readable name of this image list.
Definition: ImageList.cpp:719
Project * project() const
Returns the Project this WorkOrder is attached to.
Definition: WorkOrder.cpp:1116
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
QStringList internalData() const
Gets the internal data for this WorkOrder.
Definition: WorkOrder.cpp:1186
IException errors() const
Get the accumulated error list from this functor&#39;s run.
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:38
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:126
This represents a cube in a project-based GUI interface.
Definition: Image.h:91
ProjectImageExportFunctor(QString destination)
Create an image export functor that will copy the image&#39;s cubes into the given destination directory...
bool execute()
Prompts the user for input.
void setProgressRange(int, int)
Sets the progress range of the WorkOrder.
Definition: WorkOrder.cpp:1167
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:99
This functor is meant for QtConcurrentMap.
Cube * cube()
Get the Cube pointer associated with this display property.
Definition: Image.cpp:254
virtual bool execute()
The (child) implementation of this method should prompt the user/gather state by any means necessary...
Definition: WorkOrder.cpp:1078
ImageList * imageList()
a pointer to the ImageList for this WorkOrder.
Definition: WorkOrder.cpp:565
void setInternalData(QStringList data)
Sets the internal data for this WorkOrder.
Definition: WorkOrder.cpp:1130
void closeCube()
Cleans up the Cube pointer.
Definition: Image.cpp:272
void * operator()(Image *const &imageToExport)
Write the given image&#39;s cube into the destination folder (preserves the base name).

U.S. Department of the Interior | U.S. Geological Survey
ISIS | Privacy & Disclaimers | Astrogeology Research Program
To contact us, please post comments and questions on the ISIS Support Center
File Modified: 07/12/2023 23:17:59