#include "listviewdelegate.h"
#include <QApplication>

ListviewDelegate::ListviewDelegate()
    {
    // TODO Auto-generated constructor stub

    }

ListviewDelegate::~ListviewDelegate()
    {
    // TODO Auto-generated destructor stub
    }

//alocate each item size in listview.
QSize ListviewDelegate::sizeHint(const QStyleOptionViewItem &  option ,
                              const QModelIndex & index) const
{
    QIcon icon = qvariant_cast<QIcon>(index.data(IconRole));
    QSize iconsize = icon.actualSize(option.decorationSize);
	if (index.data(IconRole).isValid())
		return(QSize(iconsize.width()*3+10,iconsize.height()+4));

    return(QSize(0,10));

}
void ListviewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                           const QModelIndex &index) const
 {
    QStyledItemDelegate::paint(painter,option,index);

    painter->save();

	if (option.state & QStyle::State_Selected)
		painter->fillRect(option.rect, QColor(200,200,200));
		//painter->setPen(opt.palette.color(cg, QPalette::HighlightedText));
	else
		painter->fillRect(option.rect, QColor(170,170,170));
		//painter->setPen(opt.palette.color(cg, QPalette::Text));

    QFont font = QApplication::font();
    QFont SubFont = QApplication::font();
    //font.setPixelSize(font.weight()+);
	font.setPixelSize(10);
    font.setBold(true);
    QFontMetrics fm(font);

	QColor threadColor = qvariant_cast<QColor>(index.data(threadRole));
    QIcon icon = qvariant_cast<QIcon>(index.data(IconRole));

	//paint if there is no icon (if true this is a linkstitch)
	if (!index.data(IconRole).isValid())
	{
		QPen pen(Qt::black, 1);
		painter->setPen(pen);
		painter->drawRect(option.rect.adjusted(1,1,-1,-1));

		QRect threadRect = option.rect;
		threadRect.setRight(8);
		painter->fillRect(threadRect, threadColor);

		painter->restore();
		return;
	}

	QIcon type = qvariant_cast<QIcon>(index.data(typeRole));
    QIcon pattern = qvariant_cast<QIcon>(index.data(patternRole));
    QString indexText = qvariant_cast<QString>(index.data(IndexRole));
    QString nbPoints = qvariant_cast<QString>(index.data(NbpointsRole));

    QSize iconsize = icon.actualSize(option.decorationSize);

	QRect threadRect = option.rect;
    QRect iconRect = option.rect;
	QRect typeRect = option.rect;
	QRect patternRect = option.rect;

	threadRect.setRight(8);

	iconRect.setLeft(threadRect.right()+2);
    iconRect.setRight(threadRect.right()+iconsize.width()+2);
    iconRect.setTop(iconRect.top()+2);

	typeRect.setLeft(iconRect.right()+2);
	typeRect.setRight(typeRect.left()+iconsize.width()+2);
	typeRect.setTop(typeRect.top()+2);

	patternRect.setLeft(typeRect.right()+2);
	patternRect.setRight(patternRect.left()+iconsize.width()+2);
	patternRect.setTop(patternRect.top()+2);

	//int indexTextWidth = fm.horizontalAdvance(indexText); //must use this one since Qt 5.11
	int indexTextWidth = fm.width(indexText);

	QRect indexRect = iconRect;
	indexRect.setLeft(indexRect.right()-indexTextWidth-2);
	indexRect.setTop(indexRect.bottom()-font.pixelSize()-2);

	//int nbPointsTextWidth = fm.horizontalAdvance(nbPoints); //must use this one since Qt 5.11
	int nbPointsTextWidth = fm.width(nbPoints);

	QRect nbPointsRect = patternRect;
	nbPointsRect.setLeft(nbPointsRect.right()-nbPointsTextWidth-2);
	nbPointsRect.setTop(nbPointsRect.bottom()-font.pixelSize()-2);

	painter->fillRect(threadRect, threadColor);

	painter->drawPixmap(QPoint(iconRect.left(),iconRect.top()),icon.pixmap(iconsize.width(),iconsize.height()));
	painter->drawPixmap(QPoint(typeRect.left(),typeRect.top()),type.pixmap(iconsize.width(),iconsize.height()));
	painter->drawPixmap(QPoint(patternRect.left(),patternRect.top()),pattern.pixmap(iconsize.width(),iconsize.height()));


    painter->setFont(font);
    QPen pen(Qt::black, 1);
    painter->setPen(pen);
    // draw index
    painter->fillRect(indexRect, QColor(255,255,255,127));
	painter->drawText(indexRect.adjusted(1,-1,-1,1),Qt::AlignRight | Qt::AlignBottom,indexText);
    // draw nb points
	painter->fillRect(nbPointsRect, QColor(255,255,255,127));
	painter->drawText(nbPointsRect.adjusted(1,-1,-1,1),Qt::AlignRight | Qt::AlignBottom,nbPoints);

    painter->restore();

 }

