BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
ItemDelegateForHTML.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Tool/ItemDelegateForHTML.cpp
6 //! @brief Implements class ItemDelegateForHTML
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include <QAbstractItemView>
17 #include <QAbstractTextDocumentLayout>
18 #include <QApplication>
19 #include <QPainter>
20 #include <QTextDocument>
21 
22 namespace {
23 
24 bool hasHtml(const QString& t)
25 {
26  return t.contains("<") && t.contains(">");
27 }
28 
29 } // namespace
30 
31 
33  : QStyledItemDelegate(parent)
34 {
35 }
36 
37 void ItemDelegateForHTML::paint(QPainter* painter, const QStyleOptionViewItem& option,
38  const QModelIndex& index) const
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 }
76 
77 QSize ItemDelegateForHTML::sizeHint(const QStyleOptionViewItem& option,
78  const QModelIndex& index) const
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 }
95 
96 QSize ItemDelegateForHTML::sizeHint(const QString& text)
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 }
104 
105 QString ItemDelegateForHTML::anchorAtGlobalPos(QAbstractItemView* view, const QModelIndex& index,
106  const QPoint& globalPos) const
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 }
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