/*
* 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
namespace mmp {
ProjectReader::ProjectReader(MainWindow *window) : _window(window)
{
}
bool ProjectReader::isValidVersion(const QString& versionString)
{
QRegularExpression re(MM::SUPPORTED_FILE_VERSIONS);
QRegularExpressionMatch match = re.match(versionString);
return match.hasMatch();
}
bool ProjectReader::readFile(QIODevice *device)
{
QString errorStr;
int errorLine;
int errorColumn;
QDomDocument doc;
if (!doc.setContent(device, false, &errorStr, &errorLine, &errorColumn)) {
std::cerr << "Error: Parse error at line " << errorLine << ", "
<< "column " << errorColumn << ": "
<< qPrintable(errorStr) << std::endl;
return false;
}
QDomElement root = doc.documentElement();
QString projectVersion = root.attribute("version");
// The handling of the version number will get fancier as we go.
if (root.tagName() != "project") {
_xml.raiseError(QObject::tr("The contents of this file does not look like a MapMap project."));
return false;
} else if (! this->isValidVersion(projectVersion)) {
_xml.raiseError(
QObject::tr("The version of MapMap %1 used to save this file is not readable by this MapMap version %2.").arg(
projectVersion, MM::VERSION));
return false;
}
parseProject(root);
return (! _xml.hasError() );
}
QString ProjectReader::errorString() const
{
return QObject::tr("%1\nLine %2, column %3")
.arg(_xml.errorString())
.arg(_xml.lineNumber())
.arg(_xml.columnNumber());
}
void ProjectReader::parseProject(const QDomElement& project)
{
// TODO: this is dangerous if we have
MappingManager& manager = _window->getMappingManager();
manager.clearAll();
QDomElement paints = project.firstChildElement(ProjectLabels::PAINTS);
QDomElement mappings = project.firstChildElement(ProjectLabels::MAPPINGS);
// Parse paints.
QDomNode paintNode = paints.firstChild();
while (!paintNode.isNull())
{
Paint::ptr paint = parsePaint(paintNode.toElement());
if (paint.isNull())
{
qDebug() << "Problem creating paint." << endl;
}
else
{
manager.addPaint(paint);
_window->addPaintItem(paint->getId(), paint->getIcon(), paint->getName());
// Locate media file if not found
if (paint->getSourceType() == Paint::SourceType::Video)
{
QSharedPointer