From dce8f0b35d45ad6c1fcf1087a82bc58422c5fdaa Mon Sep 17 00:00:00 2001 From: Alexandre Quessy Date: Mon, 9 Dec 2013 15:20:33 -0500 Subject: [PATCH] Saves an XML file. --- Common.cpp | 1 + DestinationGLCanvas.cpp | 1 + MainWindow.cpp | 33 ++++++++----- Mapper.cpp | 1 + MapperGLCanvas.cpp | 1 + MappingManager.cpp | 1 + ProjectWriter.cpp | 103 ++++++++++++++++++++++++++++++++++++++++ ProjectWriter.h | 37 +++++++++++++++ Shape.h | 5 ++ SourceGLCanvas.cpp | 1 + Util.cpp | 1 + libremapping.pro | 6 +-- 12 files changed, 177 insertions(+), 14 deletions(-) create mode 100644 ProjectWriter.cpp create mode 100644 ProjectWriter.h diff --git a/Common.cpp b/Common.cpp index 5a7da35..fd13f09 100644 --- a/Common.cpp +++ b/Common.cpp @@ -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 diff --git a/DestinationGLCanvas.cpp b/DestinationGLCanvas.cpp index d9a5660..d240e86 100644 --- a/DestinationGLCanvas.cpp +++ b/DestinationGLCanvas.cpp @@ -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 diff --git a/MainWindow.cpp b/MainWindow.cpp index 771cf7d..2ddec44 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -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) diff --git a/Mapper.cpp b/Mapper.cpp index a203f87..4591c4f 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -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 diff --git a/MapperGLCanvas.cpp b/MapperGLCanvas.cpp index 9717451..363f8bc 100644 --- a/MapperGLCanvas.cpp +++ b/MapperGLCanvas.cpp @@ -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 diff --git a/MappingManager.cpp b/MappingManager.cpp index 48d7898..cf6332d 100644 --- a/MappingManager.cpp +++ b/MappingManager.cpp @@ -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 diff --git a/ProjectWriter.cpp b/ProjectWriter.cpp new file mode 100644 index 0000000..3cbc69e --- /dev/null +++ b/ProjectWriter.cpp @@ -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 . + */ +#include "ProjectWriter.h" +#include + +ProjectWriter::ProjectWriter(MappingManager *manager) : + _manager(manager) +{ + _xml.setAutoFormatting(true); +} + +bool ProjectWriter::writeFile(QIODevice *device) +{ + _xml.setDevice(device); + + _xml.writeStartDocument(); + _xml.writeDTD(""); + _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 +} + diff --git a/ProjectWriter.h b/ProjectWriter.h new file mode 100644 index 0000000..05ce4cc --- /dev/null +++ b/ProjectWriter.h @@ -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 . + */ + +#include +#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; +}; diff --git a/Shape.h b/Shape.h index e0d9770..259569a 100644 --- a/Shape.h +++ b/Shape.h @@ -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_ */ diff --git a/SourceGLCanvas.cpp b/SourceGLCanvas.cpp index b2bb219..ff4ec78 100644 --- a/SourceGLCanvas.cpp +++ b/SourceGLCanvas.cpp @@ -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 diff --git a/Util.cpp b/Util.cpp index 0e84cd2..78126e3 100644 --- a/Util.cpp +++ b/Util.cpp @@ -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 diff --git a/libremapping.pro b/libremapping.pro index 9a71e25..354cff5 100644 --- a/libremapping.pro +++ b/libremapping.pro @@ -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)