|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2016 The Qt Company Ltd. |
|
4 ** Contact: https://www.qt.io/licensing/ |
|
5 ** |
|
6 ** This file is part of the Qt Charts module of the Qt Toolkit. |
|
7 ** |
|
8 ** $QT_BEGIN_LICENSE:GPL$ |
|
9 ** Commercial License Usage |
|
10 ** Licensees holding valid commercial Qt licenses may use this file in |
|
11 ** accordance with the commercial license agreement provided with the |
|
12 ** Software or, alternatively, in accordance with the terms contained in |
|
13 ** a written agreement between you and The Qt Company. For licensing terms |
|
14 ** and conditions see https://www.qt.io/terms-conditions. For further |
|
15 ** information use the contact form at https://www.qt.io/contact-us. |
|
16 ** |
|
17 ** GNU General Public License Usage |
|
18 ** Alternatively, this file may be used under the terms of the GNU |
|
19 ** General Public License version 3 or (at your option) any later version |
|
20 ** approved by the KDE Free Qt Foundation. The licenses are as published by |
|
21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
|
22 ** included in the packaging of this file. Please review the following |
|
23 ** information to ensure the GNU General Public License requirements will |
|
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
|
25 ** |
|
26 ** $QT_END_LICENSE$ |
|
27 ** |
|
28 ****************************************************************************/ |
|
29 |
|
30 #include "callout.h" |
|
31 #include <QtGui/QPainter> |
|
32 #include <QtGui/QFontMetrics> |
|
33 #include <QtWidgets/QGraphicsSceneMouseEvent> |
|
34 #include <QtGui/QMouseEvent> |
|
35 #include <QtCharts/QChart> |
|
36 |
|
37 Callout::Callout(QChart *chart, QAbstractSeries *series) : |
|
38 QGraphicsItem(chart), m_chart(chart), m_series(series) |
|
39 { |
|
40 } |
|
41 |
|
42 |
|
43 QRectF Callout::boundingRect() const |
|
44 { |
|
45 QPointF anchor = mapFromParent(m_chart->mapToPosition(m_anchor, m_series)); |
|
46 QRectF rect; |
|
47 rect.setLeft(qMin(m_rect.left(), anchor.x())); |
|
48 rect.setRight(qMax(m_rect.right(), anchor.x())); |
|
49 rect.setTop(qMin(m_rect.top(), anchor.y())); |
|
50 rect.setBottom(qMax(m_rect.bottom(), anchor.y())); |
|
51 return rect; |
|
52 } |
|
53 |
|
54 |
|
55 void Callout::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
56 { |
|
57 Q_UNUSED(option) |
|
58 Q_UNUSED(widget) |
|
59 QPainterPath path; |
|
60 painter->setPen(Qt::black); |
|
61 path.addRoundedRect(m_rect, 5, 5); |
|
62 |
|
63 QPointF anchor = mapFromParent(m_chart->mapToPosition(m_anchor, m_series)); |
|
64 if (!m_rect.contains(anchor)) { |
|
65 QPointF point1, point2; |
|
66 |
|
67 // establish the position of the anchor point in relation to m_rect |
|
68 bool above = anchor.y() <= m_rect.top(); |
|
69 bool aboveCenter = anchor.y() > m_rect.top() && anchor.y() <= m_rect.center().y(); |
|
70 bool belowCenter = anchor.y() > m_rect.center().y() && anchor.y() <= m_rect.bottom(); |
|
71 bool below = anchor.y() > m_rect.bottom(); |
|
72 |
|
73 bool onLeft = anchor.x() <= m_rect.left(); |
|
74 bool leftOfCenter = anchor.x() > m_rect.left() && anchor.x() <= m_rect.center().x(); |
|
75 bool rightOfCenter = anchor.x() > m_rect.center().x() && anchor.x() <= m_rect.right(); |
|
76 bool onRight = anchor.x() > m_rect.right(); |
|
77 |
|
78 // get the nearest m_rect corner. |
|
79 qreal x = (onRight + rightOfCenter) * m_rect.width(); |
|
80 qreal y = (below + belowCenter) * m_rect.height(); |
|
81 bool cornerCase = (above && onLeft) || (above && onRight) || (below && onLeft) || (below && onRight); |
|
82 bool vertical = qAbs(anchor.x() - x) > qAbs(anchor.y() - y); |
|
83 |
|
84 qreal x1 = x + leftOfCenter * 10 - rightOfCenter * 20 + cornerCase * !vertical * (onLeft * 10 - onRight * 20); |
|
85 qreal y1 = y + aboveCenter * 10 - belowCenter * 20 + cornerCase * vertical * (above * 10 - below * 20);; |
|
86 point1.setX(x1); |
|
87 point1.setY(y1); |
|
88 |
|
89 qreal x2 = x + leftOfCenter * 20 - rightOfCenter * 10 + cornerCase * !vertical * (onLeft * 20 - onRight * 10);; |
|
90 qreal y2 = y + aboveCenter * 20 - belowCenter * 10 + cornerCase * vertical * (above * 20 - below * 10);; |
|
91 point2.setX(x2); |
|
92 point2.setY(y2); |
|
93 |
|
94 path.moveTo(point1); |
|
95 path.lineTo(anchor); |
|
96 path.lineTo(point2); |
|
97 path = path.simplified(); |
|
98 } |
|
99 painter->setBrush(QColor(255, 255, 205)); |
|
100 painter->drawPath(path); |
|
101 painter->drawText(m_textRect, m_text); |
|
102 } |
|
103 |
|
104 |
|
105 void Callout::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
106 { |
|
107 event->setAccepted(true); |
|
108 } |
|
109 |
|
110 |
|
111 void Callout::mouseMoveEvent(QGraphicsSceneMouseEvent *event) |
|
112 { |
|
113 if (event->buttons() & Qt::LeftButton){ |
|
114 setPos(mapToParent(event->pos() - event->buttonDownPos(Qt::LeftButton))); |
|
115 event->setAccepted(true); |
|
116 } else { |
|
117 event->setAccepted(false); |
|
118 } |
|
119 } |
|
120 |
|
121 |
|
122 void Callout::setSeries(QAbstractSeries *series) { m_series = series; } |
|
123 |
|
124 |
|
125 void Callout::setText(const QString &text) |
|
126 { |
|
127 m_text = text; |
|
128 QFontMetrics metrics(m_font); |
|
129 m_textRect = metrics.boundingRect(QRect(0, 0, 150, 150), Qt::AlignLeft, m_text); |
|
130 m_textRect.translate(5, 5); |
|
131 prepareGeometryChange(); |
|
132 m_rect = m_textRect.adjusted(-5, -5, 5, 5); |
|
133 } |
|
134 |
|
135 |
|
136 void Callout::setAnchor(QPointF point) |
|
137 { |
|
138 m_anchor = point; |
|
139 } |
|
140 |
|
141 |
|
142 void Callout::updateGeometry() |
|
143 { |
|
144 prepareGeometryChange(); |
|
145 setPos(m_chart->mapToPosition(m_anchor, m_series) + QPoint(10, -50)); |
|
146 } |
|
147 |