/***************************************************************************
 *   Copyright (C) 2005 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 "evasmileypopup.h"
#include "evaresource.h"
#include "evatable.h"

#include <qlabel.h>
#include <qmovie.h>

EvaSmileyPopup::EvaSmileyPopup(QWidget* parent, const char* name, WFlags fl)
    : QWidget(parent,name,fl)
{
	if ( !name )
		setName( "SmileyPopup" );

	facesTable = new EvaTable( this, "facesTable" );
	facesTable->setReadOnly(true);
	facesTable->setFrameShape( QTable::Box );
	facesTable->setFrameShadow( QTable::Raised );
	resize( QSize(382, 183).expandedTo(minimumSizeHint()) );
	clearWState( WState_Polished );    
	
	QHeader *vheader = facesTable->verticalHeader();
	QHeader *hheader = facesTable->horizontalHeader();
	vheader->hide();
	hheader->hide();
	facesTable->setLeftMargin(0);
	facesTable->setTopMargin(0);
	facesTable->setMidLineWidth(1);
	facesTable->setLineWidth(1);
	//facesTable->setShowGrid( false );
	facesTable->setSelectionMode(QTable::Single);
	facesTable->setHScrollBarMode(QScrollView::AlwaysOff);
	facesTable->setVScrollBarMode(QScrollView::Auto);
	//facesTable->setPaletteBackgroundColor(Qt::lightGray);
	
	facesTable->setNumRows(7);
	facesTable->setNumCols(15);
}

EvaSmileyPopup::~EvaSmileyPopup()
{
	delete facesTable;
}

void EvaSmileyPopup::setupSmiley(EvaImageResource *images)
{
	for(int row=0; row<7; row++){
		
		for(int col=0; col<15; col++){
			if(row == 6 && col == 6) {
				col = 14;
				break;
			};
			QLabel *smiley = new QLabel(facesTable);
			const QMovie *mov = images->getSmiley(row * 15 + col);
			if(mov) smiley->setMovie(*mov);
			facesTable->setCellWidget(row, col, smiley);
			facesTable->setColumnWidth(col, 25);
			facesTable->setRowStretchable ( row, false );
		}
		facesTable->setRowHeight(row, 25);
		
	}	
	facesTable->setGeometry(1,1,26*15-9, 26*7);

	QObject::connect( facesTable, SIGNAL( mouseReleased(QMouseEvent*)), SLOT(mouseReleased(QMouseEvent*)) );
}

void EvaSmileyPopup::mouseReleased(QMouseEvent *e)
{
	if(e->button() == Qt::RightButton)
		this->hide();
	int row = facesTable->rowAt(e->y());
	int col = facesTable->columnAt(e->x());
	if(row == 6 && col > 5) return;
	
	if(e->button() == Qt::LeftButton){
		emit smileySelected((Q_UINT8)(row * 15 + col));
		if( ! ((e->state() & Qt::ControlButton) == Qt::ControlButton) )
			this->hide();
	}	
}



