/***************************************************************************
 *   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 "mindmapspellchecker.h"
#include "qtextdocumentspellchecker.h"
#include "qaspell.h"
#include "blockpropertymodel.h"
#include "mindmap.h"

#include <QStringListModel>

MindMapSpellChecker::MindMapSpellChecker( QObject *parent )
		: QObject( parent )
{
	m_speller = new QASpell();
	m_speller->configureSpellchecker("en_US");
	m_suggestions = new QStringListModel(this);
}


MindMapSpellChecker::~MindMapSpellChecker()
{
}

MindMapSpellChecker::MindMapSpellChecker( MindMap * map, QObject * parent )
		: QObject( parent ), m_map(map)
{
	m_speller = new QASpell();
	m_speller->configureSpellchecker("en_US");
	m_suggestions = new QStringListModel(this);
}

void MindMapSpellChecker::updateSuggestions( const QString & word )
{
		m_suggestions->setStringList( m_speller->getSuggestions( word ) );
}

QAbstractItemModel * MindMapSpellChecker::suggestionsModel( ) const
{
	return m_suggestions;
}

void MindMapSpellChecker::replaceStringInProperty( Block *block,  const QString & property, const QString & original, const QString & replacement, bool replaceAll )
{
	BlockPropertyModel model( m_map, m_map->commandStack());
	model.setBlock( block );

	QString sourceString = model.data( model.indexForProperty(property) ).toString();

	if( replaceAll )
	{
		sourceString.replace( original, replacement, Qt::CaseSensitive );
	}
	else
	{
		int start = sourceString.indexOf( original );
		sourceString.replace( start, original.size(), replacement );
	}

	model.setData( model.indexForProperty(property), sourceString );

}



