/*
Copyright (C) 2006 Evan Teran
                   eteran@alum.rit.edu

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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "QCatagoryList.h"
#include "RegisterViewDelegate.h"
#include <QHeaderView>
#include <QTreeView>
#include <QMouseEvent>

//------------------------------------------------------------------------------
// Name: QCatagoryList(QWidget *parent)
// Desc: 
//------------------------------------------------------------------------------
QCatagoryList::QCatagoryList(QWidget *parent) : QTreeWidget(parent) {

	// set the delagate
	setItemDelegate(new RegisterViewDelegate(this, this));
	
	
	// hide the exapander, since we provide our own
	setRootIsDecorated(false);
	
	setColumnCount(1);
	header()->hide();
	header()->setResizeMode(QHeaderView::Stretch);
	
	connect(this, SIGNAL(itemPressed(QTreeWidgetItem *, int)), this, SLOT(handleMousePress(QTreeWidgetItem*)));

}

//------------------------------------------------------------------------------
// Name: handleMousePress(QTreeWidgetItem *item)
// Desc: 
//------------------------------------------------------------------------------
void QCatagoryList::handleMousePress(QTreeWidgetItem *item) {
	if(isCatagory(item)) {
	    setItemExpanded(item, !isItemExpanded(item));
	}
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void QCatagoryList::mouseDoubleClickEvent(QMouseEvent * event) {
	
	QTreeWidgetItem *const p = itemAt(event->pos());
	if(p != 0) {
		emit itemDoubleClicked(p, 0);
	}
}

//------------------------------------------------------------------------------
// Name: addCatagory(const QString &name)
// Desc: 
//------------------------------------------------------------------------------
QTreeWidgetItem *QCatagoryList::addCatagory(const QString &name) {
	QTreeWidgetItem *const cat = new QTreeWidgetItem(this);
	cat->setText(0, name);
	setItemExpanded(cat, true);
	return cat;
}

//------------------------------------------------------------------------------
// Name: addItem(QTreeWidgetItem *catagory, const QString &value)
// Desc: 
//------------------------------------------------------------------------------
QTreeWidgetItem *QCatagoryList::addItem(QTreeWidgetItem *catagory, const QString &value) {
	QTreeWidgetItem *const item = new QTreeWidgetItem(catagory);
	item->setText(0, value);
	return item;
}

//------------------------------------------------------------------------------
// Name: isCatagory(QTreeWidgetItem *item) const
// Desc: 
//------------------------------------------------------------------------------
bool QCatagoryList::isCatagory(QTreeWidgetItem *item) const {
	return item != 0 && item->parent() == 0;
}
