gepetto-viewer  6.0.0
An user-friendly Graphical Interface
qcpitemrichtext.hh
Go to the documentation of this file.
1 #ifndef QCP_ITEM_RICH_TEXT
2 #define QCP_ITEM_RICH_TEXT
3 #include <qcustomplot.h>
4 
5 #include <QTextDocument>
6 
8  Q_OBJECT
9  Q_PROPERTY(QString text READ text WRITE setText)
10  public:
11  QCPItemRichText(QCustomPlot *parentPlot) : QCPItemText(parentPlot) {}
12  virtual ~QCPItemRichText() {}
13 
14  void setText(const QString &text) {
16  doc.setHtml(text);
17  }
18 
19  protected:
20  // reimplemented virtual methods:
21  virtual void draw(QCPPainter *painter) {
22  QPointF pos(position->pixelPoint());
23  QTransform transform = painter->transform();
24  transform.translate(pos.x(), pos.y());
25  if (!qFuzzyIsNull(mRotation)) transform.rotate(mRotation);
26  painter->setFont(mainFont());
27  QRect textRect = painter->fontMetrics().boundingRect(
28  0, 0, 0, 0, Qt::TextDontClip | mTextAlignment, mText);
29  QRect textBoxRect = textRect.adjusted(-mPadding.left(), -mPadding.top(),
30  mPadding.right(), mPadding.bottom());
31  QPointF textPos = getTextDrawPoint(
32  QPointF(0, 0), textBoxRect,
33  mPositionAlignment); // 0, 0 because the transform does the translation
34  textRect.moveTopLeft(textPos.toPoint() +
35  QPoint(mPadding.left(), mPadding.top()));
36  textBoxRect.moveTopLeft(textPos.toPoint());
37  qreal clipPadF = mainPen().widthF();
38  int clipPad = (int)clipPadF;
39  QRect boundingRect =
40  textBoxRect.adjusted(-clipPad, -clipPad, clipPad, clipPad);
41  if (transform.mapRect(boundingRect)
42  .intersects(painter->transform().mapRect(clipRect()))) {
43  painter->setTransform(transform);
44  if ((mainBrush().style() != Qt::NoBrush &&
45  mainBrush().color().alpha() != 0) ||
46  (mainPen().style() != Qt::NoPen && mainPen().color().alpha() != 0)) {
47  painter->setPen(mainPen());
48  painter->setBrush(mainBrush());
49  painter->drawRect(textBoxRect);
50  }
51  painter->setBrush(Qt::NoBrush);
52  painter->setPen(QPen(mainColor()));
53  doc.setDefaultFont(mainFont());
54  doc.drawContents(painter);
55  }
56  }
57 
58  QTextDocument doc;
59 };
60 #endif // QCP_ITEM_RICH_TEXT
Definition: qcpitemrichtext.hh:7
QCPItemRichText(QCustomPlot *parentPlot)
Definition: qcpitemrichtext.hh:11
QTextDocument doc
Definition: qcpitemrichtext.hh:58
virtual void draw(QCPPainter *painter)
Definition: qcpitemrichtext.hh:21
virtual ~QCPItemRichText()
Definition: qcpitemrichtext.hh:12
void setText(const QString &text)
Definition: qcpitemrichtext.hh:14
Definition: qcustomplot.h:3877
QColor color() const
Definition: qcustomplot.h:3901
QPen mainPen() const
void setText(const QString &text)
QPointF getTextDrawPoint(const QPointF &pos, const QRectF &rect, Qt::Alignment positionAlignment) const
QColor mainColor() const
QBrush mainBrush() const
QFont mainFont() const
Definition: qcustomplot.h:414
void setPen(const QPen &pen)
Definition: qcustomplot.h:1925
#define QCP_LIB_DECL
Definition: qcustomplot.h:87