Isis 3 Programmer Reference
ControlList.cpp
Go to the documentation of this file.
1 
23 #include "ControlList.h"
24 
25 #include <QAction>
26 #include <QColorDialog>
27 #include <QDebug>
28 #include <QDir>
29 #include <QFile>
30 #include <QFuture>
31 #include <QInputDialog>
32 #include <QLabel>
33 #include <QProgressDialog>
34 #include <QtConcurrentMap>
35 #include <QXmlStreamWriter>
36 
37 #include "Color.h"
38 #include "FileName.h"
39 #include "IException.h"
40 #include "Project.h"
41 #include "XmlStackedHandlerReader.h"
42 
43 namespace Isis {
51  ControlList::ControlList(QString name, QString path, QObject *parent) : QObject(parent) {
52  m_name = name;
53  m_path = path;
54  }
55 
56 
63  }
64 
65 
73  append(controls);
74  }
75 
76 
85  QObject(parent) {
86  xmlReader->pushContentHandler(new XmlHandler(this, project));
87  }
88 
89 
96  QList<Control *>(other) {
97  m_name = other.m_name;
98  m_path = other.m_path;
99  }
100 
101 
109  foreach (QString fileName, fileNames) {
110  try {
111  Control *control = new Control(fileName);
112  append(control);
113  }
114  catch (IException &) {
115  }
116  }
117  }
118 
119 
124  emit deletingList(this);
125  }
126 
127 
135  void ControlList::append(Control * const &value) {
137  emit countChanged(count());
138  }
139 
140 
150  emit countChanged(count());
151  }
152 
153 
160  bool countChanging = count();
162  if (countChanging) {
163  emit countChanged(count());
164  }
165  }
166 
167 
178  iterator result = QList<Control *>::erase(pos);
179  emit countChanged(count());
180  return result;
181  }
182 
183 
194  QList<Control *>::iterator ControlList::erase(iterator begin, iterator end) {
195  iterator result = QList<Control *>::erase(begin, end);
196  emit countChanged(count());
197  return result;
198  }
199 
200 
209  void ControlList::insert(int i, Control * const &value) {
210  QList<Control *>::insert(i, value);
211 
212  emit countChanged(count());
213  }
214 
215 
226  QList<Control *>::iterator ControlList::insert(iterator before, Control * const &value) {
227  iterator result = QList<Control *>::insert(before, value);
228  emit countChanged(count());
229  return result;
230  }
231 
232 
240  void ControlList::prepend(Control * const &value) {
242  emit countChanged(count());
243  }
244 
245 
254  void ControlList::push_back(Control * const &value) {
256  emit countChanged(count());
257  }
258 
259 
268  void ControlList::push_front(Control * const &value) {
270  emit countChanged(count());
271  }
272 
273 
283  int ControlList::removeAll(Control * const &value) {
284  int result = QList<Control *>::removeAll(value);
285 
286  if (result != 0) {
287  emit countChanged(count());
288  }
289 
290  return result;
291  }
292 
293 
301  void ControlList::removeAt(int i) {
303  emit countChanged(count());
304  }
305 
306 
314  emit countChanged(count());
315  }
316 
317 
325  emit countChanged(count());
326  }
327 
328 
338  bool ControlList::removeOne(Control * const &value) {
339  bool result = QList<Control *>::removeOne(value);
340 
341  if (result) {
342  emit countChanged(count());
343  }
344 
345  return result;
346  }
347 
348 
357  QList<Control *>::swap(other);
358 
359  if (count() != other.count()) {
360  emit countChanged(count());
361  }
362  }
363 
364 
375  Control * result = QList<Control *>::takeAt(i);
376  emit countChanged(count());
377  return result;
378  }
379 
380 
390  emit countChanged(count());
391  return result;
392  }
393 
394 
404  emit countChanged(count());
405  return result;
406  }
407 
408 
420 
421  if (other.count()) {
422  emit countChanged(count());
423  }
424 
425  return *this;
426  }
427 
428 
440  emit countChanged(count());
441  return *this;
442  }
443 
444 
454  ControlList &ControlList::operator<<(const QList<Control *> &other) {
456 
457  if (other.count()) {
458  emit countChanged(count());
459  }
460 
461  return *this;
462  }
463 
464 
476  emit countChanged(count());
477  return *this;
478  }
479 
480 
491  bool countChanging = (rhs.count() != count());
493 
494  if (countChanging) {
495  emit countChanged(count());
496  }
497 
498  return *this;
499  }
500 
501 
512  bool countChanging = (rhs.count() != count());
514 
515  m_name = rhs.m_name;
516  m_path = rhs.m_path;
517 
518  if (countChanging) {
519  emit countChanged(count());
520  }
521 
522  return *this;
523  }
524 
525 
532  void ControlList::setName(QString newName) {
533  m_name = newName;
534  }
535 
536 
543  void ControlList::setPath(QString newPath) {
544  m_path = newPath;
545  }
546 
547 
553  QString ControlList::name() const {
554  return m_name;
555  }
556 
557 
564  QString ControlList::path() const {
565  return m_path;
566  }
567 
568 
577  foreach (Control *control, *this) {
578  control->deleteFromDisk();
579  }
580 
581  if (!m_path.isEmpty()) {
582  QFile::remove(project->cnetRoot() + "/" + m_path + "/controlNetworks.xml");
583 
584  QDir dir;
585  dir.rmdir(project->cnetRoot() + "/" + m_path);
586  }
587  }
588 
589 
613  void ControlList::save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot)
614  const {
615  stream.writeStartElement("controlList");
616  stream.writeAttribute("name", m_name);
617  stream.writeAttribute("path", m_path);
618 
619  FileName settingsFileName(
620  Project::cnetRoot(newProjectRoot.toString()) + "/" + m_path +
621  "/controlNetworks.xml");
622 
623  if (!settingsFileName.dir().mkpath(settingsFileName.path())) {
625  QString("Failed to create directory [%1]")
626  .arg(settingsFileName.path()),
627  _FILEINFO_);
628  }
629 
630  QFile controlListContentsFile(settingsFileName.toString());
631 
632  if (!controlListContentsFile.open(QIODevice::ReadWrite | QIODevice::Truncate)) {
634  QString("Unable to save control information for [%1] because [%2] could not be opened "
635  "for writing")
636  .arg(m_name).arg(settingsFileName.original()),
637  _FILEINFO_);
638  }
639 
640  QXmlStreamWriter controlDetailsWriter(&controlListContentsFile);
641  controlDetailsWriter.setAutoFormatting(true);
642  controlDetailsWriter.writeStartDocument();
643 
644  int countWidth = QString("%1L").arg(count()).size() - 1;
645  QChar paddingChar('0');
646 
647  QLabel *progressLabel = new QLabel;
648 
649  QProgressDialog progressDialog;
650  progressDialog.setLabel(progressLabel);
651  progressDialog.setRange(-1, count());
652  progressDialog.setValue(-1);
653 
654  controlDetailsWriter.writeStartElement("controls");
655 
656  // Only copy controls if saving to new location
657  if (project->newProjectRoot() != project->projectRoot()) {
658  QFuture<void *> future = QtConcurrent::mapped(*this,
659  CopyControlDataFunctor(project, newProjectRoot));
660  for (int i = 0; i < count(); i++) {
661  int newProgressValue = progressDialog.value() + 1;
662  progressLabel->setText(
663  tr("Saving Control Information for [%1] - %L2/%L3 done")
664  .arg(m_name)
665  .arg(newProgressValue, countWidth, 10, paddingChar)
666  .arg(count()));
667  progressDialog.setValue(newProgressValue);
668  future.resultAt(i);
669  }
670 
671  progressLabel->setText(tr("Finalizing..."));
672  progressDialog.setRange(0, 0);
673  progressDialog.setValue(0);
674  }
675 
676  foreach (Control *control, *this) {
677  control->save(controlDetailsWriter, project, newProjectRoot);
678  }
679 
680  controlDetailsWriter.writeEndElement();
681 
682  controlDetailsWriter.writeEndDocument();
683 
684  stream.writeEndElement();
685  }
686 
687 
695  FileName newProjectRoot) {
696  m_project = project;
697  m_newProjectRoot = newProjectRoot;
698  }
699 
700 
707  m_project = other.m_project;
708  m_newProjectRoot = other.m_newProjectRoot;
709  }
710 
711 
716  }
717 
718 
725  controlToCopy->copyToNewProjectRoot(m_project, m_newProjectRoot);
726  return NULL;
727  }
728 
729 
738  const CopyControlDataFunctor &rhs) {
739  m_project = rhs.m_project;
740  m_newProjectRoot = rhs.m_newProjectRoot;
741  return *this;
742  }
743 
744 
753  m_controlList = controlList;
754  m_project = project;
755  }
756 
757 
769  bool ControlList::XmlHandler::startElement(const QString &namespaceURI, const QString &localName,
770  const QString &qName, const QXmlAttributes &atts) {
771  if (XmlStackedHandler::startElement(namespaceURI, localName, qName, atts)) {
772  if (localName == "controlList") {
773  QString name = atts.value("name");
774  QString path = atts.value("path");
775 
776  if (!name.isEmpty()) {
777  m_controlList->setName(name);
778  }
779 
780  if (!path.isEmpty()) {
781  m_controlList->setPath(path);
782  }
783  }
784  else if (localName == "controlNet") {
785  m_controlList->append(new Control(m_project->cnetRoot() + "/" +
786  m_controlList->path(), reader()));
787  }
788  }
789 
790  return true;
791  }
792 
793 
807  bool ControlList::XmlHandler::endElement(const QString &namespaceURI, const QString &localName,
808  const QString &qName) {
809  if (localName == "controlList") {
810  XmlHandler handler(m_controlList, m_project);
811 
813  reader.pushContentHandler(&handler);
814  reader.setErrorHandler(&handler);
815 
816  QString controlListXmlPath = m_project->cnetRoot() + "/" + m_controlList->path() +
817  "/controlNetworks.xml";
818  QFile file(controlListXmlPath);
819 
820  if (!file.open(QFile::ReadOnly)) {
822  QString("Unable to open [%1] with read access")
823  .arg(controlListXmlPath),
824  _FILEINFO_);
825  }
826 
827  QXmlInputSource xmlInputSource(&file);
828  if (!reader.parse(xmlInputSource))
830  tr("Failed to open control list XML [%1]").arg(controlListXmlPath),
831  _FILEINFO_);
832  }
833 
834  return XmlStackedHandler::endElement(namespaceURI, localName, qName);
835  }
836 }
This represents an ISIS control net in a project-based GUI interface.
Definition: Control.h:79
void save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot) const
Convert this control list into XML format for saving/restoring capabilities.
QString path() const
Returns the path of the file name.
Definition: FileName.cpp:119
FileName m_newProjectRoot
The filename of the destination project&#39;s root.
Definition: ControlList.h:130
QString cnetRoot() const
Get where control networks ought to be stored inside the project.
Definition: Project.cpp:2029
ControlList(QString name, QString path, QObject *parent=NULL)
Create an control list from a control list name and path (does not read Controls).
Definition: ControlList.cpp:51
The main project for ipce.
Definition: Project.h:289
~ControlList()
Destructor.
iterator erase(iterator pos)
Erases a control pointer from the control list at the specified position.
void deleteFromDisk()
Delete the control net from disk.
Definition: Control.cpp:326
void insert(int i, Control *const &value)
Inserts a control pointer at the specified position in the control list.
File name manipulation and expansion.
Definition: FileName.h:116
Maintains a list of Controls so that control nets can easily be copied from one Project to another...
Definition: ControlList.h:36
void setName(QString newName)
Set the human-readable name of this control list.
void push_front(Control *const &value)
Equivalent to prepend(value)
QString path() const
Get the path to these controls in the control list (relative to project root).
void copyToNewProjectRoot(const Project *project, FileName newProjectRoot)
Copies the files of the given Project to the given location.
Definition: Control.cpp:286
const Project * m_project
Project to copy the control list to.
Definition: ControlList.h:129
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
~CopyControlDataFunctor()
CopyControlDataFunctor destructor.
CopyControlDataFunctor & operator=(const CopyControlDataFunctor &rhs)
CopyControlDataFunctor assignment operator.
bool removeOne(Control *const &value)
Removes the first occurence of the control pointer from the control list.
QDebug operator<<(QDebug dbg, const Isis::Angle &angleToPrint)
Display an Angle for a debugging statement.
Definition: Angle.cpp:383
void removeLast()
Removes the last control pointer from the control list.
QString m_name
Name of the ControlList.
Definition: ControlList.h:162
virtual bool endElement(const QString &namespaceURI, const QString &localName, const QString &qName)
Handle an XML end element.
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
XmlHandler(ControlList *controlList, Project *project)
Create an XML Handler (reader/writer) that can populate the ControlList class data.
virtual void pushContentHandler(XmlStackedHandler *newHandler)
Push a contentHandler and maybe continue parsing...
Nested class used to write the ControlList object information to an XML file for the purposes of savi...
Definition: ControlList.h:144
Control * takeFirst()
Removes the first control pointer from the control list and returns it.
void clear()
Clears the control list.
QString original() const
Returns the full file name including the file path.
Definition: FileName.cpp:228
ControlList & operator+=(const QList< Control *> &other)
Appends control pointers from the other list to this control list.
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
QString newProjectRoot() const
Get the top-level folder of the new project.
Definition: Project.cpp:1675
void append(Control *const &value)
Appends a control pointer to the control list.
void prepend(Control *const &value)
Prepends a control pointer to the control list.
CopyControlDataFunctor(const Project *project, FileName newProjectRoot)
CopyControlDataFunctor constructor.
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
QString name() const
Get the human-readable name of this control list.
virtual bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts)
Handle an XML start element.
ControlList & operator<<(const QList< Control *> &other)
Appends a list of other control pointers to this control list.
void push_back(Control *const &value)
Equivalent to append(value)
Isis exception class.
Definition: IException.h:107
void swap(QList< Control *> &other)
Swaps this control list&#39;s control pointers with the other list of control pointers.
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
Control * takeLast()
Removes the last control pointer from the control list and returns it.
void setPath(QString newPath)
Set the relative path (from the project root) to this control list&#39;s folder.
QString m_path
This stores the directory name that contains the controls in this control list.
Definition: ControlList.h:174
ControlList & operator=(const QList< Control *> &rhs)
Assigns another list of control pointers to this control list.
Control * takeAt(int i)
Remove the control pointer at the specified index and returns it.
void removeAt(int i)
Removes the control pointer at the specified index.
void deleteFromDisk(Project *project)
Delete all of the contained Controls from disk.
Manage a stack of content handlers for reading XML files.
void removeFirst()
Removes the first control pointer from the control list.
void * operator()(Control *const &controlToCopy)
Copies the Control from one project to another.
int removeAll(Control *const &value)
Removes all occurences of the control pointer in the control list.
This functor is used for copying the control nets between two projects quickly.
Definition: ControlList.h:118