Isis 3 Programmer Reference
ControlList.cpp
1
7/* SPDX-License-Identifier: CC0-1.0 */
8
9#include "ControlList.h"
10
11#include <QAction>
12#include <QColorDialog>
13#include <QDebug>
14#include <QDir>
15#include <QFile>
16#include <QFuture>
17#include <QInputDialog>
18#include <QLabel>
19#include <QProgressDialog>
20#include <QtConcurrentMap>
21#include <QXmlStreamWriter>
22
23#include "Color.h"
24#include "FileName.h"
25#include "IException.h"
26#include "Project.h"
27
28namespace Isis {
36 ControlList::ControlList(QString name, QString path, QObject *parent) : QObject(parent) {
37 m_name = name;
38 m_path = path;
39 }
40
41
48 }
49
50
57 ControlList::ControlList(QList<Control *> controls, QObject *parent) : QObject(parent) {
58 append(controls);
59 }
60
67 QList<Control *>(other) {
68 m_name = other.m_name;
69 m_path = other.m_path;
70 }
71
72
80 foreach (QString fileName, fileNames) {
81 try {
82 Control *control = new Control(fileName);
83 append(control);
84 }
85 catch (IException &) {
86 }
87 }
88 }
89
90
95 emit deletingList(this);
96 }
97
98
106 void ControlList::append(Control * const &value) {
108 emit countChanged(count());
109 }
110
111
119 void ControlList::append(const QList<Control *> &value) {
121 emit countChanged(count());
122 }
123
124
131 bool countChanging = count();
133 if (countChanging) {
134 emit countChanged(count());
135 }
136 }
137
138
149 iterator result = QList<Control *>::erase(pos);
150 emit countChanged(count());
151 return result;
152 }
153
154
165 QList<Control *>::iterator ControlList::erase(iterator begin, iterator end) {
166 iterator result = QList<Control *>::erase(begin, end);
167 emit countChanged(count());
168 return result;
169 }
170
171
180 void ControlList::insert(int i, Control * const &value) {
181 QList<Control *>::insert(i, value);
182
183 emit countChanged(count());
184 }
185
186
197 QList<Control *>::iterator ControlList::insert(iterator before, Control * const &value) {
198 iterator result = QList<Control *>::insert(before, value);
199 emit countChanged(count());
200 return result;
201 }
202
203
211 void ControlList::prepend(Control * const &value) {
213 emit countChanged(count());
214 }
215
216
225 void ControlList::push_back(Control * const &value) {
227 emit countChanged(count());
228 }
229
230
239 void ControlList::push_front(Control * const &value) {
241 emit countChanged(count());
242 }
243
244
254 int ControlList::removeAll(Control * const &value) {
255 int result = QList<Control *>::removeAll(value);
256
257 if (result != 0) {
258 emit countChanged(count());
259 }
260
261 return result;
262 }
263
264
274 emit countChanged(count());
275 }
276
277
285 emit countChanged(count());
286 }
287
288
296 emit countChanged(count());
297 }
298
299
309 bool ControlList::removeOne(Control * const &value) {
310 bool result = QList<Control *>::removeOne(value);
311
312 if (result) {
313 emit countChanged(count());
314 }
315
316 return result;
317 }
318
319
327 void ControlList::swap(QList<Control *> &other) {
329
330 if (count() != other.count()) {
331 emit countChanged(count());
332 }
333 }
334
335
346 Control * result = QList<Control *>::takeAt(i);
347 emit countChanged(count());
348 return result;
349 }
350
351
361 emit countChanged(count());
362 return result;
363 }
364
365
375 emit countChanged(count());
376 return result;
377 }
378
379
389 ControlList &ControlList::operator+=(const QList<Control *> &other) {
391
392 if (other.count()) {
393 emit countChanged(count());
394 }
395
396 return *this;
397 }
398
399
411 emit countChanged(count());
412 return *this;
413 }
414
415
425 ControlList &ControlList::operator<<(const QList<Control *> &other) {
427
428 if (other.count()) {
429 emit countChanged(count());
430 }
431
432 return *this;
433 }
434
435
447 emit countChanged(count());
448 return *this;
449 }
450
451
461 ControlList &ControlList::operator=(const QList<Control *> &rhs) {
462 bool countChanging = (rhs.count() != count());
464
465 if (countChanging) {
466 emit countChanged(count());
467 }
468
469 return *this;
470 }
471
472
483 bool countChanging = (rhs.count() != count());
485
486 m_name = rhs.m_name;
487 m_path = rhs.m_path;
488
489 if (countChanging) {
490 emit countChanged(count());
491 }
492
493 return *this;
494 }
495
496
503 void ControlList::setName(QString newName) {
504 m_name = newName;
505 }
506
507
514 void ControlList::setPath(QString newPath) {
515 m_path = newPath;
516 }
517
518
524 QString ControlList::name() const {
525 return m_name;
526 }
527
528
535 QString ControlList::path() const {
536 return m_path;
537 }
538
539
548 foreach (Control *control, *this) {
549 control->deleteFromDisk();
550 }
551
552 if (!m_path.isEmpty()) {
553 QFile::remove(project->cnetRoot() + "/" + m_path + "/controlNetworks.xml");
554
555 QDir dir;
556 dir.rmdir(project->cnetRoot() + "/" + m_path);
557 }
558 }
559
560
584 void ControlList::save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot)
585 const {
586 stream.writeStartElement("controlList");
587 stream.writeAttribute("name", m_name);
588 stream.writeAttribute("path", m_path);
589
590 FileName settingsFileName(
591 Project::cnetRoot(newProjectRoot.toString()) + "/" + m_path +
592 "/controlNetworks.xml");
593
594 if (!settingsFileName.dir().mkpath(settingsFileName.path())) {
596 QString("Failed to create directory [%1]")
597 .arg(settingsFileName.path()),
598 _FILEINFO_);
599 }
600
601 QFile controlListContentsFile(settingsFileName.toString());
602
603 if (!controlListContentsFile.open(QIODevice::ReadWrite | QIODevice::Truncate)) {
605 QString("Unable to save control information for [%1] because [%2] could not be opened "
606 "for writing")
607 .arg(m_name).arg(settingsFileName.original()),
608 _FILEINFO_);
609 }
610
611 QXmlStreamWriter controlDetailsWriter(&controlListContentsFile);
612 controlDetailsWriter.setAutoFormatting(true);
613 controlDetailsWriter.writeStartDocument();
614
615 int countWidth = QString("%1L").arg(count()).size() - 1;
616 QChar paddingChar('0');
617
618 QLabel *progressLabel = new QLabel;
619
620 QProgressDialog progressDialog;
621 progressDialog.setLabel(progressLabel);
622 progressDialog.setRange(-1, count());
623 progressDialog.setValue(-1);
624
625 controlDetailsWriter.writeStartElement("controls");
626
627 // Only copy controls if saving to new location
628 if (project->newProjectRoot() != project->projectRoot()) {
629 QFuture<void *> future = QtConcurrent::mapped(*this,
630 CopyControlDataFunctor(project, newProjectRoot));
631 for (int i = 0; i < count(); i++) {
632 int newProgressValue = progressDialog.value() + 1;
633 progressLabel->setText(
634 tr("Saving Control Information for [%1] - %L2/%L3 done")
635 .arg(m_name)
636 .arg(newProgressValue, countWidth, 10, paddingChar)
637 .arg(count()));
638 progressDialog.setValue(newProgressValue);
639 future.resultAt(i);
640 }
641
642 progressLabel->setText(tr("Finalizing..."));
643 progressDialog.setRange(0, 0);
644 progressDialog.setValue(0);
645 }
646
647 foreach (Control *control, *this) {
648 control->save(controlDetailsWriter, project, newProjectRoot);
649 }
650
651 controlDetailsWriter.writeEndElement();
652
653 controlDetailsWriter.writeEndDocument();
654
655 stream.writeEndElement();
656 }
657
658
666 FileName newProjectRoot) {
667 m_project = project;
668 m_newProjectRoot = newProjectRoot;
669 }
670
671
678 m_project = other.m_project;
679 m_newProjectRoot = other.m_newProjectRoot;
680 }
681
682
688
689
696 controlToCopy->copyToNewProjectRoot(m_project, m_newProjectRoot);
697 return NULL;
698 }
699
700
709 const CopyControlDataFunctor &rhs) {
710 m_project = rhs.m_project;
711 m_newProjectRoot = rhs.m_newProjectRoot;
712 return *this;
713 }
714}
This represents an ISIS control net in a project-based GUI interface.
Definition Control.h:65
void deleteFromDisk()
Delete the control net from disk.
Definition Control.cpp:314
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
This functor is used for copying the control nets between two projects quickly.
CopyControlDataFunctor & operator=(const CopyControlDataFunctor &rhs)
CopyControlDataFunctor assignment operator.
~CopyControlDataFunctor()
CopyControlDataFunctor destructor.
void * operator()(Control *const &controlToCopy)
Copies the Control from one project to another.
const Project * m_project
Project to copy the control list to.
FileName m_newProjectRoot
The filename of the destination project's root.
CopyControlDataFunctor(const Project *project, FileName newProjectRoot)
CopyControlDataFunctor constructor.
Maintains a list of Controls so that control nets can easily be copied from one Project to another,...
Definition ControlList.h:42
void deleteFromDisk(Project *project)
Delete all of the contained Controls from disk.
void removeLast()
Removes the last control pointer from the control list.
iterator erase(iterator pos)
Erases a control pointer from the control list at the specified position.
void append(Control *const &value)
Appends a control pointer to the control list.
~ControlList()
Destructor.
void push_back(Control *const &value)
Equivalent to append(value)
ControlList & operator+=(const QList< Control * > &other)
Appends control pointers from the other list to this control list.
void insert(int i, Control *const &value)
Inserts a control pointer at the specified position in the control list.
QString path() const
Get the path to these controls in the control list (relative to project root).
QString name() const
Get the human-readable name of this control list.
void setName(QString newName)
Set the human-readable name of this control list.
void removeAt(int i)
Removes the control pointer at the specified index.
bool removeOne(Control *const &value)
Removes the first occurence of the control pointer from the control list.
void push_front(Control *const &value)
Equivalent to prepend(value)
Control * takeAt(int i)
Remove the control pointer at the specified index and returns it.
Control * takeFirst()
Removes the first control pointer from the control list and returns it.
ControlList & operator=(const QList< Control * > &rhs)
Assigns another list of control pointers to this control list.
int removeAll(Control *const &value)
Removes all occurences of the control pointer in the control list.
void save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot) const
Convert this control list into XML format for saving/restoring capabilities.
void setPath(QString newPath)
Set the relative path (from the project root) to this control list's folder.
void prepend(Control *const &value)
Prepends a control pointer to the control list.
QString m_name
Name of the ControlList.
ControlList & operator<<(const QList< Control * > &other)
Appends a list of other control pointers to this control list.
void swap(QList< Control * > &other)
Swaps this control list's control pointers with the other list of control pointers.
void clear()
Clears the control list.
QString m_path
This stores the directory name that contains the controls in this control list.
Control * takeLast()
Removes the last control pointer from the control list and returns it.
void removeFirst()
Removes the first control pointer from the control list.
ControlList(QString name, QString path, QObject *parent=NULL)
Create an control list from a control list name and path (does not read Controls).
File name manipulation and expansion.
Definition FileName.h:100
QString path() const
Returns the path of the file name.
Definition FileName.cpp:103
QDir dir() const
Returns the path of the file's parent directory as a QDir object.
Definition FileName.cpp:465
QString original() const
Returns the full file name including the file path.
Definition FileName.cpp:212
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
@ 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
QString newProjectRoot() const
Get the top-level folder of the new project.
Definition Project.cpp:1736
QString cnetRoot() const
Get where control networks ought to be stored inside the project.
Definition Project.cpp:2090
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.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16