@@ -1,284 +1,286 | |||||
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 "qbarseries.h" |
|
7 | #include "qbarseries.h" | |
8 | #include "qstackedbarseries.h" |
|
8 | #include "qstackedbarseries.h" | |
9 | #include "qpercentbarseries.h" |
|
9 | #include "qpercentbarseries.h" | |
10 | #include "qlineseries.h" |
|
10 | #include "qlineseries.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 "axisanimationitem_p.h" |
|
15 | #include "axisanimationitem_p.h" | |
16 | #include "barpresenter_p.h" |
|
16 | #include "barpresenter_p.h" | |
17 | #include "stackedbarpresenter_p.h" |
|
17 | #include "stackedbarpresenter_p.h" | |
18 | #include "percentbarpresenter_p.h" |
|
18 | #include "percentbarpresenter_p.h" | |
19 | #include "linechartitem_p.h" |
|
19 | #include "linechartitem_p.h" | |
20 | #include "linechartanimationitem_p.h" |
|
20 | #include "linechartanimationitem_p.h" | |
21 | #include "piepresenter_p.h" |
|
21 | #include "piepresenter_p.h" | |
22 | #include "scatterpresenter_p.h" |
|
22 | #include "scatterpresenter_p.h" | |
23 |
|
23 | |||
24 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
24 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
25 |
|
25 | |||
26 | ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), |
|
26 | ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), | |
27 | m_chart(chart), |
|
27 | m_chart(chart), | |
28 | m_dataset(dataset), |
|
28 | m_dataset(dataset), | |
29 | m_chartTheme(0), |
|
29 | m_chartTheme(0), | |
30 | m_marginSize(0), |
|
30 | m_marginSize(0), | |
31 | m_rect(QRectF(QPoint(0,0),m_chart->size())), |
|
31 | m_rect(QRectF(QPoint(0,0),m_chart->size())), | |
32 | m_options(0) |
|
32 | m_options(0) | |
33 | { |
|
33 | { | |
34 | createConnections(); |
|
34 | createConnections(); | |
35 | setChartTheme(QChart::ChartThemeDefault); |
|
35 | setChartTheme(QChart::ChartThemeDefault); | |
36 |
|
36 | |||
37 | } |
|
37 | } | |
38 |
|
38 | |||
39 | ChartPresenter::~ChartPresenter() |
|
39 | ChartPresenter::~ChartPresenter() | |
40 | { |
|
40 | { | |
41 | } |
|
41 | } | |
42 |
|
42 | |||
43 | void ChartPresenter::createConnections() |
|
43 | void ChartPresenter::createConnections() | |
44 | { |
|
44 | { | |
45 | QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged())); |
|
45 | QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged())); | |
46 | QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*)),this,SLOT(handleSeriesAdded(QSeries*))); |
|
46 | QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*)),this,SLOT(handleSeriesAdded(QSeries*))); | |
47 | QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*))); |
|
47 | QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*))); | |
48 | QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*)),this,SLOT(handleAxisAdded(QChartAxis*))); |
|
48 | QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*)),this,SLOT(handleAxisAdded(QChartAxis*))); | |
49 | QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*))); |
|
49 | QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*))); | |
50 | QObject::connect(m_dataset,SIGNAL(seriesDomainChanged(QSeries*,const Domain&)),this,SLOT(handleSeriesDomainChanged(QSeries*,const Domain&))); |
|
50 | QObject::connect(m_dataset,SIGNAL(seriesDomainChanged(QSeries*,const Domain&)),this,SLOT(handleSeriesDomainChanged(QSeries*,const Domain&))); | |
51 | QObject::connect(m_dataset,SIGNAL(axisLabelsChanged(QChartAxis*,const QStringList&)),this,SLOT(handleAxisLabelsChanged(QChartAxis*,const QStringList&))); |
|
51 | QObject::connect(m_dataset,SIGNAL(axisLabelsChanged(QChartAxis*,const QStringList&)),this,SLOT(handleAxisLabelsChanged(QChartAxis*,const QStringList&))); | |
52 | } |
|
52 | } | |
53 |
|
53 | |||
54 |
|
54 | |||
55 | QRectF ChartPresenter::geometry() const |
|
55 | QRectF ChartPresenter::geometry() const | |
56 | { |
|
56 | { | |
57 | return m_rect; |
|
57 | return m_rect; | |
58 | } |
|
58 | } | |
59 |
|
59 | |||
60 | void ChartPresenter::handleGeometryChanged() |
|
60 | void ChartPresenter::handleGeometryChanged() | |
61 | { |
|
61 | { | |
62 | m_rect = QRectF(QPoint(0,0),m_chart->size()); |
|
62 | m_rect = QRectF(QPoint(0,0),m_chart->size()); | |
63 | m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize); |
|
63 | m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize); | |
64 | Q_ASSERT(m_rect.isValid()); |
|
64 | Q_ASSERT(m_rect.isValid()); | |
65 | emit geometryChanged(m_rect); |
|
65 | emit geometryChanged(m_rect); | |
66 | } |
|
66 | } | |
67 |
|
67 | |||
68 | int ChartPresenter::margin() const |
|
68 | int ChartPresenter::margin() const | |
69 | { |
|
69 | { | |
70 | return m_marginSize; |
|
70 | return m_marginSize; | |
71 | } |
|
71 | } | |
72 |
|
72 | |||
73 | void ChartPresenter::setMargin(int margin) |
|
73 | void ChartPresenter::setMargin(int margin) | |
74 | { |
|
74 | { | |
75 | m_marginSize = margin; |
|
75 | m_marginSize = margin; | |
76 | } |
|
76 | } | |
77 |
|
77 | |||
78 | void ChartPresenter::handleAxisAdded(QChartAxis* axis) |
|
78 | void ChartPresenter::handleAxisAdded(QChartAxis* axis) | |
79 | { |
|
79 | { | |
80 |
|
80 | |||
81 | AxisItem* item ; |
|
81 | AxisItem* item ; | |
82 |
|
82 | |||
83 | if(!m_options.testFlag(QChart::GridAxisAnimations)) |
|
83 | if(!m_options.testFlag(QChart::GridAxisAnimations)) | |
84 | { |
|
84 | { | |
85 | item = new AxisItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart); |
|
85 | item = new AxisItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart); | |
86 | }else{ |
|
86 | }else{ | |
87 | item = new AxisAnimationItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart); |
|
87 | item = new AxisAnimationItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart); | |
88 | } |
|
88 | } | |
89 |
|
89 | |||
90 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
90 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | |
91 | QObject::connect(axis,SIGNAL(update(QChartAxis*)),item,SLOT(handleAxisUpdate(QChartAxis*))); |
|
91 | QObject::connect(axis,SIGNAL(update(QChartAxis*)),item,SLOT(handleAxisUpdate(QChartAxis*))); | |
92 |
|
92 | |||
93 | item->handleAxisUpdate(axis); |
|
93 | item->handleAxisUpdate(axis); | |
94 | item->handleGeometryChanged(m_rect); |
|
94 | item->handleGeometryChanged(m_rect); | |
95 | m_chartTheme->decorate(axis,item); |
|
95 | m_chartTheme->decorate(axis,item); | |
96 | m_axisItems.insert(axis,item); |
|
96 | m_axisItems.insert(axis,item); | |
97 | } |
|
97 | } | |
98 |
|
98 | |||
99 | void ChartPresenter::handleAxisRemoved(QChartAxis* axis) |
|
99 | void ChartPresenter::handleAxisRemoved(QChartAxis* axis) | |
100 | { |
|
100 | { | |
101 | AxisItem* item = m_axisItems.take(axis); |
|
101 | AxisItem* item = m_axisItems.take(axis); | |
102 | Q_ASSERT(item); |
|
102 | Q_ASSERT(item); | |
103 | delete item; |
|
103 | delete item; | |
104 | } |
|
104 | } | |
105 |
|
105 | |||
106 |
|
106 | |||
107 | void ChartPresenter::handleSeriesAdded(QSeries* series) |
|
107 | void ChartPresenter::handleSeriesAdded(QSeries* series) | |
108 | { |
|
108 | { | |
109 | switch(series->type()) |
|
109 | switch(series->type()) | |
110 | { |
|
110 | { | |
111 | case QSeries::SeriesTypeLine: { |
|
111 | case QSeries::SeriesTypeLine: { | |
112 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); |
|
112 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); | |
113 | LineChartItem* item; |
|
113 | LineChartItem* item; | |
114 | if(m_options.testFlag(QChart::SeriesAnimations)){ |
|
114 | if(m_options.testFlag(QChart::SeriesAnimations)){ | |
115 | item = new LineChartAnimationItem(this,lineSeries,m_chart); |
|
115 | item = new LineChartAnimationItem(this,lineSeries,m_chart); | |
116 | }else{ |
|
116 | }else{ | |
117 | item = new LineChartItem(this,lineSeries,m_chart); |
|
117 | item = new LineChartItem(this,lineSeries,m_chart); | |
118 | } |
|
118 | } | |
119 | m_chartTheme->decorate(item,lineSeries,m_chartItems.count()); |
|
119 | m_chartTheme->decorate(item,lineSeries,m_chartItems.count()); | |
120 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
120 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | |
121 | QObject::connect(lineSeries,SIGNAL(pointReplaced(int)),item,SLOT(handlePointReplaced(int))); |
|
121 | QObject::connect(lineSeries,SIGNAL(pointReplaced(int)),item,SLOT(handlePointReplaced(int))); | |
122 | QObject::connect(lineSeries,SIGNAL(pointAdded(int)),item,SLOT(handlePointAdded(int))); |
|
122 | QObject::connect(lineSeries,SIGNAL(pointAdded(int)),item,SLOT(handlePointAdded(int))); | |
123 | QObject::connect(lineSeries,SIGNAL(pointRemoved(int)),item,SLOT(handlePointRemoved(int))); |
|
123 | QObject::connect(lineSeries,SIGNAL(pointRemoved(int)),item,SLOT(handlePointRemoved(int))); | |
|
124 | QObject::connect(lineSeries,SIGNAL(updated()),item,SLOT(handleUpdated())); | |||
124 | m_chartItems.insert(series,item); |
|
125 | m_chartItems.insert(series,item); | |
125 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
126 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); | |
|
127 | item->handleUpdated(); | |||
126 | break; |
|
128 | break; | |
127 | } |
|
129 | } | |
128 |
|
130 | |||
129 | case QSeries::SeriesTypeBar: { |
|
131 | case QSeries::SeriesTypeBar: { | |
130 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); |
|
132 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); | |
131 | BarPresenter* item = new BarPresenter(barSeries,m_chart); |
|
133 | BarPresenter* item = new BarPresenter(barSeries,m_chart); | |
132 | m_chartTheme->decorate(item,barSeries,m_chartItems.count()); |
|
134 | m_chartTheme->decorate(item,barSeries,m_chartItems.count()); | |
133 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
135 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | |
134 | QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
136 | QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); | |
135 | m_chartItems.insert(series,item); |
|
137 | m_chartItems.insert(series,item); | |
136 | // m_axisXItem->setVisible(false); |
|
138 | // m_axisXItem->setVisible(false); | |
137 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
139 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); | |
138 | break; |
|
140 | break; | |
139 | } |
|
141 | } | |
140 |
|
142 | |||
141 | case QSeries::SeriesTypeStackedBar: { |
|
143 | case QSeries::SeriesTypeStackedBar: { | |
142 |
|
144 | |||
143 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); |
|
145 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); | |
144 | StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart); |
|
146 | StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart); | |
145 | m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count()); |
|
147 | m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count()); | |
146 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
148 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | |
147 | QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
149 | QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); | |
148 | m_chartItems.insert(series,item); |
|
150 | m_chartItems.insert(series,item); | |
149 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
151 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); | |
150 | break; |
|
152 | break; | |
151 | } |
|
153 | } | |
152 |
|
154 | |||
153 | case QSeries::SeriesTypePercentBar: { |
|
155 | case QSeries::SeriesTypePercentBar: { | |
154 |
|
156 | |||
155 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); |
|
157 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); | |
156 | PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart); |
|
158 | PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart); | |
157 | m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count()); |
|
159 | m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count()); | |
158 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
160 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | |
159 | QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
161 | QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); | |
160 | m_chartItems.insert(series,item); |
|
162 | m_chartItems.insert(series,item); | |
161 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); |
|
163 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); | |
162 | break; |
|
164 | break; | |
163 | } |
|
165 | } | |
164 | case QSeries::SeriesTypeScatter: { |
|
166 | case QSeries::SeriesTypeScatter: { | |
165 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); |
|
167 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); | |
166 | ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart); |
|
168 | ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart); | |
167 | QObject::connect(scatterPresenter, SIGNAL(clicked()), scatterSeries, SIGNAL(clicked())); |
|
169 | QObject::connect(scatterPresenter, SIGNAL(clicked()), scatterSeries, SIGNAL(clicked())); | |
168 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), |
|
170 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), | |
169 | scatterPresenter, SLOT(handleGeometryChanged(const QRectF&))); |
|
171 | scatterPresenter, SLOT(handleGeometryChanged(const QRectF&))); | |
170 | m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count()); |
|
172 | m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count()); | |
171 | m_chartItems.insert(scatterSeries, scatterPresenter); |
|
173 | m_chartItems.insert(scatterSeries, scatterPresenter); | |
172 | if(m_rect.isValid()) scatterPresenter->handleGeometryChanged(m_rect); |
|
174 | if(m_rect.isValid()) scatterPresenter->handleGeometryChanged(m_rect); | |
173 | break; |
|
175 | break; | |
174 | } |
|
176 | } | |
175 | case QSeries::SeriesTypePie: { |
|
177 | case QSeries::SeriesTypePie: { | |
176 | QPieSeries *s = qobject_cast<QPieSeries *>(series); |
|
178 | QPieSeries *s = qobject_cast<QPieSeries *>(series); | |
177 | PiePresenter* pie = new PiePresenter(m_chart, s); |
|
179 | PiePresenter* pie = new PiePresenter(m_chart, s); | |
178 | m_chartTheme->decorate(pie, s, m_chartItems.count()); |
|
180 | m_chartTheme->decorate(pie, s, m_chartItems.count()); | |
179 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&))); |
|
181 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&))); | |
180 |
|
182 | |||
181 | // Hide all from background when there is only piechart |
|
183 | // Hide all from background when there is only piechart | |
182 | // TODO: refactor this ugly code... should be one setting for this |
|
184 | // TODO: refactor this ugly code... should be one setting for this | |
183 | if (m_chartItems.count() == 0) { |
|
185 | if (m_chartItems.count() == 0) { | |
184 | m_chart->axisX()->setAxisVisible(false); |
|
186 | m_chart->axisX()->setAxisVisible(false); | |
185 | m_chart->axisY()->setAxisVisible(false); |
|
187 | m_chart->axisY()->setAxisVisible(false); | |
186 | m_chart->axisX()->setGridVisible(false); |
|
188 | m_chart->axisX()->setGridVisible(false); | |
187 | m_chart->axisY()->setGridVisible(false); |
|
189 | m_chart->axisY()->setGridVisible(false); | |
188 | m_chart->axisX()->setLabelsVisible(false); |
|
190 | m_chart->axisX()->setLabelsVisible(false); | |
189 | m_chart->axisY()->setLabelsVisible(false); |
|
191 | m_chart->axisY()->setLabelsVisible(false); | |
190 | m_chart->axisX()->setShadesVisible(false); |
|
192 | m_chart->axisX()->setShadesVisible(false); | |
191 | m_chart->axisY()->setShadesVisible(false); |
|
193 | m_chart->axisY()->setShadesVisible(false); | |
192 | m_chart->setChartBackgroundBrush(Qt::transparent); |
|
194 | m_chart->setChartBackgroundBrush(Qt::transparent); | |
193 | } |
|
195 | } | |
194 |
|
196 | |||
195 | m_chartItems.insert(series, pie); |
|
197 | m_chartItems.insert(series, pie); | |
196 | pie->handleGeometryChanged(m_rect); |
|
198 | pie->handleGeometryChanged(m_rect); | |
197 | break; |
|
199 | break; | |
198 | } |
|
200 | } | |
199 | default: { |
|
201 | default: { | |
200 | qDebug()<< "Series type" << series->type() << "not implemented."; |
|
202 | qDebug()<< "Series type" << series->type() << "not implemented."; | |
201 | break; |
|
203 | break; | |
202 | } |
|
204 | } | |
203 | } |
|
205 | } | |
204 | } |
|
206 | } | |
205 |
|
207 | |||
206 | void ChartPresenter::handleSeriesRemoved(QSeries* series) |
|
208 | void ChartPresenter::handleSeriesRemoved(QSeries* series) | |
207 | { |
|
209 | { | |
208 | ChartItem* item = m_chartItems.take(series); |
|
210 | ChartItem* item = m_chartItems.take(series); | |
209 | delete item; |
|
211 | delete item; | |
210 | } |
|
212 | } | |
211 |
|
213 | |||
212 | void ChartPresenter::handleSeriesDomainChanged(QSeries* series, const Domain& domain) |
|
214 | void ChartPresenter::handleSeriesDomainChanged(QSeries* series, const Domain& domain) | |
213 | { |
|
215 | { | |
214 | m_chartItems.value(series)->handleDomainChanged(domain); |
|
216 | m_chartItems.value(series)->handleDomainChanged(domain); | |
215 | } |
|
217 | } | |
216 |
|
218 | |||
217 | void ChartPresenter::handleAxisLabelsChanged(QChartAxis* axis,const QStringList& labels) |
|
219 | void ChartPresenter::handleAxisLabelsChanged(QChartAxis* axis,const QStringList& labels) | |
218 | { |
|
220 | { | |
219 | m_axisItems.value(axis)->handleLabelsChanged(axis,labels); |
|
221 | m_axisItems.value(axis)->handleLabelsChanged(axis,labels); | |
220 | } |
|
222 | } | |
221 |
|
223 | |||
222 | void ChartPresenter::setChartTheme(QChart::ChartTheme theme) |
|
224 | void ChartPresenter::setChartTheme(QChart::ChartTheme theme) | |
223 | { |
|
225 | { | |
224 | delete m_chartTheme; |
|
226 | delete m_chartTheme; | |
225 |
|
227 | |||
226 | m_chartTheme = ChartTheme::createTheme(theme); |
|
228 | m_chartTheme = ChartTheme::createTheme(theme); | |
227 |
|
229 | |||
228 | m_chartTheme->decorate(m_chart); |
|
230 | m_chartTheme->decorate(m_chart); | |
229 | QMapIterator<QSeries*,ChartItem*> i(m_chartItems); |
|
231 | QMapIterator<QSeries*,ChartItem*> i(m_chartItems); | |
230 |
|
232 | |||
231 | int index=0; |
|
233 | int index=0; | |
232 | while (i.hasNext()) { |
|
234 | while (i.hasNext()) { | |
233 | i.next(); |
|
235 | i.next(); | |
234 | m_chartTheme->decorate(i.value(),i.key(),index); |
|
236 | m_chartTheme->decorate(i.value(),i.key(),index); | |
235 | index++; |
|
237 | index++; | |
236 | } |
|
238 | } | |
237 |
|
239 | |||
238 | QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems); |
|
240 | QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems); | |
239 | while (j.hasNext()) { |
|
241 | while (j.hasNext()) { | |
240 | j.next(); |
|
242 | j.next(); | |
241 | m_chartTheme->decorate(j.key(),j.value()); |
|
243 | m_chartTheme->decorate(j.key(),j.value()); | |
242 | } |
|
244 | } | |
243 | } |
|
245 | } | |
244 |
|
246 | |||
245 | QChart::ChartTheme ChartPresenter::chartTheme() |
|
247 | QChart::ChartTheme ChartPresenter::chartTheme() | |
246 | { |
|
248 | { | |
247 | return m_chartTheme->id(); |
|
249 | return m_chartTheme->id(); | |
248 | } |
|
250 | } | |
249 |
|
251 | |||
250 | void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) |
|
252 | void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) | |
251 | { |
|
253 | { | |
252 | if(m_options!=options) { |
|
254 | if(m_options!=options) { | |
253 |
|
255 | |||
254 | m_options=options; |
|
256 | m_options=options; | |
255 |
|
257 | |||
256 | //recreate elements |
|
258 | //recreate elements | |
257 |
|
259 | |||
258 | QList<QChartAxis*> axisList = m_axisItems.uniqueKeys(); |
|
260 | QList<QChartAxis*> axisList = m_axisItems.uniqueKeys(); | |
259 | QList<QSeries*> seriesList = m_chartItems.uniqueKeys(); |
|
261 | QList<QSeries*> seriesList = m_chartItems.uniqueKeys(); | |
260 |
|
262 | |||
261 | foreach(QChartAxis* axis, axisList) { |
|
263 | foreach(QChartAxis* axis, axisList) { | |
262 | handleAxisRemoved(axis); |
|
264 | handleAxisRemoved(axis); | |
263 | handleAxisAdded(axis); |
|
265 | handleAxisAdded(axis); | |
264 | } |
|
266 | } | |
265 | foreach(QSeries* series, seriesList) { |
|
267 | foreach(QSeries* series, seriesList) { | |
266 | handleSeriesRemoved(series); |
|
268 | handleSeriesRemoved(series); | |
267 | handleSeriesAdded(series); |
|
269 | handleSeriesAdded(series); | |
268 | } |
|
270 | } | |
269 |
|
271 | |||
270 | //now reintialize view data |
|
272 | //now reintialize view data | |
271 | //TODO: make it more nice |
|
273 | //TODO: make it more nice | |
272 | m_dataset->setDomain(m_dataset->domainIndex()); |
|
274 | m_dataset->setDomain(m_dataset->domainIndex()); | |
273 | } |
|
275 | } | |
274 | } |
|
276 | } | |
275 |
|
277 | |||
276 | QChart::AnimationOptions ChartPresenter::animationOptions() const |
|
278 | QChart::AnimationOptions ChartPresenter::animationOptions() const | |
277 | { |
|
279 | { | |
278 | return m_options; |
|
280 | return m_options; | |
279 | } |
|
281 | } | |
280 |
|
282 | |||
281 |
|
283 | |||
282 | #include "moc_chartpresenter_p.cpp" |
|
284 | #include "moc_chartpresenter_p.cpp" | |
283 |
|
285 | |||
284 | QTCOMMERCIALCHART_END_NAMESPACE |
|
286 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,221 +1,219 | |||||
1 | #include "linechartitem_p.h" |
|
1 | #include "linechartitem_p.h" | |
2 | #include "qlineseries.h" |
|
2 | #include "qlineseries.h" | |
3 | #include "chartpresenter_p.h" |
|
3 | #include "chartpresenter_p.h" | |
4 | #include <QPainter> |
|
4 | #include <QPainter> | |
5 |
|
5 | |||
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | //TODO: optimize : remove points which are not visible |
|
9 | //TODO: optimize : remove points which are not visible | |
10 |
|
10 | |||
11 | LineChartItem::LineChartItem(ChartPresenter* presenter, QLineSeries* series,QGraphicsItem *parent):ChartItem(parent), |
|
11 | LineChartItem::LineChartItem(ChartPresenter* presenter, QLineSeries* series,QGraphicsItem *parent):ChartItem(parent), | |
12 | m_presenter(presenter), |
|
12 | m_presenter(presenter), | |
13 | m_series(series), |
|
13 | m_series(series), | |
14 | m_items(this) |
|
14 | m_items(this) | |
15 | { |
|
15 | { | |
16 | //m_items.setZValue(ChartPresenter::LineChartZValue); |
|
16 | //m_items.setZValue(ChartPresenter::LineChartZValue); | |
17 | setZValue(ChartPresenter::LineChartZValue); |
|
17 | setZValue(ChartPresenter::LineChartZValue); | |
18 | } |
|
18 | } | |
19 |
|
19 | |||
20 | QRectF LineChartItem::boundingRect() const |
|
20 | QRectF LineChartItem::boundingRect() const | |
21 | { |
|
21 | { | |
22 | return m_rect; |
|
22 | return m_rect; | |
23 | } |
|
23 | } | |
24 |
|
24 | |||
25 | QPainterPath LineChartItem::shape() const |
|
25 | QPainterPath LineChartItem::shape() const | |
26 | { |
|
26 | { | |
27 | return m_path; |
|
27 | return m_path; | |
28 | } |
|
28 | } | |
29 |
|
29 | |||
30 | void LineChartItem::createPoints(int count) |
|
30 | void LineChartItem::createPoints(int count) | |
31 | { |
|
31 | { | |
32 | for (int i = 0; i < count; ++i) { |
|
32 | for (int i = 0; i < count; ++i) { | |
33 | QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3); |
|
33 | QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3); | |
34 | m_items.addToGroup(item); |
|
34 | m_items.addToGroup(item); | |
35 | } |
|
35 | } | |
36 | } |
|
36 | } | |
37 |
|
37 | |||
38 | void LineChartItem::clearPoints(int count) |
|
38 | void LineChartItem::clearPoints(int count) | |
39 | { |
|
39 | { | |
40 | QList<QGraphicsItem *> items = m_items.childItems(); |
|
40 | QList<QGraphicsItem *> items = m_items.childItems(); | |
41 |
|
41 | |||
42 | for (int i = 0; i < count; ++i) { |
|
42 | for (int i = 0; i < count; ++i) { | |
43 | delete(items.takeLast()); |
|
43 | delete(items.takeLast()); | |
44 | } |
|
44 | } | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 | QPointF LineChartItem::calculateGeometryPoint(int index) const |
|
47 | QPointF LineChartItem::calculateGeometryPoint(int index) const | |
48 | { |
|
48 | { | |
49 | const qreal deltaX = m_size.width()/m_domain.spanX(); |
|
49 | const qreal deltaX = m_size.width()/m_domain.spanX(); | |
50 | const qreal deltaY = m_size.height()/m_domain.spanY(); |
|
50 | const qreal deltaY = m_size.height()/m_domain.spanY(); | |
51 | qreal x = (m_series->x(index) - m_domain.m_minX)* deltaX; |
|
51 | qreal x = (m_series->x(index) - m_domain.m_minX)* deltaX; | |
52 | qreal y = (m_series->y(index) - m_domain.m_minY)*-deltaY + m_size.height(); |
|
52 | qreal y = (m_series->y(index) - m_domain.m_minY)*-deltaY + m_size.height(); | |
53 | return QPointF(x,y); |
|
53 | return QPointF(x,y); | |
54 | } |
|
54 | } | |
55 |
|
55 | |||
56 | QVector<QPointF> LineChartItem::calculateGeometryPoints() const |
|
56 | QVector<QPointF> LineChartItem::calculateGeometryPoints() const | |
57 | { |
|
57 | { | |
58 | const qreal deltaX = m_size.width()/m_domain.spanX(); |
|
58 | const qreal deltaX = m_size.width()/m_domain.spanX(); | |
59 | const qreal deltaY = m_size.height()/m_domain.spanY(); |
|
59 | const qreal deltaY = m_size.height()/m_domain.spanY(); | |
60 |
|
60 | |||
61 | QVector<QPointF> points; |
|
61 | QVector<QPointF> points; | |
62 | points.reserve(m_series->count()); |
|
62 | points.reserve(m_series->count()); | |
63 | for (int i = 0; i < m_series->count(); ++i) { |
|
63 | for (int i = 0; i < m_series->count(); ++i) { | |
64 | qreal x = (m_series->x(i) - m_domain.m_minX)* deltaX; |
|
64 | qreal x = (m_series->x(i) - m_domain.m_minX)* deltaX; | |
65 | qreal y = (m_series->y(i) - m_domain.m_minY)*-deltaY + m_size.height(); |
|
65 | qreal y = (m_series->y(i) - m_domain.m_minY)*-deltaY + m_size.height(); | |
66 | points << QPointF(x,y); |
|
66 | points << QPointF(x,y); | |
67 | } |
|
67 | } | |
68 | return points; |
|
68 | return points; | |
69 | } |
|
69 | } | |
70 |
|
70 | |||
71 | void LineChartItem::updateItem(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints) |
|
71 | void LineChartItem::updateItem(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints) | |
72 | { |
|
72 | { | |
73 | applyGeometry(newPoints); |
|
73 | applyGeometry(newPoints); | |
74 | oldPoints = newPoints; |
|
74 | oldPoints = newPoints; | |
75 | } |
|
75 | } | |
76 |
|
76 | |||
77 | void LineChartItem::updateItem(QVector<QPointF>& oldPoints,int index,QPointF& newPoint) |
|
77 | void LineChartItem::updateItem(QVector<QPointF>& oldPoints,int index,QPointF& newPoint) | |
78 | { |
|
78 | { | |
79 | oldPoints.replace(index,newPoint); |
|
79 | oldPoints.replace(index,newPoint); | |
80 | applyGeometry(oldPoints); |
|
80 | applyGeometry(oldPoints); | |
81 | } |
|
81 | } | |
82 |
|
82 | |||
83 | void LineChartItem::applyGeometry(QVector<QPointF>& points) |
|
83 | void LineChartItem::applyGeometry(QVector<QPointF>& points) | |
84 | { |
|
84 | { | |
85 | if(points.size()==0) return; |
|
85 | if(points.size()==0) return; | |
86 |
|
86 | |||
87 | QList<QGraphicsItem*> items = m_items.childItems(); |
|
87 | QList<QGraphicsItem*> items = m_items.childItems(); | |
88 |
|
88 | |||
89 | QPainterPath path; |
|
89 | QPainterPath path; | |
90 | const QPointF& point = points.at(0); |
|
90 | const QPointF& point = points.at(0); | |
91 | path.moveTo(point); |
|
91 | path.moveTo(point); | |
92 | QGraphicsItem* item = items.at(0); |
|
92 | QGraphicsItem* item = items.at(0); | |
93 | item->setPos(point.x()-1,point.y()-1); |
|
93 | item->setPos(point.x()-1,point.y()-1); | |
94 | if(!m_clipRect.contains(point)) item->setVisible(false); |
|
94 | if(!m_clipRect.contains(point)) item->setVisible(false); | |
95 |
|
95 | |||
96 | for(int i=1 ; i< points.size();i++) { |
|
96 | for(int i=1 ; i< points.size();i++) { | |
97 | QGraphicsItem* item = items.at(i); |
|
97 | QGraphicsItem* item = items.at(i); | |
98 | const QPointF& point = points.at(i); |
|
98 | const QPointF& point = points.at(i); | |
99 | item->setPos(point.x()-1,point.y()-1); |
|
99 | item->setPos(point.x()-1,point.y()-1); | |
100 | if(!m_clipRect.contains(point)) item->setVisible(false); |
|
100 | if(!m_clipRect.contains(point)) item->setVisible(false); | |
101 | path.lineTo(point); |
|
101 | path.lineTo(point); | |
102 | } |
|
102 | } | |
103 |
|
103 | |||
104 | prepareGeometryChange(); |
|
104 | prepareGeometryChange(); | |
105 | m_path = path; |
|
105 | m_path = path; | |
106 | m_rect = path.boundingRect(); |
|
106 | m_rect = path.boundingRect(); | |
107 | } |
|
107 | } | |
108 |
|
108 | |||
109 | void LineChartItem::setPen(const QPen& pen) |
|
109 | void LineChartItem::setPen(const QPen& pen) | |
110 | { |
|
110 | { | |
111 | m_pen = pen; |
|
111 | m_pen = pen; | |
112 | } |
|
112 | } | |
113 |
|
113 | |||
114 | //handlers |
|
114 | //handlers | |
115 |
|
115 | |||
116 | void LineChartItem::handlePointAdded(int index) |
|
116 | void LineChartItem::handlePointAdded(int index) | |
117 | { |
|
117 | { | |
118 | Q_ASSERT(index<m_series->count()); |
|
118 | Q_ASSERT(index<m_series->count()); | |
119 | Q_ASSERT(index>=0); |
|
119 | Q_ASSERT(index>=0); | |
120 |
|
120 | |||
121 | QPointF point = calculateGeometryPoint(index); |
|
121 | QPointF point = calculateGeometryPoint(index); | |
122 | createPoints(1); |
|
122 | createPoints(1); | |
123 | QVector<QPointF> points = m_points; |
|
123 | QVector<QPointF> points = m_points; | |
124 | points.insert(index,point); |
|
124 | points.insert(index,point); | |
125 | updateItem(m_points,points); |
|
125 | updateItem(m_points,points); | |
126 | update(); |
|
126 | update(); | |
127 | } |
|
127 | } | |
128 | void LineChartItem::handlePointRemoved(int index) |
|
128 | void LineChartItem::handlePointRemoved(int index) | |
129 | { |
|
129 | { | |
130 | Q_ASSERT(index<m_series->count()); |
|
130 | Q_ASSERT(index<m_series->count()); | |
131 | Q_ASSERT(index>=0); |
|
131 | Q_ASSERT(index>=0); | |
132 | QPointF point = calculateGeometryPoint(index); |
|
132 | QPointF point = calculateGeometryPoint(index); | |
133 | clearPoints(1); |
|
133 | clearPoints(1); | |
134 | QVector<QPointF> points = m_points; |
|
134 | QVector<QPointF> points = m_points; | |
135 | points.remove(index); |
|
135 | points.remove(index); | |
136 | updateItem(m_points,points); |
|
136 | updateItem(m_points,points); | |
137 | update(); |
|
137 | update(); | |
138 | } |
|
138 | } | |
139 |
|
139 | |||
140 | void LineChartItem::handlePointReplaced(int index) |
|
140 | void LineChartItem::handlePointReplaced(int index) | |
141 | { |
|
141 | { | |
142 | Q_ASSERT(index<m_series->count()); |
|
142 | Q_ASSERT(index<m_series->count()); | |
143 | Q_ASSERT(index>=0); |
|
143 | Q_ASSERT(index>=0); | |
144 | QPointF point = calculateGeometryPoint(index); |
|
144 | QPointF point = calculateGeometryPoint(index); | |
145 | updateItem(m_points,index,point); |
|
145 | updateItem(m_points,index,point); | |
146 | update(); |
|
146 | update(); | |
147 | } |
|
147 | } | |
148 |
|
148 | |||
149 | void LineChartItem::handleDomainChanged(const Domain& domain) |
|
149 | void LineChartItem::handleDomainChanged(const Domain& domain) | |
150 | { |
|
150 | { | |
151 |
|
151 | |||
152 | m_domain = domain; |
|
152 | m_domain = domain; | |
153 |
|
153 | |||
154 | if(m_domain.isEmpty()) return; |
|
154 | if(m_domain.isEmpty()) return; | |
155 | if(!m_clipRect.isValid()) return; |
|
155 | if(!m_clipRect.isValid()) return; | |
156 |
|
156 | |||
157 | QVector<QPointF> points = calculateGeometryPoints(); |
|
157 | QVector<QPointF> points = calculateGeometryPoints(); | |
158 |
|
158 | |||
159 | int diff = m_points.size() - points.size(); |
|
159 | int diff = m_points.size() - points.size(); | |
160 |
|
160 | |||
161 | if(diff>0) { |
|
161 | if(diff>0) { | |
162 | clearPoints(diff); |
|
162 | clearPoints(diff); | |
163 | } |
|
163 | } | |
164 | else if(diff<0) { |
|
164 | else if(diff<0) { | |
165 | createPoints(-diff); |
|
165 | createPoints(-diff); | |
166 | } |
|
166 | } | |
167 |
|
167 | |||
168 | updateItem(m_points,points); |
|
168 | updateItem(m_points,points); | |
169 | update(); |
|
169 | update(); | |
170 | } |
|
170 | } | |
171 |
|
171 | |||
172 | void LineChartItem::handleGeometryChanged(const QRectF& rect) |
|
172 | void LineChartItem::handleGeometryChanged(const QRectF& rect) | |
173 | { |
|
173 | { | |
174 | Q_ASSERT(rect.isValid()); |
|
174 | Q_ASSERT(rect.isValid()); | |
175 | m_size=rect.size(); |
|
175 | m_size=rect.size(); | |
176 | m_clipRect=rect.translated(-rect.topLeft()); |
|
176 | m_clipRect=rect.translated(-rect.topLeft()); | |
177 | setPos(rect.topLeft()); |
|
177 | setPos(rect.topLeft()); | |
178 |
|
178 | |||
179 | if(m_domain.isEmpty()) return; |
|
179 | if(m_domain.isEmpty()) return; | |
180 |
|
180 | |||
181 | QVector<QPointF> points = calculateGeometryPoints(); |
|
181 | QVector<QPointF> points = calculateGeometryPoints(); | |
182 |
|
182 | |||
183 | int diff = m_points.size() - points.size(); |
|
183 | int diff = m_points.size() - points.size(); | |
184 |
|
184 | |||
185 | if(diff>0) { |
|
185 | if(diff>0) { | |
186 | clearPoints(diff); |
|
186 | clearPoints(diff); | |
187 | } |
|
187 | } | |
188 | else if(diff<0) { |
|
188 | else if(diff<0) { | |
189 | createPoints(-diff); |
|
189 | createPoints(-diff); | |
190 | } |
|
190 | } | |
191 |
|
191 | |||
192 | updateItem(m_points,points); |
|
192 | updateItem(m_points,points); | |
193 | update(); |
|
193 | update(); | |
194 | } |
|
194 | } | |
195 |
|
195 | |||
196 |
void LineChartItem::handle |
|
196 | void LineChartItem::handleUpdated() | |
197 | { |
|
197 | { | |
198 | if(m_points.count()==0) return; |
|
|||
199 |
|
||||
200 | m_items.setVisible(m_series->pointsVisible()); |
|
198 | m_items.setVisible(m_series->pointsVisible()); | |
201 | setPen(m_series->pen()); |
|
199 | setPen(m_series->pen()); | |
202 | update(); |
|
200 | update(); | |
203 | } |
|
201 | } | |
204 |
|
202 | |||
205 | //painter |
|
203 | //painter | |
206 |
|
204 | |||
207 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
205 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
208 | { |
|
206 | { | |
209 | Q_UNUSED(widget); |
|
207 | Q_UNUSED(widget); | |
210 | Q_UNUSED(option); |
|
208 | Q_UNUSED(option); | |
211 | painter->save(); |
|
209 | painter->save(); | |
212 | painter->setPen(m_pen); |
|
210 | painter->setPen(m_pen); | |
213 | painter->setClipRect(m_clipRect); |
|
211 | painter->setClipRect(m_clipRect); | |
214 | painter->drawPath(m_path); |
|
212 | painter->drawPath(m_path); | |
215 | painter->restore(); |
|
213 | painter->restore(); | |
216 | } |
|
214 | } | |
217 |
|
215 | |||
218 |
|
216 | |||
219 | #include "moc_linechartitem_p.cpp" |
|
217 | #include "moc_linechartitem_p.cpp" | |
220 |
|
218 | |||
221 | QTCOMMERCIALCHART_END_NAMESPACE |
|
219 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,61 +1,61 | |||||
1 | #ifndef LINECHARTITEM_H |
|
1 | #ifndef LINECHARTITEM_H | |
2 | #define LINECHARTITEM_H |
|
2 | #define LINECHARTITEM_H | |
3 |
|
3 | |||
4 | #include "qchartglobal.h" |
|
4 | #include "qchartglobal.h" | |
5 | #include "chartitem_p.h" |
|
5 | #include "chartitem_p.h" | |
6 | #include <QPen> |
|
6 | #include <QPen> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | class ChartPresenter; |
|
10 | class ChartPresenter; | |
11 | class QLineSeries; |
|
11 | class QLineSeries; | |
12 |
|
12 | |||
13 | class LineChartItem : public QObject , public ChartItem |
|
13 | class LineChartItem : public QObject , public ChartItem | |
14 | { |
|
14 | { | |
15 | Q_OBJECT |
|
15 | Q_OBJECT | |
16 | public: |
|
16 | public: | |
17 | LineChartItem(ChartPresenter* presenter, QLineSeries* series,QGraphicsItem *parent = 0); |
|
17 | LineChartItem(ChartPresenter* presenter, QLineSeries* series,QGraphicsItem *parent = 0); | |
18 | ~ LineChartItem(){}; |
|
18 | ~ LineChartItem(){}; | |
19 |
|
19 | |||
20 | //from QGraphicsItem |
|
20 | //from QGraphicsItem | |
21 | QRectF boundingRect() const; |
|
21 | QRectF boundingRect() const; | |
22 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
22 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
23 | QPainterPath shape() const; |
|
23 | QPainterPath shape() const; | |
24 |
|
24 | |||
25 | void setPen(const QPen& pen); |
|
25 | void setPen(const QPen& pen); | |
26 | void setPointsVisible(bool visible); |
|
26 | void setPointsVisible(bool visible); | |
27 |
|
27 | |||
28 | public slots: |
|
28 | public slots: | |
29 | void handlePointAdded(int index); |
|
29 | void handlePointAdded(int index); | |
30 | void handlePointRemoved(int index); |
|
30 | void handlePointRemoved(int index); | |
31 | void handlePointReplaced(int index); |
|
31 | void handlePointReplaced(int index); | |
32 |
void handle |
|
32 | void handleUpdated(); | |
33 | void handleDomainChanged(const Domain& domain); |
|
33 | void handleDomainChanged(const Domain& domain); | |
34 | void handleGeometryChanged(const QRectF& size); |
|
34 | void handleGeometryChanged(const QRectF& size); | |
35 |
|
35 | |||
36 | public: |
|
36 | public: | |
37 | virtual void updateItem(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints); |
|
37 | virtual void updateItem(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints); | |
38 | virtual void updateItem(QVector<QPointF>& oldPoints,int index,QPointF& newPoint); |
|
38 | virtual void updateItem(QVector<QPointF>& oldPoints,int index,QPointF& newPoint); | |
39 | void applyGeometry(QVector<QPointF>& points); |
|
39 | void applyGeometry(QVector<QPointF>& points); | |
40 | void createPoints(int count); |
|
40 | void createPoints(int count); | |
41 | void clearPoints(int count); |
|
41 | void clearPoints(int count); | |
42 | QPointF calculateGeometryPoint(int index) const; |
|
42 | QPointF calculateGeometryPoint(int index) const; | |
43 | QVector<QPointF> calculateGeometryPoints() const; |
|
43 | QVector<QPointF> calculateGeometryPoints() const; | |
44 |
|
44 | |||
45 | private: |
|
45 | private: | |
46 | ChartPresenter* m_presenter; |
|
46 | ChartPresenter* m_presenter; | |
47 | QPainterPath m_path; |
|
47 | QPainterPath m_path; | |
|
48 | QLineSeries* m_series; | |||
48 | QSizeF m_size; |
|
49 | QSizeF m_size; | |
49 | QRectF m_rect; |
|
50 | QRectF m_rect; | |
50 | QRectF m_clipRect; |
|
51 | QRectF m_clipRect; | |
51 | Domain m_domain; |
|
52 | Domain m_domain; | |
52 | QGraphicsItemGroup m_items; |
|
53 | QGraphicsItemGroup m_items; | |
53 | QVector<QPointF> m_points; |
|
54 | QVector<QPointF> m_points; | |
54 | QLineSeries* m_series; |
|
|||
55 | QPen m_pen; |
|
55 | QPen m_pen; | |
56 |
|
56 | |||
57 | }; |
|
57 | }; | |
58 |
|
58 | |||
59 | QTCOMMERCIALCHART_END_NAMESPACE |
|
59 | QTCOMMERCIALCHART_END_NAMESPACE | |
60 |
|
60 | |||
61 | #endif |
|
61 | #endif |
@@ -1,213 +1,224 | |||||
1 | #include "qlineseries.h" |
|
1 | #include "qlineseries.h" | |
2 |
|
2 | |||
3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
4 |
|
4 | |||
5 | /*! |
|
5 | /*! | |
6 | \class QLineSeries |
|
6 | \class QLineSeries | |
7 | \brief The QLineSeries class is used for making line charts. |
|
7 | \brief The QLineSeries class is used for making line charts. | |
8 |
|
8 | |||
9 | \mainclass |
|
9 | \mainclass | |
10 |
|
10 | |||
11 | A line chart is used to show information as a series of data points |
|
11 | A line chart is used to show information as a series of data points | |
12 | connected by straight lines. |
|
12 | connected by straight lines. | |
13 |
|
13 | |||
14 | \image linechart.png |
|
14 | \image linechart.png | |
15 |
|
15 | |||
16 | Creating basic line chart is simple: |
|
16 | Creating basic line chart is simple: | |
17 | \code |
|
17 | \code | |
18 | QLineSeries* series = new QLineSeries(); |
|
18 | QLineSeries* series = new QLineSeries(); | |
19 | series->add(0, 6); |
|
19 | series->add(0, 6); | |
20 | series->add(2, 4); |
|
20 | series->add(2, 4); | |
21 | ... |
|
21 | ... | |
22 | chartView->addSeries(series); |
|
22 | chartView->addSeries(series); | |
23 | \endcode |
|
23 | \endcode | |
24 | */ |
|
24 | */ | |
25 |
|
25 | |||
26 | /*! |
|
26 | /*! | |
27 | \fn virtual QSeriesType QLineSeries::type() const |
|
27 | \fn virtual QSeriesType QLineSeries::type() const | |
28 | \brief Returns type of series. |
|
28 | \brief Returns type of series. | |
29 | \sa QSeries, QSeriesType |
|
29 | \sa QSeries, QSeriesType | |
30 | */ |
|
30 | */ | |
31 |
|
31 | |||
32 | /*! |
|
32 | /*! | |
33 | \fn QPen QLineSeries::pen() const |
|
33 | \fn QPen QLineSeries::pen() const | |
34 | \brief Returns the pen used to draw line for this series. |
|
34 | \brief Returns the pen used to draw line for this series. | |
35 | \sa setPen() |
|
35 | \sa setPen() | |
36 | */ |
|
36 | */ | |
37 |
|
37 | |||
38 | /*! |
|
38 | /*! | |
39 | \fn bool QLineSeries::pointsVisible() const |
|
39 | \fn bool QLineSeries::pointsVisible() const | |
40 | \brief Returns if the points are drawn for this series. |
|
40 | \brief Returns if the points are drawn for this series. | |
41 | \sa setPointsVisible() |
|
41 | \sa setPointsVisible() | |
42 | */ |
|
42 | */ | |
43 |
|
43 | |||
44 |
|
44 | |||
45 | /*! |
|
45 | /*! | |
46 | \fn void QLineSeries::pointReplaced(int index) |
|
46 | \fn void QLineSeries::pointReplaced(int index) | |
47 | \brief \internal \a index |
|
47 | \brief \internal \a index | |
48 | */ |
|
48 | */ | |
49 |
|
49 | |||
50 | /*! |
|
50 | /*! | |
51 | \fn void QLineSeries::pointAdded(int index) |
|
51 | \fn void QLineSeries::pointAdded(int index) | |
52 | \brief \internal \a index |
|
52 | \brief \internal \a index | |
53 | */ |
|
53 | */ | |
54 |
|
54 | |||
55 | /*! |
|
55 | /*! | |
56 | \fn void QLineSeries::pointRemoved(int index) |
|
56 | \fn void QLineSeries::pointRemoved(int index) | |
57 | \brief \internal \a index |
|
57 | \brief \internal \a index | |
58 | */ |
|
58 | */ | |
59 |
|
59 | |||
60 | /*! |
|
60 | /*! | |
|
61 | \fn void QLineSeries::updated(int index) | |||
|
62 | \brief \internal | |||
|
63 | */ | |||
|
64 | ||||
|
65 | /*! | |||
61 | Constructs empty series object which is a child of \a parent. |
|
66 | Constructs empty series object which is a child of \a parent. | |
62 | When series object is added to QChartView or QChart instance ownerships is transfered. |
|
67 | When series object is added to QChartView or QChart instance ownerships is transfered. | |
63 | */ |
|
68 | */ | |
64 | QLineSeries::QLineSeries(QObject* parent):QSeries(parent), |
|
69 | QLineSeries::QLineSeries(QObject* parent):QSeries(parent), | |
65 | m_pointsVisible(false) |
|
70 | m_pointsVisible(false) | |
66 | { |
|
71 | { | |
67 | } |
|
72 | } | |
68 | /*! |
|
73 | /*! | |
69 | Destroys the object. Series added to QChartView or QChart instances are owned by those, |
|
74 | Destroys the object. Series added to QChartView or QChart instances are owned by those, | |
70 | and are deleted when mentioned object are destroyed. |
|
75 | and are deleted when mentioned object are destroyed. | |
71 | */ |
|
76 | */ | |
72 | QLineSeries::~QLineSeries() |
|
77 | QLineSeries::~QLineSeries() | |
73 | { |
|
78 | { | |
74 | } |
|
79 | } | |
75 |
|
80 | |||
76 | /*! |
|
81 | /*! | |
77 | Adds data point \a x \a y to the series. Points are connected with lines on the chart. |
|
82 | Adds data point \a x \a y to the series. Points are connected with lines on the chart. | |
78 | */ |
|
83 | */ | |
79 | void QLineSeries::add(qreal x,qreal y) |
|
84 | void QLineSeries::add(qreal x,qreal y) | |
80 | { |
|
85 | { | |
81 | Q_ASSERT(m_x.size() == m_y.size()); |
|
86 | Q_ASSERT(m_x.size() == m_y.size()); | |
82 | m_x<<x; |
|
87 | m_x<<x; | |
83 | m_y<<y; |
|
88 | m_y<<y; | |
84 | emit pointAdded(m_x.size()-1); |
|
89 | emit pointAdded(m_x.size()-1); | |
85 | } |
|
90 | } | |
86 |
|
91 | |||
87 | /*! |
|
92 | /*! | |
88 | This is an overloaded function. |
|
93 | This is an overloaded function. | |
89 | Adds data \a point to the series. Points are connected with lines on the chart. |
|
94 | Adds data \a point to the series. Points are connected with lines on the chart. | |
90 | */ |
|
95 | */ | |
91 | void QLineSeries::add(const QPointF& point) |
|
96 | void QLineSeries::add(const QPointF& point) | |
92 | { |
|
97 | { | |
93 | add(point.x(),point.y()); |
|
98 | add(point.x(),point.y()); | |
94 | } |
|
99 | } | |
95 |
|
100 | |||
96 | /*! |
|
101 | /*! | |
97 | Modifies \a y value for given \a x a value. |
|
102 | Modifies \a y value for given \a x a value. | |
98 | */ |
|
103 | */ | |
99 | void QLineSeries::replace(qreal x,qreal y) |
|
104 | void QLineSeries::replace(qreal x,qreal y) | |
100 | { |
|
105 | { | |
101 | int index = m_x.indexOf(x); |
|
106 | int index = m_x.indexOf(x); | |
102 | m_x[index]=x; |
|
107 | m_x[index]=x; | |
103 | m_y[index]=y; |
|
108 | m_y[index]=y; | |
104 | emit pointReplaced(index); |
|
109 | emit pointReplaced(index); | |
105 | } |
|
110 | } | |
106 |
|
111 | |||
107 | /*! |
|
112 | /*! | |
108 | This is an overloaded function. |
|
113 | This is an overloaded function. | |
109 | Replaces current y value of for given \a point x value with \a point y value. |
|
114 | Replaces current y value of for given \a point x value with \a point y value. | |
110 | */ |
|
115 | */ | |
111 | void QLineSeries::replace(const QPointF& point) |
|
116 | void QLineSeries::replace(const QPointF& point) | |
112 | { |
|
117 | { | |
113 | replace(point.x(),point.y()); |
|
118 | replace(point.x(),point.y()); | |
114 | } |
|
119 | } | |
115 |
|
120 | |||
116 | /*! |
|
121 | /*! | |
117 | Removes current \a x and y value. |
|
122 | Removes current \a x and y value. | |
118 | */ |
|
123 | */ | |
119 | void QLineSeries::remove(qreal x) |
|
124 | void QLineSeries::remove(qreal x) | |
120 | { |
|
125 | { | |
121 | int index = m_x.indexOf(x); |
|
126 | int index = m_x.indexOf(x); | |
122 | m_x.remove(index); |
|
127 | m_x.remove(index); | |
123 | m_y.remove(index); |
|
128 | m_y.remove(index); | |
124 | emit pointRemoved(index); |
|
129 | emit pointRemoved(index); | |
125 | } |
|
130 | } | |
126 |
|
131 | |||
127 | /*! |
|
132 | /*! | |
128 | Removes current \a point x value. Note \a point y value is ignored. |
|
133 | Removes current \a point x value. Note \a point y value is ignored. | |
129 | */ |
|
134 | */ | |
130 | void QLineSeries::remove(const QPointF& point) |
|
135 | void QLineSeries::remove(const QPointF& point) | |
131 | { |
|
136 | { | |
132 | remove(point.x()); |
|
137 | remove(point.x()); | |
133 | } |
|
138 | } | |
134 |
|
139 | |||
135 | /*! |
|
140 | /*! | |
136 | Clears all the data. |
|
141 | Clears all the data. | |
137 | */ |
|
142 | */ | |
138 | void QLineSeries::clear() |
|
143 | void QLineSeries::clear() | |
139 | { |
|
144 | { | |
140 | m_x.clear(); |
|
145 | m_x.clear(); | |
141 | m_y.clear(); |
|
146 | m_y.clear(); | |
142 | } |
|
147 | } | |
143 |
|
148 | |||
144 | /*! |
|
149 | /*! | |
145 | \internal \a pos |
|
150 | \internal \a pos | |
146 | */ |
|
151 | */ | |
147 | qreal QLineSeries::x(int pos) const |
|
152 | qreal QLineSeries::x(int pos) const | |
148 | { |
|
153 | { | |
149 | return m_x.at(pos); |
|
154 | return m_x.at(pos); | |
150 | } |
|
155 | } | |
151 |
|
156 | |||
152 | /*! |
|
157 | /*! | |
153 | \internal \a pos |
|
158 | \internal \a pos | |
154 | */ |
|
159 | */ | |
155 | qreal QLineSeries::y(int pos) const |
|
160 | qreal QLineSeries::y(int pos) const | |
156 | { |
|
161 | { | |
157 | return m_y.at(pos); |
|
162 | return m_y.at(pos); | |
158 | } |
|
163 | } | |
159 |
|
164 | |||
160 | /*! |
|
165 | /*! | |
161 | Returns number of data points within series. |
|
166 | Returns number of data points within series. | |
162 | */ |
|
167 | */ | |
163 | int QLineSeries::count() const |
|
168 | int QLineSeries::count() const | |
164 | { |
|
169 | { | |
165 | Q_ASSERT(m_x.size() == m_y.size()); |
|
170 | Q_ASSERT(m_x.size() == m_y.size()); | |
166 |
|
171 | |||
167 | return m_x.size(); |
|
172 | return m_x.size(); | |
168 |
|
173 | |||
169 | } |
|
174 | } | |
170 |
|
175 | |||
171 | /*! |
|
176 | /*! | |
172 | Sets \a pen used for drawing given series.. |
|
177 | Sets \a pen used for drawing given series.. | |
173 | */ |
|
178 | */ | |
174 | void QLineSeries::setPen(const QPen& pen) |
|
179 | void QLineSeries::setPen(const QPen& pen) | |
175 | { |
|
180 | { | |
|
181 | if(pen!=m_pen){ | |||
176 | m_pen=pen; |
|
182 | m_pen=pen; | |
|
183 | emit updated(); | |||
|
184 | } | |||
177 | } |
|
185 | } | |
178 |
|
186 | |||
179 | /*! |
|
187 | /*! | |
180 | Sets if data points are \a visible and should be drawn on line. |
|
188 | Sets if data points are \a visible and should be drawn on line. | |
181 | */ |
|
189 | */ | |
182 | void QLineSeries::setPointsVisible(bool visible) |
|
190 | void QLineSeries::setPointsVisible(bool visible) | |
183 | { |
|
191 | { | |
|
192 | if(m_pointsVisible!=visible){ | |||
184 | m_pointsVisible=visible; |
|
193 | m_pointsVisible=visible; | |
|
194 | emit updated(); | |||
|
195 | } | |||
185 | } |
|
196 | } | |
186 |
|
197 | |||
187 | QDebug operator<< (QDebug debug, const QLineSeries series) |
|
198 | QDebug operator<< (QDebug debug, const QLineSeries series) | |
188 | { |
|
199 | { | |
189 | Q_ASSERT(series.m_x.size() == series.m_y.size()); |
|
200 | Q_ASSERT(series.m_x.size() == series.m_y.size()); | |
190 |
|
201 | |||
191 | int size = series.m_x.size(); |
|
202 | int size = series.m_x.size(); | |
192 |
|
203 | |||
193 | for (int i=0;i<size;i++) { |
|
204 | for (int i=0;i<size;i++) { | |
194 | debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") "; |
|
205 | debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") "; | |
195 | } |
|
206 | } | |
196 | return debug.space(); |
|
207 | return debug.space(); | |
197 | } |
|
208 | } | |
198 |
|
209 | |||
199 | /*! |
|
210 | /*! | |
200 | Stream operator for adding a data \a point to the series. |
|
211 | Stream operator for adding a data \a point to the series. | |
201 | \sa add() |
|
212 | \sa add() | |
202 | */ |
|
213 | */ | |
203 |
|
214 | |||
204 | QLineSeries& QLineSeries::operator<< (const QPointF &point) |
|
215 | QLineSeries& QLineSeries::operator<< (const QPointF &point) | |
205 | { |
|
216 | { | |
206 | add(point); |
|
217 | add(point); | |
207 | return *this; |
|
218 | return *this; | |
208 | } |
|
219 | } | |
209 |
|
220 | |||
210 |
|
221 | |||
211 | #include "moc_qlineseries.cpp" |
|
222 | #include "moc_qlineseries.cpp" | |
212 |
|
223 | |||
213 | QTCOMMERCIALCHART_END_NAMESPACE |
|
224 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,56 +1,57 | |||||
1 | #ifndef QLINESERIES_H_ |
|
1 | #ifndef QLINESERIES_H_ | |
2 | #define QLINESERIES_H_ |
|
2 | #define QLINESERIES_H_ | |
3 |
|
3 | |||
4 | #include "qchartglobal.h" |
|
4 | #include "qchartglobal.h" | |
5 | #include "qseries.h" |
|
5 | #include "qseries.h" | |
6 | #include <QDebug> |
|
6 | #include <QDebug> | |
7 | #include <QPen> |
|
7 | #include <QPen> | |
8 | #include <QBrush> |
|
8 | #include <QBrush> | |
9 |
|
9 | |||
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
11 |
|
11 | |||
12 | class QTCOMMERCIALCHART_EXPORT QLineSeries : public QSeries |
|
12 | class QTCOMMERCIALCHART_EXPORT QLineSeries : public QSeries | |
13 | { |
|
13 | { | |
14 | Q_OBJECT |
|
14 | Q_OBJECT | |
15 | public: |
|
15 | public: | |
16 | QLineSeries(QObject* parent=0); |
|
16 | QLineSeries(QObject* parent=0); | |
17 | virtual ~QLineSeries(); |
|
17 | virtual ~QLineSeries(); | |
18 |
|
18 | |||
19 | public: // from QChartSeries |
|
19 | public: // from QChartSeries | |
20 | virtual QSeriesType type() const { return QSeries::SeriesTypeLine;} |
|
20 | virtual QSeriesType type() const { return QSeries::SeriesTypeLine;} | |
21 | void add(qreal x, qreal y); |
|
21 | void add(qreal x, qreal y); | |
22 | void add(const QPointF& point); |
|
22 | void add(const QPointF& point); | |
23 | void replace(qreal x,qreal y); |
|
23 | void replace(qreal x,qreal y); | |
24 | void replace(const QPointF& point); |
|
24 | void replace(const QPointF& point); | |
25 | void remove(qreal x); |
|
25 | void remove(qreal x); | |
26 | void remove(const QPointF& point); |
|
26 | void remove(const QPointF& point); | |
27 | void clear(); |
|
27 | void clear(); | |
28 |
|
28 | |||
29 | void setPen(const QPen& pen); |
|
29 | void setPen(const QPen& pen); | |
30 | QPen pen() const { return m_pen;} |
|
30 | QPen pen() const { return m_pen;} | |
31 |
|
31 | |||
32 | void setPointsVisible(bool visible); |
|
32 | void setPointsVisible(bool visible); | |
33 | bool pointsVisible() const {return m_pointsVisible;} |
|
33 | bool pointsVisible() const {return m_pointsVisible;} | |
34 |
|
34 | |||
35 | int count() const; |
|
35 | int count() const; | |
36 | qreal x(int pos) const; |
|
36 | qreal x(int pos) const; | |
37 | qreal y(int pos) const; |
|
37 | qreal y(int pos) const; | |
38 |
|
38 | |||
39 | QLineSeries& operator << (const QPointF &point); |
|
39 | QLineSeries& operator << (const QPointF &point); | |
40 | friend QDebug operator<< (QDebug d, const QLineSeries series); |
|
40 | friend QDebug operator<< (QDebug d, const QLineSeries series); | |
41 |
|
41 | |||
42 | signals: |
|
42 | signals: | |
43 | void pointReplaced(int index); |
|
43 | void pointReplaced(int index); | |
44 | void pointRemoved(int index); |
|
44 | void pointRemoved(int index); | |
45 | void pointAdded(int index); |
|
45 | void pointAdded(int index); | |
|
46 | void updated(); | |||
46 |
|
47 | |||
47 | private: |
|
48 | private: | |
48 | QVector<qreal> m_x; |
|
49 | QVector<qreal> m_x; | |
49 | QVector<qreal> m_y; |
|
50 | QVector<qreal> m_y; | |
50 | QPen m_pen; |
|
51 | QPen m_pen; | |
51 | bool m_pointsVisible; |
|
52 | bool m_pointsVisible; | |
52 | }; |
|
53 | }; | |
53 |
|
54 | |||
54 | QTCOMMERCIALCHART_END_NAMESPACE |
|
55 | QTCOMMERCIALCHART_END_NAMESPACE | |
55 |
|
56 | |||
56 | #endif |
|
57 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now