16 #include <QAbstractItemView>
17 #include <QAbstractTextDocumentLayout>
18 #include <QApplication>
20 #include <QTextDocument>
24 bool hasHtml(
const QString& t)
26 return t.contains(
"<") && t.contains(
">");
33 : QStyledItemDelegate(parent)
38 const QModelIndex& index)
const
40 QStyleOptionViewItem options = option;
41 initStyleOption(&options, index);
42 if (!hasHtml(options.text)) {
43 QStyledItemDelegate::paint(painter, option, index);
49 doc.setHtml(options.text);
53 const QWidget* widget = option.widget;
54 QStyle* style = widget ? widget->style() : QApplication::style();
55 style->drawControl(QStyle::CE_ItemViewItem, &options, painter, widget);
58 QSize iconSize = options.icon.actualSize(options.rect.size());
59 painter->translate(options.rect.left() + iconSize.width(), options.rect.top());
60 QRect clip(0, 0, options.rect.width() + iconSize.width(), options.rect.height());
62 painter->setClipRect(clip);
63 QAbstractTextDocumentLayout::PaintContext ctx;
66 QPalette::ColorGroup cg =
67 options.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
68 if (cg == QPalette::Normal && !(options.state & QStyle::State_Active))
69 cg = QPalette::Inactive;
70 ctx.palette.setColor(QPalette::Text, option.palette.color(cg, QPalette::Text));
73 doc.documentLayout()->draw(painter, ctx);
78 const QModelIndex& index)
const
80 QSize s = QStyledItemDelegate::sizeHint(option, index);
83 const int h = QStyledItemDelegate::sizeHint(option, index).height();
84 s.setHeight(std::max(s.height(), h));
86 QStyleOptionViewItem options = option;
87 initStyleOption(&options, index);
90 s.setHeight(std::max(s.height(), s2.height() + 10));
91 s.setWidth(s2.width() + h);
100 doc.setTextWidth(10000 );
101 QSize size = QSize(doc.idealWidth(), doc.size().height());
106 const QPoint& globalPos)
const
108 QString text = index.model()->data(index, Qt::DisplayRole).toString();
113 QRect r = view->visualRect(index);
114 QPoint P = view->viewport()->mapFromGlobal(globalPos);
115 QPoint P2 = P - r.topLeft();
117 QAbstractTextDocumentLayout::PaintContext ctx;
118 return doc.documentLayout()->anchorAt(P2);
Defines class ItemDelegateForHTML.
ItemDelegateForHTML(QObject *parent)
QString anchorAtGlobalPos(QAbstractItemView *view, const QModelIndex &index, const QPoint &globalPos) const
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override