Isis 3 Programmer Reference
ImportControlNetWorkOrder.cpp
Go to the documentation of this file.
1 
24 
25 #include <QDebug>
26 #include <QFileDialog>
27 #include <QFileInfo>
28 #include <QMessageBox>
29 #include <QtConcurrentMap>
30 
31 #include "Control.h"
32 #include "ControlList.h"
33 #include "ControlNet.h"
34 #include "IException.h"
35 #include "Progress.h"
36 #include "Project.h"
37 #include "ProjectItem.h"
38 #include "ProjectItemModel.h"
39 
40 namespace Isis {
41 
48  WorkOrder(project) {
49 
50  // This is an asynchronous workorder
51  m_isSynchronous = false;
52  m_list = NULL;
53  m_watcher = NULL;
54  m_isUndoable = false;
55 
56  QAction::setText(tr("Import &Control Networks..."));
57 
59 
61  connect(m_watcher, SIGNAL(resultReadyAt(int)), this, SLOT(cnetReady(int)));
62  }
63 
64 
71  WorkOrder(other) {
72  m_watcher = NULL;
74  connect(m_watcher, SIGNAL(resultReadyAt(int)), this, SLOT(cnetReady(int)));
75  }
76 
77 
82  delete m_watcher;
83  m_watcher = NULL;
84  }
85 
86 
93  return new ImportControlNetWorkOrder(*this);
94  }
95 
96 
108 
109  if (item) {
110  return (item->text() == "Control Networks");
111  }
112  return false;
113  }
114 
115 
127  QUndoCommand::setText(tr("Import Control Networks"));
128 
130 
131  QStringList cnetFileNames = QFileDialog::getOpenFileNames(
132  qobject_cast<QWidget *>(parent()),
133  tr("Import Control Networks"), "",
134  tr("Isis control nets (*.net);;All Files (*)"));
135 
136  if (!cnetFileNames.isEmpty()) {
137  QUndoCommand::setText(tr("Import %1 Control Networks").arg(cnetFileNames.count()));
138  }
139 
140  setInternalData(cnetFileNames);
141 
142  return internalData().count() > 0;
143  }
144 
152  QDir cnetFolder = project()->addCnetFolder("controlNetworks");
153 
154  QStringList cnetFileNames = internalData();
155 
156  QList< QPair<FileName, Progress *> > cnetFileNamesAndProgress;
157  foreach (FileName fileName, cnetFileNames) {
158  Progress *readProgress = new Progress;
159  cnetFileNamesAndProgress.append(qMakePair(fileName, readProgress));
160  readProgress->DisableAutomaticDisplay();
161  m_readProgresses.append(readProgress);
162  }
163 
164  CreateControlsFunctor functor(project(), cnetFolder);
165  m_watcher->setFuture(QtConcurrent::mapped(cnetFileNamesAndProgress,
166  functor));
167 
168  while (!m_watcher->isFinished()) {
169  setProgressRange(0, 100 * m_readProgresses.count());
170  int totalProgress = 0;
171 
172  for (int i = 0; i < m_readProgresses.count(); i++) {
173  Progress *progress = m_readProgresses[i];
174 
175  if (m_watcher->future().isResultReadyAt(i)) {
176  totalProgress += 100;
177  }
178  else if (progress->MaximumSteps() > 0) {
179  double progressPercent = progress->CurrentStep() / (double)progress->MaximumSteps();
180  // Estimating the Read is 90% and Write 10% (ish)
181  totalProgress += qRound(progressPercent * 90);
182  }
183  }
184 
185  setProgressValue(totalProgress);
186 
187  m_warning = functor.errors().toString();
188 
189  QThread::yieldCurrentThread();
190  }
191  }
192 
200 
201  if (m_warning != "") {
202  project()->warn(m_warning);
203  }
204 
205  foreach (Progress *progress, m_readProgresses) {
206  delete progress;
207  }
208  m_status = WorkOrderFinished;
209  m_readProgresses.clear();
210 
211  // If one control network was imported, no active control has been set, and no
212  // other control networks exist in the project, then activeControl() will set
213  // the active control to the newly imported control network.
214  project()->activeControl();
215  }
216 
224  Project *project, QDir destinationFolder) : m_errors(new IException) {
225  m_project = project;
226  m_destinationFolder = destinationFolder;
227  }
228 
229 
240  IException result;
241 
242  result.append(*m_errors);
243 
244  return result;
245  }
246 
247 
256  const QPair<FileName, Progress *> &cnetFileNameAndProgress) {
257 
258  Control *control = NULL;
259  try {
260  QString cnetFileName = cnetFileNameAndProgress.first.original();
261  ControlNet *cnet = new ControlNet();
262  cnet->SetMutex(m_project->mutex());
263  cnet->ReadControl(cnetFileName, cnetFileNameAndProgress.second);
264 
265  QString baseFilename = FileName(cnetFileName).name();
266  QString destination = m_destinationFolder.canonicalPath() + "/" + baseFilename;
267 
268  cnet->Write(destination);
269 
270  delete cnet;
271  cnet = NULL;
272 
273  control = new Control(m_project, destination);
274  control->closeControlNet();
275  }
276  catch (IException &e) {
277  m_errors->append(e);
278  return NULL;
279  }
280  return control;
281  }
282 
283 
290  QMutexLocker locker(project()->workOrderMutex());
291  Control *control = m_watcher->resultAt(ready);
292 
293  if (control) {
294  project()->addControl(control);
295  m_list = project()->controls().last();
296  project()->setClean(false);
297  }
298  else {
299  m_list = NULL;
300  }
301  }
302 }
This represents an ISIS control net in a project-based GUI interface.
Definition: Control.h:79
virtual ImportControlNetWorkOrder * clone() const
This method clones the current ImportControlNetWorkOrder and returns it.
$Date$ $Revision$
void setProgressValue(int)
Sets the current progress value for the WorkOrder.
Definition: WorkOrder.cpp:1382
QFutureWatcher< Control * > * m_watcher
QFutureWatcher, allows for asynchronous import.
void ReadControl(const QString &filename, Progress *progress=0)
Reads in the control points from the given file.
Definition: ControlNet.cpp:263
The main project for ipce.
Definition: Project.h:289
CreateControlsFunctor(Project *project, QDir destinationFolder)
CreateControlsFunctor constructor.
void SetMutex(QMutex *mutex)
Set mutex to lock for making Naif calls.
File name manipulation and expansion.
Definition: FileName.h:116
void DisableAutomaticDisplay()
Turns off updating the Isis Gui when CheckStatus() is called.
Definition: Progress.cpp:177
virtual bool isExecutable(ProjectItem *item)
This method returns true if the user clicked on a project tree node with the text "Control Networks"...
ControlList * m_list
List of controls added to project.
virtual bool setupExecution()
This sets up the state for the work order.
Definition: WorkOrder.cpp:1275
QList< Progress * > m_readProgresses
Keeps track of import progress.
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
Control * operator()(const QPair< FileName, Progress *> &cnetFilename)
Reads and writes the control network(s) asynchronously.
void addControl(Control *control)
Add the given Control&#39;s to the current project.
Definition: Project.cpp:958
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
Definition: FileName.cpp:178
Control * activeControl()
Return the Active Control (control network)
Definition: Project.cpp:1903
void closeControlNet()
Cleans up the ControlNet pointer.
Definition: Control.cpp:206
Program progress reporter.
Definition: Progress.h:58
QDir m_destinationFolder
The directory to copy the control net too.
a control network
Definition: ControlNet.h:271
Provide Undo/redo abilities, serialization, and history for an operation.
Definition: WorkOrder.h:322
Add control networks to a project c Asks the user for a list of control nets and copies them into the...
void cnetReady(int ready)
Adds the control net to the project.
bool m_isSynchronous
This is defaulted to true.
Definition: WorkOrder.h:541
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
bool setupExecution()
Sets up the work order for execution.
ImportControlNetWorkOrder(Project *project)
Creates a work order to import a control network.
int MaximumSteps() const
Returns the maximum number of steps of the progress.
Definition: Progress.cpp:188
QList< ControlList * > controls()
Return controls in project.
Definition: Project.cpp:2038
void setProgressRange(int, int)
Sets the progress range of the WorkOrder.
Definition: WorkOrder.cpp:1372
void execute()
Imports the control network asynchronously.
void setModifiesDiskState(bool changesProjectOnDisk)
Definition: WorkOrder.cpp:1688
QString toString() const
Returns a string representation of this exception.
Definition: IException.cpp:553
Unless noted otherwise, the portions of Isis written by the USGS are public domain.
QString m_warning
String of any errors/warnings that occurred during import.
IException errors() const
Indicates if any errors occurred during the import.
Represents an item of a ProjectItemModel in Qt&#39;s model-view framework.
Definition: ProjectItem.h:146
Isis exception class.
Definition: IException.h:107
int CurrentStep() const
Returns the current step of the progress.
Definition: Progress.cpp:201
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
This is used for work orders that will not undo or redo (See createsCleanState()) ...
Definition: WorkOrder.h:342
Project * project() const
Returns the Project this WorkOrder is attached to.
Definition: WorkOrder.cpp:1314
QDir addCnetFolder(QString prefix)
Create and return the name of a folder for placing control networks.
Definition: Project.cpp:926
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
QPointer< Project > m_project
A pointer to the Project this WorkOrder is attached to.
Definition: WorkOrder.h:647
void Write(const QString &filename, bool pvl=false)
Writes out the control network.
Definition: ControlNet.cpp:303
void setClean(bool value)
Function to change the clean state of the project.
Definition: Project.cpp:1595