BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
ItemDelegateForHTML Class Reference

Description

For representing HTML text in an item *‍/.

Definition at line 21 of file ItemDelegateForHTML.h.

Inheritance diagram for ItemDelegateForHTML:
[legend]
Collaboration diagram for ItemDelegateForHTML:
[legend]

Public Member Functions

 ItemDelegateForHTML (QObject *parent)
 
QString anchorAtGlobalPos (QAbstractItemView *view, const QModelIndex &index, const QPoint &globalPos) const
 

Protected Member Functions

void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
 
QSize sizeHint (const QStyleOptionViewItem &option, const QModelIndex &index) const override
 

Static Private Member Functions

static QSize sizeHint (const QString &text)
 

Constructor & Destructor Documentation

◆ ItemDelegateForHTML()

ItemDelegateForHTML::ItemDelegateForHTML ( QObject *  parent)

Definition at line 32 of file ItemDelegateForHTML.cpp.

33  : QStyledItemDelegate(parent)
34 {
35 }

Member Function Documentation

◆ anchorAtGlobalPos()

QString ItemDelegateForHTML::anchorAtGlobalPos ( QAbstractItemView *  view,
const QModelIndex &  index,
const QPoint &  globalPos 
) const

Definition at line 105 of file ItemDelegateForHTML.cpp.

107 {
108  QString text = index.model()->data(index, Qt::DisplayRole).toString();
109 
110  QTextDocument doc;
111  doc.setHtml(text);
112 
113  QRect r = view->visualRect(index);
114  QPoint P = view->viewport()->mapFromGlobal(globalPos);
115  QPoint P2 = P - r.topLeft();
116 
117  QAbstractTextDocumentLayout::PaintContext ctx;
118  return doc.documentLayout()->anchorAt(P2);
119 }

◆ paint()

void ItemDelegateForHTML::paint ( QPainter *  painter,
const QStyleOptionViewItem &  option,
const QModelIndex &  index 
) const
overrideprotected

Definition at line 37 of file ItemDelegateForHTML.cpp.

39 {
40  QStyleOptionViewItem options = option;
41  initStyleOption(&options, index);
42  if (!hasHtml(options.text)) {
43  QStyledItemDelegate::paint(painter, option, index);
44  return;
45  }
46 
47  painter->save();
48  QTextDocument doc;
49  doc.setHtml(options.text);
50 
51  options.text = "";
52 
53  const QWidget* widget = option.widget;
54  QStyle* style = widget ? widget->style() : QApplication::style();
55  style->drawControl(QStyle::CE_ItemViewItem, &options, painter, widget);
56 
57  // shift text right to make icon visible
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());
61 
62  painter->setClipRect(clip);
63  QAbstractTextDocumentLayout::PaintContext ctx;
64 
65  // set text color (see qcommonstyle.cpp, QCommonStyle::drawControl, case CE_ItemViewItem)
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));
71 
72  ctx.clip = clip;
73  doc.documentLayout()->draw(painter, ctx);
74  painter->restore();
75 }

◆ sizeHint() [1/2]

QSize ItemDelegateForHTML::sizeHint ( const QString &  text)
staticprivate

Definition at line 96 of file ItemDelegateForHTML.cpp.

97 {
98  QTextDocument doc;
99  doc.setHtml(text);
100  doc.setTextWidth(10000 /*options.rect.width()*/);
101  QSize size = QSize(doc.idealWidth(), doc.size().height());
102  return size;
103 }

◆ sizeHint() [2/2]

QSize ItemDelegateForHTML::sizeHint ( const QStyleOptionViewItem &  option,
const QModelIndex &  index 
) const
overrideprotected

Definition at line 77 of file ItemDelegateForHTML.cpp.

79 {
80  QSize s = QStyledItemDelegate::sizeHint(option, index);
81 
82  // get size of parent; this is the minimum size
83  const int h = QStyledItemDelegate::sizeHint(option, index).height();
84  s.setHeight(std::max(s.height(), h));
85 
86  QStyleOptionViewItem options = option;
87  initStyleOption(&options, index);
88 
89  auto s2 = sizeHint(options.text);
90  s.setHeight(std::max(s.height(), s2.height() + 10));
91  s.setWidth(s2.width() + h); // +h: icon
92 
93  return s;
94 }
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override

The documentation for this class was generated from the following files: