##// END OF EJS Templates
Minor fix.
Marek Rosa -
r1694:8ec588a26fd8
parent child
Show More
@@ -1,249 +1,250
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 #include <QDebug>
31 31
32 32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 33
34 34 QPointF offset(qreal angle, qreal length)
35 35 {
36 36 qreal dx = qSin(angle*(M_PI/180)) * length;
37 37 qreal dy = qCos(angle*(M_PI/180)) * length;
38 38 return QPointF(dx, -dy);
39 39 }
40 40
41 41 PieSliceItem::PieSliceItem(QGraphicsItem* parent)
42 42 :QGraphicsObject(parent),
43 43 m_hovered(false)
44 44 {
45 45 setAcceptHoverEvents(true);
46 46 setAcceptedMouseButtons(Qt::MouseButtonMask);
47 47 setZValue(ChartPresenter::PieSeriesZValue);
48 48 }
49 49
50 50 PieSliceItem::~PieSliceItem()
51 51 {
52 52 // If user is hovering over the slice and it gets destroyed we do
53 53 // not get a hover leave event. So we must emit the signal here.
54 54 if (m_hovered)
55 55 emit hovered(false);
56 56 }
57 57
58 58 QRectF PieSliceItem::boundingRect() const
59 59 {
60 60 return m_boundingRect;
61 61 }
62 62
63 63 QPainterPath PieSliceItem::shape() const
64 64 {
65 65 // Don't include the label and label arm.
66 66 // This is used to detect a mouse clicks. We do not want clicks from label.
67 67 return m_slicePath;
68 68 }
69 69
70 70 void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
71 71 {
72 72 painter->save();
73 73 painter->setClipRect(parentItem()->boundingRect());
74 74 painter->setPen(m_data.m_slicePen);
75 75 painter->setBrush(m_data.m_sliceBrush);
76 76 painter->drawPath(m_slicePath);
77 77 painter->restore();
78 78
79 79 if (m_data.m_isLabelVisible) {
80 80 painter->save();
81 81
82 82 // Pen for label arm not defined in the QPieSeries api, let's use brush's color instead
83 83 // Also, the drawText actually uses the pen color for the text color (unlike QGraphicsSimpleTextItem)
84 84 painter->setPen(m_data.m_labelBrush.color());
85 85 painter->setBrush(m_data.m_labelBrush);
86 86 painter->setFont(m_data.m_labelFont);
87 87 if (m_data.m_donut) {
88 88 painter->translate(m_labelTextRect.center());
89 89 painter->rotate(m_data.m_startAngle + m_data.m_angleSpan / 2);
90 90 painter->drawText(-m_labelTextRect.width() / 2, -m_labelTextRect.height() / 2, m_labelTextRect.width(), m_labelTextRect.height(), Qt::AlignCenter, m_data.m_labelText);
91 91 }else if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
92 92 painter->setClipRect(parentItem()->boundingRect());
93 93 painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color());
94 painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText);
94 95 } else { // QPieSlice::LabelInside
95 96 painter->setClipPath(m_slicePath);
96 97 painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText);
97 98 }
98 99
99 100 painter->restore();
100 101 }
101 102 }
102 103
103 104 void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
104 105 {
105 106 m_hovered = true;
106 107 emit hovered(true);
107 108 }
108 109
109 110 void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
110 111 {
111 112 m_hovered = false;
112 113 emit hovered(false);
113 114 }
114 115
115 116 void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
116 117 {
117 118 emit clicked(event->buttons());
118 119 }
119 120
120 121 void PieSliceItem::setLayout(const PieSliceData &sliceData)
121 122 {
122 123 m_data = sliceData;
123 124 updateGeometry();
124 125 update();
125 126 }
126 127
127 128 void PieSliceItem::updateGeometry()
128 129 {
129 130 if (m_data.m_radius <= 0)
130 131 return;
131 132
132 133 prepareGeometryChange();
133 134
134 135 // slice path
135 136 qreal centerAngle;
136 137 QPointF armStart;
137 138 m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, &centerAngle, &armStart);
138 139
139 140 // text rect
140 141 QFontMetricsF fm(m_data.m_labelFont);
141 142 m_labelTextRect = QRectF(0, 0, fm.width(m_data.m_labelText), fm.height());
142 143
143 144 // label arm path
144 145 QPointF labelTextStart;
145 146 m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart);
146 147
147 148 // text position
148 149 if (m_data.m_donut) {
149 150 QPointF donutCenter = m_data.m_center + offset(centerAngle, m_data.m_innerRadius + (m_data.m_radius - m_data.m_innerRadius) / 2);
150 151 m_labelTextRect.moveCenter(donutCenter);
151 152 } else if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
152 153 m_labelTextRect.moveBottomLeft(labelTextStart);
153 154 } else {// QPieSlice::LabelInside
154 155 QPointF sliceCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2);
155 156 m_labelTextRect.moveCenter(sliceCenter);
156 157 }
157 158
158 159 // bounding rect
159 160 if (m_data.m_isLabelVisible)
160 161 m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect);
161 162 else
162 163 m_boundingRect = m_slicePath.boundingRect();
163 164 }
164 165
165 166 QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
166 167 {
167 168 if (slice->isExploded()) {
168 169 qreal centerAngle = slice->startAngle() + (slice->angleSpan()/2);
169 170 qreal len = radius * slice->explodeDistanceFactor();
170 171 point += offset(centerAngle, len);
171 172 }
172 173 return point;
173 174 }
174 175
175 176 QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF* armStart)
176 177 {
177 178 // calculate center angle
178 179 *centerAngle = startAngle + (angleSpan/2);
179 180
180 181 // calculate slice rectangle
181 182 QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2);
182 183
183 184 // slice path
184 185 QPainterPath path;
185 186 if (m_data.m_donut) {
186 187 QRectF insideRect(center.x() - m_data.m_innerRadius, center.y()-m_data.m_innerRadius, m_data.m_innerRadius*2, m_data.m_innerRadius*2);
187 188 path.arcMoveTo(rect, -startAngle + 90);
188 189 path.arcTo(rect, -startAngle + 90, -angleSpan);
189 190 path.arcTo(insideRect, -startAngle + 90 - angleSpan, angleSpan);
190 191 path.closeSubpath();
191 192 } else {
192 193 path.moveTo(rect.center());
193 194 path.arcTo(rect, -startAngle + 90, -angleSpan);
194 195 path.closeSubpath();
195 196 }
196 197
197 198 // calculate label arm start point
198 199 *armStart = center;
199 200 *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP);
200 201
201 202 return path;
202 203 }
203 204
204 205 QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart)
205 206 {
206 207 // Normalize the angle to 0-360 range
207 208 // NOTE: We are using int here on purpose. Depenging on platform and hardware
208 209 // qreal can be a double, float or something the user gives to the Qt configure
209 210 // (QT_COORD_TYPE). Compilers do not seem to support modulo for double or float
210 211 // but there are fmod() and fmodf() functions for that. So instead of some #ifdef
211 212 // that might break we just use int. Precision for this is just fine for our needs.
212 213 int normalized = angle * 10.0;
213 214 normalized = normalized % 3600;
214 215 if (normalized < 0)
215 216 normalized += 3600;
216 217 angle = (qreal) normalized / 10.0;
217 218
218 219 // prevent label arm pointing straight down because it will look bad
219 220 if (angle < 180 && angle > 170)
220 221 angle = 170;
221 222 if (angle > 180 && angle < 190)
222 223 angle = 190;
223 224
224 225 // line from slice to label
225 226 QPointF parm1 = start + offset(angle, length);
226 227
227 228 // line to underline the label
228 229 QPointF parm2 = parm1;
229 230 if (angle < 180) { // arm swings the other way on the left side
230 231 parm2 += QPointF(textWidth, 0);
231 232 *textStart = parm1;
232 233 }
233 234 else {
234 235 parm2 += QPointF(-textWidth,0);
235 236 *textStart = parm2;
236 237 }
237 238
238 239 QPainterPath path;
239 240 path.moveTo(start);
240 241 path.lineTo(parm1);
241 242 path.lineTo(parm2);
242 243
243 244 return path;
244 245 }
245 246
246 247 #include "moc_piesliceitem_p.cpp"
247 248
248 249 QTCOMMERCIALCHART_END_NAMESPACE
249 250
General Comments 0
You need to be logged in to leave comments. Login now