##// END OF EJS Templates
Make pie work better with chartwidgettest
Jani Honkonen -
r163:daab1c215b03
parent child
Show More
@@ -138,9 +138,8 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
138 break;
138 break;
139 }
139 }
140 case QChartSeries::SeriesTypePie: {
140 case QChartSeries::SeriesTypePie: {
141 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
141 QPieSeries *s = qobject_cast<QPieSeries *>(series);
142 PiePresenter* pie = new PiePresenter(m_chart, pieSeries);
142 PiePresenter* pie = new PiePresenter(m_chart, s);
143 QObject::connect(pieSeries, SIGNAL(changed(const PieChangeSet&)), pie, SLOT(handleSeriesChanged(const PieChangeSet&)));
144 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
143 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
145 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)), pie, SLOT(handleDomainChanged(const Domain&)));
144 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)), pie, SLOT(handleDomainChanged(const Domain&)));
146 m_chartItems.insert(series, pie);
145 m_chartItems.insert(series, pie);
@@ -7,12 +7,15
7 #include "stackedbarchartseries.h"
7 #include "stackedbarchartseries.h"
8 #include "percentbarchartseries.h"
8 #include "percentbarchartseries.h"
9 #include "qlinechartseries.h"
9 #include "qlinechartseries.h"
10 #include "qpieseries.h"
11
10 //items
12 //items
11 #include "axisitem_p.h"
13 #include "axisitem_p.h"
12 #include "bargroup.h"
14 #include "bargroup.h"
13 #include "stackedbargroup.h"
15 #include "stackedbargroup.h"
14 #include "linechartitem_p.h"
16 #include "linechartitem_p.h"
15 #include "percentbargroup.h"
17 #include "percentbargroup.h"
18 #include "piepresenter.h"
16
19
17 //themes
20 //themes
18 #include "chartthemevanilla_p.h"
21 #include "chartthemevanilla_p.h"
@@ -98,6 +101,12 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count)
98 decorate(i,s,count);
101 decorate(i,s,count);
99 break;
102 break;
100 }
103 }
104 case QChartSeries::SeriesTypePie: {
105 QPieSeries* s = static_cast<QPieSeries*>(series);
106 PiePresenter* i = static_cast<PiePresenter*>(item);
107 decorate(i,s,count);
108 break;
109 }
101 default:
110 default:
102 qDebug()<<"Wrong item to be decorated by theme";
111 qDebug()<<"Wrong item to be decorated by theme";
103 break;
112 break;
@@ -146,5 +155,16 void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,i
146 item->addColor(QColor(255,128,0,128));
155 item->addColor(QColor(255,128,0,128));
147 }
156 }
148
157
158 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
159 {
160 // TODO: Does not work well. We need to generate enough different colors
161 // based on available theme and not use the same color twice.
162 for (int i=0; i<series->count(); i++) {
163 QPieSlice slice = series->slice(i);
164 slice.m_color = m_seriesColor.at(i % m_seriesColor.count());
165 series->update(i, slice);
166 }
167 }
168
149
169
150 QTCOMMERCIALCHART_END_NAMESPACE
170 QTCOMMERCIALCHART_END_NAMESPACE
@@ -17,6 +17,8 class StackedBarGroup;
17 class StackedBarChartSeries;
17 class StackedBarChartSeries;
18 class PercentBarChartSeries;
18 class PercentBarChartSeries;
19 class PercentBarGroup;
19 class PercentBarGroup;
20 class PiePresenter;
21 class QPieSeries;
20
22
21 class ChartTheme
23 class ChartTheme
22 {
24 {
@@ -31,6 +33,7 public:
31 void decorate(BarGroup* item, BarChartSeries* series,int count);
33 void decorate(BarGroup* item, BarChartSeries* series,int count);
32 void decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count);
34 void decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count);
33 void decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count);
35 void decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count);
36 void decorate(PiePresenter* item, QPieSeries* series, int count);
34
37
35 protected:
38 protected:
36 QChart::ChartTheme m_id;
39 QChart::ChartTheme m_id;
@@ -2,6 +2,7
2 #include "piepresenter.h"
2 #include "piepresenter.h"
3 #include "pieslice.h"
3 #include "pieslice.h"
4 #include <QDebug>
4 #include <QDebug>
5 #include <QTime>
5
6
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8
@@ -11,8 +12,14 PiePresenter::PiePresenter(QGraphicsItem *parent, QPieSeries *series) :
11 {
12 {
12 Q_ASSERT(parent);
13 Q_ASSERT(parent);
13 Q_ASSERT(series);
14 Q_ASSERT(series);
15
14 m_rect = parentItem()->boundingRect();
16 m_rect = parentItem()->boundingRect();
15 setAcceptHoverEvents(true);
17 setAcceptHoverEvents(true);
18 qsrand(QTime::currentTime().msec()); // for random color generation
19
20 connect(series, SIGNAL(changed(const PieChangeSet&)), this, SLOT(handleSeriesChanged(const PieChangeSet&)));
21 connect(series, SIGNAL(sizeFactorChanged()), this, SLOT(updateGeometry()));
22 connect(series, SIGNAL(positionChanged()), this, SLOT(updateGeometry()));
16 }
23 }
17
24
18 PiePresenter::~PiePresenter()
25 PiePresenter::~PiePresenter()
@@ -21,14 +28,21 PiePresenter::~PiePresenter()
21 delete m_slices.takeLast();
28 delete m_slices.takeLast();
22 }
29 }
23
30
24 void PiePresenter::handleSeriesChanged(const PieChangeSet& changeSet)
31 void PiePresenter::handleSeriesChanged(const PieChangeSet& /*changeSet*/)
25 {
32 {
26 const qreal fullPie = 360;
33 const qreal fullPie = 360;
27 qreal total = 0;
34 qreal total = 0;
28
35
29 // calculate total
36 // calculate total and set random color if there is no color
30 foreach (QPieSlice sliceData, m_pieSeries->slices())
37 for (int i=0; i<m_pieSeries->count(); i++) {
31 total += sliceData.m_value;
38 QPieSlice& slice = m_pieSeries->m_slices[i];
39 total += slice.m_value;
40 if (slice.m_color == QColor::Invalid) {
41 slice.m_color.setRed(qrand() % 255);
42 slice.m_color.setGreen(qrand() % 255);
43 slice.m_color.setBlue(qrand() % 255);
44 }
45 }
32
46
33 // TODO: no need to create new slices in case size changed; we should re-use the existing ones
47 // TODO: no need to create new slices in case size changed; we should re-use the existing ones
34 while (m_slices.count())
48 while (m_slices.count())
@@ -22,13 +22,13 public: // from QGraphicsItem
22 void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) {}
22 void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) {}
23
23
24 public:
24 public:
25 void updateGeometry();
26 QRectF pieRect() const { return m_pieRect; }
25 QRectF pieRect() const { return m_pieRect; }
27
26
28 public Q_SLOTS:
27 public Q_SLOTS:
29 void handleSeriesChanged(const PieChangeSet& changeSet);
28 void handleSeriesChanged(const PieChangeSet& changeSet);
30 void handleDomainChanged(const Domain& domain);
29 void handleDomainChanged(const Domain& domain);
31 void handleGeometryChanged(const QRectF& rect);
30 void handleGeometryChanged(const QRectF& rect);
31 void updateGeometry();
32
32
33 private:
33 private:
34 friend class PieSlice;
34 friend class PieSlice;
@@ -4,6 +4,7
4 #include <QPainter>
4 #include <QPainter>
5 #include <QDebug>
5 #include <QDebug>
6 #include <qmath.h>
6 #include <qmath.h>
7 #include <QGraphicsSceneEvent>
7
8
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10
@@ -63,23 +64,20 void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option
63 painter->drawText(m_center, sliceData().m_label);
64 painter->drawText(m_center, sliceData().m_label);
64 }
65 }
65
66
66 void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent * event)
67 void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
67 {
68 {
68 QGraphicsItem::hoverEnterEvent(event);
69 m_brush = QBrush(sliceData().m_color.lighter());
69 m_brush = QBrush(sliceData().m_color.lighter());
70 update();
70 update();
71 }
71 }
72
72
73 void PieSlice::hoverLeaveEvent(QGraphicsSceneHoverEvent * event)
73 void PieSlice::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
74 {
74 {
75 QGraphicsItem::hoverLeaveEvent(event);
76 m_brush = QBrush(sliceData().m_color);
75 m_brush = QBrush(sliceData().m_color);
77 update();
76 update();
78 }
77 }
79
78
80 void PieSlice::mousePressEvent(QGraphicsSceneMouseEvent *event)
79 void PieSlice::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/)
81 {
80 {
82 QGraphicsItem::mousePressEvent(event);
83 QPieSlice data = sliceData();
81 QPieSlice data = sliceData();
84 data.m_isExploded = !data.m_isExploded;
82 data.m_isExploded = !data.m_isExploded;
85 (static_cast<PiePresenter*>(parentItem()))->m_pieSeries->update(m_seriesIndex, data);
83 (static_cast<PiePresenter*>(parentItem()))->m_pieSeries->update(m_seriesIndex, data);
@@ -17,8 +17,19 QPieSeries::~QPieSeries()
17
17
18 }
18 }
19
19
20 void QPieSeries::set(QList<QPieSlice> slices)
20 bool QPieSeries::setData(QList<qreal> data)
21 {
21 {
22 QList<QPieSlice> slices;
23 foreach (int value, data)
24 slices << QPieSlice(value, QString::number(value));
25 return set(slices);
26 }
27
28 bool QPieSeries::set(QList<QPieSlice> slices)
29 {
30 if (!slices.count())
31 return false;
32
22 PieChangeSet changeSet;
33 PieChangeSet changeSet;
23
34
24 for (int i=slices.count(); i<m_slices.count(); i++)
35 for (int i=slices.count(); i<m_slices.count(); i++)
@@ -33,21 +44,26 void QPieSeries::set(QList<QPieSlice> slices)
33
44
34 m_slices = slices;
45 m_slices = slices;
35 emit changed(changeSet);
46 emit changed(changeSet);
47 return true;
36 }
48 }
37
49
38 void QPieSeries::add(QList<QPieSlice> slices)
50 bool QPieSeries::add(QList<QPieSlice> slices)
39 {
51 {
52 if (!slices.count())
53 return false;
54
40 PieChangeSet changeSet;
55 PieChangeSet changeSet;
41 for (int i=0; i<slices.count(); i++)
56 for (int i=0; i<slices.count(); i++)
42 changeSet.m_added << m_slices.count() + i;
57 changeSet.m_added << m_slices.count() + i;
43
58
44 m_slices += slices;
59 m_slices += slices;
45 emit changed(changeSet);
60 emit changed(changeSet);
61 return true;
46 }
62 }
47
63
48 void QPieSeries::add(QPieSlice slice)
64 bool QPieSeries::add(QPieSlice slice)
49 {
65 {
50 add(QList<QPieSlice>() << slice);
66 return add(QList<QPieSlice>() << slice);
51 }
67 }
52
68
53 QPieSlice QPieSeries::slice(int index) const
69 QPieSlice QPieSeries::slice(int index) const
@@ -71,15 +87,21 bool QPieSeries::update(int index, QPieSlice slice)
71
87
72 void QPieSeries::setSizeFactor(qreal factor)
88 void QPieSeries::setSizeFactor(qreal factor)
73 {
89 {
74 if (factor > 0.0)
90 if (factor < 0.0)
91 return;
92
93 if (m_sizeFactor != factor) {
75 m_sizeFactor = factor;
94 m_sizeFactor = factor;
76 emit sizeFactorChanged();
95 emit sizeFactorChanged();
96 }
77 }
97 }
78
98
79 void QPieSeries::setPosition(PiePosition position)
99 void QPieSeries::setPosition(PiePosition position)
80 {
100 {
81 m_position = position;
101 if (m_position != position) {
82 emit positionChanged();
102 m_position = position;
103 emit positionChanged();
104 }
83 }
105 }
84
106
85 #include "moc_qpieseries.cpp"
107 #include "moc_qpieseries.cpp"
@@ -15,14 +15,14 class QPieSlice
15 {
15 {
16 public:
16 public:
17 QPieSlice()
17 QPieSlice()
18 :m_value(0), m_label("<empty>"), m_color(Qt::white), m_isExploded(false) {}
18 :m_value(0), m_label("<empty>"), m_color(QColor::Invalid), m_isExploded(false) {}
19
19
20 QPieSlice(qreal value, QString label, QColor color, bool exploded = false)
20 QPieSlice(qreal value, QString label = "<empty>", QColor color = QColor::Invalid, bool exploded = false)
21 :m_value(value), m_label(label), m_color(color), m_isExploded(exploded) {}
21 :m_value(value), m_label(label), m_color(color), m_isExploded(exploded) {}
22 public:
22 public:
23 qreal m_value;
23 qreal m_value;
24 QString m_label;
24 QString m_label;
25 QColor m_color;
25 QColor m_color; // TODO: should we even define color here?
26 bool m_isExploded;
26 bool m_isExploded;
27 };
27 };
28
28
@@ -53,11 +53,12 public:
53
53
54 public: // from QChartSeries
54 public: // from QChartSeries
55 QChartSeriesType type() const { return QChartSeries::SeriesTypePie; }
55 QChartSeriesType type() const { return QChartSeries::SeriesTypePie; }
56 virtual bool setData(QList<qreal> data);
56
57
57 public:
58 public:
58 void set(QList<QPieSlice> slices);
59 bool set(QList<QPieSlice> slices);
59 void add(QList<QPieSlice> slices);
60 bool add(QList<QPieSlice> slices);
60 void add(QPieSlice slice);
61 bool add(QPieSlice slice);
61
62
62 int count() const { return m_slices.count(); }
63 int count() const { return m_slices.count(); }
63
64
@@ -84,6 +85,7 Q_SIGNALS:
84
85
85 private:
86 private:
86 Q_DISABLE_COPY(QPieSeries)
87 Q_DISABLE_COPY(QPieSeries)
88 friend class PiePresenter;
87 // TODO: use PIML
89 // TODO: use PIML
88 QList<QPieSlice> m_slices;
90 QList<QPieSlice> m_slices;
89 qreal m_sizeFactor;
91 qreal m_sizeFactor;
General Comments 0
You need to be logged in to leave comments. Login now