Isis 3 Programmer Reference
Control.cpp
1 #include "Control.h"
2 
3 #include <QDebug>
4 #include <QDir>
5 #include <QFileInfo>
6 #include <QMutex>
7 #include <QString>
8 #include <QUuid>
9 #include <QXmlStreamWriter>
10 
12 #include "ControlNet.h"
13 #include "FileName.h"
14 #include "IException.h"
15 #include "IString.h"
16 #include "Project.h"
17 #include "PvlObject.h"
18 #include "XmlStackedHandlerReader.h"
19 
20 namespace Isis {
27  Control::Control(QString cNetFileName, QObject *parent) : QObject(parent) {
28 
29  m_fileName = cNetFileName;
30 
31  m_controlNet = NULL;
32  m_displayProperties = NULL;
33  m_project = NULL;
34  m_modified = false;
35 
36  try {
38  }
39  catch (IException &e) {
40  throw IException(e, IException::Programmer, "Error opening control net.", _FILEINFO_);
41  }
42 
44  = new ControlDisplayProperties(FileName(m_fileName).name(), this);
45 
46  m_id = new QUuid(QUuid::createUuid());
47  }
48 
49 
56  Control::Control(Project *project, QString cNetFileName, QObject *parent) : QObject(parent) {
57 
58  m_fileName = cNetFileName;
59 
60  m_controlNet = NULL;
61  m_displayProperties = NULL;
62  m_project = project;
63  m_modified = false;
64 
66  = new ControlDisplayProperties(FileName(m_fileName).name(), this);
67 
68  m_id = new QUuid(QUuid::createUuid());
69  }
70 
71 
79  Control::Control(ControlNet *controlNet, QString cnetFileName, QObject *parent) :
80  QObject(parent) {
81 
82  m_fileName = cnetFileName;
83 
85  m_displayProperties = NULL;
86  m_project = NULL;
87  m_modified = false;
88 
90  = new ControlDisplayProperties(FileName(m_fileName).name(), this);
91 
92  m_id = new QUuid(QUuid::createUuid());
93  }
94 
95 
103  Control::Control(FileName cnetFolder, XmlStackedHandlerReader *xmlReader, QObject *parent) :
104  QObject(parent) {
105  m_controlNet = NULL;
106  m_displayProperties = NULL;
107  m_id = NULL;
108  m_project = NULL;
109  m_modified = false;
110 
111  xmlReader->pushContentHandler(new XmlHandler(this, cnetFolder));
112  }
113 
114 
119  delete m_controlNet;
120  m_controlNet = NULL;
121 
122  delete m_id;
123  m_id = NULL;
124 
125  // Image is a "Qt" parent of display properties, so the Image QObject
126  // destructor will take care of deleting the display props. See call to
127  // DisplayProperties' constructor.
128  m_displayProperties = NULL;
129 
130  // TODO: If control net is modified, prompt for save before destroying??
131 
132  }
133 
134 
143  if (!m_controlNet) {
144  openControlNet();
145  }
146 
147  return m_controlNet;
148  }
149 
150 
158  if (!m_controlNet) {
159  try {
161  if (m_project) {
162  m_controlNet->SetMutex(m_project->mutex());
163  }
164  m_modified = false;
165 
166  }
167  catch (IException &e) {
168  throw IException(e, IException::Programmer, "Error opening control net.", _FILEINFO_);
169  }
170  }
171  }
172 
173 
183  bool Control::write() {
184 
185  if (!m_controlNet) {
186  return false;
187  }
188 
189  try {
191  }
192  catch (IException &e) {
193  throw IException(e, IException::Programmer, "Cannot write control net.", _FILEINFO_);
194  }
195 
196  m_modified = false;
197  return true;
198  }
199 
200 
207  if (m_controlNet) {
208  delete m_controlNet;
209  m_controlNet = NULL;
210  }
211  m_modified = false;
212  }
213 
214 
222  return m_modified;
223  }
224 
225 
233  void Control::setModified(bool modified) {
234 
235  m_modified = modified;
236  }
237 
238 
245  return m_displayProperties;
246  }
247 
248 
255  return m_displayProperties;
256  }
257 
258 
264  QString Control::fileName() const {
265  return m_fileName;
266  }
267 
268 
274  QString Control::id() const {
275  return m_id->toString().remove(QRegExp("[{}]"));
276  }
277 
278 
286  void Control::copyToNewProjectRoot(const Project *project, FileName newProjectRoot) {
287 
288  if (FileName(newProjectRoot).toString() != FileName(project->projectRoot()).toString()) {
289 
290  QString newNetworkPath = project->cnetRoot(newProjectRoot.toString()) + "/" +
291  FileName(m_fileName).dir().dirName() + "/" + FileName(m_fileName).name();
292 
293  // If there is active control & it has been modified, write to disk instead of copying
294  // Leave control net at old location in unmodified state
295  if (isModified()) {
296  controlNet()->Write(newNetworkPath);
297  setModified(false);
298  }
299  else {
300  QString oldNetworkPath = project->cnetRoot(project->projectRoot()) + "/" +
301  FileName(m_fileName).dir().dirName() + "/" + FileName(m_fileName).name();
302  if (!QFile::copy(oldNetworkPath,newNetworkPath) ) {
303  throw IException(IException::Io, "Error saving control net.", _FILEINFO_);
304  }
305  }
306  }
307  // Project "Save" to current location, if active control exists & is modified, write to disk
308  // Note: It does not look like this code is ever executed. If project is saved with a
309  // "Save" this method is not called.
310  else {
311  if (isModified()) {
312  write();
313  setModified(false);
314  }
315  }
316 
317  }
318 
319 
327 
328  if (!QFile::remove(m_fileName)) {
330  tr("Could not remove file [%1]").arg(m_fileName),
331  _FILEINFO_);
332  }
333 
334  // If we're the last thing in the folder, remove the folder too.
335  QDir dir;
336  dir.rmdir(FileName(m_fileName).path());
337  m_modified = false;
338  }
339 
340 
348  closeControlNet();
349 
350  FileName original(m_fileName);
351  FileName newName(project->cnetRoot() + "/" +
352  original.dir().dirName() + "/" + original.name());
353  m_fileName = newName.expanded();
354  }
355 
356 
365  void Control::save(QXmlStreamWriter &stream, const Project *project,
366  FileName newProjectRoot) const {
367  stream.writeStartElement("controlNet");
368  stream.writeAttribute("id", m_id->toString());
369  // Change filename to new path
370  stream.writeAttribute("fileName", FileName(m_fileName).name());
371 
372  m_displayProperties->save(stream, project, newProjectRoot);
373 
374  stream.writeEndElement();
375  }
376 
377 
386  m_xmlHandlerControl = control;
387  m_xmlHandlerCnetFolderName = cnetFolder;
388  }
389 
390 
403  bool Control::XmlHandler::startElement(const QString &namespaceURI, const QString &localName,
404  const QString &qName, const QXmlAttributes &atts) {
405  if (XmlStackedHandler::startElement(namespaceURI, localName, qName, atts)) {
406  if (localName == "controlNet") {
407  QString id = atts.value("id");
408  QString path = atts.value("path");
409  QString fileName = atts.value("fileName");
410 
411  if (!id.isEmpty()) {
412  delete m_xmlHandlerControl->m_id;
413  m_xmlHandlerControl->m_id = NULL;
414  m_xmlHandlerControl->m_id = new QUuid(id.toLatin1());
415  }
416 
417  if (!fileName.isEmpty()) {
418  m_xmlHandlerControl->m_fileName = m_xmlHandlerCnetFolderName.expanded() + "/" + fileName;
419  }
420  }
421  else if (localName == "displayProperties") {
422  m_xmlHandlerControl->m_displayProperties = new ControlDisplayProperties(reader());
423  }
424  }
425 
426  return true;
427  }
428 }
This represents an ISIS control net in a project-based GUI interface.
Definition: Control.h:79
The main project for ipce.
Definition: Project.h:289
void SetMutex(QMutex *mutex)
Set mutex to lock for making Naif calls.
void deleteFromDisk()
Delete the control net from disk.
Definition: Control.cpp:326
File name manipulation and expansion.
Definition: FileName.h:116
Control(QString cnetFileName, QObject *parent=0)
Create a Control from control net located on disk.
Definition: Control.cpp:27
ControlDisplayProperties * m_displayProperties
Contains the display properties for this Control object.
Definition: Control.h:140
QMutex * mutex()
Return mutex used for Naif calls.
Definition: Project.cpp:1658
QUuid * m_id
A unique ID for this Control.
Definition: Control.h:149
void copyToNewProjectRoot(const Project *project, FileName newProjectRoot)
Copies the files of the given Project to the given location.
Definition: Control.cpp:286
FileName m_xmlHandlerCnetFolderName
The name of the folder for the control xml.
Definition: Control.h:131
Nested class used to write the Control object information to an XML file for the purpose of saving an...
Definition: Control.h:119
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
Definition: FileName.cpp:178
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:226
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:162
A type of error that occurred when performing an actual I/O operation.
Definition: IException.h:171
void save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot) const
Method to write this Control object&#39;s member data to an XML stream.
Definition: Control.cpp:365
static QString cnetRoot(QString projectRoot)
Appends the root directory name &#39;cnets&#39; to the project.
Definition: Project.cpp:2019
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:403
void closeControlNet()
Cleans up the ControlNet pointer.
Definition: Control.cpp:206
This is the GUI communication mechanism for cubes.
void openControlNet()
Sets the ControlNet from the control net file name provided in the constructor.
Definition: Control.cpp:157
bool isModified()
Has this control been modified?
Definition: Control.cpp:221
a control network
Definition: ControlNet.h:271
bool write()
Write control net to disk.
Definition: Control.cpp:183
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
~Control()
Destroys Control object.
Definition: Control.cpp:118
virtual void pushContentHandler(XmlStackedHandler *newHandler)
Push a contentHandler and maybe continue parsing...
QString projectRoot() const
Get the top-level folder of the project.
Definition: Project.cpp:1666
QDir dir() const
Returns the path of the file&#39;s parent directory as a QDir object.
Definition: FileName.cpp:481
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:347
ControlNet * controlNet()
Open and return a pointer to the ControlNet for this Control.
Definition: Control.cpp:142
XmlHandler(Control *control, FileName cnetFolder)
Constructor for the Control object&#39;s XmlHandler.
Definition: Control.cpp:385
ControlNet * m_controlNet
A pointer to the ControlNet object associated with this Control object.
Definition: Control.h:82
QString m_fileName
Project associated with this control.
Definition: Control.h:143
ControlDisplayProperties * displayProperties()
Access a pointer to the display properties for the control network.
Definition: Control.cpp:244
QString toString() const
Returns a QString of the full file name including the file path, excluding the attributes with any Is...
Definition: FileName.cpp:531
void setModified(bool modified=true)
Sets the modification state of this control.
Definition: Control.cpp:233
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
Control * m_xmlHandlerControl
A pointer to the Control object to be read or written.
Definition: Control.h:129
QString id() const
Access the unique ID associated with this Control.
Definition: Control.cpp:274
Manage a stack of content handlers for reading XML files.
QString fileName() const
Access the name of the control network file associated with this Control.
Definition: Control.cpp:264
void Write(const QString &filename, bool pvl=false)
Writes out the control network.
Definition: ControlNet.cpp:303