
#include "QCatagoryList.h"
#include <QHeaderView>

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
SheetDelegate::SheetDelegate(QTreeView *view, QWidget *parent) : QItemDelegate(parent), m_view(view) {
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void SheetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
	const QAbstractItemModel *const model = index.model();
	Q_ASSERT(model);

	if (!model->parent(index).isValid()) {
		// this is a top-level item.
		QStyleOptionButton buttonOption;

		buttonOption.state = option.state;
	#ifdef Q_WS_MAC
		buttonOption.state |= QStyle::State_Raised;
	#endif
		buttonOption.state &= ~QStyle::State_HasFocus;

		buttonOption.rect		= option.rect;
		buttonOption.palette	= option.palette;
		buttonOption.features	= QStyleOptionButton::None;
		m_view->style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter, m_view);

		static const int i = 9; // ### hardcoded in qcommonstyle.cpp
		const QRect r = option.rect;

		QStyleOption branchOption;
		branchOption.rect		= QRect(r.left() + i / 2, r.top() + (r.height() - i) / 2, i, i);
		branchOption.palette	= option.palette;
		branchOption.state		= QStyle::State_Children;

		if (m_view->isExpanded(index)) {
			branchOption.state |= QStyle::State_Open;
		}

		m_view->style()->drawPrimitive(QStyle::PE_IndicatorBranch, &branchOption, painter, m_view);

		// draw text
		const QRect textrect = QRect(r.left() + i * 2, r.top(), r.width() - ((5*i)/2), r.height());
		const QString text = elidedText(option.fontMetrics, textrect.width(), Qt::ElideMiddle, model->data(index, Qt::DisplayRole).toString());
		m_view->style()->drawItemText(painter, textrect, Qt::AlignCenter, option.palette, m_view->isEnabled(), text);

	} else {
		QItemDelegate::paint(painter, option, index);
	}
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
QSize SheetDelegate::sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const {
    return QItemDelegate::sizeHint(opt, index) + QSize(2, 2);
}

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

	setItemDelegate(new SheetDelegate(this, this));
	setRootIsDecorated(false);
	setColumnCount(1);
	header()->hide();
	header()->setResizeMode(QHeaderView::Stretch);
	
    connect(this, SIGNAL(itemPressed(QTreeWidgetItem *,int)), this, SLOT(handleMousePress(QTreeWidgetItem*)));
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
QCatagoryList::~QCatagoryList() {

}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void QCatagoryList::handleMousePress(QTreeWidgetItem *item) {
	if(item != 0 && item->parent() == 0) {
	    setItemExpanded(item, !isItemExpanded(item));		
	}
}

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

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