Isis 3.0 Programmer Reference
Home
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  QFuture<void *> future = QtConcurrent::mapped(*this,
657  CopyControlDataFunctor(project, newProjectRoot));
658  for (int i = 0; i < count(); i++) {
659  int newProgressValue = progressDialog.value() + 1;
660  progressLabel->setText(
661  tr("Saving Control Information for [%1] - %L2/%L3 done")
662  .arg(m_name)
663  .arg(newProgressValue, countWidth, 10, paddingChar)
664  .arg(count()));
665  progressDialog.setValue(newProgressValue);
666  future.resultAt(i);
667  }
668 
669  progressLabel->setText(tr("Finalizing..."));
670  progressDialog.setRange(0, 0);
671  progressDialog.setValue(0);
672 
673  foreach (Control *control, *this) {
674  control->save(controlDetailsWriter, project, newProjectRoot);
675  }
676 
677  controlDetailsWriter.writeEndElement();
678 
679  controlDetailsWriter.writeEndDocument();
680 
681  stream.writeEndElement();
682  }
683 
684 
692  FileName newProjectRoot) {
693  m_project = project;
694  m_newProjectRoot = newProjectRoot;
695  }
696 
697 
704  m_project = other.m_project;
705  m_newProjectRoot = other.m_newProjectRoot;
706  }
707 
708 
713  }
714 
715 
722  controlToCopy->copyToNewProjectRoot(m_project, m_newProjectRoot);
723  return NULL;
724  }
725 
726 
735  const CopyControlDataFunctor &rhs) {
736  m_project = rhs.m_project;
737  m_newProjectRoot = rhs.m_newProjectRoot;
738  return *this;
739  }
740 
741 
750  m_controlList = controlList;
751  m_project = project;
752  }
753 
754 
766  bool ControlList::XmlHandler::startElement(const QString &namespaceURI, const QString &localName,
767  const QString &qName, const QXmlAttributes &atts) {
768  if (XmlStackedHandler::startElement(namespaceURI, localName, qName, atts)) {
769  if (localName == "controlList") {
770  QString name = atts.value("name");
771  QString path = atts.value("path");
772 
773  if (!name.isEmpty()) {
774  m_controlList->setName(name);
775  }
776 
777  if (!path.isEmpty()) {
778  m_controlList->setPath(path);
779  }
780  }
781  else if (localName == "controlNet") {
782  m_controlList->append(new Control(m_project->cnetRoot() + "/" +
783  m_controlList->path(), reader()));
784  }
785  }
786 
787  return true;
788  }
789 
790 
804  bool ControlList::XmlHandler::endElement(const QString &namespaceURI, const QString &localName,
805  const QString &qName) {
806  if (localName == "controlList") {
807  XmlHandler handler(m_controlList, m_project);
808 
810  reader.pushContentHandler(&handler);
811  reader.setErrorHandler(&handler);
812 
813  QString controlListXmlPath = m_project->cnetRoot() + "/" + m_controlList->path() +
814  "/controlNetworks.xml";
815  QFile file(controlListXmlPath);
816 
817  if (!file.open(QFile::ReadOnly)) {
819  QString("Unable to open [%1] with read access")
820  .arg(controlListXmlPath),
821  _FILEINFO_);
822  }
823 
824  QXmlInputSource xmlInputSource(&file);
825  if (!reader.parse(xmlInputSource))
827  tr("Failed to open control list XML [%1]").arg(controlListXmlPath),
828  _FILEINFO_);
829  }
830 
831  return XmlStackedHandler::endElement(namespaceURI, localName, qName);
832  }
833 }
This represents an ISIS control net in a project-based GUI interface.
Definition: Control.h:57
FileName m_newProjectRoot
The filename of the destination project&#39;s root.
Definition: ControlList.h:128
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 cnetsuite.
Definition: Project.h:105
~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:210
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:111
Maintains a list of Controls so that control nets can easily be copied from one Project to another...
Definition: ControlList.h:34
void setName(QString newName)
Set the human-readable name of this control list.
void push_front(Control *const &value)
Equivalent to prepend(value)
ControlList & operator<<(const QList< Control * > &other)
Appends a list of other control pointers to this control list.
void copyToNewProjectRoot(const Project *project, FileName newProjectRoot)
Copies the files of the given Project to the given location.
Definition: Control.cpp:193
const Project * m_project
Project to copy the control list to.
Definition: ControlList.h:127
void swap(QList< Control * > &other)
Swaps this control list&#39;s control pointers with the other list of control pointers.
A type of error that occurred when performing an actual I/O operation.
Definition: IException.h:163
QString cnetRoot() const
Get where control networks ought to be stored inside the project.
Definition: Project.cpp:1230
static QString cnetRoot(QString projectRoot)
Appends the root directory name &#39;cnets&#39; to the project.
Definition: Project.cpp:1220
~CopyControlDataFunctor()
CopyControlDataFunctor destructor.
CopyControlDataFunctor & operator=(const CopyControlDataFunctor &rhs)
CopyControlDataFunctor assignment operator.
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:248
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:379
void removeLast()
Removes the last control pointer from the control list.
void save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot) const
Convert this control list into XML format for saving/restoring capabilities.
QString m_name
Name of the ControlList.
Definition: ControlList.h:160
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:38
XmlHandler(ControlList *controlList, Project *project)
Create an XML Handler (reader/writer) that can populate the ControlList class data.
QString name() const
Get the human-readable name of this control list.
Nested class used to write the ControlList object information to an XML file for the purposes of savi...
Definition: ControlList.h:142
Control * takeFirst()
Removes the first control pointer from the control list and returns it.
void clear()
Clears the control list.
ControlList & operator+=(const QList< Control * > &other)
Appends control pointers from the other list to this control list.
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.
virtual bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts)
Handle an XML start element.
void push_back(Control *const &value)
Equivalent to append(value)
Isis exception class.
Definition: IException.h:99
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:172
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.
ControlList & operator=(const QList< Control * > &rhs)
Assigns another list of control pointers to this control list.
void deleteFromDisk(Project *project)
Delete all of the contained Controls from disk.
QString path() const
Returns the path.
Definition: FileName.cpp:88
his enables stack-based XML parsing of 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:116
QString path() const
Get the path to these controls in the control list (relative to project root).