//****************************************************************************
//Copyright (C) 2005-2006 Beijing BlueDJ Technology Co.,Ltd. All rights reserved.

//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 (in the file LICENSE.GPL); if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

//Please visit http://www.bluedj.com for more infomation about us.
//Contact us at ggwizard@gmail.com or darkdong@gmail.com.
//****************************************************************************/

#include "MJSelect.h"
#include "mjrule.h"

unsigned char g_chMJImageCardOrder[] = {
    MAHJONG_CARD_WAN|1,MAHJONG_CARD_WAN|2,MAHJONG_CARD_WAN|3,
    MAHJONG_CARD_WAN|4,MAHJONG_CARD_WAN|5,MAHJONG_CARD_WAN|6,
    MAHJONG_CARD_WAN|7,MAHJONG_CARD_WAN|8,MAHJONG_CARD_WAN|9,
    
    MAHJONG_CARD_TIAO|1,MAHJONG_CARD_TIAO|2,MAHJONG_CARD_TIAO|3,
    MAHJONG_CARD_TIAO|4,MAHJONG_CARD_TIAO|5,MAHJONG_CARD_TIAO|6,
    MAHJONG_CARD_TIAO|7,MAHJONG_CARD_TIAO|8,MAHJONG_CARD_TIAO|9,
    
    MAHJONG_CARD_TONG|1,MAHJONG_CARD_TONG|2,MAHJONG_CARD_TONG|3,
    MAHJONG_CARD_TONG|4,MAHJONG_CARD_TONG|5,MAHJONG_CARD_TONG|6,
    MAHJONG_CARD_TONG|7,MAHJONG_CARD_TONG|8,MAHJONG_CARD_TONG|9,
    
    MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_DONG,MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_NAN,
    MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_XI,MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_BEI,
    
    MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_ZHONG,MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_FA,
    MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_BAI,
    
    MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_PLUM,MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_ORCHID,
    MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_BAMBOO,MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_MUM,
    
    MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_SPRING,MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_SUMMER,
    MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_AUTUMN,MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_WINTER,
    0
};

QPixmap CreateCombinedPixmap(unsigned char *buf,unsigned char len)
{
	int i;
	QList<unsigned char> list;
	for (i = 0;i< len; i++)
		list<< buf[i];
	qSort( list );

    QPixmap pixcards(":/BaseRes/image/mahjong/mjcardsb.png");

	int w	= pixcards.width()/44;
	int h	= pixcards.height();
	int offset	= 0;

	QPixmap pixs(len*w,h);
	pixs.fill();
	QPainter painter(&pixs);

	for( i = 0;i< list.size(); i++) {
		unsigned char chCardNumber = list.at(i) & 0x3F;
		int j = 0;
		while(g_chMJImageCardOrder[j] != 0)
		{
			if(g_chMJImageCardOrder[j] == chCardNumber) {
				//painter.begin(&pixs);
				painter.drawPixmap(offset,0,pixcards,j*w,0,w,-1);
				//painter.end();
				offset+= w;
				break;
			}
			j++;
		}
	}
    return pixs;
}

MJSelect::MJSelect( QWidget * parent, Qt::WFlags f )
	:QDialog( parent, f )
{
	setupUi(this);
	
	m_bg	= new QButtonGroup(this);
	
	/*
	unsigned char buf[2] = {0x23,0x24};
	QPixmap	pix	= CreateCombinedPixmap( buf, 2 );
	
	QRadioButton	*r1	= new QRadioButton( "radio1",this );
	r1->setIconSize( pix.size() );
	r1->setIcon(pix);
	bg->addButton( r1 );
	vboxLayout1->addWidget( r1 );
	
	QRadioButton	*r2	= new QRadioButton( "radio2",this );
	bg->addButton( r2 );
	vboxLayout1->addWidget( r2 );*/
}
MJSelect::~MJSelect()
{
}

void MJSelect::addRow( unsigned char *buf, unsigned char len )
{
	QRadioButton *row	= new QRadioButton(this);
	QPixmap	pix	= CreateCombinedPixmap(buf,len);
	row->setIconSize( pix.size() );
	row->setIcon(pix);
	row->setChecked(true);
	m_bg->addButton( row );
	vboxLayout1->addWidget( row );
	
}
int MJSelect::indexOfSelected() const
{
	QAbstractButton *checkedButton	= m_bg->checkedButton();
	
	QList<QAbstractButton *> list	= m_bg->buttons();
	for ( int i = 0; i < list.size(); i++ ) {
		if ( checkedButton == list.at(i) )
			return i;
	}
	return -1;
}
