##// END OF EJS Templates
Emit hover leave signal from PieSliceItem when it gets destroyed....
Jani Honkonen -
r1083:b264b1ddc4c8
parent child
Show More
@@ -1,62 +1,63
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "drilldownslice.h"
22 22
23 23 QTCOMMERCIALCHART_USE_NAMESPACE
24 24
25 25 DrilldownSlice::DrilldownSlice(qreal value, QString prefix, QAbstractSeries* drilldownSeries)
26 26 :m_drilldownSeries(drilldownSeries),
27 27 m_prefix(prefix)
28 28 {
29 29 setValue(value);
30 30 updateLabel();
31 31 setLabelFont(QFont("Arial", 8));
32 32 connect(this, SIGNAL(changed()), this, SLOT(updateLabel()));
33 33 connect(this, SIGNAL(hovered(bool)), this, SLOT(showHighlight(bool)));
34 34 }
35 35
36 36 DrilldownSlice::~DrilldownSlice()
37 37 {
38 38
39 39 }
40 40
41 41 QAbstractSeries* DrilldownSlice::drilldownSeries() const
42 42 {
43 43 return m_drilldownSeries;
44 44 }
45 45
46 46 void DrilldownSlice::updateLabel()
47 47 {
48 48 QString label = m_prefix;
49 49 label += " $";
50 50 label += QString::number(this->value());
51 51 label += ", ";
52 52 label += QString::number(this->percentage()*100, 'f', 1);
53 53 label += "%";
54 54 setLabel(label);
55 55 }
56 56
57 57 void DrilldownSlice::showHighlight(bool show)
58 58 {
59 59 setLabelVisible(show);
60 setExploded(show);
60 61 }
61 62
62 63 #include "moc_drilldownslice.cpp"
@@ -1,214 +1,220
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "piesliceitem_p.h"
22 22 #include "piechartitem_p.h"
23 23 #include "qpieseries.h"
24 24 #include "qpieslice.h"
25 25 #include "chartpresenter_p.h"
26 26 #include <QPainter>
27 27 #include <qmath.h>
28 28 #include <QGraphicsSceneEvent>
29 29 #include <QTime>
30 30
31 31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 32
33 33 #define PI 3.14159265 // TODO: is this defined in some header?
34 34
35 35 QPointF offset(qreal angle, qreal length)
36 36 {
37 37 qreal dx = qSin(angle*(PI/180)) * length;
38 38 qreal dy = qCos(angle*(PI/180)) * length;
39 39 return QPointF(dx, -dy);
40 40 }
41 41
42 42 PieSliceItem::PieSliceItem(QGraphicsItem* parent)
43 :QGraphicsObject(parent)
43 :QGraphicsObject(parent),
44 m_hovered(false)
44 45 {
45 46 setAcceptHoverEvents(true);
46 47 setAcceptedMouseButtons(Qt::MouseButtonMask);
47 48 setZValue(ChartPresenter::PieSeriesZValue);
48 49 }
49 50
50 51 PieSliceItem::~PieSliceItem()
51 52 {
52
53 // If user is hovering over the slice and it gets destroyed we do
54 // not get a hover leave event. So we must emit the signal here.
55 if (m_hovered)
56 emit hovered(false);
53 57 }
54 58
55 59 QRectF PieSliceItem::boundingRect() const
56 60 {
57 61 return m_boundingRect;
58 62 }
59 63
60 64 QPainterPath PieSliceItem::shape() const
61 65 {
62 66 // Don't include the label and label arm.
63 67 // This is used to detect a mouse clicks. We do not want clicks from label.
64 68 return m_slicePath;
65 69 }
66 70
67 71 void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
68 72 {
69 73 painter->save();
70 74 painter->setClipRect(parentItem()->boundingRect());
71 75 painter->setPen(m_data.m_slicePen);
72 76 painter->setBrush(m_data.m_sliceBrush);
73 77 painter->drawPath(m_slicePath);
74 78 painter->restore();
75 79
76 80 if (m_data.m_isLabelVisible) {
77 81 painter->setClipRect(parentItem()->boundingRect());
78 82 painter->setPen(m_data.m_labelPen);
79 83 painter->drawPath(m_labelArmPath);
80 84 // the pen color will affect the font color as well
81 85 painter->setFont(m_data.m_labelFont);
82 86 painter->drawText(m_labelTextRect.bottomLeft(), m_data.m_labelText);
83 87 }
84 88 }
85 89
86 90 void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
87 91 {
92 m_hovered = true;
88 93 emit hovered(true);
89 94 }
90 95
91 96 void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
92 97 {
98 m_hovered = false;
93 99 emit hovered(false);
94 100 }
95 101
96 102 void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
97 103 {
98 104 emit clicked(event->buttons());
99 105 }
100 106
101 107 void PieSliceItem::setLayout(const PieSliceData &sliceData)
102 108 {
103 109 m_data = sliceData;
104 110 updateGeometry();
105 111 update();
106 112 }
107 113
108 114 void PieSliceItem::updateGeometry()
109 115 {
110 116 if (m_data.m_radius <= 0)
111 117 return;
112 118
113 119 prepareGeometryChange();
114 120
115 121 // update slice path
116 122 qreal centerAngle;
117 123 QPointF armStart;
118 124 m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, &centerAngle, &armStart);
119 125
120 126 // update text rect
121 127 m_labelTextRect = labelTextRect(m_data.m_labelFont, m_data.m_labelText);
122 128
123 129 // update label arm path
124 130 QPointF labelTextStart;
125 131 m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart);
126 132
127 133 // update text position
128 134 m_labelTextRect.moveBottomLeft(labelTextStart);
129 135
130 136 // update bounding rect
131 137 if (m_data.m_isLabelVisible)
132 138 m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect);
133 139 else
134 140 m_boundingRect = m_slicePath.boundingRect();
135 141 }
136 142
137 143 QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
138 144 {
139 145 if (slice->isExploded()) {
140 146 qreal centerAngle = slice->startAngle() + ((slice->endAngle() - slice->startAngle())/2);
141 147 qreal len = radius * slice->explodeDistanceFactor();
142 148 qreal dx = qSin(centerAngle*(PI/180)) * len;
143 149 qreal dy = -qCos(centerAngle*(PI/180)) * len;
144 150 point += QPointF(dx, dy);
145 151 }
146 152 return point;
147 153 }
148 154
149 155 QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF* armStart)
150 156 {
151 157 // calculate center angle
152 158 *centerAngle = startAngle + (angleSpan/2);
153 159
154 160 // calculate slice rectangle
155 161 QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2);
156 162
157 163 // slice path
158 164 // TODO: draw the shape so that it might have a hole in the center
159 165 QPainterPath path;
160 166 path.moveTo(rect.center());
161 167 path.arcTo(rect, -startAngle + 90, -angleSpan);
162 168 path.closeSubpath();
163 169
164 170 // calculate label arm start point
165 171 *armStart = center;
166 172 *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP);
167 173
168 174 return path;
169 175 }
170 176
171 177 QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart)
172 178 {
173 179 // prevent label arm pointing straight down because it will look bad
174 180 if (angle < 180 && angle > 170)
175 181 angle = 170;
176 182 if (angle > 180 && angle < 190)
177 183 angle = 190;
178 184
179 185 // line from slice to label
180 186 qreal dx = qSin(angle*(PI/180)) * length;
181 187 qreal dy = -qCos(angle*(PI/180)) * length;
182 188 QPointF parm1 = start + QPointF(dx, dy);
183 189
184 190 // line to underline the label
185 191 QPointF parm2 = parm1;
186 192 if (angle < 180) { // arm swings the other way on the left side
187 193 parm2 += QPointF(textWidth, 0);
188 194 *textStart = parm1;
189 195 }
190 196 else {
191 197 parm2 += QPointF(-textWidth,0);
192 198 *textStart = parm2;
193 199 }
194 200
195 201 // elevate the text position a bit so that it does not hit the line
196 202 *textStart += QPointF(0, -3);
197 203
198 204 QPainterPath path;
199 205 path.moveTo(start);
200 206 path.lineTo(parm1);
201 207 path.lineTo(parm2);
202 208
203 209 return path;
204 210 }
205 211
206 212 QRectF PieSliceItem::labelTextRect(QFont font, QString text)
207 213 {
208 214 QFontMetricsF fm(font);
209 215 return fm.boundingRect(text);
210 216 }
211 217
212 218 #include "moc_piesliceitem_p.cpp"
213 219
214 220 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,79 +1,80
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef PIESLICEITEM_H
22 22 #define PIESLICEITEM_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include "charttheme_p.h"
26 26 #include "qpieseries.h"
27 27 #include "pieslicedata_p.h"
28 28 #include <QGraphicsItem>
29 29 #include <QRectF>
30 30 #include <QColor>
31 31 #include <QPen>
32 32
33 33 #define PIESLICE_LABEL_GAP 5
34 34
35 35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 36 class PieChartItem;
37 37 class PieSliceLabel;
38 38 class QPieSlice;
39 39
40 40 class PieSliceItem : public QGraphicsObject
41 41 {
42 42 Q_OBJECT
43 43
44 44 public:
45 45 PieSliceItem(QGraphicsItem* parent = 0);
46 46 ~PieSliceItem();
47 47
48 48 // from QGraphicsItem
49 49 QRectF boundingRect() const;
50 50 QPainterPath shape() const;
51 51 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
52 52 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
53 53 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
54 54 void mousePressEvent(QGraphicsSceneMouseEvent *event);
55 55
56 56 void setLayout(const PieSliceData &sliceData);
57 57 static QPointF sliceCenter(QPointF point, qreal radius, QPieSlice *slice);
58 58
59 59 Q_SIGNALS:
60 60 void clicked(Qt::MouseButtons buttons);
61 61 void hovered(bool state);
62 62
63 63 private:
64 64 void updateGeometry();
65 65 QPainterPath slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF *armStart);
66 66 QPainterPath labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart);
67 67 QRectF labelTextRect(QFont font, QString text);
68 68
69 69 private:
70 70 PieSliceData m_data;
71 71 QRectF m_boundingRect;
72 72 QPainterPath m_slicePath;
73 73 QPainterPath m_labelArmPath;
74 74 QRectF m_labelTextRect;
75 bool m_hovered;
75 76 };
76 77
77 78 QTCOMMERCIALCHART_END_NAMESPACE
78 79
79 80 #endif // PIESLICEITEM_H
General Comments 0
You need to be logged in to leave comments. Login now