##// END OF EJS Templates
Added insert, remove and other common methodds to QDonutGroup. Donut example added
Marek Rosa -
r1693:8b01a00ae1fe
parent child
Show More
@@ -0,0 +1,8
1 !include( ../examples.pri ) {
2 error( "Couldn't find the examples.pri file!" )
3 }
4
5 TARGET = donut
6 SOURCES += main.cpp\
7 widget.cpp
8 HEADERS += widget.h
@@ -0,0 +1,11
1 #include <QApplication>
2 #include "widget.h"
3
4 int main(int argc, char *argv[])
5 {
6 QApplication a(argc, argv);
7 Widget w;
8 w.show();
9
10 return a.exec();
11 }
@@ -0,0 +1,65
1 #include "widget.h"
2 #include <QChartView>
3 #include <QPieSeries>
4 #include <QPieSlice>
5 #include <QTime>
6 #include <QGridLayout>
7 #include <QTimer>
8
9 QTCOMMERCIALCHART_USE_NAMESPACE
10
11 Widget::Widget(QWidget *parent)
12 : QWidget(parent)
13 {
14 setMinimumSize(800, 600);
15 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
16
17 QChartView *chartView = new QChartView;
18 chartView->setRenderHint(QPainter::Antialiasing);
19
20 for (int i = 0; i < 7; i++) {
21 QPieSeries *donut = new QPieSeries;
22 donut->setLabelsVisible();
23 for (int j = 0; j < 4; j++) {
24 qreal value = 100 + qrand() % 100;
25 donut->append(QString("%1").arg(value), value);
26 donut->slices().last()->setLabelVisible(true);
27 // QFont labelFont = donut->slices().last()->labelFont();
28 // // labelFont.setUnderline(true);
29 // labelFont.setBold(true);
30 // donut->slices().last()->setLabelFont(labelFont);
31 donut->slices().last()->setLabelColor(Qt::white);
32 }
33 qreal phase = qrand() % 180;
34 donut->setPieStartAngle(phase);
35 donut->setPieEndAngle(360 + phase);
36 chartView->chart()->addSeries(donut);
37 m_donutsGroup.append(donut);
38 }
39
40 // create main layout
41 QGridLayout* mainLayout = new QGridLayout;
42 mainLayout->addWidget(chartView, 1, 1);
43 setLayout(mainLayout);
44
45 chartView->chart()->setAnimationOptions(QChart::AllAnimations);
46
47 QTimer *updateTimer = new QTimer(this);
48 connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateRotation()));
49 updateTimer->start(750);
50 }
51
52 Widget::~Widget()
53 {
54
55 }
56
57 void Widget::updateRotation()
58 {
59 for (int i = 0; i < m_donutsGroup.count(); i++) {
60 QPieSeries *donut = m_donutsGroup.donuts().at(i);
61 qreal phaseShift = -50 + qrand() % 100;
62 donut->setPieStartAngle(donut->pieStartAngle() + phaseShift);
63 donut->setPieEndAngle(donut->pieEndAngle() + phaseShift);
64 }
65 }
@@ -0,0 +1,24
1 #ifndef WIDGET_H
2 #define WIDGET_H
3
4 #include <QWidget>
5 #include <QDonutGroup>
6
7 QTCOMMERCIALCHART_USE_NAMESPACE
8
9 class Widget : public QWidget
10 {
11 Q_OBJECT
12
13 public:
14 Widget(QWidget *parent = 0);
15 ~Widget();
16
17 public slots:
18 void updateRotation();
19
20 private:
21 QDonutGroup m_donutsGroup;
22 };
23
24 #endif // WIDGET_H
@@ -1,29 +1,30
1 1 CURRENTLY_BUILDING_COMPONENTS = "examples"
2 2 !include( ../config.pri ) {
3 3 error( "Couldn't find the config.pri file!" )
4 4 }
5 5
6 6 TEMPLATE = subdirs
7 7 SUBDIRS += \
8 8 areachart \
9 9 customchart \
10 10 linechart \
11 11 percentbarchart \
12 12 piechart \
13 13 piechartdrilldown \
14 14 presenterchart \
15 15 scatterchart \
16 16 scatterinteractions \
17 17 splinechart \
18 18 stackedbarchart \
19 19 stackedbarchartdrilldown \
20 20 zoomlinechart \
21 21 modeldata \
22 22 barchart \
23 23 legend \
24 24 barmodelmapper \
25 25 qmlpiechart \
26 26 lineandbar \
27 27 horizontalbarchart \
28 28 horizontalstackedbarchart \
29 horizontalpercentbarchart
29 horizontalpercentbarchart \
30 donut
@@ -1,241 +1,249
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 if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
87 if (m_data.m_donut) {
88 painter->translate(m_labelTextRect.center());
89 painter->rotate(m_data.m_startAngle + m_data.m_angleSpan / 2);
90 painter->drawText(-m_labelTextRect.width() / 2, -m_labelTextRect.height() / 2, m_labelTextRect.width(), m_labelTextRect.height(), Qt::AlignCenter, m_data.m_labelText);
91 }else if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
88 92 painter->setClipRect(parentItem()->boundingRect());
89 93 painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color());
90 94 } else { // QPieSlice::LabelInside
91 95 painter->setClipPath(m_slicePath);
96 painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText);
92 97 }
93 painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText);
94 98
95 99 painter->restore();
96 100 }
97 101 }
98 102
99 103 void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
100 104 {
101 105 m_hovered = true;
102 106 emit hovered(true);
103 107 }
104 108
105 109 void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
106 110 {
107 111 m_hovered = false;
108 112 emit hovered(false);
109 113 }
110 114
111 115 void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
112 116 {
113 117 emit clicked(event->buttons());
114 118 }
115 119
116 120 void PieSliceItem::setLayout(const PieSliceData &sliceData)
117 121 {
118 122 m_data = sliceData;
119 123 updateGeometry();
120 124 update();
121 125 }
122 126
123 127 void PieSliceItem::updateGeometry()
124 128 {
125 129 if (m_data.m_radius <= 0)
126 130 return;
127 131
128 132 prepareGeometryChange();
129 133
130 134 // slice path
131 135 qreal centerAngle;
132 136 QPointF armStart;
133 137 m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, &centerAngle, &armStart);
134 138
135 139 // text rect
136 140 QFontMetricsF fm(m_data.m_labelFont);
137 141 m_labelTextRect = QRectF(0, 0, fm.width(m_data.m_labelText), fm.height());
138 142
139 143 // label arm path
140 144 QPointF labelTextStart;
141 145 m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart);
142 146
143 147 // text position
144 if (m_data.m_labelPosition == QPieSlice::LabelOutside)
148 if (m_data.m_donut) {
149 QPointF donutCenter = m_data.m_center + offset(centerAngle, m_data.m_innerRadius + (m_data.m_radius - m_data.m_innerRadius) / 2);
150 m_labelTextRect.moveCenter(donutCenter);
151 } else if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
145 152 m_labelTextRect.moveBottomLeft(labelTextStart);
146 else {// QPieSlice::LabelInside
153 } else {// QPieSlice::LabelInside
147 154 QPointF sliceCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2);
148 155 m_labelTextRect.moveCenter(sliceCenter);
149 156 }
150 157
151 158 // bounding rect
152 159 if (m_data.m_isLabelVisible)
153 160 m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect);
154 161 else
155 162 m_boundingRect = m_slicePath.boundingRect();
156 163 }
157 164
158 165 QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
159 166 {
160 167 if (slice->isExploded()) {
161 168 qreal centerAngle = slice->startAngle() + (slice->angleSpan()/2);
162 169 qreal len = radius * slice->explodeDistanceFactor();
163 170 point += offset(centerAngle, len);
164 171 }
165 172 return point;
166 173 }
167 174
168 175 QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF* armStart)
169 176 {
170 177 // calculate center angle
171 178 *centerAngle = startAngle + (angleSpan/2);
172 179
173 180 // calculate slice rectangle
174 181 QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2);
175 182
176 183 // slice path
177 184 QPainterPath path;
178 185 if (m_data.m_donut) {
179 186 QRectF insideRect(center.x() - m_data.m_innerRadius, center.y()-m_data.m_innerRadius, m_data.m_innerRadius*2, m_data.m_innerRadius*2);
180 187 path.arcMoveTo(rect, -startAngle + 90);
181 188 path.arcTo(rect, -startAngle + 90, -angleSpan);
182 189 path.arcTo(insideRect, -startAngle + 90 - angleSpan, angleSpan);
183 190 path.closeSubpath();
184 191 } else {
185 192 path.moveTo(rect.center());
186 193 path.arcTo(rect, -startAngle + 90, -angleSpan);
187 194 path.closeSubpath();
188 195 }
189 196
190 197 // calculate label arm start point
191 198 *armStart = center;
192 199 *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP);
193 200
194 201 return path;
195 202 }
196 203
197 204 QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart)
198 205 {
199 206 // Normalize the angle to 0-360 range
200 207 // NOTE: We are using int here on purpose. Depenging on platform and hardware
201 208 // qreal can be a double, float or something the user gives to the Qt configure
202 209 // (QT_COORD_TYPE). Compilers do not seem to support modulo for double or float
203 210 // but there are fmod() and fmodf() functions for that. So instead of some #ifdef
204 211 // that might break we just use int. Precision for this is just fine for our needs.
205 212 int normalized = angle * 10.0;
206 213 normalized = normalized % 3600;
207 214 if (normalized < 0)
208 215 normalized += 3600;
209 216 angle = (qreal) normalized / 10.0;
210 217
211 218 // prevent label arm pointing straight down because it will look bad
212 219 if (angle < 180 && angle > 170)
213 220 angle = 170;
214 221 if (angle > 180 && angle < 190)
215 222 angle = 190;
216 223
217 224 // line from slice to label
218 225 QPointF parm1 = start + offset(angle, length);
219 226
220 227 // line to underline the label
221 228 QPointF parm2 = parm1;
222 229 if (angle < 180) { // arm swings the other way on the left side
223 230 parm2 += QPointF(textWidth, 0);
224 231 *textStart = parm1;
225 232 }
226 233 else {
227 234 parm2 += QPointF(-textWidth,0);
228 235 *textStart = parm2;
229 236 }
230 237
231 238 QPainterPath path;
232 239 path.moveTo(start);
233 240 path.lineTo(parm1);
234 241 path.lineTo(parm2);
235 242
236 243 return path;
237 244 }
238 245
239 246 #include "moc_piesliceitem_p.cpp"
240 247
241 248 QTCOMMERCIALCHART_END_NAMESPACE
249
@@ -1,39 +1,75
1 1 #include "qdonutgroup.h"
2 2 #include "qdonutgroup_p.h"
3 3
4 4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 5
6 6 QDonutGroup::QDonutGroup(QObject *parent) :
7 7 QObject(parent),
8 8 d_ptr(new QDonutGroupPrivate(this))
9 9 {
10 10 }
11 11
12 12 void QDonutGroup::append(QPieSeries *donut)
13 13 {
14 insert(count(), donut);
15 }
16
17 bool QDonutGroup::insert(int index, QPieSeries* donut)
18 {
14 19 if (donut == 0)
15 return;
20 return false;
16 21
17 22 donut->setDonut();
18 23 Q_D(QDonutGroup);
19 d->m_donuts.append(donut);
24 d->m_donuts.insert(index, donut);
20 25 qreal donutFraction = 1.0 / (d->m_donuts.count() + 1);
21 26 for(int i = 0; i < d->m_donuts.count(); i++) {
22 27 d->m_donuts[i]->setPieSize( (i + 2) * donutFraction);
23 28 d->m_donuts[i]->setDonutInnerSize( (i + 1) * donutFraction);
24 29 }
30 return true;
31 }
32
33 bool QDonutGroup::remove(QPieSeries* donut)
34 {
35 Q_D(QDonutGroup);
36 int index = d->m_donuts.indexOf(donut);
37 if (index == -1)
38 return false;
39 else
40 d->m_donuts.removeOne(donut);
41
42 return true;
43 }
44
45 void QDonutGroup::clear()
46 {
47 Q_D(QDonutGroup);
48 d->m_donuts.clear();
49 }
50
51 QList<QPieSeries*> QDonutGroup::donuts() const
52 {
53 Q_D(const QDonutGroup);
54 return d->m_donuts;
55 }
56
57 int QDonutGroup::count() const
58 {
59 Q_D(const QDonutGroup);
60 return d->m_donuts.count();
25 61 }
26 62
27 63 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
28 64
29 65 QDonutGroupPrivate::QDonutGroupPrivate(QDonutGroup *q):
30 66 QObject(q),
31 67 q_ptr(q)
32 68 {
33 69 //
34 70 }
35 71
36 72 #include "moc_qdonutgroup.cpp"
37 73 #include "moc_qdonutgroup_p.cpp"
38 74
39 75 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,27 +1,35
1 1 #ifndef QDONUTGROUP_H
2 2 #define QDONUTGROUP_H
3 3
4 4 #include <QObject>
5 5 #include <QPieSeries>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 //class QPieSeries;
10 10 class QDonutGroupPrivate;
11 11
12 12 class QTCOMMERCIALCHART_EXPORT QDonutGroup : public QObject
13 13 {
14 14 Q_OBJECT
15 15 public:
16 16 explicit QDonutGroup(QObject *parent = 0);
17 17
18 18 void append(QPieSeries *donut);
19 19
20 bool insert(int index, QPieSeries* donut);
21
22 bool remove(QPieSeries* donut);
23 void clear();
24
25 QList<QPieSeries*> donuts() const;
26 int count() const;
27
20 28 protected:
21 29 QDonutGroupPrivate * const d_ptr;
22 30 Q_DECLARE_PRIVATE(QDonutGroup)
23 31 };
24 32
25 33 QTCOMMERCIALCHART_END_NAMESPACE
26 34
27 35 #endif // QDONUTGROUP_H
General Comments 0
You need to be logged in to leave comments. Login now