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 #include "XmlStackedHandlerReader.h"
27 
28 namespace Isis {
35  Control::Control(QString cNetFileName, QObject *parent) : QObject(parent) {
36 
37  m_fileName = cNetFileName;
38 
39  m_controlNet = NULL;
40  m_displayProperties = NULL;
41  m_project = NULL;
42  m_modified = false;
43 
44  try {
46  }
47  catch (IException &e) {
48  throw IException(e, IException::Programmer, "Error opening control net.", _FILEINFO_);
49  }
50 
52  = new ControlDisplayProperties(FileName(m_fileName).name(), this);
53 
54  m_id = new QUuid(QUuid::createUuid());
55  }
56 
57 
64  Control::Control(Project *project, QString cNetFileName, QObject *parent) : QObject(parent) {
65 
66  m_fileName = cNetFileName;
67 
68  m_controlNet = NULL;
69  m_displayProperties = NULL;
70  m_project = project;
71  m_modified = false;
72 
74  = new ControlDisplayProperties(FileName(m_fileName).name(), this);
75 
76  m_id = new QUuid(QUuid::createUuid());
77  }
78 
79 
87  Control::Control(ControlNet *controlNet, QString cnetFileName, QObject *parent) :
88  QObject(parent) {
89 
90  m_fileName = cnetFileName;
91 
93  m_displayProperties = NULL;
94  m_project = NULL;
95  m_modified = false;
96 
98  = new ControlDisplayProperties(FileName(m_fileName).name(), this);
99 
100  m_id = new QUuid(QUuid::createUuid());
101  }
102 
103 
111  Control::Control(FileName cnetFolder, XmlStackedHandlerReader *xmlReader, QObject *parent) :
112  QObject(parent) {
113  m_controlNet = NULL;
114  m_displayProperties = NULL;
115  m_id = NULL;
116  m_project = NULL;
117  m_modified = false;
118 
119  xmlReader->pushContentHandler(new XmlHandler(this, cnetFolder));
120  }
121 
122 
127  delete m_controlNet;
128  m_controlNet = NULL;
129 
130  delete m_id;
131  m_id = NULL;
132 
133  // Image is a "Qt" parent of display properties, so the Image QObject
134  // destructor will take care of deleting the display props. See call to
135  // DisplayProperties' constructor.
136  m_displayProperties = NULL;
137 
138  // TODO: If control net is modified, prompt for save before destroying??
139 
140  }
141 
142 
151  if (!m_controlNet) {
152  openControlNet();
153  }
154 
155  return m_controlNet;
156  }
157 
158 
166  if (!m_controlNet) {
167  try {
169  if (m_project) {
170  m_controlNet->SetMutex(m_project->mutex());
171  }
172  m_modified = false;
173 
174  }
175  catch (IException &e) {
176  throw IException(e, IException::Programmer, "Error opening control net.", _FILEINFO_);
177  }
178  }
179  }
180 
181 
191  bool Control::write() {
192 
193  if (!m_controlNet) {
194  return false;
195  }
196 
197  try {
199  }
200  catch (IException &e) {
201  throw IException(e, IException::Programmer, "Cannot write control net.", _FILEINFO_);
202  }
203 
204  m_modified = false;
205  return true;
206  }
207 
208 
215  if (m_controlNet) {
216  delete m_controlNet;
217  m_controlNet = NULL;
218  }
219  m_modified = false;
220  }
221 
222 
230  return m_modified;
231  }
232 
233 
241  void Control::setModified(bool modified) {
242 
243  m_modified = modified;
244  }
245 
246 
253  return m_displayProperties;
254  }
255 
256 
263  return m_displayProperties;
264  }
265 
266 
272  QString Control::fileName() const {
273  return m_fileName;
274  }
275 
276 
282  QString Control::id() const {
283  return m_id->toString().remove(QRegExp("[{}]"));
284  }
285 
286 
294  void Control::copyToNewProjectRoot(const Project *project, FileName newProjectRoot) {
295 
296  if (FileName(newProjectRoot).toString() != FileName(project->projectRoot()).toString()) {
297 
298  QString newNetworkPath = project->cnetRoot(newProjectRoot.toString()) + "/" +
299  FileName(m_fileName).dir().dirName() + "/" + FileName(m_fileName).name();
300 
301  // If there is active control & it has been modified, write to disk instead of copying
302  // Leave control net at old location in unmodified state
303  if (isModified()) {
304  controlNet()->Write(newNetworkPath);
305  setModified(false);
306  }
307  else {
308  QString oldNetworkPath = project->cnetRoot(project->projectRoot()) + "/" +
309  FileName(m_fileName).dir().dirName() + "/" + FileName(m_fileName).name();
310  if (!QFile::copy(oldNetworkPath,newNetworkPath) ) {
311  throw IException(IException::Io, "Error saving control net.", _FILEINFO_);
312  }
313  }
314  }
315  // Project "Save" to current location, if active control exists & is modified, write to disk
316  // Note: It does not look like this code is ever executed. If project is saved with a
317  // "Save" this method is not called.
318  else {
319  if (isModified()) {
320  write();
321  setModified(false);
322  }
323  }
324 
325  }
326 
327 
335 
336  if (!QFile::remove(m_fileName)) {
338  tr("Could not remove file [%1]").arg(m_fileName),
339  _FILEINFO_);
340  }
341 
342  // If we're the last thing in the folder, remove the folder too.
343  QDir dir;
344  dir.rmdir(FileName(m_fileName).path());
345  m_modified = false;
346  }
347 
348 
356  closeControlNet();
357 
358  FileName original(m_fileName);
359  FileName newName(project->cnetRoot() + "/" +
360  original.dir().dirName() + "/" + original.name());
361  m_fileName = newName.expanded();
362  }
363 
364 
373  void Control::save(QXmlStreamWriter &stream, const Project *project,
374  FileName newProjectRoot) const {
375  stream.writeStartElement("controlNet");
376  stream.writeAttribute("id", m_id->toString());
377  // Change filename to new path
378  stream.writeAttribute("fileName", FileName(m_fileName).name());
379 
380  m_displayProperties->save(stream, project, newProjectRoot);
381 
382  stream.writeEndElement();
383  }
384 
385 
394  m_xmlHandlerControl = control;
395  m_xmlHandlerCnetFolderName = cnetFolder;
396  }
397 
398 
411  bool Control::XmlHandler::startElement(const QString &namespaceURI, const QString &localName,
412  const QString &qName, const QXmlAttributes &atts) {
413  if (XmlStackedHandler::startElement(namespaceURI, localName, qName, atts)) {
414  if (localName == "controlNet") {
415  QString id = atts.value("id");
416  QString path = atts.value("path");
417  QString fileName = atts.value("fileName");
418 
419  if (!id.isEmpty()) {
420  delete m_xmlHandlerControl->m_id;
421  m_xmlHandlerControl->m_id = NULL;
422  m_xmlHandlerControl->m_id = new QUuid(id.toLatin1());
423  }
424 
425  if (!fileName.isEmpty()) {
426  m_xmlHandlerControl->m_fileName = m_xmlHandlerCnetFolderName.expanded() + "/" + fileName;
427  }
428  }
429  else if (localName == "displayProperties") {
430  m_xmlHandlerControl->m_displayProperties = new ControlDisplayProperties(reader());
431  }
432  }
433 
434  return true;
435  }
436 }
Isis::IException::Io
@ Io
A type of error that occurred when performing an actual I/O operation.
Definition: IException.h:155
Isis::Control::Control
Control(QString cnetFileName, QObject *parent=0)
Create a Control from control net located on disk.
Definition: Control.cpp:35
Isis::Control::m_displayProperties
ControlDisplayProperties * m_displayProperties
Contains the display properties for this Control object.
Definition: Control.h:127
Isis::ControlNet::SetMutex
void SetMutex(QMutex *mutex)
Set mutex to lock for making Naif calls.
Definition: ControlNet.cpp:1666
Isis::Control::XmlHandler::XmlHandler
XmlHandler(Control *control, FileName cnetFolder)
Constructor for the Control object's XmlHandler.
Definition: Control.cpp:393
Project.h
Isis::Project::mutex
QMutex * mutex()
Return mutex used for Naif calls.
Definition: Project.cpp:1658
Isis::Control::deleteFromDisk
void deleteFromDisk()
Delete the control net from disk.
Definition: Control.cpp:334
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::XmlStackedHandlerReader::pushContentHandler
virtual void pushContentHandler(XmlStackedHandler *newHandler)
Push a contentHandler and maybe continue parsing...
Definition: XmlStackedHandlerReader.cpp:55
Isis::ControlNet::Write
void Write(const QString &filename, bool pvl=false)
Writes out the control network.
Definition: ControlNet.cpp:311
Isis::Control::XmlHandler::m_xmlHandlerCnetFolderName
FileName m_xmlHandlerCnetFolderName
The name of the folder for the control xml.
Definition: Control.h:118
Isis::Project::cnetRoot
static QString cnetRoot(QString projectRoot)
Appends the root directory name 'cnets' to the project.
Definition: Project.cpp:2019
Isis::Control::XmlHandler
Nested class used to write the Control object information to an XML file for the purpose of saving an...
Definition: Control.h:106
Isis::ControlDisplayProperties
This is the GUI communication mechanism for cubes.
Definition: ControlDisplayProperties.h:59
Isis::Control
This represents an ISIS control net in a project-based GUI interface.
Definition: Control.h:66
Isis::Control::m_id
QUuid * m_id
A unique ID for this Control.
Definition: Control.h:136
Isis::Control::copyToNewProjectRoot
void copyToNewProjectRoot(const Project *project, FileName newProjectRoot)
Copies the files of the given Project to the given location.
Definition: Control.cpp:294
Isis::toString
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:211
Isis::XmlStackedHandlerReader
Manage a stack of content handlers for reading XML files.
Definition: XmlStackedHandlerReader.h:30
Isis::Control::save
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:373
Isis::Control::isModified
bool isModified()
@description Has this control been modified?
Definition: Control.cpp:229
Isis::Control::~Control
~Control()
Destroys Control object.
Definition: Control.cpp:126
Isis::Project
The main project for ipce.
Definition: Project.h:289
Isis::Control::closeControlNet
void closeControlNet()
Cleans up the ControlNet pointer.
Definition: Control.cpp:214
Isis::Control::write
bool write()
@description Write control net to disk.
Definition: Control.cpp:191
Isis::FileName::expanded
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:196
Isis::Control::controlNet
ControlNet * controlNet()
Open and return a pointer to the ControlNet for this Control.
Definition: Control.cpp:150
Isis::FileName::dir
QDir dir() const
Returns the path of the file's parent directory as a QDir object.
Definition: FileName.cpp:465
Isis::Control::openControlNet
void openControlNet()
Sets the ControlNet from the control net file name provided in the constructor.
Definition: Control.cpp:165
Isis::Control::setModified
void setModified(bool modified=true)
@description Sets the modification state of this control.
Definition: Control.cpp:241
Isis::ControlNet
a control network
Definition: ControlNet.h:257
Isis::Control::XmlHandler::m_xmlHandlerControl
Control * m_xmlHandlerControl
A pointer to the Control object to be read or written.
Definition: Control.h:116
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::FileName::toString
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::Project::projectRoot
QString projectRoot() const
Get the top-level folder of the project.
Definition: Project.cpp:1666
Isis::Control::updateFileName
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:355
Isis::Control::m_controlNet
ControlNet * m_controlNet
A pointer to the ControlNet object associated with this Control object.
Definition: Control.h:69
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
Isis::Control::XmlHandler::startElement
virtual bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts)
Method to read the given XML formatted attribute for a Control object into the XmlHandler.
Definition: Control.cpp:411
Isis::Control::m_fileName
QString m_fileName
Project associated with this control.
Definition: Control.h:130
Isis::Control::displayProperties
ControlDisplayProperties * displayProperties()
Access a pointer to the display properties for the control network.
Definition: Control.cpp:252
QObject
Isis::Control::id
QString id() const
Access the unique ID associated with this Control.
Definition: Control.cpp:282
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::Control::fileName
QString fileName() const
Access the name of the control network file associated with this Control.
Definition: Control.cpp:272