Isis 3 Programmer Reference
Control.cpp
1
7/* SPDX-License-Identifier: CC0-1.0 */
8
9#include "Control.h"
10
11#include <QDebug>
12#include <QDir>
13#include <QFileInfo>
14#include <QMutex>
15#include <QString>
16#include <QUuid>
17#include <QXmlStreamWriter>
18
19#include "ControlDisplayProperties.h"
20#include "ControlNet.h"
21#include "FileName.h"
22#include "IException.h"
23#include "IString.h"
24#include "Project.h"
25#include "PvlObject.h"
26
27namespace Isis {
34 Control::Control(QString cNetFileName, QObject *parent) : QObject(parent) {
35
36 m_fileName = cNetFileName;
37
38 m_controlNet = NULL;
40 m_project = NULL;
41 m_modified = false;
42
43 try {
45 }
46 catch (IException &e) {
47 throw IException(e, IException::Programmer, "Error opening control net.", _FILEINFO_);
48 }
49
51 = new ControlDisplayProperties(FileName(m_fileName).name(), this);
52
53 m_id = new QUuid(QUuid::createUuid());
54 }
55
56
63 Control::Control(Project *project, QString cNetFileName, QObject *parent) : QObject(parent) {
64
65 m_fileName = cNetFileName;
66
67 m_controlNet = NULL;
69 m_project = project;
70 m_modified = false;
71
73 = new ControlDisplayProperties(FileName(m_fileName).name(), this);
74
75 m_id = new QUuid(QUuid::createUuid());
76 }
77
78
86 Control::Control(ControlNet *controlNet, QString cnetFileName, QObject *parent) :
87 QObject(parent) {
88
89 m_fileName = cnetFileName;
90
93 m_project = NULL;
94 m_modified = false;
95
97 = new ControlDisplayProperties(FileName(m_fileName).name(), this);
98
99 m_id = new QUuid(QUuid::createUuid());
100 }
101
102
107 delete m_controlNet;
108 m_controlNet = NULL;
109
110 delete m_id;
111 m_id = NULL;
112
113 // Image is a "Qt" parent of display properties, so the Image QObject
114 // destructor will take care of deleting the display props. See call to
115 // DisplayProperties' constructor.
116 m_displayProperties = NULL;
117
118 // TODO: If control net is modified, prompt for save before destroying??
119
120 }
121
122
131 if (!m_controlNet) {
133 }
134
135 return m_controlNet;
136 }
137
138
146 if (!m_controlNet) {
147 try {
149 if (m_project) {
150 m_controlNet->SetMutex(m_project->mutex());
151 }
152 m_modified = false;
153
154 }
155 catch (IException &e) {
156 throw IException(e, IException::Programmer, "Error opening control net.", _FILEINFO_);
157 }
158 }
159 }
160
161
172
173 if (!m_controlNet) {
174 return false;
175 }
176
177 try {
179 }
180 catch (IException &e) {
181 throw IException(e, IException::Programmer, "Cannot write control net.", _FILEINFO_);
182 }
183
184 m_modified = false;
185 return true;
186 }
187
188
195 if (m_controlNet) {
196 delete m_controlNet;
197 m_controlNet = NULL;
198 }
199 m_modified = false;
200 }
201
202
210 return m_modified;
211 }
212
213
221 void Control::setModified(bool modified) {
222
223 m_modified = modified;
224 }
225
226
235
236
245
246
252 QString Control::fileName() const {
253 return m_fileName;
254 }
255
256
262 QString Control::id() const {
263 return m_id->toString().remove(QRegExp("[{}]"));
264 }
265
266
274 void Control::copyToNewProjectRoot(const Project *project, FileName newProjectRoot) {
275
276 if (FileName(newProjectRoot).toString() != FileName(project->projectRoot()).toString()) {
277
278 QString newNetworkPath = project->cnetRoot(newProjectRoot.toString()) + "/" +
279 FileName(m_fileName).dir().dirName() + "/" + FileName(m_fileName).name();
280
281 // If there is active control & it has been modified, write to disk instead of copying
282 // Leave control net at old location in unmodified state
283 if (isModified()) {
284 controlNet()->Write(newNetworkPath);
285 setModified(false);
286 }
287 else {
288 QString oldNetworkPath = project->cnetRoot(project->projectRoot()) + "/" +
289 FileName(m_fileName).dir().dirName() + "/" + FileName(m_fileName).name();
290 if (!QFile::copy(oldNetworkPath,newNetworkPath) ) {
291 throw IException(IException::Io, "Error saving control net.", _FILEINFO_);
292 }
293 }
294 }
295 // Project "Save" to current location, if active control exists & is modified, write to disk
296 // Note: It does not look like this code is ever executed. If project is saved with a
297 // "Save" this method is not called.
298 else {
299 if (isModified()) {
300 write();
301 setModified(false);
302 }
303 }
304
305 }
306
307
315
316 if (!QFile::remove(m_fileName)) {
318 tr("Could not remove file [%1]").arg(m_fileName),
319 _FILEINFO_);
320 }
321
322 // If we're the last thing in the folder, remove the folder too.
323 QDir dir;
324 dir.rmdir(FileName(m_fileName).path());
325 m_modified = false;
326 }
327
328
337
338 FileName original(m_fileName);
339 FileName newName(project->cnetRoot() + "/" +
340 original.dir().dirName() + "/" + original.name());
341 m_fileName = newName.expanded();
342 }
343
344
353 void Control::save(QXmlStreamWriter &stream, const Project *project,
354 FileName newProjectRoot) const {
355 stream.writeStartElement("controlNet");
356 stream.writeAttribute("id", m_id->toString());
357 // Change filename to new path
358 stream.writeAttribute("fileName", FileName(m_fileName).name());
359
360 m_displayProperties->save(stream, project, newProjectRoot);
361
362 stream.writeEndElement();
363 }
364}
This is the GUI communication mechanism for cubes.
QString id() const
Access the unique ID associated with this Control.
Definition Control.cpp:262
ControlNet * controlNet()
Open and return a pointer to the ControlNet for this Control.
Definition Control.cpp:130
ControlDisplayProperties * displayProperties()
Access a pointer to the display properties for the control network.
Definition Control.cpp:232
QString m_fileName
Project associated with this control.
Definition Control.h:104
void deleteFromDisk()
Delete the control net from disk.
Definition Control.cpp:314
Control(QString cnetFileName, QObject *parent=0)
Create a Control from control net located on disk.
Definition Control.cpp:34
bool write()
@description Write control net to disk.
Definition Control.cpp:171
void copyToNewProjectRoot(const Project *project, FileName newProjectRoot)
Copies the files of the given Project to the given location.
Definition Control.cpp:274
void setModified(bool modified=true)
@description Sets the modification state of this control.
Definition Control.cpp:221
ControlNet * m_controlNet
A pointer to the ControlNet object associated with this Control object.
Definition Control.h:68
bool isModified()
@description Has this control been modified?
Definition Control.cpp:209
~Control()
Destroys Control object.
Definition Control.cpp:106
void updateFileName(Project *)
Change the on-disk file name for this control to be where the control ought to be in the given projec...
Definition Control.cpp:335
void save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot) const
Method to write this Control object's member data to an XML stream.
Definition Control.cpp:353
void openControlNet()
Sets the ControlNet from the control net file name provided in the constructor.
Definition Control.cpp:145
QString fileName() const
Access the name of the control network file associated with this Control.
Definition Control.cpp:252
ControlDisplayProperties * m_displayProperties
Contains the display properties for this Control object.
Definition Control.h:101
QUuid * m_id
A unique ID for this Control.
Definition Control.h:110
void closeControlNet()
Cleans up the ControlNet pointer.
Definition Control.cpp:194
a control network
Definition ControlNet.h:258
void Write(const QString &filename, bool pvl=false)
Writes out the control network.
void SetMutex(QMutex *mutex)
Set mutex to lock for making Naif calls.
File name manipulation and expansion.
Definition FileName.h:100
QDir dir() const
Returns the path of the file's parent directory as a QDir object.
Definition FileName.cpp:465
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
Definition FileName.cpp:162
QString toString() const
Returns a QString of the full file name including the file path, excluding the attributes with any Is...
Definition FileName.cpp:515
Isis exception class.
Definition IException.h:91
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
@ Io
A type of error that occurred when performing an actual I/O operation.
Definition IException.h:155
The main project for ipce.
Definition Project.h:287
static QString cnetRoot(QString projectRoot)
Appends the root directory name 'cnets' to the project.
Definition Project.cpp:2080
QMutex * mutex()
Return mutex used for Naif calls.
Definition Project.cpp:1719
QString projectRoot() const
Get the top-level folder of the project.
Definition Project.cpp:1727
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition IString.cpp:211