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

#include <QTextDocument>
#include <QTextCursor>
#include <QStringListModel>

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

QTextDocumentSpellChecker::QTextDocumentSpellChecker( QTextDocument * document, QObject * parent )
: QObject(parent), m_watchedDocument(document)
{
	m_speller = new QASpell();
	m_speller->configureSpellchecker("en_US");
	m_suggestions = new QStringListModel(this);
}


QTextDocumentSpellChecker::~QTextDocumentSpellChecker()
{
	delete m_speller;
}

QTextDocument* QTextDocumentSpellChecker::watchedDocument() const
{
	return m_watchedDocument;
}


void QTextDocumentSpellChecker::setWatchedDocument(QTextDocument* theValue)
{
	m_watchedDocument = theValue;
}

void QTextDocumentSpellChecker::replaceString( QTextCursor * cursor, const QString & replacement )
{
	if( cursor->isNull() )
		return;

	cursor->select(QTextCursor::WordUnderCursor);
	cursor->removeSelectedText();
	cursor->insertText(replacement);
	cursor->movePosition( QTextCursor::NextWord );
}

bool QTextDocumentSpellChecker::replaceStringInDocument( const QTextCursor & cursor, const QString & original, const QString & replacement, bool replaceAll )
{
	QTextCursor word = cursor;
	word.movePosition(QTextCursor::StartOfWord);
	word = m_watchedDocument->find(original, word, QTextDocument::FindWholeWords|QTextDocument::FindCaseSensitively);

	if( word.isNull() )
	{
		return false;
	}

	if( replaceAll )
	{
		while( !word.isNull() )
		{
			replaceString( &word, replacement );
			word = m_watchedDocument->find(original, word, QTextDocument::FindWholeWords|QTextDocument::FindCaseSensitively);
		}
	}
	else
		replaceString( &word, replacement );

	return true;
}

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

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

QTextCursor QTextDocumentSpellChecker::findNextMisspelling( const QTextCursor & cursor ) const
{
	QTextCursor newCursor = cursor;

	while( !newCursor.atEnd() )
	{
		newCursor.select(QTextCursor::WordUnderCursor);
		// TODO: Check if the current letter is a hyphen.
		if( !m_speller->checkWord( newCursor.selectedText() ) )
		{
			newCursor.movePosition(QTextCursor::StartOfWord);
			return newCursor;
		}
		newCursor.movePosition(QTextCursor::NextWord);
	}
	return newCursor;
}



