##// END OF EJS Templates
Make pie work better with chartwidgettest
Jani Honkonen -
r163:daab1c215b03
parent child
Show More
@@ -1,264 +1,263
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include "chartdataset_p.h"
4 #include "chartdataset_p.h"
5 #include "charttheme_p.h"
5 #include "charttheme_p.h"
6 //series
6 //series
7 #include "barchartseries.h"
7 #include "barchartseries.h"
8 #include "stackedbarchartseries.h"
8 #include "stackedbarchartseries.h"
9 #include "percentbarchartseries.h"
9 #include "percentbarchartseries.h"
10 #include "qlinechartseries.h"
10 #include "qlinechartseries.h"
11 #include "qpieseries.h"
11 #include "qpieseries.h"
12 #include "qscatterseries.h"
12 #include "qscatterseries.h"
13 //items
13 //items
14 #include "axisitem_p.h"
14 #include "axisitem_p.h"
15 #include "bargroup.h"
15 #include "bargroup.h"
16 #include "stackedbargroup.h"
16 #include "stackedbargroup.h"
17 #include "linechartitem_p.h"
17 #include "linechartitem_p.h"
18 #include "percentbargroup.h"
18 #include "percentbargroup.h"
19 #include "linechartanimationitem_p.h"
19 #include "linechartanimationitem_p.h"
20 #include "piepresenter.h"
20 #include "piepresenter.h"
21 #include "scatterpresenter.h"
21 #include "scatterpresenter.h"
22
22
23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24
24
25 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
25 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
26 m_chart(chart),
26 m_chart(chart),
27 m_dataset(dataset),
27 m_dataset(dataset),
28 m_chartTheme(0),
28 m_chartTheme(0),
29 m_domainIndex(0),
29 m_domainIndex(0),
30 m_marginSize(0),
30 m_marginSize(0),
31 m_axisX(new QChartAxis(this)),
31 m_axisX(new QChartAxis(this)),
32 m_axisY(new QChartAxis(this)),
32 m_axisY(new QChartAxis(this)),
33 m_rect(QRectF(QPoint(0,0),m_chart->size()))
33 m_rect(QRectF(QPoint(0,0),m_chart->size()))
34 {
34 {
35 setChartTheme(QChart::ChartThemeDefault);
35 setChartTheme(QChart::ChartThemeDefault);
36 m_axisItems[m_axisX] = new AxisItem(m_axisX,AxisItem::X_AXIS,m_chart);
36 m_axisItems[m_axisX] = new AxisItem(m_axisX,AxisItem::X_AXIS,m_chart);
37 m_axisItems[m_axisY] = new AxisItem(m_axisY,AxisItem::Y_AXIS,m_chart);
37 m_axisItems[m_axisY] = new AxisItem(m_axisY,AxisItem::Y_AXIS,m_chart);
38 createConnections();
38 createConnections();
39 }
39 }
40
40
41 ChartPresenter::~ChartPresenter()
41 ChartPresenter::~ChartPresenter()
42 {
42 {
43 }
43 }
44
44
45 void ChartPresenter::createConnections()
45 void ChartPresenter::createConnections()
46 {
46 {
47 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
47 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
48 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
48 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
49
49
50 QMapIterator<QChartAxis*,AxisItem*> i(m_axisItems);
50 QMapIterator<QChartAxis*,AxisItem*> i(m_axisItems);
51
51
52 while (i.hasNext()) {
52 while (i.hasNext()) {
53 i.next();
53 i.next();
54 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),i.value(),SLOT(handleGeometryChanged(const QRectF&)));
54 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),i.value(),SLOT(handleGeometryChanged(const QRectF&)));
55 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),i.value(),SLOT(handleDomainChanged(const Domain&)));
55 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),i.value(),SLOT(handleDomainChanged(const Domain&)));
56 }
56 }
57 }
57 }
58
58
59 void ChartPresenter::handleGeometryChanged()
59 void ChartPresenter::handleGeometryChanged()
60 {
60 {
61 m_rect = QRectF(QPoint(0,0),m_chart->size());
61 m_rect = QRectF(QPoint(0,0),m_chart->size());
62 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
62 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
63 Q_ASSERT(m_rect.isValid());
63 Q_ASSERT(m_rect.isValid());
64 emit geometryChanged(m_rect);
64 emit geometryChanged(m_rect);
65 }
65 }
66
66
67 int ChartPresenter::margin() const
67 int ChartPresenter::margin() const
68 {
68 {
69 return m_marginSize;
69 return m_marginSize;
70 }
70 }
71
71
72 void ChartPresenter::setMargin(int margin)
72 void ChartPresenter::setMargin(int margin)
73 {
73 {
74 m_marginSize = margin;
74 m_marginSize = margin;
75 }
75 }
76
76
77 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
77 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
78 {
78 {
79 switch(series->type())
79 switch(series->type())
80 {
80 {
81 case QChartSeries::SeriesTypeLine: {
81 case QChartSeries::SeriesTypeLine: {
82 QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series);
82 QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series);
83 LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart);
83 LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart);
84 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
84 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
85 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
85 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
86 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
86 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
87 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
87 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
88 m_chartItems.insert(series,item);
88 m_chartItems.insert(series,item);
89 break;
89 break;
90 }
90 }
91
91
92 case QChartSeries::SeriesTypeBar: {
92 case QChartSeries::SeriesTypeBar: {
93 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
93 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
94 BarGroup* item = new BarGroup(*barSeries,m_chart);
94 BarGroup* item = new BarGroup(*barSeries,m_chart);
95 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
95 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
96 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
96 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
97 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
97 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
98 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
98 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
99 m_chartItems.insert(series,item);
99 m_chartItems.insert(series,item);
100 // m_axisXItem->setVisible(false);
100 // m_axisXItem->setVisible(false);
101 break;
101 break;
102 }
102 }
103
103
104 case QChartSeries::SeriesTypeStackedBar: {
104 case QChartSeries::SeriesTypeStackedBar: {
105
105
106 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
106 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
107 StackedBarGroup* item = new StackedBarGroup(*stackedBarSeries,m_chart);
107 StackedBarGroup* item = new StackedBarGroup(*stackedBarSeries,m_chart);
108 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
108 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
109 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
109 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
110 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
110 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
111 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
111 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
112 m_chartItems.insert(series,item);
112 m_chartItems.insert(series,item);
113 break;
113 break;
114 }
114 }
115
115
116 case QChartSeries::SeriesTypePercentBar: {
116 case QChartSeries::SeriesTypePercentBar: {
117
117
118 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
118 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
119 PercentBarGroup* item = new PercentBarGroup(*percentBarSeries,m_chart);
119 PercentBarGroup* item = new PercentBarGroup(*percentBarSeries,m_chart);
120 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
120 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
121 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
121 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
122 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
122 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
123 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
123 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
124 m_chartItems.insert(series,item);
124 m_chartItems.insert(series,item);
125 break;
125 break;
126 }
126 }
127 case QChartSeries::SeriesTypeScatter: {
127 case QChartSeries::SeriesTypeScatter: {
128 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
128 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
129 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
129 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
130 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
130 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
131 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
131 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
132 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)),
132 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)),
133 scatterPresenter, SLOT(handleDomainChanged(const Domain&)));
133 scatterPresenter, SLOT(handleDomainChanged(const Domain&)));
134 // scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
134 // scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
135 // scatterSeries->d->setParentItem(this);
135 // scatterSeries->d->setParentItem(this);
136 // scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
136 // scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
137 m_chartItems.insert(scatterSeries, scatterPresenter);
137 m_chartItems.insert(scatterSeries, scatterPresenter);
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);
147 break;
146 break;
148 }
147 }
149 default: {
148 default: {
150 qDebug()<< "Series type" << series->type() << "not implemented.";
149 qDebug()<< "Series type" << series->type() << "not implemented.";
151 break;
150 break;
152 }
151 }
153 }
152 }
154
153
155 if(m_rect.isValid()) emit geometryChanged(m_rect);
154 if(m_rect.isValid()) emit geometryChanged(m_rect);
156 }
155 }
157
156
158 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
157 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
159 {
158 {
160 //TODO:
159 //TODO:
161 }
160 }
162
161
163 void ChartPresenter::zoomInToRect(const QRectF& rect)
162 void ChartPresenter::zoomInToRect(const QRectF& rect)
164 {
163 {
165 if(!rect.isValid()) return;
164 if(!rect.isValid()) return;
166 QRectF r = rect.normalized();
165 QRectF r = rect.normalized();
167 r.translate(-m_marginSize, -m_marginSize);
166 r.translate(-m_marginSize, -m_marginSize);
168 Domain domain (m_dataset->domain().subDomain(r,m_rect.width(),m_rect.height()));
167 Domain domain (m_dataset->domain().subDomain(r,m_rect.width(),m_rect.height()));
169 m_dataset->addDomain(domain);
168 m_dataset->addDomain(domain);
170 }
169 }
171
170
172 void ChartPresenter::zoomIn()
171 void ChartPresenter::zoomIn()
173 {
172 {
174 if (!m_dataset->nextDomain()) {
173 if (!m_dataset->nextDomain()) {
175 QRectF rect = m_rect;
174 QRectF rect = m_rect;
176 rect.setWidth(rect.width()/2);
175 rect.setWidth(rect.width()/2);
177 rect.setHeight(rect.height()/2);
176 rect.setHeight(rect.height()/2);
178 rect.moveCenter(m_rect.center());
177 rect.moveCenter(m_rect.center());
179 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
178 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
180 m_dataset->addDomain(domain);
179 m_dataset->addDomain(domain);
181 }
180 }
182 }
181 }
183
182
184 void ChartPresenter::zoomOut()
183 void ChartPresenter::zoomOut()
185 {
184 {
186 m_dataset->previousDomain();
185 m_dataset->previousDomain();
187 }
186 }
188
187
189 void ChartPresenter::zoomReset()
188 void ChartPresenter::zoomReset()
190 {
189 {
191 m_dataset->clearDomains();
190 m_dataset->clearDomains();
192 }
191 }
193
192
194 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
193 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
195 {
194 {
196 delete m_chartTheme;
195 delete m_chartTheme;
197
196
198 m_chartTheme = ChartTheme::createTheme(theme);
197 m_chartTheme = ChartTheme::createTheme(theme);
199
198
200 m_chartTheme->decorate(m_chart);
199 m_chartTheme->decorate(m_chart);
201 QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems);
200 QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems);
202
201
203 int index=0;
202 int index=0;
204 while (i.hasNext()) {
203 while (i.hasNext()) {
205 i.next();
204 i.next();
206 index++;
205 index++;
207 m_chartTheme->decorate(i.value(),i.key(),index);
206 m_chartTheme->decorate(i.value(),i.key(),index);
208 }
207 }
209 }
208 }
210
209
211
210
212 QChart::ChartTheme ChartPresenter::chartTheme()
211 QChart::ChartTheme ChartPresenter::chartTheme()
213 {
212 {
214 return m_chartTheme->id();
213 return m_chartTheme->id();
215 }
214 }
216
215
217 QChartAxis* ChartPresenter::axisX()
216 QChartAxis* ChartPresenter::axisX()
218 {
217 {
219 return m_axisX;
218 return m_axisX;
220 }
219 }
221
220
222 QChartAxis* ChartPresenter::axisY()
221 QChartAxis* ChartPresenter::axisY()
223 {
222 {
224 return m_axisY;
223 return m_axisY;
225 }
224 }
226
225
227 QChartAxis* ChartPresenter::addAxisX()
226 QChartAxis* ChartPresenter::addAxisX()
228 {
227 {
229 //only one axis
228 //only one axis
230 if(m_axisX==0){
229 if(m_axisX==0){
231 m_axisX = new QChartAxis(this);
230 m_axisX = new QChartAxis(this);
232 m_axisItems[m_axisX] = new AxisItem(m_axisX,AxisItem::X_AXIS,m_chart);
231 m_axisItems[m_axisX] = new AxisItem(m_axisX,AxisItem::X_AXIS,m_chart);
233 }
232 }
234 return m_axisX;
233 return m_axisX;
235 }
234 }
236
235
237 QChartAxis* ChartPresenter::addAxisY()
236 QChartAxis* ChartPresenter::addAxisY()
238 {
237 {
239 if(m_axisY==0){
238 if(m_axisY==0){
240 m_axisY = new QChartAxis(this);
239 m_axisY = new QChartAxis(this);
241 m_axisItems[m_axisY] = new AxisItem(m_axisY,AxisItem::Y_AXIS,m_chart);
240 m_axisItems[m_axisY] = new AxisItem(m_axisY,AxisItem::Y_AXIS,m_chart);
242 return m_axisY;
241 return m_axisY;
243 }
242 }
244
243
245 QChartAxis* axis = new QChartAxis(this);
244 QChartAxis* axis = new QChartAxis(this);
246 m_axisItems[axis] = new AxisItem(axis,AxisItem::Y_AXIS,m_chart);
245 m_axisItems[axis] = new AxisItem(axis,AxisItem::Y_AXIS,m_chart);
247 return axis;
246 return axis;
248 }
247 }
249
248
250 void ChartPresenter::removeAxis(QChartAxis* axis)
249 void ChartPresenter::removeAxis(QChartAxis* axis)
251 {
250 {
252 AxisItem* item = m_axisItems.take(axis);
251 AxisItem* item = m_axisItems.take(axis);
253 if(item){
252 if(item){
254 delete item;
253 delete item;
255 delete axis;
254 delete axis;
256 }
255 }
257 //reset pointers to default ones
256 //reset pointers to default ones
258 if(axis == m_axisX) m_axisX=0;
257 if(axis == m_axisX) m_axisX=0;
259 else if(axis == m_axisY) m_axisY=0;
258 else if(axis == m_axisY) m_axisY=0;
260 }
259 }
261
260
262 #include "moc_chartpresenter_p.cpp"
261 #include "moc_chartpresenter_p.cpp"
263
262
264 QTCOMMERCIALCHART_END_NAMESPACE
263 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,150 +1,170
1 #include "charttheme_p.h"
1 #include "charttheme_p.h"
2 #include "qchart.h"
2 #include "qchart.h"
3
3
4
4
5 //series
5 //series
6 #include "barchartseries.h"
6 #include "barchartseries.h"
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"
19 #include "chartthemeicy_p.h"
22 #include "chartthemeicy_p.h"
20 #include "chartthemegrayscale_p.h"
23 #include "chartthemegrayscale_p.h"
21 #include "chartthemescientific_p.h"
24 #include "chartthemescientific_p.h"
22
25
23
26
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
28
26 /* TODO
29 /* TODO
27 case QChart::ChartThemeUnnamed1:
30 case QChart::ChartThemeUnnamed1:
28 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
31 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
29 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
32 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
30 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
33 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
31 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
34 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
32 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
35 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
33
36
34 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
37 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
35 m_gradientEndColor = QColor(QRgb(0xffafafaf));
38 m_gradientEndColor = QColor(QRgb(0xffafafaf));
36 */
39 */
37
40
38 ChartTheme::ChartTheme(QChart::ChartTheme id)
41 ChartTheme::ChartTheme(QChart::ChartTheme id)
39 {
42 {
40 m_id = id;
43 m_id = id;
41 m_seriesColor.append(QRgb(0xff000000));
44 m_seriesColor.append(QRgb(0xff000000));
42 m_seriesColor.append(QRgb(0xff707070));
45 m_seriesColor.append(QRgb(0xff707070));
43 m_gradientStartColor = QColor(QRgb(0xffffffff));
46 m_gradientStartColor = QColor(QRgb(0xffffffff));
44 m_gradientEndColor = QColor(QRgb(0xffafafaf));
47 m_gradientEndColor = QColor(QRgb(0xffafafaf));
45 }
48 }
46
49
47
50
48 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
51 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
49 {
52 {
50 switch(theme) {
53 switch(theme) {
51 case QChart::ChartThemeDefault:
54 case QChart::ChartThemeDefault:
52 return new ChartTheme();
55 return new ChartTheme();
53 case QChart::ChartThemeVanilla:
56 case QChart::ChartThemeVanilla:
54 return new ChartThemeVanilla();
57 return new ChartThemeVanilla();
55 case QChart::ChartThemeIcy:
58 case QChart::ChartThemeIcy:
56 return new ChartThemeIcy();
59 return new ChartThemeIcy();
57 case QChart::ChartThemeGrayscale:
60 case QChart::ChartThemeGrayscale:
58 return new ChartThemeGrayscale();
61 return new ChartThemeGrayscale();
59 case QChart::ChartThemeScientific:
62 case QChart::ChartThemeScientific:
60 return new ChartThemeScientific();
63 return new ChartThemeScientific();
61 }
64 }
62 }
65 }
63
66
64 void ChartTheme::decorate(QChart* chart)
67 void ChartTheme::decorate(QChart* chart)
65 {
68 {
66 QLinearGradient backgroundGradient;
69 QLinearGradient backgroundGradient;
67 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
70 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
68 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
71 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
69 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
72 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
70 chart->setChartBackgroundBrush(backgroundGradient);
73 chart->setChartBackgroundBrush(backgroundGradient);
71 }
74 }
72 //TODO helper to by removed later
75 //TODO helper to by removed later
73 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count)
76 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count)
74 {
77 {
75 switch(series->type())
78 switch(series->type())
76 {
79 {
77 case QChartSeries::SeriesTypeLine: {
80 case QChartSeries::SeriesTypeLine: {
78 QLineChartSeries* s = static_cast<QLineChartSeries*>(series);
81 QLineChartSeries* s = static_cast<QLineChartSeries*>(series);
79 LineChartItem* i = static_cast<LineChartItem*>(item);
82 LineChartItem* i = static_cast<LineChartItem*>(item);
80 decorate(i,s,count);
83 decorate(i,s,count);
81 break;
84 break;
82 }
85 }
83 case QChartSeries::SeriesTypeBar: {
86 case QChartSeries::SeriesTypeBar: {
84 BarChartSeries* b = static_cast<BarChartSeries*>(series);
87 BarChartSeries* b = static_cast<BarChartSeries*>(series);
85 BarGroup* i = static_cast<BarGroup*>(item);
88 BarGroup* i = static_cast<BarGroup*>(item);
86 decorate(i,b,count);
89 decorate(i,b,count);
87 break;
90 break;
88 }
91 }
89 case QChartSeries::SeriesTypeStackedBar: {
92 case QChartSeries::SeriesTypeStackedBar: {
90 StackedBarChartSeries* s = static_cast<StackedBarChartSeries*>(series);
93 StackedBarChartSeries* s = static_cast<StackedBarChartSeries*>(series);
91 StackedBarGroup* i = static_cast<StackedBarGroup*>(item);
94 StackedBarGroup* i = static_cast<StackedBarGroup*>(item);
92 decorate(i,s,count);
95 decorate(i,s,count);
93 break;
96 break;
94 }
97 }
95 case QChartSeries::SeriesTypePercentBar: {
98 case QChartSeries::SeriesTypePercentBar: {
96 PercentBarChartSeries* s = static_cast<PercentBarChartSeries*>(series);
99 PercentBarChartSeries* s = static_cast<PercentBarChartSeries*>(series);
97 PercentBarGroup* i = static_cast<PercentBarGroup*>(item);
100 PercentBarGroup* i = static_cast<PercentBarGroup*>(item);
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;
104 }
113 }
105
114
106 }
115 }
107
116
108 void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count)
117 void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count)
109 {
118 {
110 QPen pen;
119 QPen pen;
111 if(pen != series->pen()){
120 if(pen != series->pen()){
112 item->setPen(series->pen());
121 item->setPen(series->pen());
113 return;
122 return;
114 }
123 }
115 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
124 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
116 pen.setWidthF(2);
125 pen.setWidthF(2);
117 item->setPen(pen);
126 item->setPen(pen);
118 }
127 }
119
128
120 void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count)
129 void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count)
121 {
130 {
122 item->addColor(QColor(255,0,0,128));
131 item->addColor(QColor(255,0,0,128));
123 item->addColor(QColor(255,255,0,128));
132 item->addColor(QColor(255,255,0,128));
124 item->addColor(QColor(0,255,0,128));
133 item->addColor(QColor(0,255,0,128));
125 item->addColor(QColor(0,0,255,128));
134 item->addColor(QColor(0,0,255,128));
126 item->addColor(QColor(255,128,0,128));
135 item->addColor(QColor(255,128,0,128));
127 }
136 }
128
137
129 void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count)
138 void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count)
130 {
139 {
131 // Add some fugly colors for 5 fist series...
140 // Add some fugly colors for 5 fist series...
132 item->addColor(QColor(255,0,0,128));
141 item->addColor(QColor(255,0,0,128));
133 item->addColor(QColor(255,255,0,128));
142 item->addColor(QColor(255,255,0,128));
134 item->addColor(QColor(0,255,0,128));
143 item->addColor(QColor(0,255,0,128));
135 item->addColor(QColor(0,0,255,128));
144 item->addColor(QColor(0,0,255,128));
136 item->addColor(QColor(255,128,0,128));
145 item->addColor(QColor(255,128,0,128));
137 }
146 }
138
147
139 void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count)
148 void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count)
140 {
149 {
141 // Add some fugly colors for 5 fist series...
150 // Add some fugly colors for 5 fist series...
142 item->addColor(QColor(255,0,0,128));
151 item->addColor(QColor(255,0,0,128));
143 item->addColor(QColor(255,255,0,128));
152 item->addColor(QColor(255,255,0,128));
144 item->addColor(QColor(0,255,0,128));
153 item->addColor(QColor(0,255,0,128));
145 item->addColor(QColor(0,0,255,128));
154 item->addColor(QColor(0,0,255,128));
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
@@ -1,45 +1,48
1 #ifndef CHARTTHEME_H
1 #ifndef CHARTTHEME_H
2 #define CHARTTHEME_H
2 #define CHARTTHEME_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qchart.h"
5 #include "qchart.h"
6 #include <QColor>
6 #include <QColor>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class ChartItem;
10 class ChartItem;
11 class QChartSeries;
11 class QChartSeries;
12 class LineChartItem;
12 class LineChartItem;
13 class QLineChartSeries;
13 class QLineChartSeries;
14 class BarGroup;
14 class BarGroup;
15 class BarChartSeries;
15 class BarChartSeries;
16 class StackedBarGroup;
16 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 {
23 protected:
25 protected:
24 explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeDefault);
26 explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeDefault);
25 public:
27 public:
26 static ChartTheme* createTheme(QChart::ChartTheme theme);
28 static ChartTheme* createTheme(QChart::ChartTheme theme);
27 QChart::ChartTheme id() const {return m_id;}
29 QChart::ChartTheme id() const {return m_id;}
28 void decorate(QChart* chart);
30 void decorate(QChart* chart);
29 void decorate(ChartItem* item, QChartSeries* series,int count);
31 void decorate(ChartItem* item, QChartSeries* series,int count);
30 void decorate(LineChartItem* item, QLineChartSeries*, int count);
32 void decorate(LineChartItem* item, QLineChartSeries*, int count);
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;
37 QColor m_gradientStartColor;
40 QColor m_gradientStartColor;
38 QColor m_gradientEndColor;
41 QColor m_gradientEndColor;
39 QList<QColor> m_seriesColor;
42 QList<QColor> m_seriesColor;
40
43
41 };
44 };
42
45
43 QTCOMMERCIALCHART_END_NAMESPACE
46 QTCOMMERCIALCHART_END_NAMESPACE
44
47
45 #endif // CHARTTHEME_H
48 #endif // CHARTTHEME_H
@@ -1,113 +1,127
1
1
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
8 PiePresenter::PiePresenter(QGraphicsItem *parent, QPieSeries *series) :
9 PiePresenter::PiePresenter(QGraphicsItem *parent, QPieSeries *series) :
9 ChartItem(parent),
10 ChartItem(parent),
10 m_pieSeries(series)
11 m_pieSeries(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()
19 {
26 {
20 while (m_slices.count())
27 while (m_slices.count())
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())
35 delete m_slices.takeLast();
49 delete m_slices.takeLast();
36
50
37 // create slices
51 // create slices
38 qreal angle = 0;
52 qreal angle = 0;
39 for (int i=0; i<m_pieSeries->count(); i++) {
53 for (int i=0; i<m_pieSeries->count(); i++) {
40 QPieSlice sliceData = m_pieSeries->slice(i);
54 QPieSlice sliceData = m_pieSeries->slice(i);
41 qreal span = sliceData.m_value / total * fullPie;
55 qreal span = sliceData.m_value / total * fullPie;
42 PieSlice *slice = new PieSlice(this, i, angle, span);
56 PieSlice *slice = new PieSlice(this, i, angle, span);
43 m_slices.append(slice);
57 m_slices.append(slice);
44 angle += span;
58 angle += span;
45 }
59 }
46 }
60 }
47
61
48 void PiePresenter::updateGeometry()
62 void PiePresenter::updateGeometry()
49 {
63 {
50 prepareGeometryChange();
64 prepareGeometryChange();
51
65
52 m_pieRect = m_rect;
66 m_pieRect = m_rect;
53
67
54 if (m_pieRect.width() < m_pieRect.height()) {
68 if (m_pieRect.width() < m_pieRect.height()) {
55 m_pieRect.setWidth(m_pieRect.width() * m_pieSeries->sizeFactor());
69 m_pieRect.setWidth(m_pieRect.width() * m_pieSeries->sizeFactor());
56 m_pieRect.setHeight(m_pieRect.width());
70 m_pieRect.setHeight(m_pieRect.width());
57 m_pieRect.moveCenter(m_rect.center());
71 m_pieRect.moveCenter(m_rect.center());
58 } else {
72 } else {
59 m_pieRect.setHeight(m_pieRect.height() * m_pieSeries->sizeFactor());
73 m_pieRect.setHeight(m_pieRect.height() * m_pieSeries->sizeFactor());
60 m_pieRect.setWidth(m_pieRect.height());
74 m_pieRect.setWidth(m_pieRect.height());
61 m_pieRect.moveCenter(m_rect.center());
75 m_pieRect.moveCenter(m_rect.center());
62 }
76 }
63
77
64 switch (m_pieSeries->position()) {
78 switch (m_pieSeries->position()) {
65 case QPieSeries::PiePositionTopLeft: {
79 case QPieSeries::PiePositionTopLeft: {
66 m_pieRect.setHeight(m_pieRect.height() / 2);
80 m_pieRect.setHeight(m_pieRect.height() / 2);
67 m_pieRect.setWidth(m_pieRect.height());
81 m_pieRect.setWidth(m_pieRect.height());
68 m_pieRect.moveCenter(QPointF(m_rect.center().x() / 2, m_rect.center().y() / 2));
82 m_pieRect.moveCenter(QPointF(m_rect.center().x() / 2, m_rect.center().y() / 2));
69 break;
83 break;
70 }
84 }
71 case QPieSeries::PiePositionTopRight: {
85 case QPieSeries::PiePositionTopRight: {
72 m_pieRect.setHeight(m_pieRect.height() / 2);
86 m_pieRect.setHeight(m_pieRect.height() / 2);
73 m_pieRect.setWidth(m_pieRect.height());
87 m_pieRect.setWidth(m_pieRect.height());
74 m_pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, m_rect.center().y() / 2));
88 m_pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, m_rect.center().y() / 2));
75 break;
89 break;
76 }
90 }
77 case QPieSeries::PiePositionBottomLeft: {
91 case QPieSeries::PiePositionBottomLeft: {
78 m_pieRect.setHeight(m_pieRect.height() / 2);
92 m_pieRect.setHeight(m_pieRect.height() / 2);
79 m_pieRect.setWidth(m_pieRect.height());
93 m_pieRect.setWidth(m_pieRect.height());
80 m_pieRect.moveCenter(QPointF(m_rect.center().x() / 2, (m_rect.center().y() / 2) * 3));
94 m_pieRect.moveCenter(QPointF(m_rect.center().x() / 2, (m_rect.center().y() / 2) * 3));
81 break;
95 break;
82 }
96 }
83 case QPieSeries::PiePositionBottomRight: {
97 case QPieSeries::PiePositionBottomRight: {
84 m_pieRect.setHeight(m_pieRect.height() / 2);
98 m_pieRect.setHeight(m_pieRect.height() / 2);
85 m_pieRect.setWidth(m_pieRect.height());
99 m_pieRect.setWidth(m_pieRect.height());
86 m_pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, (m_rect.center().y() / 2) * 3));
100 m_pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, (m_rect.center().y() / 2) * 3));
87 break;
101 break;
88 }
102 }
89 default:
103 default:
90 break;
104 break;
91 }
105 }
92
106
93 qDebug() << "presentation rect:" << m_rect;
107 qDebug() << "presentation rect:" << m_rect;
94 qDebug() << "pie rect:" << m_pieRect;
108 qDebug() << "pie rect:" << m_pieRect;
95 foreach (PieSlice *slice, m_slices)
109 foreach (PieSlice *slice, m_slices)
96 slice->updateGeometry();
110 slice->updateGeometry();
97 }
111 }
98
112
99 void PiePresenter::handleDomainChanged(const Domain& domain)
113 void PiePresenter::handleDomainChanged(const Domain& domain)
100 {
114 {
101 // TODO
115 // TODO
102 }
116 }
103
117
104 void PiePresenter::handleGeometryChanged(const QRectF& rect)
118 void PiePresenter::handleGeometryChanged(const QRectF& rect)
105 {
119 {
106 m_rect = rect;
120 m_rect = rect;
107 updateGeometry();
121 updateGeometry();
108
122
109 }
123 }
110
124
111 #include "moc_piepresenter.cpp"
125 #include "moc_piepresenter.cpp"
112
126
113 QTCOMMERCIALCHART_END_NAMESPACE
127 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,43 +1,43
1 #ifndef PIEPRESENTER_H
1 #ifndef PIEPRESENTER_H
2 #define PIEPRESENTER_H
2 #define PIEPRESENTER_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitem_p.h"
5 #include "qpieseries.h"
5 #include "qpieseries.h"
6
6
7 class QGraphicsItem;
7 class QGraphicsItem;
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 class PieSlice;
9 class PieSlice;
10
10
11 class PiePresenter : public QObject, public ChartItem
11 class PiePresenter : public QObject, public ChartItem
12 {
12 {
13 Q_OBJECT
13 Q_OBJECT
14
14
15 public:
15 public:
16 // TODO: use a generic data class instead of x and y
16 // TODO: use a generic data class instead of x and y
17 PiePresenter(QGraphicsItem *parent, QPieSeries *series);
17 PiePresenter(QGraphicsItem *parent, QPieSeries *series);
18 ~PiePresenter();
18 ~PiePresenter();
19
19
20 public: // from QGraphicsItem
20 public: // from QGraphicsItem
21 QRectF boundingRect() const { return m_rect; }
21 QRectF boundingRect() const { return m_rect; }
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;
35 QList<PieSlice*> m_slices;
35 QList<PieSlice*> m_slices;
36 QPieSeries *m_pieSeries;
36 QPieSeries *m_pieSeries;
37 QRectF m_rect;
37 QRectF m_rect;
38 QRectF m_pieRect;
38 QRectF m_pieRect;
39 };
39 };
40
40
41 QTCOMMERCIALCHART_END_NAMESPACE
41 QTCOMMERCIALCHART_END_NAMESPACE
42
42
43 #endif // PIEPRESENTER_H
43 #endif // PIEPRESENTER_H
@@ -1,124 +1,122
1 #include "pieslice.h"
1 #include "pieslice.h"
2 #include "piepresenter.h"
2 #include "piepresenter.h"
3 #include "qpieseries.h"
3 #include "qpieseries.h"
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
10 #define PI 3.14159265
11 #define PI 3.14159265
11 #define EXPLODE_OFFSET 20
12 #define EXPLODE_OFFSET 20
12
13
13 QPointF offset(qreal angle, qreal length)
14 QPointF offset(qreal angle, qreal length)
14 {
15 {
15 qreal dx = qSin(angle*(PI/180)) * length;
16 qreal dx = qSin(angle*(PI/180)) * length;
16 qreal dy = qCos(angle*(PI/180)) * length;
17 qreal dy = qCos(angle*(PI/180)) * length;
17 return QPointF(dx, -dy);
18 return QPointF(dx, -dy);
18 }
19 }
19
20
20 PieSlice::PieSlice(PiePresenter *presenter, int seriesIndex, qreal startAngle, qreal span)
21 PieSlice::PieSlice(PiePresenter *presenter, int seriesIndex, qreal startAngle, qreal span)
21 :QGraphicsItem(presenter),
22 :QGraphicsItem(presenter),
22 m_label(new QGraphicsTextItem(this)),
23 m_label(new QGraphicsTextItem(this)),
23 m_seriesIndex(seriesIndex),
24 m_seriesIndex(seriesIndex),
24 m_startAngle(startAngle),
25 m_startAngle(startAngle),
25 m_span(span)
26 m_span(span)
26 {
27 {
27 Q_ASSERT(presenter);
28 Q_ASSERT(presenter);
28 setAcceptHoverEvents(true);
29 setAcceptHoverEvents(true);
29 setAcceptedMouseButtons(Qt::LeftButton);
30 setAcceptedMouseButtons(Qt::LeftButton);
30 updateGeometry();
31 updateGeometry();
31
32
32 // TODO: use themes
33 // TODO: use themes
33 m_pen = QPen(Qt::black);
34 m_pen = QPen(Qt::black);
34 m_brush = QBrush(sliceData().m_color);
35 m_brush = QBrush(sliceData().m_color);
35 }
36 }
36
37
37 PieSlice::~PieSlice()
38 PieSlice::~PieSlice()
38 {
39 {
39 }
40 }
40
41
41 QRectF PieSlice::boundingRect() const
42 QRectF PieSlice::boundingRect() const
42 {
43 {
43 return m_rect;
44 return m_rect;
44 }
45 }
45
46
46 QPainterPath PieSlice::shape() const
47 QPainterPath PieSlice::shape() const
47 {
48 {
48 return m_path;
49 return m_path;
49 }
50 }
50
51
51 void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
52 void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
52 {
53 {
53 painter->setRenderHint(QPainter::Antialiasing);
54 painter->setRenderHint(QPainter::Antialiasing);
54
55
55 // TODO: themes
56 // TODO: themes
56 painter->setPen(m_pen);
57 painter->setPen(m_pen);
57 painter->setBrush(m_brush);
58 painter->setBrush(m_brush);
58 painter->drawPath(m_path);
59 painter->drawPath(m_path);
59
60
60 // Draw the label
61 // Draw the label
61 // TODO: do this better
62 // TODO: do this better
62 //QTextItem text;
63 //QTextItem text;
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);
86 }
84 }
87
85
88 void PieSlice::updateGeometry()
86 void PieSlice::updateGeometry()
89 {
87 {
90 prepareGeometryChange();
88 prepareGeometryChange();
91
89
92 PiePresenter *presenter = static_cast<PiePresenter*>(parentItem());
90 PiePresenter *presenter = static_cast<PiePresenter*>(parentItem());
93 QRectF rect = presenter->pieRect();
91 QRectF rect = presenter->pieRect();
94 rect.adjust(EXPLODE_OFFSET, EXPLODE_OFFSET, -EXPLODE_OFFSET ,-EXPLODE_OFFSET);
92 rect.adjust(EXPLODE_OFFSET, EXPLODE_OFFSET, -EXPLODE_OFFSET ,-EXPLODE_OFFSET);
95
93
96 qreal centerAngle = m_startAngle + (m_span/2);
94 qreal centerAngle = m_startAngle + (m_span/2);
97
95
98 if (presenter->m_pieSeries->slice(m_seriesIndex).m_isExploded) {
96 if (presenter->m_pieSeries->slice(m_seriesIndex).m_isExploded) {
99 QPointF d = offset((centerAngle), EXPLODE_OFFSET);
97 QPointF d = offset((centerAngle), EXPLODE_OFFSET);
100 rect.translate(d.x(), d.y());
98 rect.translate(d.x(), d.y());
101 }
99 }
102
100
103 qreal angle = (-m_startAngle) + (90);
101 qreal angle = (-m_startAngle) + (90);
104 qreal span = -m_span;
102 qreal span = -m_span;
105
103
106 QPainterPath path;
104 QPainterPath path;
107 path.moveTo(rect.center());
105 path.moveTo(rect.center());
108 path.arcTo(rect, angle, span);
106 path.arcTo(rect, angle, span);
109
107
110 // TODO: draw the shape so that it might have a hole in the center
108 // TODO: draw the shape so that it might have a hole in the center
111
109
112 m_path = path;
110 m_path = path;
113 m_rect = path.boundingRect();
111 m_rect = path.boundingRect();
114
112
115 qreal radius = rect.height() / 2;
113 qreal radius = rect.height() / 2;
116 m_center = rect.center() + offset(centerAngle, radius / 2);
114 m_center = rect.center() + offset(centerAngle, radius / 2);
117 }
115 }
118
116
119 QPieSlice PieSlice::sliceData()
117 QPieSlice PieSlice::sliceData()
120 {
118 {
121 return (static_cast<PiePresenter*>(parentItem()))->m_pieSeries->slice(m_seriesIndex);
119 return (static_cast<PiePresenter*>(parentItem()))->m_pieSeries->slice(m_seriesIndex);
122 }
120 }
123
121
124 QTCOMMERCIALCHART_END_NAMESPACE
122 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,87 +1,109
1 #include "qpieseries.h"
1 #include "qpieseries.h"
2 #include "piepresenter.h"
2 #include "piepresenter.h"
3 #include "pieslice.h"
3 #include "pieslice.h"
4 #include <QDebug>
4 #include <QDebug>
5
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 QPieSeries::QPieSeries(QObject *parent) :
8 QPieSeries::QPieSeries(QObject *parent) :
9 QChartSeries(parent),
9 QChartSeries(parent),
10 m_sizeFactor(1.0),
10 m_sizeFactor(1.0),
11 m_position(PiePositionMaximized)
11 m_position(PiePositionMaximized)
12 {
12 {
13 }
13 }
14
14
15 QPieSeries::~QPieSeries()
15 QPieSeries::~QPieSeries()
16 {
16 {
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++)
25 changeSet.m_removed << i;
36 changeSet.m_removed << i;
26
37
27 for (int i=0; i<slices.count(); i++) {
38 for (int i=0; i<slices.count(); i++) {
28 if (i < m_slices.count())
39 if (i < m_slices.count())
29 changeSet.m_changed << i;
40 changeSet.m_changed << i;
30 else
41 else
31 changeSet.m_added << i;
42 changeSet.m_added << i;
32 }
43 }
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
54 {
70 {
55 if ((index >= 0) && (index < m_slices.count()))
71 if ((index >= 0) && (index < m_slices.count()))
56 return m_slices.at(index);
72 return m_slices.at(index);
57 return QPieSlice();
73 return QPieSlice();
58 }
74 }
59
75
60 bool QPieSeries::update(int index, QPieSlice slice)
76 bool QPieSeries::update(int index, QPieSlice slice)
61 {
77 {
62 if ((index >= 0) && (index < m_slices.count())) {
78 if ((index >= 0) && (index < m_slices.count())) {
63 m_slices[index] = slice;
79 m_slices[index] = slice;
64 PieChangeSet changeSet;
80 PieChangeSet changeSet;
65 changeSet.m_changed << index;
81 changeSet.m_changed << index;
66 emit changed(changeSet);
82 emit changed(changeSet);
67 return true;
83 return true;
68 }
84 }
69 return false;
85 return false;
70 }
86 }
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"
86
108
87 QTCOMMERCIALCHART_END_NAMESPACE
109 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,95 +1,97
1 #ifndef PIESERIES_H
1 #ifndef PIESERIES_H
2 #define PIESERIES_H
2 #define PIESERIES_H
3
3
4 #include "qchartseries.h"
4 #include "qchartseries.h"
5 #include <QObject>
5 #include <QObject>
6 #include <QRectF>
6 #include <QRectF>
7 #include <QColor>
7 #include <QColor>
8
8
9 class QGraphicsObject;
9 class QGraphicsObject;
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 class PiePresenter;
11 class PiePresenter;
12 class PieSlice;
12 class PieSlice;
13
13
14 class QPieSlice
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
29 class PieChangeSet
29 class PieChangeSet
30 {
30 {
31 public:
31 public:
32 QList<int> m_added;
32 QList<int> m_added;
33 QList<int> m_removed;
33 QList<int> m_removed;
34 QList<int> m_changed;
34 QList<int> m_changed;
35 };
35 };
36
36
37 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QChartSeries
37 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QChartSeries
38 {
38 {
39 Q_OBJECT
39 Q_OBJECT
40
40
41 public:
41 public:
42 enum PiePosition {
42 enum PiePosition {
43 PiePositionMaximized = 0,
43 PiePositionMaximized = 0,
44 PiePositionTopLeft,
44 PiePositionTopLeft,
45 PiePositionTopRight,
45 PiePositionTopRight,
46 PiePositionBottomLeft,
46 PiePositionBottomLeft,
47 PiePositionBottomRight
47 PiePositionBottomRight
48 };
48 };
49
49
50 public:
50 public:
51 QPieSeries(QObject *parent = 0);
51 QPieSeries(QObject *parent = 0);
52 ~QPieSeries();
52 ~QPieSeries();
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
64 QList<QPieSlice> slices() const { return m_slices; }
65 QList<QPieSlice> slices() const { return m_slices; }
65 QPieSlice slice(int index) const;
66 QPieSlice slice(int index) const;
66 bool update(int index, QPieSlice slice);
67 bool update(int index, QPieSlice slice);
67
68
68 // TODO: convenience functions
69 // TODO: convenience functions
69 //void updateValue(int sliceIndex, qreal value);
70 //void updateValue(int sliceIndex, qreal value);
70 //void updateLabel(int sliceIndex, QString label);
71 //void updateLabel(int sliceIndex, QString label);
71 //void updateColor(int sliceIndex, QColor color);
72 //void updateColor(int sliceIndex, QColor color);
72 //void updateExploded(int slizeIndex, bool exploded);
73 //void updateExploded(int slizeIndex, bool exploded);
73
74
74 void setSizeFactor(qreal sizeFactor);
75 void setSizeFactor(qreal sizeFactor);
75 qreal sizeFactor() const { return m_sizeFactor; }
76 qreal sizeFactor() const { return m_sizeFactor; }
76
77
77 void setPosition(PiePosition position);
78 void setPosition(PiePosition position);
78 PiePosition position() const { return m_position; }
79 PiePosition position() const { return m_position; }
79
80
80 Q_SIGNALS:
81 Q_SIGNALS:
81 void changed(const PieChangeSet& changeSet);
82 void changed(const PieChangeSet& changeSet);
82 void sizeFactorChanged();
83 void sizeFactorChanged();
83 void positionChanged();
84 void positionChanged();
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;
90 PiePosition m_position;
92 PiePosition m_position;
91 };
93 };
92
94
93 QTCOMMERCIALCHART_END_NAMESPACE
95 QTCOMMERCIALCHART_END_NAMESPACE
94
96
95 #endif // PIESERIES_H
97 #endif // PIESERIES_H
General Comments 0
You need to be logged in to leave comments. Login now