1
//=============================================================================
3
// File : kvi_tal_itemdelegates.cpp
4
// Creation date : Mon May 05 2008 11:25:08 by Alessandro Carbone
6
// This file is part of the KVirc irc client distribution
7
// Copyright (C) 2008 Alessandro Carbone (elfonol at gmail dot com)
9
// This program is FREE software. You can redistribute it and/or
10
// modify it under the terms of the GNU General Public License
11
// as published by the Free Software Foundation; either version 2
12
// of the License, or (at your opinion) any later version.
14
// This program is distributed in the HOPE that it will be USEFUL,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
// See the GNU General Public License for more details.
19
// You should have received a copy of the GNU General Public License
20
// along with this program. If not, write to the Free Software Foundation,
21
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
//=============================================================================
25
#include "kvi_tal_itemdelegates.h"
27
#include <QListWidget>
29
#include <QApplication>
30
#include <QAbstractTextDocumentLayout>
32
KviTalIconAndRichTextItemDelegate::KviTalIconAndRichTextItemDelegate(QAbstractItemView * pWidget)
33
: QStyledItemDelegate(pWidget), m_pParent(pWidget), m_pDefaultPix(0)
37
KviTalIconAndRichTextItemDelegate::~KviTalIconAndRichTextItemDelegate()
41
void KviTalIconAndRichTextItemDelegate::paint(QPainter * pPainter, const QStyleOptionViewItem & option, const QModelIndex & index) const
44
QStyleOptionViewItemV4 opt = option;
45
initStyleOption(&opt, index);
47
if (opt.state & QStyle::State_Selected)
48
QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem,&opt, pPainter);
50
QString szText = index.data(Qt::DisplayRole).toString();
53
QVariant value = index.data(Qt::DecorationRole);
56
if(value.canConvert<QIcon>())
58
ico = QIcon(value.value<QIcon>());
62
pPainter->drawPixmap(opt.rect.x()+LVI_BORDER,opt.rect.y()+LVI_BORDER,*m_pDefaultPix);
64
pPainter->drawPixmap(opt.rect.x()+LVI_BORDER,opt.rect.y()+LVI_BORDER,ico.pixmap(LVI_ICON_SIZE,LVI_ICON_SIZE));
68
pPainter->drawPixmap(opt.rect.x()+LVI_BORDER,opt.rect.y()+LVI_BORDER,*m_pDefaultPix);
73
doc.setDefaultFont(opt.font);
74
pPainter->translate(opt.rect.x()+LVI_AFTER_ICON,opt.rect.y()+LVI_BORDER);
75
doc.setTextWidth(opt.rect.width()-LVI_AFTER_ICON);
76
QRect cliprect = QRect(QPoint(0,0),QSize(opt.rect.width()-LVI_AFTER_ICON,opt.rect.height()));
77
doc.drawContents(pPainter,cliprect);
81
QSize KviTalIconAndRichTextItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex & index) const
83
QString szText = index.data(Qt::DisplayRole).toString();
86
doc.setDefaultFont(option.font);
87
doc.setTextWidth(option.rect.width()-LVI_AFTER_ICON);
88
int iHeight = doc.documentLayout()->documentSize().toSize().height();
89
if(iHeight < (LVI_ICON_SIZE+(2 * LVI_BORDER)))
90
iHeight = LVI_ICON_SIZE;
92
return QSize(((QListWidget *)parent())->viewport()->size().width(), iHeight + (2 * LVI_BORDER));