/***************************************************************************
 *   Copyright (C) 2004 by yunfan                                          *
 *   yunfan_zg@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 "evatipwindow.h"
#include "evaresource.h"
#include <qlabel.h>
#include <klocale.h>
#include <kapplication.h>
#include <qtimer.h>
#include <qpixmap.h>

EvaTipWindow::EvaTipWindow(EvaImageResource *res, const QString nick, const int id, const char face, const QString &message)
    : EvaTipUIBase(0, 0, WStyle_Customize | WStyle_NoBorder | WStyle_StaysOnTop | WStyle_Tool | WX11BypassWM | WDestructiveClose)
{
	images = res;
	qqNum = id;
	lblNick->setText(nick);
	
	QString msg = message;
	if(msg.stripWhiteSpace().length() > 30 ){
		msg = message.left(27) + i18n("...");
	}
	lblMessage->setText(msg);
	
	QPixmap *pic = images->getQQShow(qqNum);
	lblPixmap->setScaledContents(true);
	if(!pic){
		pic = images->getFace(images->getFaceFileIndex((uchar)face));
		lblPixmap->setScaledContents(false);
	}
	
	lblPixmap->setPixmap(*pic);
	scr = KApplication::desktop()->screenGeometry();
	setGeometry(scr.right()- width(), scr.bottom(), width(), height());
		
	timelast=0;
	timer = new QTimer(this);
	QObject::connect(timer, SIGNAL(timeout()), this,SLOT(slotTimeout()));
	timer->start(70, false);	
	show();
}

EvaTipWindow::~EvaTipWindow()
{}

void EvaTipWindow::mousePressEvent(QMouseEvent *e)
{
	if(timer->isActive())
		timer->stop();
	if( (e->button() & LeftButton)==LeftButton){
		emit requestChat(qqNum);
	}
	close();
}

void EvaTipWindow::slotTimeout()
{
	if(timelast > 100) {
		timer->stop();
		close();
	}else
		timelast ++;
	if(y() > (scr.height()- height()-35) ){
		move(scr.right()- width(), scr.height()-(height()/20)*timelast);
	};
}


