#include "postSearchListDelegate.h"
#include <QtGui>

postSearchListDelegate::postSearchListDelegate(QObject *parent) :
    QItemDelegate(parent)
{

}

void postSearchListDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
          //if (index.column() == 0) return;
    QStyleOptionViewItemV3 opt = setOptions(index, option);


    // prepare
    painter->save();
    painter->setFont(QFont("MS Shell Dlg 2",6));


    // get the data and the rectangles
    const QPixmap& pixmap
            = qvariant_cast<QPixmap>(index.data(Qt::UserRole+1));

    pixmap.scaled(QSize(80,80),Qt::KeepAspectRatio);

    QRect decorationRect = QRect(opt.rect.topLeft()+QPoint(5,10), pixmap.size());
    decorationRect.moveTo(decorationRect.left(), decorationRect.top());

    const QString& name = index.data(Qt::UserRole).toString();
    QFontMetrics fm(painter->font());
    QRect displayRect = QRect(opt.rect.topLeft()+QPoint(105,10),QSize(220,50));

    const QString& area = index.data(Qt::UserRole+3).toString();
    //QFontMetrics fm(painter->font());
    QRect displayRect2 = QRect(opt.rect.topLeft()+QPoint(105,47),QSize(fm.width(area),fm.height()));

    const QString& size = index.data(Qt::UserRole+4).toString();
    QRect displayRect3 = QRect(opt.rect.topLeft()+QPoint(105,75),QSize(fm.width(size),fm.height()));

    const QString& room = index.data(Qt::UserRole+5).toString();
    QRect displayRect4 = QRect(opt.rect.topLeft()+QPoint(215,75),QSize(fm.width(room),fm.height()));

    const QString& price = index.data(Qt::UserRole+6).toString();
    QRect displayRect5 = QRect(opt.rect.topLeft()+QPoint(105,100),QSize(fm.width(price),fm.height()));

    const QString& pps = index.data(Qt::UserRole+7).toString();
    QRect displayRect6 = QRect(opt.rect.topLeft()+QPoint(215,100),QSize(fm.width(pps),fm.height()));

    const QPixmap& pixmapSmall
            = qvariant_cast<QPixmap>(index.data(Qt::UserRole+2));
    QRect smallIconRect = QRect(opt.rect.topLeft()+QPoint(315,55), pixmapSmall.size());


    drawBackground(painter, opt, index);
    painter->drawPixmap(decorationRect, pixmap);
    painter->drawText(displayRect, name);
    painter->drawText(displayRect2, area);
    painter->drawText(displayRect3, size);
    painter->drawText(displayRect4, room);
    painter->drawText(displayRect5, price);
    painter->drawText(displayRect6, pps);
    painter->drawPixmap(smallIconRect, pixmapSmall);

    drawFocus(painter, opt, displayRect6);

    // done
    painter->restore();
}


QSize postSearchListDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
    return QSize(300,150);
}

