/***************************************************************************
 *   Copyright (C) 2006-2008 by Ian Reinhart Geiser                        *
 *   geiseri@yahoo.com                                                     *
 *                                                                         *
 *   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 2 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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "freemindimporter.h"
#include "block.h"
#include "mindmap.h"

#include <QGraphicsScene>

FreeMindImporter::FreeMindImporter( MindMap *map ) : m_mindmap(map)
{
}


FreeMindImporter::~FreeMindImporter()
{
}

bool FreeMindImporter::import( const QDomDocument & xml )
{

	QDomElement mindmapElement = xml.firstChildElement("map");
	QDomElement nodeElement = mindmapElement.firstChildElement("node");
	Block *rootNode = blockFromNode( nodeElement, 0, m_mindmap->scene() );
	if( rootNode )
		m_mindmap->setRoot( rootNode );
	else
		return false;
	rootNode->setMindmap(m_mindmap);
	rootNode->setShapeType( Block::Ellipse );

	return importChildren( rootNode, nodeElement.firstChildElement("node") );
}

bool FreeMindImporter::importChildren( Block * block, const QDomElement & childElement )
{
	QDomElement mindmapElement = childElement;

	while( !mindmapElement.isNull() )
	{
		Block *childNode = blockFromNode( mindmapElement, block, m_mindmap->scene() );
		if( childNode == 0)
			return false;
		childNode->setMindmap(m_mindmap);

		if( !importChildren( childNode, mindmapElement.firstChildElement("node")  ) )
			return false;

		mindmapElement = mindmapElement.nextSiblingElement("node");
	}
	return true;
}

Block *FreeMindImporter::blockFromNode( const QDomElement &nodeElement, Block *parent, QGraphicsScene *scene )
{
	if( nodeElement.tagName() != "node" )
		return 0;

	Block *block = new Block();
	block->setTitle( nodeElement.attribute("TEXT") );
	if( nodeElement.hasAttribute("BACKGROUND_COLOR") )
		block->setShapeColor( QColor( nodeElement.attribute("BACKGROUND_COLOR") ) );
	if( nodeElement.attribute("STYLE") == QLatin1String("bubble") )
		block->setShapeType( Block::Ellipse );
	else if(  nodeElement.attribute("STYLE") == QLatin1String("fork") )
		block->setShapeType( Block::Path );
	else
		block->setShapeType( Block::Rectangle );

	if( scene )
		scene->addItem( block );

	if( parent )
		block->setParentNode( parent );
	block->updateContents();

	return block;
}


