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:
12  : QCPItemText (parentPlot) {}
13  virtual ~QCPItemRichText() {}
14 
15  void setText(const QString &text)
16  {
17  QCPItemText::setText (text);
18  doc.setHtml(text);
19  }
20 
21 protected:
22  // reimplemented virtual methods:
23  virtual void draw(QCPPainter *painter)
24  {
25  QPointF pos(position->pixelPoint());
26  QTransform transform = painter->transform();
27  transform.translate(pos.x(), pos.y());
28  if (!qFuzzyIsNull(mRotation))
29  transform.rotate(mRotation);
30  painter->setFont(mainFont());
31  QRect textRect = painter->fontMetrics().boundingRect(0, 0, 0, 0, Qt::TextDontClip|mTextAlignment, mText);
32  QRect textBoxRect = textRect.adjusted(-mPadding.left(), -mPadding.top(), mPadding.right(), mPadding.bottom());
33  QPointF textPos = getTextDrawPoint(QPointF(0, 0), textBoxRect, mPositionAlignment); // 0, 0 because the transform does the translation
34  textRect.moveTopLeft(textPos.toPoint()+QPoint(mPadding.left(), mPadding.top()));
35  textBoxRect.moveTopLeft(textPos.toPoint());
36  qreal clipPadF = mainPen().widthF();
37  int clipPad = (int)clipPadF;
38  QRect boundingRect = textBoxRect.adjusted(-clipPad, -clipPad, clipPad, clipPad);
39  if (transform.mapRect(boundingRect).intersects(painter->transform().mapRect(clipRect())))
40  {
41  painter->setTransform(transform);
42  if ((mainBrush().style() != Qt::NoBrush && mainBrush().color().alpha() != 0) ||
43  (mainPen().style() != Qt::NoPen && mainPen().color().alpha() != 0))
44  {
45  painter->setPen(mainPen());
46  painter->setBrush(mainBrush());
47  painter->drawRect(textBoxRect);
48  }
49  painter->setBrush(Qt::NoBrush);
50  painter->setPen(QPen(mainColor()));
51  doc.setDefaultFont(mainFont());
52  doc.drawContents(painter);
53  }
54  }
55 
56  QTextDocument doc;
57 };
58 #endif // QCP_ITEM_RICH_TEXT
virtual void draw(QCPPainter *painter)
Definition: qcpitemrichtext.hh:23
QFont mainFont() const
QTextDocument doc
Definition: qcpitemrichtext.hh:56
Definition: qcustomplot.h:3421
void setPen(const QPen &pen)
#define QCP_LIB_DECL
Definition: qcustomplot.h:87
QColor mainColor() const
Definition: qcustomplot.h:1680
virtual ~QCPItemRichText()
Definition: qcpitemrichtext.hh:13
Definition: qcpitemrichtext.hh:7
Definition: qcustomplot.h:312
QBrush mainBrush() const
QPointF getTextDrawPoint(const QPointF &pos, const QRectF &rect, Qt::Alignment positionAlignment) const
void setText(const QString &text)
Definition: qcpitemrichtext.hh:15
void setText(const QString &text)
QCPItemRichText(QCustomPlot *parentPlot)
Definition: qcpitemrichtext.hh:11
QPen mainPen() const
QColor color() const
Definition: qcustomplot.h:3444
virtual QRect clipRect() const