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