/***************************************************************************
 *   Copyright (C) 2008 by DDD                                          *
 *   dedodong@163.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 <QLabel>
#include <QtGui>

#include "fxmsgwindow.h" 
#include "fxmytabwidget.h" 
#include "fxInputFace.h"

static FACES_INFO _faces_info[] = {

	{"&gt;:)",    MyRect(80,  229,  118, 259), QObject::tr("naughty")}, //淘气的
	{"o:)", MyRect(280, 191,  318, 227), QObject::tr("angel")}, //天使
	{"*-:)", MyRect(160, 153,  198, 189), QObject::tr("idea")}, //想法

//	{":)",   MyRect(1,   2, 38,  39)},
	{":-d",  MyRect(40,  2, 78,  39), QObject::tr("yuk")}, //大笑,
	{";)",   MyRect(80,  2, 118, 39), QObject::tr("blink")}, //眨眼,
	{":-o",  MyRect(120, 2, 158, 39), QObject::tr("surprise")}, //惊讶,
	{":-p",  MyRect(160, 2, 198, 39), QObject::tr("spit_tongue_smile")}, //吐舌笑脸,
	{"(h)",  MyRect(200, 2, 238, 39), QObject::tr("fiery_smile")}, //热烈笑脸,,
	{":-@",  MyRect(240, 2, 278, 39), QObject::tr("Angry")}, //生气
	{":(",   MyRect(280, 2, 318, 39), QObject::tr("sadness")}, //悲伤,


	{":'(",     MyRect(1,   40,  39,  77), QObject::tr("blubber")}, //哭泣,
	{":\"&gt;", MyRect(40,  40,  78,  77), QObject::tr("embarrass")}, //尴尬
	{"^o)",     MyRect(80,  40,  118, 77), QObject::tr("Ironically")}, //讽刺,
	//{":&amp;",  MyRect(120, 40,  158, 77)},
	{":&",	MyRect(120, 40,  158, 77), QObject::tr("Sick")}, //生病,
	{"8o|",	MyRect(160, 40,  198, 77), QObject::tr("Yaoyaqiechi")}, //咬牙切齿
	{"|-)",	MyRect(200, 40,  238, 77), QObject::tr("sleepy")}, //困
	{":-#",	MyRect(240, 40,  278, 77), QObject::tr("Privacy")}, //保密
	{"8-)",			MyRect(280, 40,  318, 77), QObject::tr("MomentisEyess")}, //转动的眼睛

	{"(s)",		MyRect(1,   78,  39,  113), QObject::tr("sleepyMoon")}, //沉睡的月亮
	{"(st)",	MyRect(40,  78,  78,  113), QObject::tr("rain")}, //下雨
	{"(o)",		MyRect(80,  78,  118, 113), QObject::tr("clock")}, //时钟
	{"(l)",		MyRect(120, 78,  158, 113), QObject::tr("hearts")}, //红心
	{"(u)",		MyRect(160, 78,  198, 113), QObject::tr("crackedhearts")}, //破碎的心
	{"(@)",		MyRect(200, 78,  238, 113), QObject::tr("catfaces")}, //猫脸
	{"(&)",		MyRect(240, 78,  278, 113), QObject::tr("dogfaces")}, //狗脸
	{"(sn)",	MyRect(280, 78,  318, 113), QObject::tr("snail")}, //蜗牛


	{"(*)",MyRect(1,   115,  39,  151), QObject::tr("star")}, //星星
	{"(#)",MyRect(40,  115,  78,  151), QObject::tr("sun")}, //太阳
	{"(r)",MyRect(80,  115,  118, 151), QObject::tr("rainbow")}, //彩虹
	{"(})",MyRect(120, 115,  158, 151), QObject::tr("leftembrace")}, //左拥抱,
	{"({)",MyRect(160, 115,  198, 151), QObject::tr("rightembrace")}, //右拥抱,
	{"(k)",MyRect(200, 115,  238, 151), QObject::tr("lip")}, //红唇
	{"(f)", MyRect(240, 115,  278, 151), QObject::tr("rose")}, //红玫瑰
	{"(w)", MyRect(280, 115,  318, 151), QObject::tr("emarcidrose")}, //凋谢的玫瑰


	{"(g)",  MyRect(1,   153,  39,  189), QObject::tr("giftbox")}, //礼品盒
	{"(^)",  MyRect(40,  153,  78,  189), QObject::tr("Birthdaycake")}, //生日蛋糕
	{"-8",   MyRect(80,  153,  118, 189), QObject::tr("music")}, //音乐
	{"(i)",  MyRect(120, 153,  158, 189), QObject::tr("bulb")}, //灯泡,
//	{"*-:)", MyRect(160, 153,  198, 189)},
	{"(c)",  MyRect(200, 153,  238, 189), QObject::tr("coffee")}, //咖啡
	{"(um)", MyRect(240, 153,  278, 189), QObject::tr("bumbershoot")}, //雨伞
	{"b)",	 MyRect(280, 153,  318, 189), QObject::tr("withsunglass")}, //带着太阳镜


	{"(mp)",MyRect(1,   191,  39,  227), QObject::tr("mobiletelephone")}, //手机
	{"(co)",MyRect(40,  191,  78,  227), QObject::tr("computer")}, //计算机
	{":-|", MyRect(80,  191,  118, 227), QObject::tr("despair")}, //失望
	{":-/", MyRect(120, 191,  158, 227), QObject::tr("confused")}, //困惑的
	{":-s", MyRect(160, 191,  198, 227), QObject::tr("afraid")}, //担心
	{")-|", MyRect(200, 191,  238, 227), QObject::tr("drink")}, //饮料
	{"(d)", MyRect(240, 191,  278, 227), QObject::tr("goblet")}, //高脚杯
	//{"o:)", MyRect(280, 191,  318, 227)},


	{":-?",		  MyRect(1,   229,  39,  259), QObject::tr("meditation")}, //沉思
	{"(y)",		  MyRect(40,  229,  78,  259), QObject::tr("Praised")}, //太棒了
	//{"&gt;:)",    MyRect(80,  229,  118, 259)},
	{":-b",		  MyRect(120, 229,  158, 259), QObject::tr("spoony")}, //笨瓜

	{":)",   MyRect(1,   2, 38,  39) , QObject::tr("smile")}, //微笑

};

bool FxInputFace::getFaces(int x, int y, QString &face, QString &str)
{
	int i;
	for (i = 0; i < sizeof(_faces_info)/sizeof(_faces_info[0]); i++) 
		if (_faces_info[i].rect.contains (x, y)) {
			face = _faces_info[i].face;
			str = _faces_info[i].str;
			return true;
		}

		face = "";
		str = "";
		return false;
}

FxInputFace::FxInputFace(QWidget *parent)
    : QDialog(parent)
{
	msgWind = NULL;
}

FxInputFace::~FxInputFace()
{

}

void FxInputFace::setMsgWindow(FxMsgWindow *Wind)
{
	msgWind = Wind;
	// add by iptton. 
	setFocusProxy(Wind);
}

#if MAC_OS
static bool shouldHide = false;
#endif

void FxInputFace::focusOutEvent ( QFocusEvent * event ) 
{
#if MAC_OS
	if (shouldHide)
	{
		shouldHide = false;
		hide();
		return;
	}

	shouldHide = true;
#else
	hide();
#endif
}

void FxInputFace::mouseReleaseEvent ( QMouseEvent * event ) 
{
#if MAC_OS
	shouldHide = false;
#endif
	hide();

	//releaseMouse();

	if (!msgWind)
		return;

	FxMyTabWidget *tabWidget = msgWind->tabWidget;
	if (!tabWidget)
		return;

	AccountTab *accountTab = (AccountTab*)(tabWidget->widget(tabWidget->currentIndex()));
	if (!accountTab)
		return;

	/*
	 QString symbol;
		 QString str;
	QString face = getFaces (event->x(), event->y());
*/

	QString face;
	QString str;
	if (!getFaces (event->x(), event->y(), face, str))
		return;
	/* this will broke the user's edit(because the cursor changed)
	QString msg = accountTab->msgSend->MsgEdit->toPlainText();
	QString newmsg = msg + face;

	accountTab->msgSend->MsgEdit->setText(newmsg);
	accountTab->msgSend->MsgEdit->moveCursor(QTextCursor::End);
	*/
	// try this and use setFocusProxy instead of setFocus()
	accountTab->msgSend->MsgEdit->textCursor().insertText(face);
}


void FxInputFace::mouseMoveEvent(QMouseEvent *event)
{
	QString face;
	QString str;
	if (!getFaces (event->x(), event->y(), face, str))
		return;

	QString text = str + "  " + face;
	QToolTip::showText(event->globalPos(), text, this);
}

