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

#include <QAction>
#include <QMenu>
#include <QContextMenuEvent>
#include <QAbstractTextDocumentLayout>

PlainTextEdit::PlainTextEdit( QWidget *parent )
: QTextEdit(parent)
{
	setAcceptRichText(false);
	m_spellCheckerAction = new QAction("Check Spelling", this );
	connect( m_spellCheckerAction, SIGNAL(triggered( bool )), this, SLOT( invokeSpellChecker()));

	new SpellingHighlighter(document());
}

void PlainTextEdit::focusOutEvent ( QFocusEvent * event )
{
    Q_UNUSED(event);
    emit editingFinished();
}

void PlainTextEdit::invokeSpellChecker( )
{
	SpellChecker::checkDocument( document(), this );
}


QAction* PlainTextEdit::spellCheckerAction() const
{
	return m_spellCheckerAction;
}

void PlainTextEdit::contextMenuEvent( QContextMenuEvent* event )
{

	QASpell speller;
	speller.configureSpellchecker("en_US");

	QTextCursor cursor = textCursor();
	int cursorPosition = document()->documentLayout()->hitTest(event->pos(), Qt::FuzzyHit);
	cursor.setPosition(cursorPosition);
	cursor.select(QTextCursor::WordUnderCursor);
	QString misspelledWord = cursor.selectedText();
	if(!misspelledWord.isEmpty() && !speller.checkWord(misspelledWord))
	{
		QStringList words = speller.getSuggestions(misspelledWord);
		QMenu *menu = new QMenu(tr("Suggestions"));
		menu->addSeparator();
		foreach(QString word, words)
		{
			QAction* action = new QAction(word, menu);
			action->setData(cursorPosition);
			menu->addAction(action);
		}
		connect(menu, SIGNAL(triggered(QAction*)),
		        this, SLOT(onSpellingActionTriggered(QAction*)));
		menu->exec(event->globalPos());
		delete menu;
	}
	else
		QTextEdit::contextMenuEvent(event);

}

void PlainTextEdit::onSpellingActionTriggered(QAction* act)
{
	QTextCursor c = textCursor();
	c.setPosition(act->data().toInt());
	c.select(QTextCursor::WordUnderCursor);
	c.insertText(act->text());
	setTextCursor(c);
}


QSize PlainTextEdit::sizeHint( ) const
{
	QFontMetrics fm( font() );
	QSize size = QTextEdit::sizeHint();

	size.setHeight( fm.height() * 4 );

	return size;
}

QSize PlainTextEdit::minimumSizeHint( ) const
{
	QFontMetrics fm( font() );
	QSize size = QTextEdit::sizeHint();

	size.setHeight( fm.height() * 2 );

	return size;
}
