Isis 3 Programmer Reference
TemplateList.cpp
1
7/* SPDX-License-Identifier: CC0-1.0 */
8
9#include "TemplateList.h"
10#include <QDir>
11
12#include "FileName.h"
13#include "IException.h"
14#include "Project.h"
15
16namespace Isis {
25 TemplateList::TemplateList(QString name, QString type, QString path, QObject *parent) : QObject(parent){
26 m_name = name;
27 m_path = path;
28 m_type = type;
29 }
30
31
38 }
39
40
47 QList<Template *>(other) {
48 m_path = other.m_path;
49 m_name = other.m_name;
50 m_type = other.m_type;
51 }
52
53
58 // At the moment, this only occurs during an ImportTemplateWorkOrder undo, where it is handled
59 }
60
61
67 QString TemplateList::name() const{
68 return m_name;
69 }
70
71
77 QString TemplateList::type() const{
78 return m_type;
79 }
80
81
87 QString TemplateList::path() const{
88 return m_path;
89 }
90
91
97 void TemplateList::setName(QString newName) {
98 m_name = newName;
99 }
100
101
107 void TemplateList::setType(QString newType) {
108 m_type = newType;
109 }
110
111
117 void TemplateList::setPath(QString newPath) {
118 m_path = newPath;
119 }
120
121
130 foreach (Template *currentTemplate, *this) {
131 currentTemplate->deleteFromDisk();
132 }
133
134 if (!m_path.isEmpty()) {
135 QFile::remove(project->templateRoot() + "/" + m_path + "/templates.xml");
136
137 QDir dir;
138 dir.rmdir(project->templateRoot() + "/" + m_path);
139 }
140 }
141
142
166 void TemplateList::save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot)
167 const {
168
169 if (m_type == "maps") {
170 stream.writeStartElement("mapTemplateList");
171 }
172 else if (m_type == "registrations") {
173 stream.writeStartElement("regTemplateList");
174 }
175 else {
177 QString("Attempting to save unsupported template file type: [%1]").arg(m_type),
178 _FILEINFO_);
179 }
180 stream.writeAttribute("name", m_name);
181 stream.writeAttribute("type", m_type);
182 stream.writeAttribute("path", m_path);
183
184 FileName settingsFileName(Project::templateRoot(newProjectRoot.toString())
185 + "/" + m_type + "/" + m_name + "/templates.xml");
186
187 if (!settingsFileName.dir().mkpath(settingsFileName.path())) {
189 QString("Failed to create directory [%1]").arg(settingsFileName.path()),
190 _FILEINFO_);
191 }
192
193 QFile templateListContentsFile(settingsFileName.toString());
194
195 if (!templateListContentsFile.open(QIODevice::ReadWrite | QIODevice::Truncate)) {
197 QString("Unable to save template information for [%1] because [%2] could not be opened "
198 "for writing")
199 .arg(m_name).arg(settingsFileName.original()),
200 _FILEINFO_);
201 }
202
203 QXmlStreamWriter templateDetailsWriter(&templateListContentsFile);
204 templateDetailsWriter.setAutoFormatting(true);
205 templateDetailsWriter.writeStartDocument();
206 templateDetailsWriter.writeStartElement("templates");
207
208 foreach (Template *currentTemplate, *this) {
209 currentTemplate->save(templateDetailsWriter, project, newProjectRoot);
210
211 QString newPath = newProjectRoot.toString() + "/templates/" + m_type + "/" + m_name;
212
213 if (currentTemplate->fileName() !=
214 newPath + "/" + FileName(currentTemplate->fileName()).name()) {
215 QFile::copy(currentTemplate->fileName(),
216 newPath + "/" + FileName(currentTemplate->fileName()).name() );
217 }
218 }
219
220 templateDetailsWriter.writeEndElement();
221 templateDetailsWriter.writeEndDocument();
222
223 stream.writeEndElement();
224 }
225}
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 name() const
Returns the name of the file excluding the path and the attributes in the file name.
Definition FileName.cpp:162
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 templateRoot(QString projectRoot)
Appends the root directory name 'templates' to the project .
Definition Project.cpp:2186
QString templateRoot() const
Accessor for the root directory of the template data.
Definition Project.cpp:2196
void setPath(QString newPath)
Set the relative path (from the project root) to this TemplateList's folder.
TemplateList(QString name, QString type, QString path, QObject *parent=NULL)
Create a template from a file name, type, and path.
QString name() const
Get the human-readable name of this TemplateList.
~TemplateList()
Destructor.
void deleteFromDisk(Project *project)
Delete all of the contained Templates from disk.
QString path() const
Get the path to these Templates in the TemplateList (relative to project root).
void save(QXmlStreamWriter &stream, const Project *project, FileName newProjectRoot) const
Convert this TemplateList into XML format for saving/restoring capabilities.
void setName(QString newName)
Set the human-readable name of this TemplateList.
QString type() const
Get the type of template in this TemplateList.
void setType(QString newType)
Set the type of template for of this TemplateList.
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