//
// C++ Implementation: kbibgroupviewgroup
//
// Description:
//
//
// Author: Thach Nguyen <thach.nguyen@rmit.edu.au>, (C) 2006
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "kbibgroupviewitem.h"
#include <qpainter.h>
#include <qpixmap.h>
#include <kstringhandler.h>
#include <qstyle.h>


#include <kapplication.h>
#include <klocale.h>
#include <kglobal.h>
#include <klibloader.h>
#include <kstandarddirs.h>
#include <kcharsets.h>

#include <qregexp.h>
#include <qdir.h>
#include <qcursor.h>
#include <qscrollview.h>

KBibGroupViewItem::KBibGroupViewItem ( QString m_value, QListViewItem *parent )
		: KListViewItem ( parent )
{
	m_list = new BibentryTable(false);
	value = m_value;
	this->setText ( 0, value );
	this->setSelectable ( true );
}

KBibGroupViewItem::KBibGroupViewItem ( QString m_value, QListView *parent )
		: KListViewItem ( parent )
{
	m_list = new BibentryTable(false);
	value = m_value;
	this->setText ( 0, value );
	this->setSelectable ( true );
}


KBibGroupViewItem::~KBibGroupViewItem()
{
	delete m_list;

}

void KBibGroupViewItem::clear(){
	if (m_list)
		delete m_list;
	m_list = new BibentryTable(false);
}

void KBibGroupViewItem::addEntry ( BibEntry *entry )
{	
	m_list->new_entry(entry);
	QString st;
	st = st.setNum ( m_list->size() );
	QString stcnt = value;
	stcnt.append ( " (" );
	stcnt.append ( st );
	stcnt.append ( ")" );
	this->setText ( 0, stcnt );
}


void KBibGroupViewItem::paintCell ( QPainter* p_, const QColorGroup& cg_,
                                     int column_, int width_, int align_ )
{
	if ( !p_ )
	{
		return;
	}

	KListViewItem::paintCell(p_, cg_, column_, width_, align_);
	int marg = listView()->itemMargin();
	int r = marg;
	const QPixmap* icon = pixmap(column_);
  	if(icon) {
    	r += icon->width() + marg;
  	}
	QRect br ( 0, height(), r, 0 );
	if(isSelected()) {
    	p_->setPen(cg_.highlightedText());
  	} else {
    	p_->setPen(cg_.text());
  	}

	p_->eraseRect(r,0, width_, height());
	if (isSelected()){
		p_->fillRect(r, 0, width_, height(), cg_.highlight());
	}
	p_->drawText ( r, 0, width_-marg-r, height(), align_ | AlignVCenter, value, -1, &br );

	QString numText = QString::fromLatin1 ( "   (%1)" ).arg ( m_list->size() );

	QColor hl = cg_.highlight();
	if (isSelected())
		p_->setPen(cg_.highlightedText());
	else{
//Use the code from tellico
  		int h1, s1, v1, h2, s2, v2;
  		cg_.background().getHsv(&h1, &s1, &v1);
		QColor contrastColor;
  		QColor hl = cg_.highlight();
  		hl.getHsv(&h2, &s2, &v2);
  		h2 += 120;
  		s2 = 255;
  		hl.setHsv(h2, s2, v2);
		if(KABS(v2-v1) < 48) {
    		if(v1 < 128) {
      			contrastColor = hl.light();
    		} 
			else {
      			contrastColor = hl.dark();
    		}
  		}
		else {
    		contrastColor = hl;
  		}
	

		p_->setPen( contrastColor );

	}
	//p_->setPen ( QColor ( 255,0,0 ) );
	QFont font = p_->font();
	font.setBold(false);
	p_->setFont(font);
	p_->drawText ( br.right(), 0, width_-marg-br.right(), height(), align_ | Qt::AlignVCenter, numText );
}


#include "kbibgroupviewitem.moc"
