Akonadi Contacts

addressdelegate.cpp
1 /*
2  This file is part of Contact Editor.
3 
4  SPDX-FileCopyrightText: 2019 Volker Krause <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "addressdelegate.h"
10 
11 #include <QAbstractTextDocumentLayout>
12 #include <QPainter>
13 #include <QTextDocument>
14 
15 using namespace ContactEditor;
16 
17 AddressDelegate::AddressDelegate(QObject *parent)
18  : QStyledItemDelegate(parent)
19 {
20 }
21 
22 QSize AddressDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
23 {
24  QStyleOptionViewItem options = option;
25  initStyleOption(&options, index);
26 
27  QTextDocument doc;
28  doc.setHtml(options.text);
29  doc.setTextWidth(options.rect.width());
30  return QSize(doc.idealWidth(), doc.size().height());
31 }
32 
33 void AddressDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
34 {
35  QStyleOptionViewItem options = option;
36  initStyleOption(&options, index);
37 
38  painter->save();
39  QTextDocument doc;
40  doc.setHtml(options.text);
41  options.text = QString();
42  options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter);
43  painter->translate(options.rect.left(), options.rect.top());
44 
45  const QRect clip(0, 0, options.rect.width(), options.rect.height());
47  ctx.clip = clip;
48  if (option.state & QStyle::State_Selected) {
49  ctx.palette.setColor(QPalette::Text, options.palette.color(QPalette::HighlightedText));
50  }
51  doc.documentLayout()->draw(painter, ctx);
52  painter->restore();
53 }
void save()
qreal idealWidth() const const
QAbstractTextDocumentLayout * documentLayout() const const
void restore()
void setTextWidth(qreal width)
void translate(const QPointF &offset)
void setHtml(const QString &html)
virtual void draw(QPainter *painter, const QAbstractTextDocumentLayout::PaintContext &context)=0
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Jun 23 2021 23:09:24 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.