File indexing completed on 2024-04-14 15:51:36
0001 /*************************************************************************** 0002 richtextitemdelegate.cpp - description 0003 ------------------- 0004 begin : Mon Oct 12 2009 0005 copyright : (C) 2009 by Dominik Seichter 0006 email : domseichter@web.de 0007 ***************************************************************************/ 0008 0009 /*************************************************************************** 0010 * * 0011 * This program is free software; you can redistribute it and/or modify * 0012 * it under the terms of the GNU General Public License as published by * 0013 * the Free Software Foundation; either version 2 of the License, or * 0014 * (at your option) any later version. * 0015 * * 0016 ***************************************************************************/ 0017 0018 #include "richtextitemdelegate.h" 0019 #include "krenamefile.h" 0020 #include "krenamemodel.h" 0021 0022 #include <QPainter> 0023 #include <QRectF> 0024 #include <QTextDocument> 0025 0026 RichTextItemDelegate::RichTextItemDelegate(QObject *parent) 0027 : QItemDelegate(parent) 0028 { 0029 m_document = new QTextDocument(this); 0030 0031 } 0032 0033 void RichTextItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 0034 { 0035 // prepare 0036 painter->save(); 0037 painter->setClipRect(option.rect); 0038 this->drawBackground(painter, option, index); 0039 0040 if (option.state & QStyle::State_Selected) { 0041 painter->fillRect(option.rect, option.palette.highlight()); 0042 } 0043 0044 //QItemDelegate::paint(painter, option, index); 0045 QPixmap pixmap = index.model()->data(index, Qt::DecorationRole).value<QPixmap>(); 0046 QString text = index.model()->data(index, Qt::DisplayRole).toString(); 0047 0048 if (!pixmap.isNull()) { 0049 int offsetX = qMax(pixmap.width(), KRenameFile::iconSize()); 0050 QRect pixmapRect(option.rect.x(), 0051 option.rect.y(), 0052 pixmap.width(), 0053 option.rect.height()); 0054 0055 QRectF textRect(0.0, 0.0, 0056 static_cast<qreal>(option.rect.width() - offsetX), 0057 static_cast<qreal>(option.rect.height())); 0058 0059 this->drawDecoration(painter, option, 0060 pixmapRect, 0061 pixmap); 0062 0063 painter->save(); 0064 painter->translate(option.rect.x() + offsetX, 0065 option.rect.y()); 0066 m_document->setHtml(text); 0067 m_document->drawContents(painter, textRect); 0068 painter->restore(); 0069 } else { 0070 painter->setFont(option.font); 0071 painter->drawText(option.rect, 0072 option.displayAlignment, 0073 text); 0074 } 0075 0076 this->drawFocus(painter, option, option.rect); 0077 0078 // done 0079 painter->restore(); 0080 } 0081 0082 QSize RichTextItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const 0083 { 0084 if (static_cast<const KRenameModel *>(index.model())->isPreviewEnabled()) { 0085 return QSize(KRenameFile::iconSize(), KRenameFile::iconSize()); 0086 } else { 0087 return QItemDelegate::sizeHint(option, index); 0088 } 0089 } 0090