##// END OF EJS Templates
Removed QDonutGroup class. Added Donut Drill down example
Marek Rosa -
r1697:b1c06caff815
parent child
Show More
@@ -0,0 +1,9
1 !include( ../examples.pri ) {
2 error( "Couldn't find the examples.pri file!" )
3 }
4
5 TARGET = donutdrilldown
6 SOURCES += main.cpp\
7 widget.cpp
8
9 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,120
1 #include "widget.h"
2 #include <QGridLayout>
3
4 #include <QPieSlice>
5 #include <QTime>
6 #include <QChartView>
7 #include <QTimer>
8
9 QTCOMMERCIALCHART_USE_NAMESPACE
10
11 Widget::Widget(QWidget *parent)
12 : QWidget(parent),
13 mainData(0)
14 {
15 setMinimumSize(800, 600);
16 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
17
18 QChartView *chartView = new QChartView;
19 chartView->setRenderHint(QPainter::Antialiasing);
20 chartView->chart()->setAnimationOptions(QChart::AllAnimations);
21
22 mainData = new QPieSeries;
23 for (int j = 0; j < 4; j++) {
24
25 // create new slice for the mainData
26 QPieSlice *slice = new QPieSlice;
27 slice->setLabelPosition(QPieSlice::LabelInside);
28 slice->setLabelColor(Qt::white);
29 mainData->append(slice);
30
31 // create a new detailed data for the slice
32 QPieSeries *donut = new QPieSeries;
33 donut->setDonut();
34 donut->setLabelsVisible();
35 donut->setDonutInnerSize(mainData->pieSize());
36 donut->setPieSize(mainData->pieSize() + 0.2);
37
38 // when mainData slice is redrawn make sure the detailed data slices are aligned with it
39 connect(slice, SIGNAL(startAngleChanged()), this, SLOT(updatedStartAngle()));
40 connect(slice, SIGNAL(angleSpanChanged()), this, SLOT(updatedAngleSpan()));
41
42 // create the detailed data
43 for (int j = 0; j < 3; j++) {
44 qreal value = 10 + qrand() % 100;
45 QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value);
46 donut->append(slice);
47 donut->slices().last()->setLabelVisible(true);
48 donut->slices().last()->setLabelColor(Qt::white);
49 }
50 detailedData.append(donut);
51
52 // update the value and label of mainData
53 slice->setValue(donut->sum());
54 slice->setLabel(QString("%1").arg(donut->sum()));
55 }
56
57 mainData->setLabelsVisible();
58 chartView->chart()->addSeries(mainData);
59 for (int i = 0; i < detailedData.count(); i++)
60 chartView->chart()->addSeries(detailedData.at(i));
61
62 // create main layout
63 QGridLayout* mainLayout = new QGridLayout;
64 mainLayout->addWidget(chartView, 1, 1);
65 setLayout(mainLayout);
66
67 // modify the value of one detailed slice every 2.5 sec
68 QTimer *updateTimer = new QTimer(this);
69 connect(updateTimer, SIGNAL(timeout()), this, SLOT(highlight()));
70 updateTimer->start(2500);
71 }
72
73 Widget::~Widget()
74 {
75
76 }
77
78 void Widget::updatedStartAngle()
79 {
80 // when the mainData slice has been updated the detailed data QPieSeries object as well
81 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
82 QPieSeries *detailsDonut = detailedData.at(slice->series()->slices().indexOf(slice));
83 detailsDonut->setPieStartAngle(slice->startAngle());
84 }
85
86 void Widget::updatedAngleSpan()
87 {
88 // when the mainData slice has been updated the detailed data QPieSeries object as well
89 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
90 QPieSeries *detailsDonut = detailedData.at(slice->series()->slices().indexOf(slice));
91 detailsDonut->setPieEndAngle(slice->startAngle() + slice->angleSpan());
92 }
93
94 void Widget::updateRotation()
95 {
96 // update the selected slice
97 qreal newValue = 10 + qrand() % 100;
98 detailedData.at(detailIndex)->slices().at(sliceIndex)->setValue(newValue);
99 detailedData.at(detailIndex)->slices().at(sliceIndex)->setLabel(QString("%1").arg(newValue));
100
101 // update the mainData slice with a new sum of the detailed data values
102 mainData->slices().at(detailIndex)->setValue(detailedData.at(detailIndex)->sum());
103 mainData->slices().at(detailIndex)->setLabel(QString("%1").arg(detailedData.at(detailIndex)->sum()));
104
105 // change the explode state of the selected slice back to normal
106 detailedData.at(detailIndex)->slices().at(sliceIndex)->setExploded(false);
107 }
108
109 void Widget::highlight()
110 {
111 // choose one random detailed data slice to be updated.
112 detailIndex = qrand() % mainData->count();
113 sliceIndex = qrand() % detailedData.at(detailIndex)->count();
114
115 // set the slice to exploded to make the change easier to observe
116 detailedData.at(detailIndex)->slices().at(sliceIndex)->setExploded();
117
118 // give the user time to focus on the slice that will be changed
119 QTimer::singleShot(1000, this, SLOT(updateRotation()));
120 }
@@ -0,0 +1,30
1 #ifndef WIDGET_H
2 #define WIDGET_H
3
4 #include <QWidget>
5 #include <QPieSeries>
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 updatedStartAngle();
19 void updatedAngleSpan();
20 void updateRotation();
21 void highlight();
22
23 private:
24 QPieSeries *mainData;
25 QList<QPieSeries *> detailedData;
26 int detailIndex;
27 int sliceIndex;
28 };
29
30 #endif // WIDGET_H
@@ -1,65 +1,95
1 #include "widget.h"
1 #include "widget.h"
2 #include <QChartView>
2 #include <QChartView>
3 #include <QPieSeries>
3 #include <QPieSeries>
4 #include <QPieSlice>
4 #include <QPieSlice>
5 #include <QTime>
5 #include <QTime>
6 #include <QGridLayout>
6 #include <QGridLayout>
7 #include <QTimer>
7 #include <QTimer>
8
8
9 QTCOMMERCIALCHART_USE_NAMESPACE
9 QTCOMMERCIALCHART_USE_NAMESPACE
10
10
11 Widget::Widget(QWidget *parent)
11 Widget::Widget(QWidget *parent)
12 : QWidget(parent)
12 : QWidget(parent)
13 {
13 {
14 setMinimumSize(800, 600);
14 setMinimumSize(800, 600);
15 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
15 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
16
16
17 QChartView *chartView = new QChartView;
17 QChartView *chartView = new QChartView;
18 chartView->setRenderHint(QPainter::Antialiasing);
18 chartView->setRenderHint(QPainter::Antialiasing);
19
19
20 for (int i = 0; i < 7; i++) {
20 qreal minSize = 0.1;
21 qreal maxSize = 0.9;
22 int donutsCount = 5;
23 for (int i = 0; i < donutsCount; i++) {
21 QPieSeries *donut = new QPieSeries;
24 QPieSeries *donut = new QPieSeries;
25 donut->setDonut();
22 donut->setLabelsVisible();
26 donut->setLabelsVisible();
23 for (int j = 0; j < 4; j++) {
27 int sliceCount = 3 + qrand() % 3;
28 for (int j = 0; j < sliceCount; j++) {
24 qreal value = 100 + qrand() % 100;
29 qreal value = 100 + qrand() % 100;
25 donut->append(QString("%1").arg(value), value);
30 QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value);
31 connect(slice, SIGNAL(hovered(bool)), this, SLOT(explodeSlice(bool)));
32 donut->append(slice);
26 donut->slices().last()->setLabelVisible(true);
33 donut->slices().last()->setLabelVisible(true);
27 // QFont labelFont = donut->slices().last()->labelFont();
34 donut->slices().last()->setLabelColor(Qt::white);
28 // // labelFont.setUnderline(true);
35 donut->setDonutInnerSize(minSize + i * (maxSize - minSize) / donutsCount);
29 // labelFont.setBold(true);
36 donut->setPieSize(minSize + (i + 1) * (maxSize - minSize) / donutsCount);
30 // donut->slices().last()->setLabelFont(labelFont);
31 donut->slices().last()->setLabelColor(Qt::white);
32 }
37 }
38 m_donuts.append(donut);
33 qreal phase = qrand() % 180;
39 qreal phase = qrand() % 180;
34 donut->setPieStartAngle(phase);
40 donut->setPieStartAngle(phase);
35 donut->setPieEndAngle(360 + phase);
41 donut->setPieEndAngle(360 + phase);
36 chartView->chart()->addSeries(donut);
42 chartView->chart()->addSeries(donut);
37 m_donutsGroup.append(donut);
38 }
43 }
39
44
40 // create main layout
45 // create main layout
41 QGridLayout* mainLayout = new QGridLayout;
46 QGridLayout* mainLayout = new QGridLayout;
42 mainLayout->addWidget(chartView, 1, 1);
47 mainLayout->addWidget(chartView, 1, 1);
43 setLayout(mainLayout);
48 setLayout(mainLayout);
44
49
45 chartView->chart()->setAnimationOptions(QChart::AllAnimations);
50 chartView->chart()->setAnimationOptions(QChart::AllAnimations);
46
51
47 QTimer *updateTimer = new QTimer(this);
52 updateTimer = new QTimer(this);
48 connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateRotation()));
53 connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateRotation()));
49 updateTimer->start(750);
54 updateTimer->start(750);
50 }
55 }
51
56
52 Widget::~Widget()
57 Widget::~Widget()
53 {
58 {
54
59
55 }
60 }
56
61
57 void Widget::updateRotation()
62 void Widget::updateRotation()
58 {
63 {
59 for (int i = 0; i < m_donutsGroup.count(); i++) {
64 // int tobeupdated = qrand() % m_donutsGroup.count();
60 QPieSeries *donut = m_donutsGroup.donuts().at(i);
65 for (int i = 0; i < m_donuts.count(); i++) {
66 QPieSeries *donut = m_donuts.at(i);
61 qreal phaseShift = -50 + qrand() % 100;
67 qreal phaseShift = -50 + qrand() % 100;
62 donut->setPieStartAngle(donut->pieStartAngle() + phaseShift);
68 donut->setPieStartAngle(donut->pieStartAngle() + phaseShift);
63 donut->setPieEndAngle(donut->pieEndAngle() + phaseShift);
69 donut->setPieEndAngle(donut->pieEndAngle() + phaseShift);
64 }
70 }
65 }
71 }
72
73 void Widget::explodeSlice(bool exploded)
74 {
75 QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
76 if (exploded) {
77 updateTimer->stop();
78 qreal sliceStartAngle = slice->startAngle();
79 qreal sliceEndAngle = slice->startAngle() + slice->angleSpan();
80
81 QPieSeries *donut = slice->series();
82 qreal seriesIndex = m_donuts.indexOf(donut);
83 for (int i = seriesIndex + 1; i < m_donuts.count(); i++) {
84 m_donuts.at(i)->setPieStartAngle(sliceEndAngle);
85 m_donuts.at(i)->setPieEndAngle(360 + sliceStartAngle);
86 }
87 } else {
88 for (int i = 0; i < m_donuts.count(); i++) {
89 m_donuts.at(i)->setPieStartAngle(0);
90 m_donuts.at(i)->setPieEndAngle(360);
91 }
92 updateTimer->start();
93 }
94 slice->setExploded(exploded);
95 }
@@ -1,24 +1,28
1 #ifndef WIDGET_H
1 #ifndef WIDGET_H
2 #define WIDGET_H
2 #define WIDGET_H
3
3
4 #include <QWidget>
4 #include <QWidget>
5 #include <QDonutGroup>
5 #include <QPieSeries>
6
7 class QTimer;
6
8
7 QTCOMMERCIALCHART_USE_NAMESPACE
9 QTCOMMERCIALCHART_USE_NAMESPACE
8
10
9 class Widget : public QWidget
11 class Widget : public QWidget
10 {
12 {
11 Q_OBJECT
13 Q_OBJECT
12
14
13 public:
15 public:
14 Widget(QWidget *parent = 0);
16 Widget(QWidget *parent = 0);
15 ~Widget();
17 ~Widget();
16
18
17 public slots:
19 public slots:
18 void updateRotation();
20 void updateRotation();
21 void explodeSlice(bool exploded);
19
22
20 private:
23 private:
21 QDonutGroup m_donutsGroup;
24 QList<QPieSeries *> m_donuts;
25 QTimer *updateTimer;
22 };
26 };
23
27
24 #endif // WIDGET_H
28 #endif // WIDGET_H
@@ -1,30 +1,31
1 CURRENTLY_BUILDING_COMPONENTS = "examples"
1 CURRENTLY_BUILDING_COMPONENTS = "examples"
2 !include( ../config.pri ) {
2 !include( ../config.pri ) {
3 error( "Couldn't find the config.pri file!" )
3 error( "Couldn't find the config.pri file!" )
4 }
4 }
5
5
6 TEMPLATE = subdirs
6 TEMPLATE = subdirs
7 SUBDIRS += \
7 SUBDIRS += \
8 areachart \
8 areachart \
9 customchart \
9 customchart \
10 linechart \
10 linechart \
11 percentbarchart \
11 percentbarchart \
12 piechart \
12 piechart \
13 piechartdrilldown \
13 piechartdrilldown \
14 presenterchart \
14 presenterchart \
15 scatterchart \
15 scatterchart \
16 scatterinteractions \
16 scatterinteractions \
17 splinechart \
17 splinechart \
18 stackedbarchart \
18 stackedbarchart \
19 stackedbarchartdrilldown \
19 stackedbarchartdrilldown \
20 zoomlinechart \
20 zoomlinechart \
21 modeldata \
21 modeldata \
22 barchart \
22 barchart \
23 legend \
23 legend \
24 barmodelmapper \
24 barmodelmapper \
25 qmlpiechart \
25 qmlpiechart \
26 lineandbar \
26 lineandbar \
27 horizontalbarchart \
27 horizontalbarchart \
28 horizontalstackedbarchart \
28 horizontalstackedbarchart \
29 horizontalpercentbarchart \
29 horizontalpercentbarchart \
30 donut
30 donut \
31 donutdrilldown
@@ -1,29 +1,26
1 INCLUDEPATH += $$PWD
1 INCLUDEPATH += $$PWD
2 DEPENDPATH += $$PWD
2 DEPENDPATH += $$PWD
3
3
4 SOURCES += \
4 SOURCES += \
5 $$PWD/qpieseries.cpp \
5 $$PWD/qpieseries.cpp \
6 $$PWD/piesliceitem.cpp \
6 $$PWD/piesliceitem.cpp \
7 $$PWD/piechartitem.cpp \
7 $$PWD/piechartitem.cpp \
8 $$PWD/qpieslice.cpp \
8 $$PWD/qpieslice.cpp \
9 $$PWD/qpiemodelmapper.cpp \
9 $$PWD/qpiemodelmapper.cpp \
10 $$PWD/qvpiemodelmapper.cpp \
10 $$PWD/qvpiemodelmapper.cpp \
11 $$PWD/qhpiemodelmapper.cpp \
11 $$PWD/qhpiemodelmapper.cpp
12 piechart/qdonutgroup.cpp
13
12
14 PRIVATE_HEADERS += \
13 PRIVATE_HEADERS += \
15 $$PWD/pieslicedata_p.h \
14 $$PWD/pieslicedata_p.h \
16 $$PWD/piechartitem_p.h \
15 $$PWD/piechartitem_p.h \
17 $$PWD/piesliceitem_p.h \
16 $$PWD/piesliceitem_p.h \
18 $$PWD/qpieslice_p.h \
17 $$PWD/qpieslice_p.h \
19 $$PWD/qpieseries_p.h \
18 $$PWD/qpieseries_p.h \
20 $$PWD/qpiemodelmapper_p.h \
19 $$PWD/qpiemodelmapper_p.h
21 $$PWD/qdonutgroup_p.h
22
20
23 PUBLIC_HEADERS += \
21 PUBLIC_HEADERS += \
24 $$PWD/qpieseries.h \
22 $$PWD/qpieseries.h \
25 $$PWD/qpieslice.h \
23 $$PWD/qpieslice.h \
26 $$PWD/qpiemodelmapper.h \
24 $$PWD/qpiemodelmapper.h \
27 $$PWD/qvpiemodelmapper.h \
25 $$PWD/qvpiemodelmapper.h \
28 $$PWD/qhpiemodelmapper.h \
26 $$PWD/qhpiemodelmapper.h
29 $$PWD/qdonutgroup.h
@@ -1,250 +1,250
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "piesliceitem_p.h"
21 #include "piesliceitem_p.h"
22 #include "piechartitem_p.h"
22 #include "piechartitem_p.h"
23 #include "qpieseries.h"
23 #include "qpieseries.h"
24 #include "qpieslice.h"
24 #include "qpieslice.h"
25 #include "chartpresenter_p.h"
25 #include "chartpresenter_p.h"
26 #include <QPainter>
26 #include <QPainter>
27 #include <qmath.h>
27 #include <qmath.h>
28 #include <QGraphicsSceneEvent>
28 #include <QGraphicsSceneEvent>
29 #include <QTime>
29 #include <QTime>
30 #include <QDebug>
30 #include <QDebug>
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 QPointF offset(qreal angle, qreal length)
34 QPointF offset(qreal angle, qreal length)
35 {
35 {
36 qreal dx = qSin(angle*(M_PI/180)) * length;
36 qreal dx = qSin(angle*(M_PI/180)) * length;
37 qreal dy = qCos(angle*(M_PI/180)) * length;
37 qreal dy = qCos(angle*(M_PI/180)) * length;
38 return QPointF(dx, -dy);
38 return QPointF(dx, -dy);
39 }
39 }
40
40
41 PieSliceItem::PieSliceItem(QGraphicsItem* parent)
41 PieSliceItem::PieSliceItem(QGraphicsItem* parent)
42 :QGraphicsObject(parent),
42 :QGraphicsObject(parent),
43 m_hovered(false)
43 m_hovered(false)
44 {
44 {
45 setAcceptHoverEvents(true);
45 setAcceptHoverEvents(true);
46 setAcceptedMouseButtons(Qt::MouseButtonMask);
46 setAcceptedMouseButtons(Qt::MouseButtonMask);
47 setZValue(ChartPresenter::PieSeriesZValue);
47 setZValue(ChartPresenter::PieSeriesZValue);
48 }
48 }
49
49
50 PieSliceItem::~PieSliceItem()
50 PieSliceItem::~PieSliceItem()
51 {
51 {
52 // If user is hovering over the slice and it gets destroyed we do
52 // If user is hovering over the slice and it gets destroyed we do
53 // not get a hover leave event. So we must emit the signal here.
53 // not get a hover leave event. So we must emit the signal here.
54 if (m_hovered)
54 if (m_hovered)
55 emit hovered(false);
55 emit hovered(false);
56 }
56 }
57
57
58 QRectF PieSliceItem::boundingRect() const
58 QRectF PieSliceItem::boundingRect() const
59 {
59 {
60 return m_boundingRect;
60 return m_boundingRect;
61 }
61 }
62
62
63 QPainterPath PieSliceItem::shape() const
63 QPainterPath PieSliceItem::shape() const
64 {
64 {
65 // Don't include the label and label arm.
65 // Don't include the label and label arm.
66 // This is used to detect a mouse clicks. We do not want clicks from label.
66 // This is used to detect a mouse clicks. We do not want clicks from label.
67 return m_slicePath;
67 return m_slicePath;
68 }
68 }
69
69
70 void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
70 void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
71 {
71 {
72 painter->save();
72 painter->save();
73 painter->setClipRect(parentItem()->boundingRect());
73 painter->setClipRect(parentItem()->boundingRect());
74 painter->setPen(m_data.m_slicePen);
74 painter->setPen(m_data.m_slicePen);
75 painter->setBrush(m_data.m_sliceBrush);
75 painter->setBrush(m_data.m_sliceBrush);
76 painter->drawPath(m_slicePath);
76 painter->drawPath(m_slicePath);
77 painter->restore();
77 painter->restore();
78
78
79 if (m_data.m_isLabelVisible) {
79 if (m_data.m_isLabelVisible) {
80 painter->save();
80 painter->save();
81
81
82 // Pen for label arm not defined in the QPieSeries api, let's use brush's color instead
82 // Pen for label arm not defined in the QPieSeries api, let's use brush's color instead
83 // Also, the drawText actually uses the pen color for the text color (unlike QGraphicsSimpleTextItem)
83 // Also, the drawText actually uses the pen color for the text color (unlike QGraphicsSimpleTextItem)
84 painter->setPen(m_data.m_labelBrush.color());
84 painter->setPen(m_data.m_labelBrush.color());
85 painter->setBrush(m_data.m_labelBrush);
85 painter->setBrush(m_data.m_labelBrush);
86 painter->setFont(m_data.m_labelFont);
86 painter->setFont(m_data.m_labelFont);
87 if (m_data.m_donut) {
87 if (m_data.m_donut) {
88 painter->translate(m_labelTextRect.center());
88 painter->translate(m_labelTextRect.center());
89 painter->rotate(m_data.m_startAngle + m_data.m_angleSpan / 2);
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);
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) {
91 } else if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
92 painter->setClipRect(parentItem()->boundingRect());
92 painter->setClipRect(parentItem()->boundingRect());
93 painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color());
93 painter->strokePath(m_labelArmPath, m_data.m_labelBrush.color());
94 painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText);
94 painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText);
95 } else { // QPieSlice::LabelInside
95 } else { // QPieSlice::LabelInside
96 painter->setClipPath(m_slicePath);
96 painter->setClipPath(m_slicePath);
97 painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText);
97 painter->drawText(m_labelTextRect, Qt::AlignCenter, m_data.m_labelText);
98 }
98 }
99
99
100 painter->restore();
100 painter->restore();
101 }
101 }
102 }
102 }
103
103
104 void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
104 void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
105 {
105 {
106 m_hovered = true;
106 m_hovered = true;
107 emit hovered(true);
107 emit hovered(true);
108 }
108 }
109
109
110 void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
110 void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
111 {
111 {
112 m_hovered = false;
112 m_hovered = false;
113 emit hovered(false);
113 emit hovered(false);
114 }
114 }
115
115
116 void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
116 void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
117 {
117 {
118 emit clicked(event->buttons());
118 emit clicked(event->buttons());
119 }
119 }
120
120
121 void PieSliceItem::setLayout(const PieSliceData &sliceData)
121 void PieSliceItem::setLayout(const PieSliceData &sliceData)
122 {
122 {
123 m_data = sliceData;
123 m_data = sliceData;
124 updateGeometry();
124 updateGeometry();
125 update();
125 update();
126 }
126 }
127
127
128 void PieSliceItem::updateGeometry()
128 void PieSliceItem::updateGeometry()
129 {
129 {
130 if (m_data.m_radius <= 0)
130 if (m_data.m_radius <= 0)
131 return;
131 return;
132
132
133 prepareGeometryChange();
133 prepareGeometryChange();
134
134
135 // slice path
135 // slice path
136 qreal centerAngle;
136 qreal centerAngle;
137 QPointF armStart;
137 QPointF armStart;
138 m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, &centerAngle, &armStart);
138 m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, &centerAngle, &armStart);
139
139
140 // text rect
140 // text rect
141 QFontMetricsF fm(m_data.m_labelFont);
141 QFontMetricsF fm(m_data.m_labelFont);
142 m_labelTextRect = QRectF(0, 0, fm.width(m_data.m_labelText), fm.height());
142 m_labelTextRect = QRectF(0, 0, fm.width(m_data.m_labelText), fm.height());
143
143
144 // label arm path
144 // label arm path
145 QPointF labelTextStart;
145 QPointF labelTextStart;
146 m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart);
146 m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart);
147
147
148 // text position
148 // text position
149 if (m_data.m_donut) {
149 if (m_data.m_donut) {
150 QPointF donutCenter = m_data.m_center + offset(centerAngle, m_data.m_innerRadius + (m_data.m_radius - m_data.m_innerRadius) / 2);
150 QPointF donutCenter = m_data.m_center + offset(centerAngle, m_data.m_innerRadius + (m_data.m_radius - m_data.m_innerRadius) / 2);
151 m_labelTextRect.moveCenter(donutCenter);
151 m_labelTextRect.moveCenter(donutCenter);
152 } else if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
152 } else if (m_data.m_labelPosition == QPieSlice::LabelOutside) {
153 m_labelTextRect.moveBottomLeft(labelTextStart);
153 m_labelTextRect.moveBottomLeft(labelTextStart);
154 } else {// QPieSlice::LabelInside
154 } else {// QPieSlice::LabelInside
155 QPointF sliceCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2);
155 QPointF sliceCenter = m_data.m_center + offset(centerAngle, m_data.m_radius / 2);
156 m_labelTextRect.moveCenter(sliceCenter);
156 m_labelTextRect.moveCenter(sliceCenter);
157 }
157 }
158
158
159 // bounding rect
159 // bounding rect
160 if (m_data.m_isLabelVisible)
160 if (m_data.m_isLabelVisible)
161 m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect);
161 m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect);
162 else
162 else
163 m_boundingRect = m_slicePath.boundingRect();
163 m_boundingRect = m_slicePath.boundingRect();
164 }
164 }
165
165
166 QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
166 QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
167 {
167 {
168 if (slice->isExploded()) {
168 if (slice->isExploded()) {
169 qreal centerAngle = slice->startAngle() + (slice->angleSpan()/2);
169 qreal centerAngle = slice->startAngle() + (slice->angleSpan()/2);
170 qreal len = radius * slice->explodeDistanceFactor();
170 qreal len = radius * slice->explodeDistanceFactor();
171 point += offset(centerAngle, len);
171 point += offset(centerAngle, len);
172 }
172 }
173 return point;
173 return point;
174 }
174 }
175
175
176 QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF* armStart)
176 QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF* armStart)
177 {
177 {
178 // calculate center angle
178 // calculate center angle
179 *centerAngle = startAngle + (angleSpan/2);
179 *centerAngle = startAngle + (angleSpan/2);
180
180
181 // calculate slice rectangle
181 // calculate slice rectangle
182 QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2);
182 QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2);
183
183
184 // slice path
184 // slice path
185 QPainterPath path;
185 QPainterPath path;
186 if (m_data.m_donut) {
186 if (m_data.m_donut) {
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 QRectF insideRect(center.x() - m_data.m_innerRadius, center.y()-m_data.m_innerRadius, m_data.m_innerRadius*2, m_data.m_innerRadius*2);
188 path.arcMoveTo(rect, -startAngle + 90);
188 path.arcMoveTo(rect, -startAngle + 90);
189 path.arcTo(rect, -startAngle + 90, -angleSpan);
189 path.arcTo(rect, -startAngle + 90, -angleSpan);
190 path.arcTo(insideRect, -startAngle + 90 - angleSpan, angleSpan);
190 path.arcTo(insideRect, -startAngle + 90 - angleSpan, angleSpan);
191 path.closeSubpath();
191 path.closeSubpath();
192 } else {
192 } else {
193 path.moveTo(rect.center());
193 path.moveTo(rect.center());
194 path.arcTo(rect, -startAngle + 90, -angleSpan);
194 path.arcTo(rect, -startAngle + 90, -angleSpan);
195 path.closeSubpath();
195 path.closeSubpath();
196 }
196 }
197
197
198 // calculate label arm start point
198 // calculate label arm start point
199 *armStart = center;
199 *armStart = center;
200 *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP);
200 *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP);
201
201
202 return path;
202 return path;
203 }
203 }
204
204
205 QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart)
205 QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart)
206 {
206 {
207 // Normalize the angle to 0-360 range
207 // Normalize the angle to 0-360 range
208 // NOTE: We are using int here on purpose. Depenging on platform and hardware
208 // NOTE: We are using int here on purpose. Depenging on platform and hardware
209 // qreal can be a double, float or something the user gives to the Qt configure
209 // qreal can be a double, float or something the user gives to the Qt configure
210 // (QT_COORD_TYPE). Compilers do not seem to support modulo for double or float
210 // (QT_COORD_TYPE). Compilers do not seem to support modulo for double or float
211 // but there are fmod() and fmodf() functions for that. So instead of some #ifdef
211 // but there are fmod() and fmodf() functions for that. So instead of some #ifdef
212 // that might break we just use int. Precision for this is just fine for our needs.
212 // that might break we just use int. Precision for this is just fine for our needs.
213 int normalized = angle * 10.0;
213 int normalized = angle * 10.0;
214 normalized = normalized % 3600;
214 normalized = normalized % 3600;
215 if (normalized < 0)
215 if (normalized < 0)
216 normalized += 3600;
216 normalized += 3600;
217 angle = (qreal) normalized / 10.0;
217 angle = (qreal) normalized / 10.0;
218
218
219 // prevent label arm pointing straight down because it will look bad
219 // prevent label arm pointing straight down because it will look bad
220 if (angle < 180 && angle > 170)
220 if (angle < 180 && angle > 170)
221 angle = 170;
221 angle = 170;
222 if (angle > 180 && angle < 190)
222 if (angle > 180 && angle < 190)
223 angle = 190;
223 angle = 190;
224
224
225 // line from slice to label
225 // line from slice to label
226 QPointF parm1 = start + offset(angle, length);
226 QPointF parm1 = start + offset(angle, length);
227
227
228 // line to underline the label
228 // line to underline the label
229 QPointF parm2 = parm1;
229 QPointF parm2 = parm1;
230 if (angle < 180) { // arm swings the other way on the left side
230 if (angle < 180) { // arm swings the other way on the left side
231 parm2 += QPointF(textWidth, 0);
231 parm2 += QPointF(textWidth, 0);
232 *textStart = parm1;
232 *textStart = parm1;
233 }
233 }
234 else {
234 else {
235 parm2 += QPointF(-textWidth,0);
235 parm2 += QPointF(-textWidth,0);
236 *textStart = parm2;
236 *textStart = parm2;
237 }
237 }
238
238
239 QPainterPath path;
239 QPainterPath path;
240 path.moveTo(start);
240 path.moveTo(start);
241 path.lineTo(parm1);
241 path.lineTo(parm1);
242 path.lineTo(parm2);
242 path.lineTo(parm2);
243
243
244 return path;
244 return path;
245 }
245 }
246
246
247 #include "moc_piesliceitem_p.cpp"
247 #include "moc_piesliceitem_p.cpp"
248
248
249 QTCOMMERCIALCHART_END_NAMESPACE
249 QTCOMMERCIALCHART_END_NAMESPACE
250
250
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now