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