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 }
Isis::WorkOrder::setupExecution
virtual bool setupExecution()
This sets up the state for the work order.
Definition: WorkOrder.cpp:1261
Isis::ImportControlNetWorkOrder::m_list
ControlList * m_list
List of controls added to project.
Definition: ImportControlNetWorkOrder.h:116
Isis::ImportControlNetWorkOrder::CreateControlsFunctor::operator()
Control * operator()(const QPair< FileName, Progress * > &cnetFilename)
Reads and writes the control network(s) asynchronously.
Definition: ImportControlNetWorkOrder.cpp:255
Isis::ControlNet::SetMutex
void SetMutex(QMutex *mutex)
Set mutex to lock for making Naif calls.
Definition: ControlNet.cpp:1666
QList
This is free and unencumbered software released into the public domain.
Definition: BoxcarCachingAlgorithm.h:13
Isis::WorkOrder
Provide Undo/redo abilities, serialization, and history for an operation.
Definition: WorkOrder.h:311
Isis::ImportControlNetWorkOrder::m_watcher
QFutureWatcher< Control * > * m_watcher
QFutureWatcher, allows for asynchronous import.
Definition: ImportControlNetWorkOrder.h:115
Project.h
Isis::FileName::name
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
Definition: FileName.cpp:162
Isis::FileName
File name manipulation and expansion.
Definition: FileName.h:100
Isis::Project::addControl
void addControl(Control *control)
Add the given Control's to the current project.
Definition: Project.cpp:958
Isis::ImportControlNetWorkOrder::CreateControlsFunctor::m_project
Project * m_project
The project to import to.
Definition: ImportControlNetWorkOrder.h:109
Isis::WorkOrder::setInternalData
void setInternalData(QStringList data)
Sets the internal data for this WorkOrder.
Definition: WorkOrder.cpp:1318
Isis::ControlNet::Write
void Write(const QString &filename, bool pvl=false)
Writes out the control network.
Definition: ControlNet.cpp:311
Isis::ImportControlNetWorkOrder::m_warning
QString m_warning
String of any errors/warnings that occurred during import.
Definition: ImportControlNetWorkOrder.h:118
Isis::Project::activeControl
Control * activeControl()
Return the Active Control (control network)
Definition: Project.cpp:1903
Isis::ImportControlNetWorkOrder::clone
virtual ImportControlNetWorkOrder * clone() const
This method clones the current ImportControlNetWorkOrder and returns it.
Definition: ImportControlNetWorkOrder.cpp:92
Isis::ImportControlNetWorkOrder::setupExecution
bool setupExecution()
Sets up the work order for execution.
Definition: ImportControlNetWorkOrder.cpp:126
Isis::Control
This represents an ISIS control net in a project-based GUI interface.
Definition: Control.h:66
Isis::ImportControlNetWorkOrder::postExecution
void postExecution()
Clears progress.
Definition: ImportControlNetWorkOrder.cpp:199
Isis::ImportControlNetWorkOrder::ImportControlNetWorkOrder
ImportControlNetWorkOrder(Project *project)
Creates a work order to import a control network.
Definition: ImportControlNetWorkOrder.cpp:47
Isis::ImportControlNetWorkOrder::CreateControlsFunctor::errors
IException errors() const
Indicates if any errors occurred during the import.
Definition: ImportControlNetWorkOrder.cpp:239
Isis::WorkOrder::m_isSynchronous
bool m_isSynchronous
This is defaulted to true.
Definition: WorkOrder.h:530
Isis::ControlNet::ReadControl
void ReadControl(const QString &filename, Progress *progress=0)
Reads in the control points from the given file.
Definition: ControlNet.cpp:271
QStringList
Isis::Project
The main project for ipce.
Definition: Project.h:289
Isis::IException::append
void append(const IException &exceptionSource)
Appends the given exception (and its list of previous exceptions) to this exception's causational exc...
Definition: IException.cpp:409
Isis::ImportControlNetWorkOrder::CreateControlsFunctor::m_destinationFolder
QDir m_destinationFolder
The directory to copy the control net too.
Definition: ImportControlNetWorkOrder.h:110
Isis::ImportControlNetWorkOrder::~ImportControlNetWorkOrder
~ImportControlNetWorkOrder()
Destructor.
Definition: ImportControlNetWorkOrder.cpp:81
Isis::Control::closeControlNet
void closeControlNet()
Cleans up the ControlNet pointer.
Definition: Control.cpp:214
Isis::ImportControlNetWorkOrder::m_readProgresses
QList< Progress * > m_readProgresses
Keeps track of import progress.
Definition: ImportControlNetWorkOrder.h:117
Isis::WorkOrder::m_isUndoable
bool m_isUndoable
Set the workorder to be undoable/redoable This is defaulted to true - his will allow the workorder to...
Definition: WorkOrder.h:523
ImportControlNetWorkOrder.h
Isis::IException::toString
QString toString() const
Returns a string representation of this exception.
Definition: IException.cpp:537
Isis::WorkOrder::project
Project * project() const
Returns the Project this WorkOrder is attached to.
Definition: WorkOrder.cpp:1300
Isis::ControlNet
a control network
Definition: ControlNet.h:257
Isis::ImportControlNetWorkOrder
Add control networks to a project c Asks the user for a list of control nets and copies them into the...
Definition: ImportControlNetWorkOrder.h:72
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::Progress
Program progress reporter.
Definition: Progress.h:42
Isis::WorkOrder::setProgressRange
void setProgressRange(int, int)
Sets the progress range of the WorkOrder.
Definition: WorkOrder.cpp:1358
Isis::Project::controls
QList< ControlList * > controls()
Return controls in project.
Definition: Project.cpp:2038
Isis::WorkOrder::m_project
QPointer< Project > m_project
A pointer to the Project this WorkOrder is attached to.
Definition: WorkOrder.h:636
Isis::Progress::MaximumSteps
int MaximumSteps() const
Returns the maximum number of steps of the progress.
Definition: Progress.cpp:172
QPair
This is free and unencumbered software released into the public domain.
Definition: CubeIoHandler.h:23
Isis::WorkOrder::WorkOrderFinished
@ WorkOrderFinished
This is used for work orders that will not undo or redo (See createsCleanState())
Definition: WorkOrder.h:331
Isis::WorkOrder::setProgressValue
void setProgressValue(int)
Sets the current progress value for the WorkOrder.
Definition: WorkOrder.cpp:1368
QFutureWatcher
This is free and unencumbered software released into the public domain.
Definition: AbstractTableModel.h:24
Isis::WorkOrder::internalData
QStringList internalData() const
Gets the internal data for this WorkOrder.
Definition: WorkOrder.cpp:1377
Isis::WorkOrder::setModifiesDiskState
void setModifiesDiskState(bool changesProjectOnDisk)
Definition: WorkOrder.cpp:1674
Isis::ImportControlNetWorkOrder::isExecutable
virtual bool isExecutable(ProjectItem *item)
This method returns true if the user clicked on a project tree node with the text "Control Networks".
Definition: ImportControlNetWorkOrder.cpp:107
Isis::Progress::DisableAutomaticDisplay
void DisableAutomaticDisplay()
Turns off updating the Isis Gui when CheckStatus() is called.
Definition: Progress.cpp:161
Isis::ImportControlNetWorkOrder::CreateControlsFunctor::CreateControlsFunctor
CreateControlsFunctor(Project *project, QDir destinationFolder)
CreateControlsFunctor constructor.
Definition: ImportControlNetWorkOrder.cpp:223
Isis::Project::addCnetFolder
QDir addCnetFolder(QString prefix)
Create and return the name of a folder for placing control networks.
Definition: Project.cpp:926
Isis::ImportControlNetWorkOrder::cnetReady
void cnetReady(int ready)
Adds the control net to the project.
Definition: ImportControlNetWorkOrder.cpp:289
Isis::ImportControlNetWorkOrder::CreateControlsFunctor
Definition: ImportControlNetWorkOrder.h:102
Isis::Progress::CurrentStep
int CurrentStep() const
Returns the current step of the progress.
Definition: Progress.cpp:185
Isis::Project::setClean
void setClean(bool value)
Function to change the clean state of the project.
Definition: Project.cpp:1595
Isis::ImportControlNetWorkOrder::execute
void execute()
Imports the control network asynchronously.
Definition: ImportControlNetWorkOrder.cpp:151
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::ProjectItem
Represents an item of a ProjectItemModel in Qt's model-view framework.
Definition: ProjectItem.h:134