##// END OF EJS Templates
Minor docs updates
Marek Rosa -
r2393:150f5f56819e
parent child
Show More
@@ -1,99 +1,99
1 1 #include "callout.h"
2 2 #include <QPainter>
3 3 #include <QFontMetrics>
4 4 #include <QGraphicsSceneMouseEvent>
5 5 #include <QMouseEvent>
6 6
7 7 Callout::Callout(QGraphicsItem * parent):
8 8 QGraphicsItem(parent)
9 9 {
10 10 }
11 11
12 12 QRectF Callout::boundingRect() const
13 13 {
14 14 QPointF anchor = mapFromParent(m_anchor);
15 15 QRectF rect;
16 16 rect.setLeft(qMin(m_rect.left(), anchor.x()));
17 17 rect.setRight(qMax(m_rect.right(), anchor.x()));
18 18 rect.setTop(qMin(m_rect.top(), anchor.y()));
19 19 rect.setBottom(qMax(m_rect.bottom(), anchor.y()));
20 20 return rect;
21 21 }
22 22
23 23 void Callout::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
24 24 {
25 25 Q_UNUSED(option)
26 26 Q_UNUSED(widget)
27 27 QPainterPath path;
28 28 path.addRoundedRect(m_rect, 5, 5);
29 29
30 30 QPointF anchor = mapFromParent(m_anchor);
31 31 if (!m_rect.contains(anchor)) {
32 32 QPointF point1, point2;
33 33
34 // establish the position of the anchor point in relation to m_textRect
34 // establish the position of the anchor point in relation to m_rect
35 35 bool above = anchor.y() <= m_rect.top();
36 36 bool aboveCenter = anchor.y() > m_rect.top() && anchor.y() <= m_rect.center().y();
37 37 bool belowCenter = anchor.y() > m_rect.center().y() && anchor.y() <= m_rect.bottom();
38 38 bool below = anchor.y() > m_rect.bottom();
39 39
40 40 bool onLeft = anchor.x() <= m_rect.left();
41 41 bool leftOfCenter = anchor.x() > m_rect.left() && anchor.x() <= m_rect.center().x();
42 42 bool rightOfCenter = anchor.x() > m_rect.center().x() && anchor.x() <= m_rect.right();
43 43 bool onRight = anchor.x() > m_rect.right();
44 44
45 // get the nearest m_textRect corner.
45 // get the nearest m_rect corner.
46 46 qreal x = (onRight + rightOfCenter) * m_rect.width();
47 47 qreal y = (below + belowCenter) * m_rect.height();
48 48 bool cornerCase = (above && onLeft) || (above && onRight) || (below && onLeft) || (below && onRight);
49 49 bool vertical = qAbs(anchor.x() - x) > qAbs(anchor.y() - y);
50 50
51 51 qreal x1 = x + leftOfCenter * 10 - rightOfCenter * 20 + cornerCase * !vertical * (onLeft * 10 - onRight * 20);
52 52 qreal y1 = y + aboveCenter * 10 - belowCenter * 20 + cornerCase * vertical * (above * 10 - below * 20);;
53 53 point1.setX(x1);
54 54 point1.setY(y1);
55 55
56 56 qreal x2 = x + leftOfCenter * 20 - rightOfCenter * 10 + cornerCase * !vertical * (onLeft * 20 - onRight * 10);;
57 57 qreal y2 = y + aboveCenter * 20 - belowCenter * 10 + cornerCase * vertical * (above * 20 - below * 10);;
58 58 point2.setX(x2);
59 59 point2.setY(y2);
60 60
61 61 path.moveTo(point1);
62 62 path.lineTo(mapFromParent(m_anchor));
63 63 path.lineTo(point2);
64 64 path = path.simplified();
65 65 }
66 66 painter->setBrush(QColor(255, 255, 255));
67 67 painter->drawPath(path);
68 68 painter->drawText(m_textRect, m_text);
69 69 }
70 70
71 71 void Callout::mousePressEvent(QGraphicsSceneMouseEvent *event)
72 72 {
73 73 event->setAccepted(true);
74 74 }
75 75
76 76 void Callout::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
77 77 {
78 78 if (event->buttons() & Qt::LeftButton){
79 79 setPos(mapToParent(event->pos() - event->buttonDownPos(Qt::LeftButton)));
80 80 event->setAccepted(true);
81 81 } else {
82 82 event->setAccepted(false);
83 83 }
84 84 }
85 85
86 86 void Callout::setText(const QString &text)
87 87 {
88 88 m_text = text;
89 89 QFontMetrics metrics(m_font);
90 90 m_textRect = metrics.boundingRect(QRect(0, 0, 150, 150), Qt::AlignLeft, m_text);
91 91 m_textRect.translate(5, 5);
92 92 prepareGeometryChange();
93 93 m_rect = m_textRect.adjusted(-5, -5, 5, 5);
94 94 }
95 95
96 96 void Callout::setAnchor(QPointF point)
97 97 {
98 98 m_anchor = point;
99 99 }
@@ -1,12 +1,14
1 1 /*!
2 2 \example demos/callout
3 3 \title Callout demo
4 4 \subtitle
5 5
6 6 This demo shows how to draw an additional element (a callout) on top of the chart.
7 7 \image demos_callout.png
8 8
9 QChart class provides two methods that map between the scene coordinates and the series' domain (defines by the axes ranges).
9 QChart class provides two methods that map between the scene coordinates and the series' domain (defined by the axes ranges).
10
10 11 QPointF QChart::mapToPosition(const QPointF &value, QAbstractSeries *series)
12
11 13 QPointF QChart::mapToValue(const QPointF &position, QAbstractSeries *series)
12 14 */
General Comments 0
You need to be logged in to leave comments. Login now