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

#include <QTimer>
#include <QTextCursor>
#include <QMessageBox>
#include <QApplication>
#include <QLineEdit>
#include <QTextDocument>

SpellChecker::SpellChecker(QWidget *parent)
	: QDialog(parent)
{
	setModal(true);
	m_ui = new Ui::SpellChecker;
	m_ui->setupUi(this);
	m_documentChecker = new QTextDocumentSpellChecker(this);
	m_ui->suggestions->setModel( m_documentChecker->suggestionsModel() );
	connect( this, SIGNAL( checkNext()), this, SLOT( next() ));
}

SpellChecker::~SpellChecker()
{
}

void SpellChecker::on_replaceWord_clicked( )
{
	m_current.setPosition(m_index);
	m_documentChecker->replaceStringInDocument(m_current,
	                                           m_currentWord,
	                                           m_ui->newWord->text());
	emit checkNext();
}

void SpellChecker::on_replaceAll_clicked( )
{
	m_current.setPosition(m_index);
	m_documentChecker->replaceStringInDocument(m_current,
	                                           m_currentWord,
	                                           m_ui->newWord->text(),
	                                           true);
	emit checkNext();
}
void SpellChecker::on_ignoreWord_clicked( )
{
	m_current.setPosition(m_index + m_currentWord.size() );
	emit checkNext();
}

void SpellChecker::on_ignoreAll_clicked( )
{
	m_current.setPosition(m_index + m_currentWord.size() );
	emit checkNext();
}

void SpellChecker::on_newWord_editingFinished(  )
{
	if( !m_ui->newWord->text().isEmpty() )
		m_documentChecker->updateSuggestions( m_ui->newWord->text() );
}

void SpellChecker::changeCurrentDocument( QTextDocument * document )
{
	m_documentChecker->setWatchedDocument(document);
}


int SpellChecker::checkDocument( QTextDocument * document, QWidget * parent )
{
    Q_UNUSED(parent);
    SpellChecker dialog(QApplication::activeWindow());
    dialog.changeCurrentDocument(document);
    dialog.m_current = QTextCursor(document);
    QTimer::singleShot(0, &dialog, SLOT(next()));
    return dialog.exec();
}

void SpellChecker::next( )
{
	m_current = m_documentChecker->findNextMisspelling(m_current);
	if( m_current.atEnd() )
	{
		QMessageBox::information(this, tr("Finished"), tr("Finished spellchecking the document."));
		accept();
		return;
	}

	m_current.select(QTextCursor::WordUnderCursor);
	m_documentChecker->updateSuggestions( m_current.selectedText() );
	m_ui->currentWord->setText( m_current.selectedText() );
	m_currentWord = m_current.selectedText();
	m_index = m_current.position();
}

void SpellChecker::on_suggestions_activated( const QModelIndex & index )
{
	m_ui->newWord->setText( index.data().toString() );
}

void SpellChecker::on_suggestions_clicked( const QModelIndex & index )
{
	m_ui->newWord->setText( index.data().toString() );
}

int SpellChecker::checkLineEdit( QLineEdit * edit )
{
	QTextDocument doc( edit->text() );
	SpellChecker dialog( QApplication::activeWindow() );
	dialog.changeCurrentDocument(&doc);
	dialog.m_current = QTextCursor(&doc);
	QTimer::singleShot(0, &dialog, SLOT(next()));
	int status = dialog.exec();
	if( status == QDialog::Accepted )
		edit->setText( doc.toPlainText() );
	return status;
}

