Saves an XML file.

This commit is contained in:
Alexandre Quessy
2013-12-09 15:20:33 -05:00
parent 7bd6768918
commit dce8f0b35d
12 changed files with 177 additions and 14 deletions
+1
View File
@@ -2,6 +2,7 @@
* Common.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
+1
View File
@@ -2,6 +2,7 @@
* DestinationGLCanvas.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
+22 -11
View File
@@ -2,6 +2,7 @@
* MainWindow.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -18,6 +19,7 @@
*/
#include "MainWindow.h"
#include "ProjectWriter.h"
MainWindow::MainWindow()
{
@@ -146,8 +148,8 @@ bool MainWindow::save()
bool MainWindow::saveAs()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save mapping"),
".", tr("Libremapping files (*.lmp)"));
QString fileName = QFileDialog::getSaveFileName(this, tr("Save mapping project"),
".", tr("LibreMapping files (*.lmp)"));
if (fileName.isEmpty())
return false;
@@ -537,16 +539,25 @@ bool MainWindow::loadFile(const QString &fileName)
bool MainWindow::saveFile(const QString &fileName)
{
// TODO: Try to write file.
// if (!spreadsheet->writeFile(fileName))
// {
// statusBar()->showMessage(tr("Saving canceled"), 2000);
// return false;
// }
QFile file(fileName);
if (! file.open(QFile::WriteOnly | QFile::Text))
{
QMessageBox::warning(this, tr("Error saving mapping project"),
tr("Cannot write file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return false;
}
setCurrentFile(fileName);
statusBar()->showMessage(tr("File saved"), 2000);
return true;
ProjectWriter writer(mappingManager);
if (writer.writeFile(&file))
{
setCurrentFile(fileName);
statusBar()->showMessage(tr("File saved"), 2000);
return true;
}
else
return false;
}
void MainWindow::setCurrentFile(const QString &fileName)
+1
View File
@@ -2,6 +2,7 @@
* Mapper.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
+1
View File
@@ -2,6 +2,7 @@
* MapperGLCanvas.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
+1
View File
@@ -2,6 +2,7 @@
* MappingManager.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
+103
View File
@@ -0,0 +1,103 @@
/*
* MainWindow.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ProjectWriter.h"
#include <sstream>
ProjectWriter::ProjectWriter(MappingManager *manager) :
_manager(manager)
{
_xml.setAutoFormatting(true);
}
bool ProjectWriter::writeFile(QIODevice *device)
{
_xml.setDevice(device);
_xml.writeStartDocument();
_xml.writeDTD("<!DOCTYPE libremapping>");
_xml.writeStartElement("project");
_xml.writeAttribute("version", "1.0");
for (int i = 0; i < _manager->nPaints(); i++)
writeItem(_manager->getPaint(i).get());
for (int i = 0; i < _manager->nMappings(); i++)
writeItem(_manager->getMapping(i).get());
_xml.writeEndDocument();
return true;
}
void ProjectWriter::writeItem(Paint *item)
{
_xml.writeStartElement("paint");
_xml.writeAttribute("id", "FIXME");
_xml.writeAttribute("type", "image");
// FIXME: check paint type before casting to Image
Image *image = (Image *) item;
_xml.writeTextElement("uri", image->getImagePath());
{
std::ostringstream os;
os << image->getWidth();
_xml.writeTextElement("width", os.str().c_str());
}
{
std::ostringstream os;
os << image->getHeight();
_xml.writeTextElement("height", os.str().c_str());
}
_xml.writeEndElement();
//_xml.writeEmptyElement("hello");
}
void ProjectWriter::writeItem(Mapping *item)
{
_xml.writeStartElement("mapping");
_xml.writeAttribute("id", "FIXME");
// TODO: item->getPaint()->getId();
_xml.writeAttribute("paint_id", "FIXME");
Shape *shape = item->getShape().get();
_xml.writeStartElement("shape");
_xml.writeAttribute("type", shape->getShapeType());
for (int i = 0; i < shape->nVertices(); i++)
{
const Point & point = shape->getVertex(i);
_xml.writeStartElement("vertex");
{
std::ostringstream os;
os << point.x;
_xml.writeAttribute("x", os.str().c_str());
}
{
std::ostringstream os;
os << point.y;
_xml.writeAttribute("y", os.str().c_str());
}
_xml.writeEndElement(); // vertex
}
_xml.writeEndElement(); // shape
_xml.writeEndElement(); // mapping
}
+37
View File
@@ -0,0 +1,37 @@
/*
* MainWindow.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QXmlStreamWriter>
#include "MappingManager.h"
#include "Mapping.h"
#include "Paint.h"
class ProjectWriter
{
public:
ProjectWriter (MappingManager *manager);
bool writeFile (QIODevice *device);
private:
void writeItem (Paint *item);
void writeItem (Mapping *item);
QXmlStreamWriter _xml;
MappingManager *_manager;
};
+5
View File
@@ -28,6 +28,7 @@
*/
struct Point
{
public:
double x;
double y;
Point(double x_, double y_) : x(x_), y(y_) {}
@@ -64,6 +65,7 @@ public:
vertices[i].x = x;
vertices[i].y = y;
}
virtual const char * getShapeType() = 0;
};
/**
@@ -81,6 +83,8 @@ public:
vertices.push_back(p4);
}
virtual ~Quad() {}
virtual const char * getShapeType() { return "quad"; }
};
/**
@@ -97,6 +101,7 @@ public:
vertices.push_back(p3);
}
virtual ~Triangle() {}
virtual const char * getShapeType() { return "triangle"; }
};
#endif /* SHAPE_H_ */
+1
View File
@@ -2,6 +2,7 @@
* SourceGLCanvas.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
+1
View File
@@ -2,6 +2,7 @@
* Util.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
+3 -3
View File
@@ -1,8 +1,8 @@
CONFIG += qt debug
TEMPLATE = app
HEADERS = MainWindow.h Util.h MapperGLCanvas.h SourceGLCanvas.h DestinationGLCanvas.h Mapper.h Mapping.h Shape.h Paint.h MappingManager.h
SOURCES = main.cpp MainWindow.cpp Util.cpp Mapper.cpp MapperGLCanvas.cpp SourceGLCanvas.cpp DestinationGLCanvas.cpp MappingManager.cpp
QT += gui opengl
HEADERS = MainWindow.h Util.h MapperGLCanvas.h SourceGLCanvas.h DestinationGLCanvas.h Mapper.h Mapping.h Shape.h Paint.h MappingManager.h ProjectWriter.h
SOURCES = main.cpp MainWindow.cpp Util.cpp Mapper.cpp MapperGLCanvas.cpp SourceGLCanvas.cpp DestinationGLCanvas.cpp MappingManager.cpp ProjectWriter.cpp
QT += gui opengl xml
RESOURCES = libremapping.qrc
docs.depends = $(HEADERS) $(SOURCES)