//****************************************************************************
//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 <QtGui>

#include "DJTreeItemDelegate.h"

DJTreeItemDelegate::DJTreeItemDelegate( quint32 selfUserId, QObject *parent, int linearDirectionType )
    : QItemDelegate(parent),m_selfUserId(selfUserId),m_linearDirectionType(linearDirectionType)
{
    DJPosColor	pc1( 0, QColor(224,238,255) );
    DJPosColor	pc2( 0.5, QColor(144,194,255) );
    DJPosColor	pc3( 1, QColor(224,238,255) );
    m_selfColor << pc1 << pc2 << pc3;
    
    DJPosColor	pc5( 0, Qt::white );
    DJPosColor	pc6( 1, QColor(225,225,225) );
    m_otherColor << pc5 << pc6;
}
DJTreeItemDelegate::DJTreeItemDelegate( quint32 selfUserId, const QList<DJPosColor>& selfColor, const QList<DJPosColor>& otherColor, QObject *parent, int linearDirectionType )
    : QItemDelegate(parent),m_selfUserId(selfUserId),m_selfColor(selfColor),m_otherColor(otherColor),m_linearDirectionType(linearDirectionType)
{
}
void DJTreeItemDelegate::setSelfUserId( quint32 selfUserId )
{
	m_selfUserId	= selfUserId;
}
void DJTreeItemDelegate::setSelfColor( const QList<DJPosColor>& pc )
{
	m_selfColor	= pc;
}
void DJTreeItemDelegate::setOtherColor( const QList<DJPosColor>& pc )
{
	m_otherColor	= pc;
}
void DJTreeItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
	QPoint	start,final;
	switch( m_linearDirectionType ) {
	case DirectionTypeTopLeft2BottomRight:
		start	= option.rect.topLeft();
		final	= option.rect.bottomRight();
		break;
	case DirectionTypeTopRight2BottomLeft:
		start	= option.rect.topRight();
		final	= option.rect.bottomLeft();
		break;
	case DirectionTypeTopLeft2BottomLeft:
	default:
		start	= option.rect.topLeft();
		final	= option.rect.bottomLeft();
		break;
	}
	QLinearGradient linearGradient( start, final );
	
	QTreeWidgetItem*	item	= static_cast<QTreeWidgetItem*>(index.internalPointer());
	if ( QTreeWidgetItem::UserType == item->type() ) {
		foreach ( DJPosColor posColor, m_selfColor ) {
			qreal	pos		= posColor.pos();
			const QColor& color	= posColor.color();
			linearGradient.setColorAt( pos, color );
		}
		painter->fillRect(option.rect,linearGradient);
	}/*else {
		for ( int i = 0; i < m_otherColor.size(); i++ ) {
			qreal	pos		= m_otherColor.at(i).pos;
			QColor	color	= m_otherColor.at(i).color;
			linearGradient.setColorAt( pos, color );
		}
	}*/
	
	QItemDelegate::paint(painter,option,index);
}
