diff --git a/MainWindow.cpp b/MainWindow.cpp index 2ddec44..90b1cfd 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -20,6 +20,7 @@ #include "MainWindow.h" #include "ProjectWriter.h" +#include "ProjectReader.h" MainWindow::MainWindow() { @@ -348,12 +349,12 @@ void MainWindow::createActions() importAction = new QAction(tr("&Import media source..."), this); importAction->setIcon(QIcon(":/images/document-import-2.png")); - importAction->setShortcut(QKeySequence::Open); + importAction->setShortcut(QKeySequence::Italic); // ctrl-I importAction->setStatusTip(tr("Import a media source file")); connect(importAction, SIGNAL(triggered()), this, SLOT(import())); exitAction = new QAction(tr("E&xit"), this); - exitAction->setShortcut(tr("Ctrl+Q")); + exitAction->setShortcut(QKeySequence::Quit); exitAction->setStatusTip(tr("Exit the application")); connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); @@ -525,15 +526,31 @@ bool MainWindow::okToContinue() bool MainWindow::loadFile(const QString &fileName) { - // TODO: Try to read file. -// if (!spreadsheet->readFile(fileName)) -// { -// statusBar()->showMessage(tr("Loading canceled"), 2000); -// return false; -// } + QFile file(fileName); - setCurrentFile(fileName); - statusBar()->showMessage(tr("File loaded"), 2000); + if (! file.open(QFile::ReadOnly | QFile::Text)) + { + QMessageBox::warning(this, tr("Error reading mapping project file"), + tr("Cannot read file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())); + return false; + } + + mappingManager->clearProject(); // FIXME: clearProject is not implemented! + ProjectReader reader(mappingManager); + if (! reader.readFile(&file)) + { + QMessageBox::warning(this, tr("Error reading mapping project file"), + tr("Parse error in file %1:\n\n%2") + .arg(fileName) + .arg(reader.errorString())); + } + else + { + statusBar()->showMessage(tr("File loaded"), 2000); + setCurrentFile(fileName); + } return true; } diff --git a/MappingManager.cpp b/MappingManager.cpp index e95f2cd..22fd8ff 100644 --- a/MappingManager.cpp +++ b/MappingManager.cpp @@ -19,6 +19,7 @@ */ #include "MappingManager.h" +#include MappingManager::MappingManager() { @@ -78,3 +79,9 @@ int MappingManager::addMapping(Mapping::ptr mapping) //bool MappingManager::removeMapping(Mapping::ptr mapping) //{ //} + +void MappingManager::clearProject() +{ + std::cout << "TODO: implement MappingManager::clearProject() !!!" << std::endl; + // We also need to update the GUI accordingly! +} diff --git a/MappingManager.h b/MappingManager.h index f3d38e2..afdaa3e 100644 --- a/MappingManager.h +++ b/MappingManager.h @@ -56,6 +56,7 @@ public: // bool removeMapping(Mapping::ptr mapping); int nMappings() const { return mappings.size(); } Mapping::ptr getMapping(int i) { return mappings[i]; } + void clearProject(); }; #endif /* MAPPINGMANAGER_H_ */ diff --git a/ProjectReader.cpp b/ProjectReader.cpp new file mode 100644 index 0000000..31e5881 --- /dev/null +++ b/ProjectReader.cpp @@ -0,0 +1,100 @@ +/* + * ProjectReader.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 "ProjectReader.h" +#include +#include +#include + +ProjectReader::ProjectReader(MappingManager *manager) : + _manager(manager) +{ +} + +bool ProjectReader::readFile(QIODevice *device) +{ + _xml.setDevice(device); + if (_xml.readNextStartElement()) + { + if (_xml.name() == "project" && _xml.attributes().value("version") == "1.0") + readProject(); + else + _xml.raiseError(QObject::tr("The file is not a libremapping version 1.0 file.")); + } + return ! _xml.error(); +} + +QString ProjectReader::errorString() const +{ + return QObject::tr("%1\nLine %2, column %3") + .arg(_xml.errorString()) + .arg(_xml.lineNumber()) + .arg(_xml.columnNumber()); +} + +void ProjectReader::readProject() +{ + // FIXME: avoid asserts + Q_ASSERT(_xml.isStartElement() && _xml.name() == "project"); + + while (_xml.readNextStartElement() && ! _xml.atEnd()) + { + if (_xml.name() == "paint") + readPaint(); //NULL); + if (_xml.name() == "mapping") + readMapping(); // NULL); + else + { + std::cout << "skip element " << _xml.name().string() << std::endl; + _xml.skipCurrentElement(); + } + } +} + +void ProjectReader::readMapping() +{ + // FIXME: we assume an Image mapping + Q_ASSERT(_xml.isStartElement() && _xml.name() == "mapping"); + const QString *paintIdAttrValue; + QXmlStreamAttributes attributes = _xml.attributes(); + + if (attributes.hasAttribute("", "paint_id")) + paintIdAttrValue = attributes.value("", "paint_id").string(); + + std::cout << "Found mapping " << "with paint ID " << paintIdAttrValue << std::endl; + + //QString title = _xml.readElementText(); + //item->setText(0, title); +} + +void ProjectReader::readPaint() +{ + // FIXME: we assume an Image mapping + Q_ASSERT(_xml.isStartElement() && _xml.name() == "paint"); + const QString *paintIdAttrValue; + const QString *typeAttrValue; + QXmlStreamAttributes attributes = _xml.attributes(); + + if (attributes.hasAttribute("", "name")) + paintIdAttrValue = attributes.value("", "name").string(); + if (attributes.hasAttribute("", "type")) + typeAttrValue = attributes.value("", "type").string(); + + std::cout << "Found " << typeAttrValue << " paint " << paintIdAttrValue << std::endl; +} diff --git a/ProjectReader.h b/ProjectReader.h new file mode 100644 index 0000000..2627830 --- /dev/null +++ b/ProjectReader.h @@ -0,0 +1,40 @@ +/* + * 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 ProjectReader +{ +public: + ProjectReader (MappingManager *manager); + bool readFile (QIODevice *device); + QString errorString() const; + +private: + void readProject(); + void readPaint(); //Paint *item); + void readMapping(); //Mapping *item); + + QXmlStreamReader _xml; + MappingManager *_manager; +}; diff --git a/ProjectWriter.cpp b/ProjectWriter.cpp index 5c53e99..881e970 100644 --- a/ProjectWriter.cpp +++ b/ProjectWriter.cpp @@ -1,5 +1,5 @@ /* - * MainWindow.cpp + * ProjecWriter.cpp * * (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com * (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net diff --git a/ProjectWriter.h b/ProjectWriter.h index fdfadfe..1f5d99b 100644 --- a/ProjectWriter.h +++ b/ProjectWriter.h @@ -1,5 +1,5 @@ /* - * MainWindow.cpp + * ProjectWriter.h * * (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com * (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net diff --git a/libremapping.pro b/libremapping.pro index 995e7c6..ff844d2 100644 --- a/libremapping.pro +++ b/libremapping.pro @@ -1,7 +1,7 @@ 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 ProjectWriter.h NameAllocator.h -SOURCES = main.cpp MainWindow.cpp Util.cpp Mapper.cpp MapperGLCanvas.cpp SourceGLCanvas.cpp DestinationGLCanvas.cpp MappingManager.cpp ProjectWriter.cpp NameAllocator.cpp +HEADERS = MainWindow.h Util.h MapperGLCanvas.h SourceGLCanvas.h DestinationGLCanvas.h Mapper.h Mapping.h Shape.h Paint.h MappingManager.h ProjectWriter.h NameAllocator.h ProjectReader.h +SOURCES = main.cpp MainWindow.cpp Util.cpp Mapper.cpp MapperGLCanvas.cpp SourceGLCanvas.cpp DestinationGLCanvas.cpp MappingManager.cpp ProjectWriter.cpp NameAllocator.cpp ProjectReader.cpp QT += gui opengl xml RESOURCES = libremapping.qrc