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
40namespace 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
60 m_watcher = new QFutureWatcher<Control *>;
61 connect(m_watcher, SIGNAL(resultReadyAt(int)), this, SLOT(cnetReady(int)));
62 }
63
64
71 WorkOrder(other) {
72 m_watcher = NULL;
73 m_watcher = new QFutureWatcher<Control *>;
74 connect(m_watcher, SIGNAL(resultReadyAt(int)), this, SLOT(cnetReady(int)));
75 }
76
77
85
86
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.
215 }
216
224 Project *project, QDir destinationFolder) : m_errors(new IException) {
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:66
void closeControlNet()
Cleans up the ControlNet pointer.
Definition Control.cpp:214
a control network
Definition ControlNet.h:258
File name manipulation and expansion.
Definition FileName.h:100
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
Definition FileName.cpp:162
Isis exception class.
Definition IException.h:91
void append(const IException &exceptionSource)
Appends the given exception (and its list of previous exceptions) to this exception's causational exc...
Control * operator()(const QPair< FileName, Progress * > &cnetFilename)
Reads and writes the control network(s) asynchronously.
IException errors() const
Indicates if any errors occurred during the import.
CreateControlsFunctor(Project *project, QDir destinationFolder)
CreateControlsFunctor constructor.
QDir m_destinationFolder
The directory to copy the control net too.
Add control networks to a project c Asks the user for a list of control nets and copies them into the...
QList< Progress * > m_readProgresses
Keeps track of import progress.
ImportControlNetWorkOrder(Project *project)
Creates a work order to import a control network.
void execute()
Imports the control network asynchronously.
void cnetReady(int ready)
Adds the control net to the project.
bool setupExecution()
Sets up the work order for execution.
ControlList * m_list
List of controls added to project.
QString m_warning
String of any errors/warnings that occurred during import.
virtual ImportControlNetWorkOrder * clone() const
This method clones the current ImportControlNetWorkOrder and returns it.
QFutureWatcher< Control * > * m_watcher
QFutureWatcher, allows for asynchronous import.
virtual bool isExecutable(ProjectItem *item)
This method returns true if the user clicked on a project tree node with the text "Control Networks".
Program progress reporter.
Definition Progress.h:42
int MaximumSteps() const
Returns the maximum number of steps of the progress.
Definition Progress.cpp:172
void DisableAutomaticDisplay()
Turns off updating the Isis Gui when CheckStatus() is called.
Definition Progress.cpp:161
int CurrentStep() const
Returns the current step of the progress.
Definition Progress.cpp:185
The main project for ipce.
Definition Project.h:289
QDir addCnetFolder(QString prefix)
Create and return the name of a folder for placing control networks.
Definition Project.cpp:925
void addControl(Control *control)
Add the given Control's to the current project.
Definition Project.cpp:957
QList< ControlList * > controls()
Return controls in project.
Definition Project.cpp:2037
void setClean(bool value)
Function to change the clean state of the project.
Definition Project.cpp:1594
Control * activeControl()
Return the Active Control (control network)
Definition Project.cpp:1902
Represents an item of a ProjectItemModel in Qt's model-view framework.
Provide Undo/redo abilities, serialization, and history for an operation.
Definition WorkOrder.h:311
void setProgressRange(int, int)
Sets the progress range of the WorkOrder.
bool m_isSynchronous
This is defaulted to true.
Definition WorkOrder.h:530
@ WorkOrderFinished
This is used for work orders that will not undo or redo (See createsCleanState())
Definition WorkOrder.h:331
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
void setProgressValue(int)
Sets the current progress value for the WorkOrder.
virtual bool setupExecution()
This sets up the state for the work order.
QStringList internalData() const
Gets the internal data for this WorkOrder.
void setModifiesDiskState(bool changesProjectOnDisk)
void setInternalData(QStringList data)
Sets the internal data for this WorkOrder.
Project * project() const
Returns the Project this WorkOrder is attached to.
QPointer< Project > m_project
A pointer to the Project this WorkOrder is attached to.
Definition WorkOrder.h:636
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16