Isis 3.0 Programmer Reference
Back | Home
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 
38 namespace Isis {
39 
40  ImportControlNetWorkOrder::ImportControlNetWorkOrder(Project *project) :
41  WorkOrder(project) {
42  m_watcher = NULL;
43 
44  QAction::setText(tr("Import &Control Networks..."));
45  QUndoCommand::setText(tr("Import Control Networks"));
46 
48 
49  m_watcher = new QFutureWatcher<Control *>;
50  connect(m_watcher, SIGNAL(resultReadyAt(int)), this, SLOT(cnetReady(int)));
51  }
52 
53 
54  ImportControlNetWorkOrder::ImportControlNetWorkOrder(const ImportControlNetWorkOrder &other) :
55  WorkOrder(other) {
56 
57  m_watcher = NULL;
58  m_watcher = new QFutureWatcher<Control *>;
59  connect(m_watcher, SIGNAL(resultReadyAt(int)), this, SLOT(cnetReady(int)));
60 
61  }
62 
63 
64  ImportControlNetWorkOrder::~ImportControlNetWorkOrder() {
65  delete m_watcher;
66  m_watcher = NULL;
67  }
68 
69 
70  ImportControlNetWorkOrder *ImportControlNetWorkOrder::clone() const {
71  return new ImportControlNetWorkOrder(*this);
72  }
73 
74 
77 
78  QStringList cnetFileNames = QFileDialog::getOpenFileNames(
79  qobject_cast<QWidget *>(parent()),
80  tr("Import Control Networks"), "",
81  tr("Isis control nets (*.net);;All Files (*)"));
82 
83  if (!cnetFileNames.isEmpty()) {
84  QUndoCommand::setText(tr("Import %1 Control Networks").arg(cnetFileNames.count()));
85  }
86 
87  setInternalData(cnetFileNames);
88 
89  return internalData().count() > 0;
90  }
91 
92 
94 
95  QDir cnetFolder = project()->addCnetFolder("controlNetworks");
96 
97  QStringList cnetFileNames = internalData();
98 
99  QList< QPair<FileName, Progress *> > cnetFileNamesAndProgress;
100  foreach (FileName fileName, cnetFileNames) {
101  Progress *readProgress = new Progress;
102  cnetFileNamesAndProgress.append(qMakePair(fileName, readProgress));
103  readProgress->DisableAutomaticDisplay();
104  m_readProgresses.append(readProgress);
105  }
106 
107  m_watcher->setFuture(QtConcurrent::mapped(cnetFileNamesAndProgress,
108  CreateControlsFunctor(project(), cnetFolder)));
109  }
110 
111 
113  while (!m_watcher->isFinished()) {
114  setProgressRange(0, 100 * m_readProgresses.count());
115  int totalProgress = 0;
116 
117  for (int i = 0; i < m_readProgresses.count(); i++) {
118  Progress *progress = m_readProgresses[i];
119 
120  if (m_watcher->future().isResultReadyAt(i)) {
121  totalProgress += 100;
122  }
123  else if (progress->MaximumSteps() > 0) {
124  double progressPercent = progress->CurrentStep() / (double)progress->MaximumSteps();
125  // Estimating the Read is 90% and Write 10% (ish)
126  totalProgress += qRound(progressPercent * 90);
127  }
128  }
129 
130  setProgressValue(totalProgress);
131 
132  QThread::yieldCurrentThread();
133  }
134  }
135 
136 
138 
139  foreach (Progress *progress, m_readProgresses) {
140  delete progress;
141  }
142  m_readProgresses.clear();
143  }
144 
145 
147  if (m_watcher->isFinished()) {
148  ControlList *list = project()->controls().last();
149  list->deleteFromDisk(project());
150  foreach (Control *control, *list) {
151  delete control;
152  }
153 
154  delete list;
155  }
156  }
157 
158 
159  ImportControlNetWorkOrder::CreateControlsFunctor::CreateControlsFunctor(
160  Project *project, QDir destinationFolder) {
161  m_project = project;
162  m_destinationFolder = destinationFolder;
163  }
164 
165 
166  Control *ImportControlNetWorkOrder::CreateControlsFunctor::operator()(
167  const QPair<FileName, Progress *> &cnetFileNameAndProgress) {
168 
169  QString cnetFileName = cnetFileNameAndProgress.first.original();
170  ControlNet *cnet = new ControlNet();
171  cnet->SetMutex(m_project->mutex());
172  cnet->ReadControl(cnetFileName, cnetFileNameAndProgress.second);
173 
174  QString baseFilename = FileName(cnetFileName).name();
175  QString destination = m_destinationFolder.canonicalPath() + "/" + baseFilename;
176 
177  cnet->Write(destination);
178 
179  Control *control = new Control(cnet, destination);
180  return control;
181  }
182 
183 
184  void ImportControlNetWorkOrder::cnetReady(int ready) {
185 
186  Control *control = m_watcher->resultAt(ready);
187  project()->addControl(control);
188  }
189 }
This represents an ISIS control net in a project-based GUI interface.
Definition: Control.h:57
void syncUndo()
This method is designed to be implemented by children work orders.
void setProgressValue(int)
Sets the current progress value for the WorkOrder.
Definition: WorkOrder.cpp:1177
The main project for cnetsuite.
Definition: Project.h:105
File name manipulation and expansion.
Definition: FileName.h:111
void DisableAutomaticDisplay()
Turns off updating the Isis Gui when CheckStatus() is called.
Definition: Progress.cpp:177
Maintains a list of Controls so that control nets can easily be copied from one Project to another...
Definition: ControlList.h:34
Project * project() const
Returns the Project this WorkOrder is attached to.
Definition: WorkOrder.cpp:1116
void addControl(Control *control)
Add the given Control&#39;s to the current project.
Definition: Project.cpp:646
void postSyncRedo()
This method is designed to be implemented by children work orders.
QStringList internalData() const
Gets the internal data for this WorkOrder.
Definition: WorkOrder.cpp:1186
int CurrentStep() const
Returns the current step of the progress.
Definition: Progress.cpp:201
int MaximumSteps() const
Returns the maximum number of steps of the progress.
Definition: Progress.cpp:188
void asyncRedo()
This method is designed to be implemented by children work orders.
Program progress reporter.
Definition: Progress.h:58
bool execute()
The (child) implementation of this method should prompt the user/gather state by any means necessary...
void syncRedo()
This method is designed to be implemented by children work orders.
void setProgressRange(int, int)
Sets the progress range of the WorkOrder.
Definition: WorkOrder.cpp:1167
void setModifiesDiskState(bool changesProjectOnDisk)
.
Definition: WorkOrder.cpp:1531
virtual bool execute()
The (child) implementation of this method should prompt the user/gather state by any means necessary...
Definition: WorkOrder.cpp:1078
void deleteFromDisk(Project *project)
Delete all of the contained Controls from disk.
QDir addCnetFolder(QString prefix)
Create and return the name of a folder for placing control networks.
Definition: Project.cpp:614
void setInternalData(QStringList data)
Sets the internal data for this WorkOrder.
Definition: WorkOrder.cpp:1130
QPointer< Project > m_project
A pointer to the Project this WorkOrder is attached to.
Definition: WorkOrder.h:399

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:20:22