/****************************************************************************
**
** Copyright (C) 1992-2004 Trolltech AS. All rights reserved.
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** Licensees holding valid Qt Preview licenses may use this file in
** accordance with the Qt Preview License Agreement provided with the
** Software.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
** information about Qt Commercial License Agreements.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "hexdelegate.h"
#include <qabstractitemmodel.h>
#include <qpainter.h>

HexDelegate::HexDelegate(QObject *parent) 
    : QItemDelegate(parent)
{
    sz = QSize(2, 2);
    textHex = "0x";
    textHex.reserve(8); // fine tuning
}

HexDelegate::~HexDelegate()
{
}

QSize HexDelegate::sizeHint(const QStyleOptionViewItem &option,
                            const QAbstractItemModel *,
                            const QModelIndex &) const
{
    static QString textSize("0xFFFFFFFF");
    return QFontMetrics(option.font).size(0, textSize) + sz;
}

void HexDelegate::paint(QPainter *painter,
                        const QStyleOptionViewItem &option,
                        const QAbstractItemModel *model,
                        const QModelIndex &index) const
{
    static QRect emptyRect;
    static QPoint pt;

    textHex.resize(2);
    textHex += QString::number(
                model->data(index, QAbstractItemModel::DisplayRole).toInt(), 16).toUpper();

    // Layout text
    QRect textRect(pt, painter->fontMetrics().size(0, textHex) + sz);
    doLayout(option, &emptyRect, &textRect, false);

    // draw the item
    drawDisplay(painter, option, textRect, textHex);
}
