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