//
// C++ Implementation: kbibgroupview
//
// Description:
//
//
// Author: Thach Nguyen <thach.nguyen@rmit.edu.au>, (C) 2006
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "kbibgroupview.h"
#include "kbib.h"
#include <klocale.h>
#include <qstringlist.h>
#include <qpixmap.h>
#include <kiconloader.h>


KBibGroupView::KBibGroupView(QWidget *parent, const char *name)
        : KListView(parent, name)
{
    m_list = 0;
    this->addColumn(i18n("GROUP"));
//    this->setShowSortIndicator(true);
	this->setSorting(0);
    this->setRootIsDecorated(true);
	groupList.setAutoDelete( TRUE );


    connect(this,SIGNAL(clicked(QListViewItem *)), this, SLOT(slotItemSelect(QListViewItem *)));
	connect(this, SIGNAL(selectionChanged (QListViewItem *)), this, SLOT(slotSelectionChanged(QListViewItem *)));
	
	createGroupView();
}


KBibGroupView::~KBibGroupView()
{
    
}

void KBibGroupView::createGroupView()
{
	if (groupAll)
		delete groupAll;
	groupList.clear();
	groupAll = new KBibGroupViewItem("ALL", this);

	QPixmap pix = KGlobal::iconLoader()->loadIcon ( QString::fromLatin1 ( "kbib" ), KIcon::Small );
	groupAll->setPixmap ( 0, pix );

	QStringList fieldNameTab = BibEntryDefTable::self()->getFieldNames();
	for (QStringList::Iterator it = fieldNameTab.begin(); it != fieldNameTab.end(); ++it){
		RefField *field = BibEntryDefTable::self()->getRefField(*it);
		if (field->allowGrouping){
			QString fn = field->name;
			fn = fn.upper();
			KBibGroupViewGroup *group = new KBibGroupViewGroup(this, fn);
			groupList.append( group );
			if (fn == QString::fromLatin1 ("AUTHOR") || fn == QString::fromLatin1 ("EDITOR") )
				pix = SmallIcon(QString::fromLatin1("person.png"));
			else if (fn == QString::fromLatin1 ("TAG") )
				pix = SmallIcon(QString::fromLatin1("tag.png") );
			else if (fn == QString::fromLatin1 ("JOURNAL") )
				pix = SmallIcon(QString::fromLatin1("journal.png") );
			else if (fn == QString::fromLatin1 ("YEAR") )
				pix = SmallIcon(QString::fromLatin1("view-calendar.png"));	
			else
				pix = SmallIcon(QString::fromLatin1("folder"));
			group->setPixmap(0, pix);
		}
	}
}


void KBibGroupView::update(){
	groupAll->clear();
	for (int i = 0; i < m_list->size(); i++)
    {
        BibEntry *entry = m_list->get_entry(i);
        groupAll->addEntry(entry);
    }
	KBibGroupViewGroup *group;
	for ( group = groupList.first(); group; group = groupList.next() ){
		group->clearGroupView();
		group->createGroup(m_list);
	}
}


void KBibGroupView::addEntry(BibEntry *entry)
{
	groupAll->addEntry(entry);
	KBibGroupViewGroup *group;
    for ( group = groupList.first(); group; group = groupList.next() ){
		group->addEntry(entry);
	}


}


void KBibGroupView::clearGroupView(void){
	if (groupAll)
		delete groupAll;
	groupAll = new KBibGroupViewItem("ALL", this);
	QPixmap pix = KGlobal::iconLoader()->loadIcon ( QString::fromLatin1 ( "kbib" ), KIcon::Small );
	groupAll->setPixmap ( 0, pix );

	KBibGroupViewGroup *group;
    for ( group = groupList.first(); group; group = groupList.next() ){
		group->clearGroupView();
	}

}

void KBibGroupView::slotItemSelect(QListViewItem *item_)
{
    if(!item_)
        return;
    KBibGroupViewItem *item = dynamic_cast<KBibGroupViewItem*>(item_);

    if (item)
    {
		m_view->updateListView(item->getEntryTable());		
        QSplitter* mbox = (QSplitter*) parent();
        KBibApp* win = (KBibApp*) mbox->parent();
		
		p_preview->setBibEntry(0);
		p_preview->updatePreview();
		win->slotNumRefMsg();
    }
}

void KBibGroupView::slotSelectionChanged(QListViewItem *item_){
	if(!item_)
        return;
	if (item_->isSelected()){
		slotItemSelect(item_);	
	}
	
}

#include "kbibgroupview.moc"
