##// END OF EJS Templates
Adds default gemoetry change call in case of new series
Michal Klocek -
r148:38a3d03baaf6
parent child
Show More
@@ -1,249 +1,251
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 //items
12 //items
13 #include "axisitem_p.h"
13 #include "axisitem_p.h"
14 #include "bargroup.h"
14 #include "bargroup.h"
15 #include "stackedbargroup.h"
15 #include "stackedbargroup.h"
16 #include "linechartitem_p.h"
16 #include "linechartitem_p.h"
17 #include "percentbargroup.h"
17 #include "percentbargroup.h"
18 #include "linechartanimationitem_p.h"
18 #include "linechartanimationitem_p.h"
19 #include "piepresenter.h"
19 #include "piepresenter.h"
20
20
21 QTCOMMERCIALCHART_BEGIN_NAMESPACE
21 QTCOMMERCIALCHART_BEGIN_NAMESPACE
22
22
23 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
23 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
24 m_chart(chart),
24 m_chart(chart),
25 m_dataset(dataset),
25 m_dataset(dataset),
26 m_chartTheme(0),
26 m_chartTheme(0),
27 m_domainIndex(0),
27 m_domainIndex(0),
28 m_marginSize(0),
28 m_marginSize(0),
29 m_rect(QRectF(QPoint(0,0),m_chart->size()))
29 m_rect(QRectF(QPoint(0,0),m_chart->size()))
30 {
30 {
31 setTheme(QChart::ChartThemeDefault);
31 setTheme(QChart::ChartThemeDefault);
32 createConnections();
32 createConnections();
33 createDeafultAxis();
33 createDeafultAxis();
34 }
34 }
35
35
36 ChartPresenter::~ChartPresenter()
36 ChartPresenter::~ChartPresenter()
37 {
37 {
38 }
38 }
39
39
40 void ChartPresenter::createDeafultAxis()
40 void ChartPresenter::createDeafultAxis()
41 {
41 {
42 //default axis
42 //default axis
43 QChartAxis* axisX = new QChartAxis(this);
43 QChartAxis* axisX = new QChartAxis(this);
44 QChartAxis* axisY = new QChartAxis(this);
44 QChartAxis* axisY = new QChartAxis(this);
45
45
46 m_axis << new AxisItem(axisX,AxisItem::X_AXIS,m_chart);
46 m_axis << new AxisItem(axisX,AxisItem::X_AXIS,m_chart);
47 m_axis << new AxisItem(axisY,AxisItem::Y_AXIS,m_chart);
47 m_axis << new AxisItem(axisY,AxisItem::Y_AXIS,m_chart);
48
48
49 foreach(AxisItem* item, m_axis) {
49 foreach(AxisItem* item, m_axis) {
50 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
50 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
51 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
51 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
52 }
52 }
53 }
53 }
54
54
55 void ChartPresenter::createConnections()
55 void ChartPresenter::createConnections()
56 {
56 {
57 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
57 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
58 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
58 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
59 }
59 }
60
60
61 void ChartPresenter::handleGeometryChanged()
61 void ChartPresenter::handleGeometryChanged()
62 {
62 {
63 m_rect = QRectF(QPoint(0,0),m_chart->size());
63 m_rect = QRectF(QPoint(0,0),m_chart->size());
64 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
64 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
65 Q_ASSERT(m_rect.isValid());
65 Q_ASSERT(m_rect.isValid());
66 emit geometryChanged(m_rect);
66 emit geometryChanged(m_rect);
67 }
67 }
68
68
69 int ChartPresenter::margin() const
69 int ChartPresenter::margin() const
70 {
70 {
71 return m_marginSize;
71 return m_marginSize;
72 }
72 }
73
73
74 void ChartPresenter::setMargin(int margin)
74 void ChartPresenter::setMargin(int margin)
75 {
75 {
76 m_marginSize = margin;
76 m_marginSize = margin;
77 }
77 }
78
78
79 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
79 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
80 {
80 {
81 switch(series->type())
81 switch(series->type())
82 {
82 {
83 case QChartSeries::SeriesTypeLine: {
83 case QChartSeries::SeriesTypeLine: {
84 QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series);
84 QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series);
85 LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart);
85 LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart);
86 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
86 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
87 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
87 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
88 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
88 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
89 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
89 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
90 m_chartItems.insert(series,item);
90 m_chartItems.insert(series,item);
91 break;
91 break;
92 }
92 }
93
93
94 case QChartSeries::SeriesTypeBar: {
94 case QChartSeries::SeriesTypeBar: {
95 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
95 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
96 BarGroup* item = new BarGroup(*barSeries,m_chart);
96 BarGroup* item = new BarGroup(*barSeries,m_chart);
97 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
97 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
98 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
98 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
99 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
99 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
100 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
100 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
101 m_chartItems.insert(series,item);
101 m_chartItems.insert(series,item);
102 // m_axisXItem->setVisible(false);
102 // m_axisXItem->setVisible(false);
103 break;
103 break;
104 }
104 }
105
105
106 case QChartSeries::SeriesTypeStackedBar: {
106 case QChartSeries::SeriesTypeStackedBar: {
107
107
108 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
108 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
109 StackedBarGroup* item = new StackedBarGroup(*stackedBarSeries,m_chart);
109 StackedBarGroup* item = new StackedBarGroup(*stackedBarSeries,m_chart);
110 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
110 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
111 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
111 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
112 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
112 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
113 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
113 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
114 m_chartItems.insert(series,item);
114 m_chartItems.insert(series,item);
115 break;
115 break;
116 }
116 }
117
117
118 case QChartSeries::SeriesTypePercentBar: {
118 case QChartSeries::SeriesTypePercentBar: {
119
119
120 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
120 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
121 PercentBarGroup* item = new PercentBarGroup(*percentBarSeries,m_chart);
121 PercentBarGroup* item = new PercentBarGroup(*percentBarSeries,m_chart);
122 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
122 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
123 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
123 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
124 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
124 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
125 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
125 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
126 m_chartItems.insert(series,item);
126 m_chartItems.insert(series,item);
127 break;
127 break;
128 }
128 }
129 /*
129 /*
130 case QChartSeries::SeriesTypeScatter: {
130 case QChartSeries::SeriesTypeScatter: {
131 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
131 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
132 scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
132 scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
133 scatterSeries->d->setParentItem(this);
133 scatterSeries->d->setParentItem(this);
134 scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
134 scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
135 m_chartItems << scatterSeries->d;
135 m_chartItems << scatterSeries->d;
136 m_chartTheme->addObserver(scatterSeries->d);
136 m_chartTheme->addObserver(scatterSeries->d);
137
137
138 foreach (qreal x, scatterSeries->d->m_x) {
138 foreach (qreal x, scatterSeries->d->m_x) {
139 domain.m_minX = qMin(domain.m_minX, x);
139 domain.m_minX = qMin(domain.m_minX, x);
140 domain.m_maxX = qMax(domain.m_maxX, x);
140 domain.m_maxX = qMax(domain.m_maxX, x);
141 }
141 }
142 foreach (qreal y, scatterSeries->d->m_y) {
142 foreach (qreal y, scatterSeries->d->m_y) {
143 domain.m_minY = qMin(domain.m_minY, y);
143 domain.m_minY = qMin(domain.m_minY, y);
144 domain.m_maxY = qMax(domain.m_maxY, y);
144 domain.m_maxY = qMax(domain.m_maxY, y);
145 }
145 }
146
146
147 break;
147 break;
148 }
148 }
149 */
149 */
150
150
151 case QChartSeries::SeriesTypePie: {
151 case QChartSeries::SeriesTypePie: {
152 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
152 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
153 PiePresenter* pie = new PiePresenter(m_chart, pieSeries);
153 PiePresenter* pie = new PiePresenter(m_chart, pieSeries);
154 pieSeries->m_piePresenter = pie; // TODO: remove this pointer passing use signals&slots
154 pieSeries->m_piePresenter = pie; // TODO: remove this pointer passing use signals&slots
155 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
155 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
156 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)), pie, SLOT(handleDomainChanged(const Domain&)));
156 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)), pie, SLOT(handleDomainChanged(const Domain&)));
157 m_chartItems.insert(series, pie);
157 m_chartItems.insert(series, pie);
158 break;
158 break;
159 }
159 }
160
160
161 default: {
161 default: {
162 qDebug()<< "Series type" << series->type() << "not implemented.";
162 qDebug()<< "Series type" << series->type() << "not implemented.";
163 break;
163 break;
164 }
164 }
165 }
165 }
166
167 if(m_rect.isValid()) emit geometryChanged(m_rect);
166 }
168 }
167
169
168 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
170 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
169 {
171 {
170 //TODO:
172 //TODO:
171 }
173 }
172
174
173 void ChartPresenter::zoomInToRect(const QRectF& rect)
175 void ChartPresenter::zoomInToRect(const QRectF& rect)
174 {
176 {
175 if(!rect.isValid()) return;
177 if(!rect.isValid()) return;
176 QRectF r = rect.normalized();
178 QRectF r = rect.normalized();
177 r.translate(-m_marginSize, -m_marginSize);
179 r.translate(-m_marginSize, -m_marginSize);
178 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
180 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
179 m_dataset->addDomain(domain);
181 m_dataset->addDomain(domain);
180 }
182 }
181
183
182 void ChartPresenter::zoomIn()
184 void ChartPresenter::zoomIn()
183 {
185 {
184 if (!m_dataset->nextDomain()) {
186 if (!m_dataset->nextDomain()) {
185 QRectF rect = m_rect;
187 QRectF rect = m_rect;
186 rect.setWidth(rect.width()/2);
188 rect.setWidth(rect.width()/2);
187 rect.setHeight(rect.height()/2);
189 rect.setHeight(rect.height()/2);
188 rect.moveCenter(m_rect.center());
190 rect.moveCenter(m_rect.center());
189 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
191 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
190 m_dataset->addDomain(domain);
192 m_dataset->addDomain(domain);
191 }
193 }
192 }
194 }
193
195
194 void ChartPresenter::zoomOut()
196 void ChartPresenter::zoomOut()
195 {
197 {
196 m_dataset->previousDomain();
198 m_dataset->previousDomain();
197 }
199 }
198
200
199 void ChartPresenter::zoomReset()
201 void ChartPresenter::zoomReset()
200 {
202 {
201 m_dataset->clearDomains();
203 m_dataset->clearDomains();
202 }
204 }
203
205
204 void ChartPresenter::setTheme(QChart::ChartThemeId theme)
206 void ChartPresenter::setTheme(QChart::ChartThemeId theme)
205 {
207 {
206 delete m_chartTheme;
208 delete m_chartTheme;
207
209
208 m_chartTheme = ChartTheme::createTheme(theme);
210 m_chartTheme = ChartTheme::createTheme(theme);
209
211
210 m_chartTheme->decorate(m_chart);
212 m_chartTheme->decorate(m_chart);
211 QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems);
213 QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems);
212
214
213 int index=0;
215 int index=0;
214 while (i.hasNext()) {
216 while (i.hasNext()) {
215 i.next();
217 i.next();
216 index++;
218 index++;
217 m_chartTheme->decorate(i.value(),i.key(),index);
219 m_chartTheme->decorate(i.value(),i.key(),index);
218 }
220 }
219 }
221 }
220
222
221
223
222 QChart::ChartThemeId ChartPresenter::theme()
224 QChart::ChartThemeId ChartPresenter::theme()
223 {
225 {
224 return (QChart::ChartThemeId) 0;
226 return (QChart::ChartThemeId) 0;
225 }
227 }
226
228
227 /*
229 /*
228 void ChartPresenter::setAxisX(const QChartAxis& axis)
230 void ChartPresenter::setAxisX(const QChartAxis& axis)
229 {
231 {
230 setAxis(m_axisXItem,axis);
232 setAxis(m_axisXItem,axis);
231 }
233 }
232 void ChartPresenter::setAxisY(const QChartAxis& axis)
234 void ChartPresenter::setAxisY(const QChartAxis& axis)
233 {
235 {
234 setAxis(m_axisYItem.at(0),axis);
236 setAxis(m_axisYItem.at(0),axis);
235 }
237 }
236
238
237 void ChartPresenter::setAxisY(const QList<QChartAxis>& axis)
239 void ChartPresenter::setAxisY(const QList<QChartAxis>& axis)
238 {
240 {
239 //TODO not implemented
241 //TODO not implemented
240 }
242 }
241
243
242 void ChartPresenter::setAxis(AxisItem *item, const QChartAxis& axis)
244 void ChartPresenter::setAxis(AxisItem *item, const QChartAxis& axis)
243 {
245 {
244 item->setVisible(axis.isAxisVisible());
246 item->setVisible(axis.isAxisVisible());
245 }
247 }
246 */
248 */
247 #include "moc_chartpresenter_p.cpp"
249 #include "moc_chartpresenter_p.cpp"
248
250
249 QTCOMMERCIALCHART_END_NAMESPACE
251 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now