##// END OF EJS Templates
Fix ownership handling: if object has a parent do not delete it
Michal Klocek -
r444:7fb10659b976
parent child
Show More
@@ -1,59 +1,59
1 #include "chartview.h"
1 #include "chartview.h"
2 #include <qlineseries.h>
2 #include <qlineseries.h>
3 #include <QTime>
3 #include <QTime>
4
4
5 ChartView::ChartView(QWidget* parent):QChartView(parent),
5 ChartView::ChartView(QWidget* parent):QChartView(parent),
6 m_index(0)
6 m_index(0)
7 {
7 {
8 QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout()));
8 QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout()));
9 m_timer.setInterval(3000);
9 m_timer.setInterval(3000);
10
10
11 QTime now = QTime::currentTime();
11 QTime now = QTime::currentTime();
12 qsrand((uint)now.msec());
12 qsrand((uint)now.msec());
13
13
14 QLineSeries* series0 = new QLineSeries();
14 QLineSeries* series0 = new QLineSeries(this);
15 QPen blue(Qt::blue);
15 QPen blue(Qt::blue);
16 blue.setWidth(3);
16 blue.setWidth(3);
17 series0->setPen(blue);
17 series0->setPen(blue);
18 QLineSeries* series1 = new QLineSeries();
18 QLineSeries* series1 = new QLineSeries(this);
19 QPen red(Qt::red);
19 QPen red(Qt::red);
20 red.setWidth(3);
20 red.setWidth(3);
21 series1->setPen(red);
21 series1->setPen(red);
22 QLineSeries* series2 = new QLineSeries();
22 QLineSeries* series2 = new QLineSeries(this);
23 QPen green(Qt::green);
23 QPen green(Qt::green);
24 green.setWidth(3);
24 green.setWidth(3);
25 series2->setPen(green);
25 series2->setPen(green);
26
26
27 int numPoints = 10;
27 int numPoints = 10;
28
28
29 for (int x = 0; x <= numPoints; ++x) {
29 for (int x = 0; x <= numPoints; ++x) {
30 series0->add(x, qrand() % 100);
30 series0->add(x, qrand() % 100);
31 series1->add(x, qrand() % 100);
31 series1->add(x, qrand() % 100);
32 series2->add(x, qrand() % 100);
32 series2->add(x, qrand() % 100);
33 }
33 }
34
34
35 addSeries(series0);
35 addSeries(series0);
36
36
37 m_series<<series0;
37 m_series<<series0;
38 m_series<<series1;
38 m_series<<series1;
39 m_series<<series2;
39 m_series<<series2;
40
40
41 m_timer.start();
41 m_timer.start();
42 }
42 }
43
43
44 ChartView::~ChartView()
44 ChartView::~ChartView()
45 {
45 {
46 if(m_series.size()==0) return;
46 if(m_series.size()==0) return;
47 removeSeries(m_series.at(m_index));
47 removeSeries(m_series.at(m_index));
48 qDeleteAll(m_series);
48 qDeleteAll(m_series);
49 }
49 }
50
50
51 void ChartView::handleTimeout()
51 void ChartView::handleTimeout()
52 {
52 {
53 if(m_series.size()==0) return;
53 if(m_series.size()==0) return;
54
54
55 removeSeries(m_series.at(m_index));
55 removeSeries(m_series.at(m_index));
56 m_index++;
56 m_index++;
57 m_index=m_index%m_series.size();
57 m_index=m_index%m_series.size();
58 addSeries(m_series.at(m_index));
58 addSeries(m_series.at(m_index));
59 }
59 }
@@ -1,288 +1,299
1 #include "chartdataset_p.h"
1 #include "chartdataset_p.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 //series
3 //series
4 #include "qlineseries.h"
4 #include "qlineseries.h"
5 #include "qareaseries.h"
5 #include "qareaseries.h"
6 #include "qbarseries.h"
6 #include "qbarseries.h"
7 #include "qstackedbarseries.h"
7 #include "qstackedbarseries.h"
8 #include "qpercentbarseries.h"
8 #include "qpercentbarseries.h"
9 #include "qpieseries.h"
9 #include "qpieseries.h"
10 #include "qscatterseries.h"
10 #include "qscatterseries.h"
11 #include "qsplineseries.h"
11 #include "qsplineseries.h"
12
12
13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
14
14
15 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
15 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
16 m_axisX(new QChartAxis(this)),
16 m_axisX(new QChartAxis(this)),
17 m_axisY(new QChartAxis(this)),
17 m_axisY(new QChartAxis(this)),
18 m_domainIndex(0),
18 m_domainIndex(0),
19 m_axisXInitialized(false)
19 m_axisXInitialized(false)
20 {
20 {
21 }
21 }
22
22
23 ChartDataSet::~ChartDataSet()
23 ChartDataSet::~ChartDataSet()
24 {
24 {
25 }
25 }
26
26
27 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
27 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
28 {
28 {
29 if(axisY==0) axisY = m_axisY;
29 if(axisY==0) axisY = m_axisY;
30
30
31 QChartAxis* axis = m_seriesAxisMap.value(series);
31 QChartAxis* axis = m_seriesAxisMap.value(series);
32
32
33 if(axis) {
33 if(axis) {
34 qWarning() << "Can not add series. Series already on the chart";
34 qWarning() << "Can not add series. Series already on the chart";
35 return;
35 return;
36 }
36 }
37
37
38 if(!series->parent()){
38 if(!series->parent()){
39 series->setParent(this); // take ownership
39 series->setParent(this); // take ownership
40 };
40 };
41
41
42 if(!axisY->parent()){
42 if(!axisY->parent()){
43 axisY->setParent(this); // take ownership
43 axisY->setParent(this); // take ownership
44 }
44 }
45
45
46 Domain* domain = m_axisDomainMap.value(axisY);
46 Domain* domain = m_axisDomainMap.value(axisY);
47
47
48 if(!domain) {
48 if(!domain) {
49 domain = new Domain();
49 domain = new Domain();
50
50
51 QObject::connect(axisY,SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeXChanged(qreal,qreal)));
51 QObject::connect(axisY,SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeXChanged(qreal,qreal)));
52 QObject::connect(axisX(),SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeYChanged(qreal,qreal)));
52 QObject::connect(axisX(),SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeYChanged(qreal,qreal)));
53 //initialize
53 //initialize
54 m_axisDomainMap.insert(axisY,domain);
54 m_axisDomainMap.insert(axisY,domain);
55 emit axisAdded(axisY,domain);
55 emit axisAdded(axisY,domain);
56 }
56 }
57
57
58 if(!m_axisXInitialized){
58 if(!m_axisXInitialized){
59 emit axisAdded(axisX(),domain);
59 emit axisAdded(axisX(),domain);
60 m_axisXInitialized=true;
60 m_axisXInitialized=true;
61 }
61 }
62
62
63 calculateDomain(series,domain);
63 calculateDomain(series,domain);
64
64
65 m_seriesAxisMap.insert(series,axisY);
65 m_seriesAxisMap.insert(series,axisY);
66 emit seriesAdded(series,domain);
66 emit seriesAdded(series,domain);
67
67
68 }
68 }
69
69
70 void ChartDataSet::removeSeries(QSeries* series)
70 void ChartDataSet::removeSeries(QSeries* series)
71 {
71 {
72
72
73 QChartAxis* axis = m_seriesAxisMap.value(series);
73 QChartAxis* axis = m_seriesAxisMap.value(series);
74
74
75 if(!axis){
75 if(!axis){
76 qWarning()<<"Can not remove series. Series not found on the chart.";
76 qWarning()<<"Can not remove series. Series not found on the chart.";
77 return;
77 return;
78 }
78 }
79 emit seriesRemoved(series);
79 emit seriesRemoved(series);
80 m_seriesAxisMap.remove(series);
80 m_seriesAxisMap.remove(series);
81
81
82 if(series->parent()==this){
82 if(series->parent()==this){
83 delete series;
83 delete series;
84 series=0;
84 series=0;
85 }
85 }
86
86
87 QList<QChartAxis*> axes = m_seriesAxisMap.values();
87 QList<QChartAxis*> axes = m_seriesAxisMap.values();
88
88
89 int i = axes.indexOf(axis);
89 int i = axes.indexOf(axis);
90
90
91 if(i==-1){
91 if(i==-1){
92 Domain* domain = m_axisDomainMap.take(axis);
92 Domain* domain = m_axisDomainMap.take(axis);
93 emit axisRemoved(axis);
93 emit axisRemoved(axis);
94 if(axis!=axisY()){
95 if(axis->parent()==this){
96 delete axis;
97 axis=0;
98 }
99 }
94 delete domain;
100 delete domain;
95 }
101 }
96
102
97 if(m_seriesAxisMap.values().size()==0)
103 if(m_seriesAxisMap.values().size()==0)
98 {
104 {
99 m_axisXInitialized=false;
105 m_axisXInitialized=false;
100 emit axisRemoved(axisX());
106 emit axisRemoved(axisX());
101 }
107 }
102 }
108 }
103
109
104 void ChartDataSet::removeAllSeries()
110 void ChartDataSet::removeAllSeries()
105 {
111 {
106
112
107 QList<QSeries*> series = m_seriesAxisMap.keys();
113 QList<QSeries*> series = m_seriesAxisMap.keys();
108
114
109 foreach(QSeries* s , series) {
115 foreach(QSeries* s , series) {
110 removeSeries(s);
116 removeSeries(s);
111 }
117 }
112
118
113 Q_ASSERT(m_seriesAxisMap.count()==0);
119 Q_ASSERT(m_seriesAxisMap.count()==0);
114 Q_ASSERT(m_axisDomainMap.count()==0);
120 Q_ASSERT(m_axisDomainMap.count()==0);
115
121
116 }
122 }
117
123
118 //to be removed with PIMPL
124 //to be removed with PIMPL
119 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const
125 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const
120 {
126 {
121 switch(series->type())
127 switch(series->type())
122 {
128 {
123 case QSeries::SeriesTypeLine: {
129 case QSeries::SeriesTypeLine: {
124
130
125 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
131 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
126
132
127 for (int i = 0; i < lineSeries->count(); i++)
133 for (int i = 0; i < lineSeries->count(); i++)
128 {
134 {
129 qreal x = lineSeries->x(i);
135 qreal x = lineSeries->x(i);
130 qreal y = lineSeries->y(i);
136 qreal y = lineSeries->y(i);
131 domain->setMinX(qMin(domain->minX(),x));
137 domain->setMinX(qMin(domain->minX(),x));
132 domain->setMinY(qMin(domain->minY(),y));
138 domain->setMinY(qMin(domain->minY(),y));
133 domain->setMaxX(qMax(domain->maxX(),x));
139 domain->setMaxX(qMax(domain->maxX(),x));
134 domain->setMaxY(qMax(domain->maxY(),y));
140 domain->setMaxY(qMax(domain->maxY(),y));
135 }
141 }
136 break;
142 break;
137 }
143 }
138 case QSeries::SeriesTypeArea: {
144 case QSeries::SeriesTypeArea: {
139
145
140 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
146 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
141
147
142 QLineSeries* upperSeries = areaSeries->upperSeries();
148 QLineSeries* upperSeries = areaSeries->upperSeries();
143 QLineSeries* lowerSeries = areaSeries->lowerSeries();
149 QLineSeries* lowerSeries = areaSeries->lowerSeries();
144
150
145 for (int i = 0; i < upperSeries->count(); i++)
151 for (int i = 0; i < upperSeries->count(); i++)
146 {
152 {
147 qreal x = upperSeries->x(i);
153 qreal x = upperSeries->x(i);
148 qreal y = upperSeries->y(i);
154 qreal y = upperSeries->y(i);
149 domain->setMinX(qMin(domain->minX(),x));
155 domain->setMinX(qMin(domain->minX(),x));
150 domain->setMinY(qMin(domain->minY(),y));
156 domain->setMinY(qMin(domain->minY(),y));
151 domain->setMaxX(qMax(domain->maxX(),x));
157 domain->setMaxX(qMax(domain->maxX(),x));
152 domain->setMaxY(qMax(domain->maxY(),y));
158 domain->setMaxY(qMax(domain->maxY(),y));
153 }
159 }
154 if(lowerSeries) {
160 if(lowerSeries) {
155 for (int i = 0; i < lowerSeries->count(); i++)
161 for (int i = 0; i < lowerSeries->count(); i++)
156 {
162 {
157 qreal x = lowerSeries->x(i);
163 qreal x = lowerSeries->x(i);
158 qreal y = lowerSeries->y(i);
164 qreal y = lowerSeries->y(i);
159 domain->setMinX(qMin(domain->minX(),x));
165 domain->setMinX(qMin(domain->minX(),x));
160 domain->setMinY(qMin(domain->minY(),y));
166 domain->setMinY(qMin(domain->minY(),y));
161 domain->setMaxX(qMax(domain->maxX(),x));
167 domain->setMaxX(qMax(domain->maxX(),x));
162 domain->setMaxY(qMax(domain->maxY(),y));
168 domain->setMaxY(qMax(domain->maxY(),y));
163 }}
169 }}
164 break;
170 break;
165 }
171 }
166 case QSeries::SeriesTypeBar: {
172 case QSeries::SeriesTypeBar: {
167 qDebug() << "QChartSeries::SeriesTypeBar";
173 qDebug() << "QChartSeries::SeriesTypeBar";
168 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
174 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
169 qreal x = barSeries->categoryCount();
175 qreal x = barSeries->categoryCount();
170 qreal y = barSeries->max();
176 qreal y = barSeries->max();
171 domain->setMinX(qMin(domain->minX(),x));
177 domain->setMinX(qMin(domain->minX(),x));
172 domain->setMinY(qMin(domain->minY(),y));
178 domain->setMinY(qMin(domain->minY(),y));
173 domain->setMaxX(qMax(domain->maxX(),x));
179 domain->setMaxX(qMax(domain->maxX(),x));
174 domain->setMaxY(qMax(domain->maxY(),y));
180 domain->setMaxY(qMax(domain->maxY(),y));
175 break;
181 break;
176 }
182 }
177 case QSeries::SeriesTypeStackedBar: {
183 case QSeries::SeriesTypeStackedBar: {
178 qDebug() << "QChartSeries::SeriesTypeStackedBar";
184 qDebug() << "QChartSeries::SeriesTypeStackedBar";
179
185
180 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
186 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
181 qreal x = stackedBarSeries->categoryCount();
187 qreal x = stackedBarSeries->categoryCount();
182 qreal y = stackedBarSeries->maxCategorySum();
188 qreal y = stackedBarSeries->maxCategorySum();
183 domain->setMinX(qMin(domain->minX(),x));
189 domain->setMinX(qMin(domain->minX(),x));
184 domain->setMinY(qMin(domain->minY(),y));
190 domain->setMinY(qMin(domain->minY(),y));
185 domain->setMaxX(qMax(domain->maxX(),x));
191 domain->setMaxX(qMax(domain->maxX(),x));
186 domain->setMaxY(qMax(domain->maxY(),y));
192 domain->setMaxY(qMax(domain->maxY(),y));
187 break;
193 break;
188 }
194 }
189 case QSeries::SeriesTypePercentBar: {
195 case QSeries::SeriesTypePercentBar: {
190 qDebug() << "QChartSeries::SeriesTypePercentBar";
196 qDebug() << "QChartSeries::SeriesTypePercentBar";
191
197
192 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
198 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
193 qreal x = percentBarSeries->categoryCount();
199 qreal x = percentBarSeries->categoryCount();
194 domain->setMinX(qMin(domain->minX(),x));
200 domain->setMinX(qMin(domain->minX(),x));
195 domain->setMinY(0);
201 domain->setMinY(0);
196 domain->setMaxX(qMax(domain->maxX(),x));
202 domain->setMaxX(qMax(domain->maxX(),x));
197 domain->setMaxY(100);
203 domain->setMaxY(100);
198 break;
204 break;
199 }
205 }
200
206
201 case QSeries::SeriesTypePie: {
207 case QSeries::SeriesTypePie: {
202 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
208 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
203 // TODO: domain stuff
209 // TODO: domain stuff
204 break;
210 break;
205 }
211 }
206
212
207 case QSeries::SeriesTypeScatter: {
213 case QSeries::SeriesTypeScatter: {
208 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
214 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
209 Q_ASSERT(scatterSeries);
215 Q_ASSERT(scatterSeries);
210 foreach (QPointF point, scatterSeries->data()) {
216 foreach (QPointF point, scatterSeries->data()) {
211 domain->setMinX(qMin(domain->minX(), point.x()));
217 domain->setMinX(qMin(domain->minX(), point.x()));
212 domain->setMinY(qMax(domain->maxX(), point.x()));
218 domain->setMinY(qMax(domain->maxX(), point.x()));
213 domain->setMaxX(qMin(domain->minY(), point.y()));
219 domain->setMaxX(qMin(domain->minY(), point.y()));
214 domain->setMaxY(qMax(domain->maxY(), point.y()));
220 domain->setMaxY(qMax(domain->maxY(), point.y()));
215 }
221 }
216 break;
222 break;
217 }
223 }
218
224
219 case QSeries::SeriesTypeSpline: {
225 case QSeries::SeriesTypeSpline: {
220 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
226 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
221
227
222 for (int i = 0; i < splineSeries->count(); i++)
228 for (int i = 0; i < splineSeries->count(); i++)
223 {
229 {
224 qreal x = splineSeries->x(i);
230 qreal x = splineSeries->x(i);
225 qreal y = splineSeries->y(i);
231 qreal y = splineSeries->y(i);
226 domain->setMinX(qMin(domain->minX(),x));
232 domain->setMinX(qMin(domain->minX(),x));
227 domain->setMinY(qMin(domain->minY(),y));
233 domain->setMinY(qMin(domain->minY(),y));
228 domain->setMaxX(qMax(domain->maxX(),x));
234 domain->setMaxX(qMax(domain->maxX(),x));
229 domain->setMaxY(qMax(domain->maxY(),y));
235 domain->setMaxY(qMax(domain->maxY(),y));
230 }
236 }
231 break;
237 break;
232 }
238 }
233
239
234 default: {
240 default: {
235 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
241 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
236 return;
242 return;
237 break;
243 break;
238 }
244 }
239
245
240 }
246 }
241 }
247 }
242
248
243 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
249 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
244 {
250 {
245 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
251 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
246 while (i.hasNext()) {
252 while (i.hasNext()) {
247 i.next();
253 i.next();
248 i.value()->zoomIn(rect,size);
254 i.value()->zoomIn(rect,size);
249 }
255 }
250 }
256 }
251
257
252 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
258 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
253 {
259 {
254 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
260 QMapIterator<QChartAxis*, Domain*> i( m_axisDomainMap);
255 while (i.hasNext()) {
261 while (i.hasNext()) {
256 i.next();
262 i.next();
257 i.value()->zoomOut(rect,size);
263 i.value()->zoomOut(rect,size);
258 }
264 }
259 }
265 }
260
266
261 QChartAxis* ChartDataSet::axisY(QSeries* series) const
267 QChartAxis* ChartDataSet::axisY(QSeries* series) const
262 {
268 {
263 if(series == 0) return m_axisY;
269 if(series == 0) return m_axisY;
264 return m_seriesAxisMap.value(series);
270 return m_seriesAxisMap.value(series);
265 }
271 }
266
272
267 Domain* ChartDataSet::domain(QSeries* series) const
273 Domain* ChartDataSet::domain(QSeries* series) const
268 {
274 {
269 QChartAxis* axis = m_seriesAxisMap.value(series);
275 QChartAxis* axis = m_seriesAxisMap.value(series);
270 if(axis){
276 if(axis){
271 return m_axisDomainMap.value(axis);
277 return m_axisDomainMap.value(axis);
272 }else
278 }else
273 return 0;
279 return 0;
274 }
280 }
275
281
276 Domain* ChartDataSet::domain(QChartAxis* axis) const
282 Domain* ChartDataSet::domain(QChartAxis* axis) const
277 {
283 {
278 return m_axisDomainMap.value(axis);
284 if(axis==axisX()) {
285 return m_axisDomainMap.value(axisY());
286 }
287 else {
288 return m_axisDomainMap.value(axis);
289 }
279 }
290 }
280
291
281 QChartAxis* ChartDataSet::axis(QSeries* series) const
292 QChartAxis* ChartDataSet::axis(QSeries* series) const
282 {
293 {
283 return m_seriesAxisMap.value(series);
294 return m_seriesAxisMap.value(series);
284 }
295 }
285
296
286 #include "moc_chartdataset_p.cpp"
297 #include "moc_chartdataset_p.cpp"
287
298
288 QTCOMMERCIALCHART_END_NAMESPACE
299 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now