Isis 3 Programmer Reference
ShapeList.cpp
1
7/* SPDX-License-Identifier: CC0-1.0 */
8
9#include "ShapeList.h"
10
11#include <QDebug>
12#include <QDir>
13#include <QFile>
14#include <QFuture>
15#include <QInputDialog>
16#include <QLabel>
17#include <QProgressDialog>
18#include <QtConcurrentMap>
19#include <QXmlStreamWriter>
20
21#include "FileName.h"
22#include "IException.h"
23#include "IString.h"
24#include "Project.h"
25
26namespace Isis {
34 ShapeList::ShapeList(QString name, QString path, QObject *parent) : QObject(parent) {
35 m_name = name;
36 m_path = path;
37 }
38
39
46 }
47
48
55 ShapeList::ShapeList(QList<Shape *> shapes, QObject *parent) : QObject(parent) {
56 append(shapes);
57 }
58
59
66 QList<Shape *>(other) {
67 m_name = other.m_name;
68 m_path = other.m_path;
69 }
70
71
78 foreach (QString fileName, fileNames) {
79 try {
80 Shape *shape = new Shape(fileName);
81 append(shape);
82 }
83 catch (IException &) {
84 }
85 }
86 }
87
88
94
95
102
103 SerialNumberList result;
104
105 for (int i = 0; i < count(); i++) {
106 result.add((*this)[i]->fileName());
107 }
108 return result;
109 }
110
111
119 void ShapeList::append(Shape * const &value) {
121 emit countChanged(count());
122 }
123
124
132 void ShapeList::append(const QList<Shape *> &value) {
134 emit countChanged(count());
135 }
136
137
144 bool countChanging = count();
146 if (countChanging) {
147 emit countChanged(count());
148 }
149 }
150
151
162 iterator result = QList<Shape *>::erase(pos);
163 emit countChanged(count());
164 return result;
165 }
166
167
180 QList<Shape *>::iterator ShapeList::erase(iterator begin, iterator end) {
181 iterator result = QList<Shape *>::erase(begin, end);
182 emit countChanged(count());
183 return result;
184 }
185
186
195 void ShapeList::insert(int i, Shape * const &value) {
196 QList<Shape *>::insert(i, value);
197
198 emit countChanged(count());
199 }
200
201
212 QList<Shape *>::iterator ShapeList::insert(iterator before, Shape * const &value) {
213 iterator result = QList<Shape *>::insert(before, value);
214 emit countChanged(count());
215 return result;
216 }
217
218
226 void ShapeList::prepend(Shape * const &value) {
228 emit countChanged(count());
229 }
230
231
240 void ShapeList::push_back(Shape * const &value) {
242 emit countChanged(count());
243 }
244
245
254 void ShapeList::push_front(Shape * const &value) {
256 emit countChanged(count());
257 }
258
259
269 int ShapeList::removeAll(Shape * const &value) {
270 int result = QList<Shape *>::removeAll(value);
271
272 if (result != 0) {
273 emit countChanged(count());
274 }
275
276 return result;
277 }
278
279
289 emit countChanged(count());
290 }
291
292
300 emit countChanged(count());
301 }
302
303
311 emit countChanged(count());
312 }
313
314
324 bool ShapeList::removeOne(Shape * const &value) {
325 bool result = QList<Shape *>::removeOne(value);
326
327 if (result) {
328 emit countChanged(count());
329 }
330
331 return result;
332 }
333
334
342 void ShapeList::swap(QList<Shape *> &other) {
344
345 if (count() != other.count()) {
346 emit countChanged(count());
347 }
348 }
349
350
361 Shape * result = QList<Shape *>::takeAt(i);
362 emit countChanged(count());
363 return result;
364 }
365
366
376 emit countChanged(count());
377 return result;
378 }
379
380
390 emit countChanged(count());
391 return result;
392 }
393
394
405 ShapeList &ShapeList::operator+=(const QList<Shape *> &other) {
407
408 if (other.count()) {
409 emit countChanged(count());
410 }
411
412 return *this;
413 }
414
415
428 emit countChanged(count());
429 return *this;
430 }
431
432
443 ShapeList &ShapeList::operator<<(const QList<Shape *> &other) {
445
446 if (other.count()) {
447 emit countChanged(count());
448 }
449
450 return *this;
451 }
452
453
466 emit countChanged(count());
467 return *this;
468 }
469
470
480 ShapeList &ShapeList::operator=(const QList<Shape *> &rhs) {
481 bool countChanging = (rhs.count() != count());
483
484 if (countChanging) {
485 emit countChanged(count());
486 }
487
488 return *this;
489 }
490
491
500 bool countChanging = (rhs.count() != count());
502
503 m_name = rhs.m_name;
504 m_path = rhs.m_path;
505
506 if (countChanging) {
507 emit countChanged(count());
508 }
509
510 return *this;
511 }
512
513
520 void ShapeList::setName(QString newName) {
521 m_name = newName;
522 }
523
524
531 void ShapeList::setPath(QString newPath) {
532 m_path = newPath;
533 }
534
535
541 QString ShapeList::name() const {
542 return m_name;
543 }
544
545
552 QString ShapeList::path() const {
553 return m_path;
554 }
555
556
565 foreach (Shape *shape, *this) {
566 shape->deleteFromDisk();
567 }
568
569 if (!m_path.isEmpty()) {
570 QFile::remove(project->shapeDataRoot() + "/" + m_path + "/shapes.xml");
571
572 QDir dir;
573 dir.rmdir(project->shapeDataRoot() + "/" + m_path);
574 }
575 }
576
577
601 void ShapeList::save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot)
602 const {
603 stream.writeStartElement("shapeList");
604 stream.writeAttribute("name", m_name);
605 stream.writeAttribute("path", m_path);
606
607 FileName settingsFileName(
608 Project::shapeDataRoot(newProjectRoot.toString()) + "/" + m_path + "/shapes.xml");
609
610 if (!settingsFileName.dir().mkpath(settingsFileName.path())) {
612 QString("Failed to create directory [%1]")
613 .arg(settingsFileName.path()),
614 _FILEINFO_);
615 }
616
617 QFile shapeListContentsFile(settingsFileName.toString());
618
619 if (!shapeListContentsFile.open(QIODevice::ReadWrite | QIODevice::Truncate)) {
621 QString("Unable to save shape information for [%1] because [%2] could not be opened for "
622 "writing")
623 .arg(m_name).arg(settingsFileName.original()),
624 _FILEINFO_);
625 }
626
627 QXmlStreamWriter shapeDetailsWriter(&shapeListContentsFile);
628 shapeDetailsWriter.setAutoFormatting(true);
629 shapeDetailsWriter.writeStartDocument();
630
631 int countWidth = QString("%1L").arg(count()).size() - 1;
632 QChar paddingChar('0');
633
634 QLabel *progressLabel = new QLabel;
635
636 QProgressDialog progressDialog;
637 progressDialog.setLabel(progressLabel);
638 progressDialog.setRange(-1, count());
639 progressDialog.setValue(-1);
640
641 shapeDetailsWriter.writeStartElement("shapes");
642 // Mapped is way faster than hundreds/thousands of run() calls... so use mapped for performance
643 QFuture<void *> future = QtConcurrent::mapped(*this,
644 CopyShapeDataFunctor(project, newProjectRoot));
645
646 for (int i = 0; i < count(); i++) {
647 int newProgressValue = progressDialog.value() + 1;
648 progressLabel->setText(
649 tr("Saving Shape Information for [%1] - %L2/%L3 done")
650 .arg(m_name)
651 .arg(newProgressValue, countWidth, 10, paddingChar)
652 .arg(count()));
653 progressDialog.setValue(newProgressValue);
654 future.resultAt(i);
655 }
656
657 progressLabel->setText(tr("Finalizing..."));
658 progressDialog.setRange(0, 0);
659 progressDialog.setValue(0);
660
661 foreach (Shape *shape, *this) {
662 shape->save(shapeDetailsWriter, project, newProjectRoot);
663 }
664
665 shapeDetailsWriter.writeEndElement();
666
667 shapeDetailsWriter.writeEndDocument();
668
669 stream.writeEndElement();
670 }
671
672
680 FileName newProjectRoot) {
681 m_project = project;
682 m_newProjectRoot = newProjectRoot;
683 }
684
685
692 m_project = other.m_project;
693 m_newProjectRoot = other.m_newProjectRoot;
694 }
695
696
702
703
713 shapeToCopy->copyToNewProjectRoot(m_project, m_newProjectRoot);
714 return NULL;
715 }
716
717
726 const CopyShapeDataFunctor &rhs) {
727 m_project = rhs.m_project;
728 m_newProjectRoot = rhs.m_newProjectRoot;
729 return *this;
730 }
731}
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 shapeDataRoot(QString projectRoot)
Appends the root directory name 'shapes' to the project .
Definition Project.cpp:2148
QString shapeDataRoot() const
Accessor for the root directory of the shape model data.
Definition Project.cpp:2158
Serial Number list generator.
void add(const QString &filename, bool def2filename=false)
Adds a new filename / serial number pair to the SerialNumberList.
This represents a shape in a project-based GUI interface.
Definition Shape.h:66
void deleteFromDisk()
Delete the shape data from disk.
Definition Shape.cpp:575
void save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot) const
Output format:
Definition Shape.cpp:815
This functor is used for copying the shapes between two projects quickly.
Definition ShapeList.h:104
CopyShapeDataFunctor(const Project *project, FileName newProjectRoot)
Constructor for CopyShapeDataFunctor.
CopyShapeDataFunctor & operator=(const CopyShapeDataFunctor &rhs)
Assignment operator for CopyShapeDataFunctor.
FileName m_newProjectRoot
This stores the path to the root of the project that is going to be copied to.
Definition ShapeList.h:122
void * operator()(Shape *const &shapeToCopy)
Copies the cub/ecub files for an shape into m_project.
~CopyShapeDataFunctor()
Destructor for CopyShapeDataFunctor.
const Project * m_project
This stores the name of the project that is going to be copied to.
Definition ShapeList.h:118
Internalizes a list of shapes and allows for operations on the entire list.
Definition ShapeList.h:31
void insert(int i, Shape *const &value)
Inserts an shape into the shape list at an index.
void append(Shape *const &value)
Appends an shape to the shape list.
void deleteFromDisk(Project *project)
Delete all of the contained Shapes from disk.
void removeAt(int i)
Removes the shape at an index.
void setName(QString newName)
Set the human-readable name of this shape list.
Shape * takeLast()
Removes and returns the last shape.
iterator erase(iterator pos)
Erases a single shape from the shape list.
SerialNumberList serialNumberList()
Creates a SerialNumberList from the shape list.
void swap(QList< Shape * > &other)
Swaps the shape list with another list of shapes.
QString m_name
This stores the shape list's name.
Definition ShapeList.h:131
void clear()
Clears the shape list.
QString m_path
This stores the directory name that contains the shapes in this shape list.
Definition ShapeList.h:143
void push_back(Shape *const &value)
Appends an shape to the end of the shape list.
void removeFirst()
Removes the shape at the front of the shape list.
void removeLast()
Removes the shape at the end of the shape list.
QString path() const
Get the path to the shapes in the shape list (relative to project root).
int removeAll(Shape *const &value)
Removes all occurances of an shape.
QString name() const
Get the human-readable name of this shape list.
void prepend(Shape *const &value)
Inserts an shape at the beginning of the shape list.
Shape * takeAt(int i)
Removes the shape at an index and returns it.
void save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot) const
Convert this shape list into XML format for saving/restoring capabilities.
ShapeList(QString name, QString path, QObject *parent=NULL)
Creates an shape list from an shape list name and path (does not read Shapes).
Definition ShapeList.cpp:34
void push_front(Shape *const &value)
Prepends an shape to the beginning of the shape list.
ShapeList & operator<<(const QList< Shape * > &other)
Appends a list of shapes to the end of the shape list.
~ShapeList()
Destructor.
Definition ShapeList.cpp:92
bool removeOne(Shape *const &value)
Removes the first occurance of an shape.
ShapeList & operator=(const QList< Shape * > &rhs)
Assigns another list of shapes to the shape list.
void setPath(QString newPath)
Set the relative path (from the project root) to this shape list's folder.
Shape * takeFirst()
Removes and returns the first shape.
ShapeList & operator+=(const QList< Shape * > &other)
Appends a list of shapes to the end of the shape list.
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