File indexing completed on 2024-12-08 05:14:48
0001 // SPDX-License-Identifier: GPL-2.0-or-later 0002 // SPDX-FileCopyrightText: 2009 Dominik Seichter <domseichter@web.de> 0003 0004 #include "richtextitemdelegate.h" 0005 #include "krenamefile.h" 0006 #include "krenamemodel.h" 0007 0008 #include <QPainter> 0009 #include <QRectF> 0010 #include <QTextDocument> 0011 0012 RichTextItemDelegate::RichTextItemDelegate(QObject *parent) 0013 : QItemDelegate(parent) 0014 { 0015 m_document = new QTextDocument(this); 0016 0017 } 0018 0019 void RichTextItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 0020 { 0021 // prepare 0022 painter->save(); 0023 painter->setClipRect(option.rect); 0024 this->drawBackground(painter, option, index); 0025 0026 if (option.state & QStyle::State_Selected) { 0027 painter->fillRect(option.rect, option.palette.highlight()); 0028 } 0029 0030 //QItemDelegate::paint(painter, option, index); 0031 QPixmap pixmap = index.model()->data(index, Qt::DecorationRole).value<QPixmap>(); 0032 QString text = index.model()->data(index, Qt::DisplayRole).toString(); 0033 0034 if (!pixmap.isNull()) { 0035 int offsetX = qMax(pixmap.width(), KRenameFile::iconSize()); 0036 QRect pixmapRect(option.rect.x(), 0037 option.rect.y(), 0038 pixmap.width(), 0039 option.rect.height()); 0040 0041 QRectF textRect(0.0, 0.0, 0042 static_cast<qreal>(option.rect.width() - offsetX), 0043 static_cast<qreal>(option.rect.height())); 0044 0045 this->drawDecoration(painter, option, 0046 pixmapRect, 0047 pixmap); 0048 0049 painter->save(); 0050 painter->translate(option.rect.x() + offsetX, 0051 option.rect.y()); 0052 m_document->setHtml(text); 0053 m_document->drawContents(painter, textRect); 0054 painter->restore(); 0055 } else { 0056 painter->setFont(option.font); 0057 painter->drawText(option.rect, 0058 option.displayAlignment, 0059 text); 0060 } 0061 0062 this->drawFocus(painter, option, option.rect); 0063 0064 // done 0065 painter->restore(); 0066 } 0067 0068 QSize RichTextItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const 0069 { 0070 if (static_cast<const KRenameModel *>(index.model())->isPreviewEnabled()) { 0071 return QSize(KRenameFile::iconSize(), KRenameFile::iconSize()); 0072 } else { 0073 return QItemDelegate::sizeHint(option, index); 0074 } 0075 } 0076 0077 #include "moc_richtextitemdelegate.cpp"