File indexing completed on 2024-11-24 04:39:28

0001 /*
0002     This file is part of Contact Editor.
0003 
0004     SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "addressdelegate.h"
0010 
0011 #include <QAbstractTextDocumentLayout>
0012 #include <QPainter>
0013 #include <QTextDocument>
0014 
0015 using namespace Akonadi;
0016 
0017 AddressDelegate::AddressDelegate(QObject *parent)
0018     : QStyledItemDelegate(parent)
0019 {
0020 }
0021 
0022 QSize AddressDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
0023 {
0024     QStyleOptionViewItem options = option;
0025     initStyleOption(&options, index);
0026 
0027     QTextDocument doc;
0028     doc.setHtml(options.text);
0029     doc.setTextWidth(options.rect.width());
0030     return QSize(doc.idealWidth(), doc.size().height());
0031 }
0032 
0033 void AddressDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0034 {
0035     QStyleOptionViewItem options = option;
0036     initStyleOption(&options, index);
0037 
0038     painter->save();
0039     QTextDocument doc;
0040     doc.setHtml(options.text);
0041     options.text = QString();
0042     options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter);
0043     painter->translate(options.rect.left(), options.rect.top());
0044 
0045     const QRect clip(0, 0, options.rect.width(), options.rect.height());
0046     QAbstractTextDocumentLayout::PaintContext ctx;
0047     ctx.clip = clip;
0048     if (option.state & QStyle::State_Selected) {
0049         ctx.palette.setColor(QPalette::Text, options.palette.color(QPalette::HighlightedText));
0050     }
0051     doc.documentLayout()->draw(painter, ctx);
0052     painter->restore();
0053 }
0054 
0055 #include "moc_addressdelegate.cpp"