##// END OF EJS Templates
Adds RemoveAllSeries method to API
Michal Klocek -
r258:09026acabba0
parent child
Show More
@@ -1,334 +1,354
1 1 #include "chartdataset_p.h"
2 2 #include "qchartaxis.h"
3 3 //series
4 4 #include "qlinechartseries.h"
5 5 #include "qbarchartseries.h"
6 6 #include "qstackedbarchartseries.h"
7 7 #include "qpercentbarchartseries.h"
8 8 #include "qpieseries.h"
9 9 #include "qscatterseries.h"
10 10
11 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 12
13 13 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
14 14 m_axisX(new QChartAxis(this)),
15 15 m_axisY(new QChartAxis(this)),
16 16 m_domainIndex(0),
17 17 m_axisXInitialized(false)
18 18 {
19 19 }
20 20
21 21 ChartDataSet::~ChartDataSet()
22 22 {
23 23 // TODO Auto-generated destructor stub
24 24 }
25 25
26 26 const Domain ChartDataSet::domain(QChartAxis *axisY) const
27 27 {
28 28 int i = m_domainMap.count(axisY);
29 29 if(i == 0){
30 30 return Domain();
31 31 }
32 32 i = i - m_domainIndex -1;
33 33 return m_domainMap.values(axisY).at(i);
34 34 }
35 35
36 36 void ChartDataSet::addSeries(QChartSeries* series, QChartAxis *axisY)
37 37 {
38 38 // TODO: we should check the series not already added
39 39
40 40 series->setParent(this); // take ownership
41 41 clearDomains();
42 42
43 43 if(axisY==0) axisY = m_axisY;
44 44 axisY->setParent(this); // take ownership
45 45
46 46 QList<QChartSeries*> seriesList = m_seriesMap.values(axisY);
47 47
48 48 QList<Domain> domainList = m_domainMap.values(axisY);
49 49
50 50 Q_ASSERT(domainList.size()<=1);
51 51
52 52 Domain domain;
53 53
54 54 if(domainList.size()>0) domain = domainList.at(0);
55 55
56 56 switch(series->type())
57 57 {
58 58 case QChartSeries::SeriesTypeLine: {
59 59
60 60 QLineChartSeries* xyseries = static_cast<QLineChartSeries*>(series);
61 61
62 62 for (int i = 0; i < xyseries->count(); i++)
63 63 {
64 64 qreal x = xyseries->x(i);
65 65 qreal y = xyseries->y(i);
66 66 domain.m_minX = qMin(domain.m_minX,x);
67 67 domain.m_minY = qMin(domain.m_minY,y);
68 68 domain.m_maxX = qMax(domain.m_maxX,x);
69 69 domain.m_maxY = qMax(domain.m_maxY,y);
70 70 }
71 71 break;
72 72 }
73 73 case QChartSeries::SeriesTypeBar: {
74 74 qDebug() << "QChartSeries::SeriesTypeBar";
75 75 QBarChartSeries* barSeries = static_cast<QBarChartSeries*>(series);
76 76 qreal x = barSeries->countCategories();
77 77 qreal y = barSeries->max();
78 78 domain.m_minX = qMin(domain.m_minX,x);
79 79 domain.m_minY = qMin(domain.m_minY,y);
80 80 domain.m_maxX = qMax(domain.m_maxX,x);
81 81 domain.m_maxY = qMax(domain.m_maxY,y);
82 82 break;
83 83 }
84 84 case QChartSeries::SeriesTypeStackedBar: {
85 85 qDebug() << "QChartSeries::SeriesTypeStackedBar";
86 86
87 87 QStackedBarChartSeries* stackedBarSeries = static_cast<QStackedBarChartSeries*>(series);
88 88 qreal x = stackedBarSeries->countCategories();
89 89 qreal y = stackedBarSeries->maxCategorySum();
90 90 domain.m_minX = qMin(domain.m_minX,x);
91 91 domain.m_minY = qMin(domain.m_minY,y);
92 92 domain.m_maxX = qMax(domain.m_maxX,x);
93 93 domain.m_maxY = qMax(domain.m_maxY,y);
94 94 break;
95 95 }
96 96 case QChartSeries::SeriesTypePercentBar: {
97 97 qDebug() << "QChartSeries::SeriesTypePercentBar";
98 98
99 99 QPercentBarChartSeries* percentBarSeries = static_cast<QPercentBarChartSeries*>(series);
100 100 qreal x = percentBarSeries->countCategories();
101 101 domain.m_minX = qMin(domain.m_minX,x);
102 102 domain.m_minY = 0;
103 103 domain.m_maxX = qMax(domain.m_maxX,x);
104 104 domain.m_maxY = 100;
105 105 break;
106 106 }
107 107
108 108 case QChartSeries::SeriesTypePie: {
109 109 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
110 110 // TODO: domain stuff
111 111 break;
112 112 }
113 113
114 114 case QChartSeries::SeriesTypeScatter: {
115 115 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
116 116 Q_ASSERT(scatterSeries);
117 117 foreach (QPointF point, scatterSeries->data()) {
118 118 domain.m_minX = qMin(domain.m_minX, point.x());
119 119 domain.m_maxX = qMax(domain.m_maxX, point.x());
120 120 domain.m_minY = qMin(domain.m_minY, point.y());
121 121 domain.m_maxY = qMax(domain.m_maxY, point.y());
122 122 }
123 123 break;
124 124 }
125 125
126 126 default: {
127 127 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
128 128 return;
129 129 break;
130 130 }
131 131
132 132 }
133 133
134 134 if(!m_domainMap.contains(axisY))
135 135 {
136 136 emit axisAdded(axisY);
137 137 QObject::connect(axisY,SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
138 138 QObject::connect(axisY,SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
139 139 QObject::connect(axisY,SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
140 140 }
141 141 m_domainMap.replace(axisY,domain);
142 142 m_seriesMap.insert(axisY,series);
143 143
144 144 if(!m_axisXInitialized)
145 145 {
146 146 emit axisAdded(axisX());
147 147 QObject::connect(axisX(),SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
148 148 QObject::connect(axisX(),SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
149 149 QObject::connect(axisX(),SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
150 150 m_axisXInitialized=true;
151 151 }
152 152
153 153
154 154 emit seriesAdded(series);
155 155 QStringList ylabels = createLabels(axisY,domain.m_minY,domain.m_maxY);
156 156 QStringList xlabels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
157 157 emit axisLabelsChanged(axisY,ylabels);
158 158 emit axisLabelsChanged(axisX(),xlabels);
159 159 emit seriesDomainChanged(series,domain);
160 160
161 161 }
162 162
163 163 void ChartDataSet::removeSeries(QChartSeries* series)
164 164 {
165 165 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
166 166 foreach(QChartAxis* axis , keys) {
167 167 if(m_seriesMap.contains(axis,series)){
168 168 emit seriesRemoved(series);
169 169 m_seriesMap.remove(axis,series);
170 170 //remove axis if no longer there
171 if(!m_seriesMap.contains(axis) && axis != m_axisY){
171 if(!m_seriesMap.contains(axis)){
172 172 emit axisRemoved(axis);
173 173 m_domainMap.remove(axis);
174 if(axis != m_axisY)
174 175 delete axis;
175 176 }
177 series->setParent(0);
176 178 break;
177 179 }
178 180 }
179 181 }
180 182
183 void ChartDataSet::removeAllSeries()
184 {
185 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
186 foreach(QChartAxis* axis , keys) {
187 QList<QChartSeries*> seriesList = m_seriesMap.values(axis);
188 for(int i =0 ; i < seriesList.size();i++ )
189 {
190 emit seriesRemoved(seriesList.at(i));
191 delete(seriesList.at(i));
192 }
193 m_seriesMap.remove(axis);
194 m_domainMap.remove(axis);
195 emit axisRemoved(axis);
196 if(axis != m_axisY) delete axis;
197 }
198 m_domainIndex=0;
199 }
200
181 201 bool ChartDataSet::nextDomain()
182 202 {
183 203 int limit = (m_domainMap.values().size()/m_domainMap.uniqueKeys().size())-1;
184 204
185 205 if (m_domainIndex < limit) {
186 206 m_domainIndex++;
187 207 setDomain(m_domainIndex);
188 208 return true;
189 209 }
190 210 else {
191 211 return false;
192 212 }
193 213 }
194 214
195 215 bool ChartDataSet::previousDomain()
196 216 {
197 217 if (m_domainIndex > 0) {
198 218 m_domainIndex--;
199 219 setDomain(m_domainIndex);
200 220 return true;
201 221 }
202 222 else {
203 223 return false;
204 224 }
205 225 }
206 226
207 227 void ChartDataSet::setDomain(int index)
208 228 {
209 229 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
210 230 foreach (QChartAxis* axis , domainList) {
211 231 int i = m_domainMap.count(axis) - index -1;
212 232 Q_ASSERT(i>=0);
213 233 Domain domain = m_domainMap.values(axis).at(i);
214 234 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
215 235 QList<QChartSeries*> seriesList = m_seriesMap.values(axis);
216 236 foreach(QChartSeries* series, seriesList) {
217 237 emit seriesDomainChanged(series,domain);
218 238 }
219 239 emit axisLabelsChanged(axis,labels);
220 240 }
221 241
222 242 Domain domain = m_domainMap.value(axisY());
223 243 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
224 244 emit axisLabelsChanged(axisX(),labels);
225 245 }
226 246
227 247 void ChartDataSet::clearDomains(int toIndex)
228 248 {
229 249 Q_ASSERT(toIndex>=0);
230 250
231 251 m_domainIndex = toIndex;
232 252
233 253 QList<QChartAxis*> keys = m_domainMap.uniqueKeys();
234 254
235 255 foreach (QChartAxis* key , keys)
236 256 {
237 257 QList<Domain> domains = m_domainMap.values(key);
238 258 m_domainMap.remove(key);
239 259 int i = domains.size() - toIndex - 1;
240 260 while(i--){
241 261 domains.removeFirst();
242 262 }
243 263 for(int j=domains.size()-1; j>=0 ;j--)
244 264 m_domainMap.insert(key,domains.at(j));
245 265 }
246 266 }
247 267
248 268 void ChartDataSet::addDomain(const QRectF& rect, const QRectF& viewport)
249 269 {
250 270 Q_ASSERT(rect.isValid());
251 271 Q_ASSERT(viewport.isValid());
252 272
253 273 clearDomains(m_domainIndex);
254 274
255 275 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
256 276
257 277 foreach (QChartAxis* axis , domainList){
258 278 Domain domain(m_domainMap.value(axis).subDomain(rect,viewport.width(),viewport.height()));
259 279 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
260 280 QList<QChartSeries*> seriesList = m_seriesMap.values(axis);
261 281 foreach(QChartSeries* series, seriesList){
262 282 emit seriesDomainChanged(series,domain);
263 283 }
264 284 emit axisLabelsChanged(axis,labels);
265 285 m_domainMap.insert(axis,domain);
266 286 }
267 287
268 288 Domain domain = m_domainMap.value(axisY());
269 289 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
270 290 emit axisLabelsChanged(axisX(),labels);
271 291
272 292 m_domainIndex++;
273 293 }
274 294
275 295 QChartAxis* ChartDataSet::axisY(QChartSeries* series) const
276 296 {
277 297 if(series == 0) return m_axisY;
278 298
279 299 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
280 300
281 301 foreach(QChartAxis* axis , keys) {
282 302 if(m_seriesMap.contains(axis,series)){
283 303 return axis;
284 304 }
285 305 }
286 306 return 0;
287 307 }
288 308
289 309 QStringList ChartDataSet::createLabels(QChartAxis* axis,qreal min, qreal max)
290 310 {
291 311 Q_ASSERT(max>=min);
292 312
293 313 QStringList labels;
294 314
295 315 int ticks = axis->ticksCount()-1;
296 316
297 317 for(int i=0; i<= ticks; i++){
298 318 qreal value = min + (i * (max - min)/ ticks);
299 319 QString label = axis->axisTickLabel(value);
300 320 if(label.isEmpty()){
301 321 labels << QString::number(value);
302 322 }else{
303 323 labels << label;
304 324 }
305 325 }
306 326 return labels;
307 327 }
308 328
309 329
310 330 void ChartDataSet::handleMinChanged(qreal min)
311 331 {
312 332
313 333 }
314 334
315 335 void ChartDataSet::handleMaxChanged(qreal max)
316 336 {
317 337
318 338 }
319 339
320 340 void ChartDataSet::handleTickChanged(QChartAxis* axis)
321 341 {
322 342 Domain domain = m_domainMap.value(axisY());
323 343 if(axis==axisX()){
324 344 QStringList labels = createLabels(axis,domain.m_minX,domain.m_maxX);
325 345 emit axisLabelsChanged(axis,labels);
326 346 }else{
327 347 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
328 348 emit axisLabelsChanged(axis,labels);
329 349 }
330 350 }
331 351
332 352 #include "moc_chartdataset_p.cpp"
333 353
334 354 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,59 +1,60
1 1 #ifndef CHARTDATASET_P_H_
2 2 #define CHARTDATASET_P_H_
3 3
4 4 #include "qchartseries.h"
5 5 #include "domain_p.h"
6 6 #include <QVector>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class QChartAxis;
11 11
12 12 class ChartDataSet : public QObject
13 13 {
14 14 Q_OBJECT
15 15 public:
16 16 ChartDataSet(QObject* parent=0);
17 17 virtual ~ChartDataSet();
18 18
19 19 void addSeries(QChartSeries* series,QChartAxis *axisY = 0);
20 20 void removeSeries(QChartSeries* series);
21 void removeAllSeries();
21 22 void addDomain(const QRectF& rect, const QRectF& viewport);
22 23 bool nextDomain();
23 24 bool previousDomain();
24 25 void clearDomains(int toIndex =0);
25 26 const Domain domain(QChartAxis *axisY) const;
26 27 int domainIndex() const {return m_domainIndex;}
27 28
28 29 QChartAxis* axisX() const { return m_axisX;};
29 30 QChartAxis* axisY(QChartSeries* series = 0) const;
30 31
31 32 signals:
32 33 void seriesAdded(QChartSeries* series);
33 34 void seriesRemoved(QChartSeries* series);
34 35 void axisAdded(QChartAxis* axis);
35 36 void axisRemoved(QChartAxis* axis);
36 37 void axisLabelsChanged(QChartAxis* axis, const QStringList& labels);
37 38 void seriesDomainChanged(QChartSeries* series,const Domain& domain);
38 39
39 40 private slots:
40 41 void handleMinChanged(qreal min);
41 42 void handleMaxChanged(qreal max);
42 43 void handleTickChanged(QChartAxis*);
43 44
44 45 private:
45 46 void setDomain(int index);
46 47 QStringList createLabels(QChartAxis* axis,qreal min, qreal max);
47 48
48 49 private:
49 50 QMultiMap<QChartAxis*, Domain> m_domainMap;
50 51 QMultiMap<QChartAxis*, QChartSeries*> m_seriesMap;
51 52 QChartAxis* m_axisX;
52 53 QChartAxis* m_axisY;
53 54 int m_domainIndex;
54 55 bool m_axisXInitialized;
55 56 };
56 57
57 58 QTCOMMERCIALCHART_END_NAMESPACE
58 59
59 60 #endif /* CHARTENGINE_P_H_ */
@@ -1,153 +1,158
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 <QGraphicsScene>
6 6 #include <QGraphicsSceneResizeEvent>
7 7 #include <QDebug>
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
12 12 m_backgroundItem(0),
13 13 m_titleItem(0),
14 14 m_dataset(new ChartDataSet(this)),
15 15 m_presenter(new ChartPresenter(this,m_dataset))
16 16 {
17 17 }
18 18
19 19 QChart::~QChart() {}
20 20
21 21 void QChart::addSeries(QChartSeries* series,QChartAxis* axisY)
22 22 {
23 23 m_dataset->addSeries(series,axisY);
24 24 }
25 25
26 26 void QChart::removeSeries(QChartSeries* series)
27 27 {
28 28 m_dataset->removeSeries(series);
29 29 }
30 30
31 void QChart::removeAllSeries()
32 {
33 m_dataset->removeAllSeries();
34 }
35
31 36 void QChart::setChartBackgroundBrush(const QBrush& brush)
32 37 {
33 38 createChartBackgroundItem();
34 39 m_backgroundItem->setBrush(brush);
35 40 m_backgroundItem->update();
36 41 }
37 42
38 43 void QChart::setChartBackgroundPen(const QPen& pen)
39 44 {
40 45 createChartBackgroundItem();
41 46 m_backgroundItem->setPen(pen);
42 47 m_backgroundItem->update();
43 48 }
44 49
45 50 void QChart::setChartTitle(const QString& title)
46 51 {
47 52 createChartTitleItem();
48 53 m_titleItem->setPlainText(title);
49 54 }
50 55
51 56 void QChart::setChartTitleFont(const QFont& font)
52 57 {
53 58 createChartTitleItem();
54 59 m_titleItem->setFont(font);
55 60 }
56 61
57 62 void QChart::createChartBackgroundItem()
58 63 {
59 64 if(!m_backgroundItem) {
60 65 m_backgroundItem = new QGraphicsRectItem(this);
61 66 m_backgroundItem->setZValue(-1);
62 67 }
63 68 }
64 69
65 70 void QChart::createChartTitleItem()
66 71 {
67 72 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
68 73 }
69 74
70 75 int QChart::margin() const
71 76 {
72 77 return m_presenter->margin();
73 78 }
74 79
75 80 void QChart::setMargin(int margin)
76 81 {
77 82 m_presenter->setMargin(margin);
78 83 }
79 84
80 85 void QChart::setChartTheme(QChart::ChartTheme theme)
81 86 {
82 87 m_presenter->setChartTheme(theme);
83 88 }
84 89
85 90 QChart::ChartTheme QChart::chartTheme() const
86 91 {
87 92 return m_presenter->chartTheme();
88 93 }
89 94
90 95 void QChart::zoomIn()
91 96 {
92 97 if (!m_dataset->nextDomain()) {
93 98 QRectF rect = m_presenter->geometry();
94 99 rect.setWidth(rect.width()/2);
95 100 rect.setHeight(rect.height()/2);
96 101 rect.moveCenter(m_presenter->geometry().center());
97 102 zoomIn(rect);
98 103 }
99 104 }
100 105
101 106 void QChart::zoomIn(const QRectF& rect)
102 107 {
103 108 if(!rect.isValid()) return;
104 109 QRectF r = rect.normalized();
105 110 int margin = m_presenter->margin();
106 111 r.translate(-margin, -margin);
107 112 m_dataset->addDomain(r,m_presenter->geometry());
108 113 }
109 114
110 115 void QChart::zoomOut()
111 116 {
112 117 m_dataset->previousDomain();
113 118 }
114 119
115 120 void QChart::zoomReset()
116 121 {
117 122 m_dataset->clearDomains();
118 123 }
119 124
120 125 QChartAxis* QChart::axisX() const
121 126 {
122 127 return m_dataset->axisX();
123 128 }
124 129
125 130 QChartAxis* QChart::axisY() const
126 131 {
127 132 return m_dataset->axisY();
128 133 }
129 134
130 135 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
131 136 {
132 137
133 138 m_rect = QRectF(QPoint(0,0),event->newSize());
134 139 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
135 140
136 141 // recalculate title position
137 142 if (m_titleItem) {
138 143 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
139 144 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
140 145 }
141 146
142 147 //recalculate background gradient
143 148 if (m_backgroundItem) {
144 149 m_backgroundItem->setRect(rect);
145 150 }
146 151
147 152 QGraphicsWidget::resizeEvent(event);
148 153 update();
149 154 }
150 155
151 156 #include "moc_qchart.cpp"
152 157
153 158 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,84 +1,85
1 1 #ifndef CHART_H
2 2 #define CHART_H
3 3
4 4 #include <qchartglobal.h>
5 5 #include <qchartseries.h>
6 6 #include <QGraphicsWidget>
7 7 #include <QLinearGradient>
8 8 #include <QFont>
9 9
10 10 class QGraphicsSceneResizeEvent;
11 11
12 12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13 13
14 14 class AxisItem;
15 15 class QChartSeries;
16 16 class PlotDomain;
17 17 class BarPresenter;
18 18 class QChartAxis;
19 19 class ChartTheme;
20 20 class ChartItem;
21 21 class ChartDataSet;
22 22 class ChartPresenter;
23 23
24 24 /*!
25 25 * TODO: define the responsibilities
26 26 */
27 27 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
28 28 {
29 29 Q_OBJECT
30 30 public:
31 31 enum ChartTheme {
32 32 /*! The default theme follows the GUI style of the Operating System */
33 33 ChartThemeDefault,
34 34 ChartThemeVanilla,
35 35 ChartThemeIcy,
36 36 ChartThemeGrayscale,
37 37 ChartThemeScientific,
38 38 //ChartThemeUnnamed1
39 39 };
40 40
41 41 public:
42 42 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
43 43 ~QChart();
44 44
45 void addSeries(QChartSeries* series,QChartAxis *axisY=0);
46 void removeSeries(QChartSeries* series);
45 void addSeries(QChartSeries* series,QChartAxis* axisY=0);// takes series ownership , takes axis ownership
46 void removeSeries(QChartSeries* series); //returns ownership , deletes axis if no series attached
47 void removeAllSeries(); // deletes series and axis
47 48
48 49 void setMargin(int margin);
49 50 int margin() const;
50 51 void setChartTheme(QChart::ChartTheme theme);
51 52 QChart::ChartTheme chartTheme() const;
52 53
53 54 void setChartTitle(const QString& title);
54 55 void setChartTitleFont(const QFont& font);
55 56 void setChartBackgroundBrush(const QBrush& brush);
56 57 void setChartBackgroundPen(const QPen& pen);
57 58
58 59 void zoomIn();
59 60 void zoomIn(const QRectF& rect);
60 61 void zoomOut();
61 62 void zoomReset();
62 63
63 64 QChartAxis* axisX() const;
64 65 QChartAxis* axisY() const;
65 66
66 67 protected:
67 68 void resizeEvent(QGraphicsSceneResizeEvent *event);
68 69
69 70 private:
70 71 inline void createChartBackgroundItem();
71 72 inline void createChartTitleItem();
72 73
73 74 private:
74 75 Q_DISABLE_COPY(QChart)
75 76 QGraphicsRectItem* m_backgroundItem;
76 77 QGraphicsTextItem* m_titleItem;
77 78 QRectF m_rect;
78 79 ChartDataSet *m_dataset;
79 80 ChartPresenter *m_presenter;
80 81 };
81 82
82 83 QTCOMMERCIALCHART_END_NAMESPACE
83 84
84 85 #endif
@@ -1,221 +1,226
1 1 #include "qchartview.h"
2 2 #include "qchart.h"
3 3 #include "qchartaxis.h"
4 4 #include <QGraphicsView>
5 5 #include <QGraphicsScene>
6 6 #include <QRubberBand>
7 7 #include <QResizeEvent>
8 8 #include <QDebug>
9 9
10 10 /*!
11 11 \class QChartView
12 12 \brief Chart widget.
13 13 */
14 14
15 15 QTCOMMERCIALCHART_BEGIN_NAMESPACE
16 16
17 17 QChartView::QChartView(QWidget *parent) :
18 18 QGraphicsView(parent),
19 19 m_scene(new QGraphicsScene(this)),
20 20 m_chart(new QChart()),
21 21 m_rubberBand(0),
22 22 m_verticalRubberBand(false),
23 23 m_horizonalRubberBand(false)
24 24 {
25 25 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
26 26 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
27 27 setScene(m_scene);
28 28 m_chart->setMargin(50);
29 29 m_scene->addItem(m_chart);
30 30 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
31 31 }
32 32
33 33 QChartView::~QChartView()
34 34 {
35 35 }
36 36
37 37 void QChartView::resizeEvent(QResizeEvent *event)
38 38 {
39 39 m_scene->setSceneRect(0,0,size().width(),size().height());
40 40 m_chart->resize(size());
41 41 QWidget::resizeEvent(event);
42 42 }
43 43
44 44 void QChartView::addSeries(QChartSeries* series,QChartAxis *axisY)
45 45 {
46 46 m_chart->addSeries(series,axisY);
47 47 }
48 48
49 49 void QChartView::removeSeries(QChartSeries* series)
50 50 {
51 51 m_chart->removeSeries(series);
52 52 }
53 53
54 void QChartView::removeAllSeries()
55 {
56 m_chart->removeAllSeries();
57 }
58
54 59 void QChartView::zoomIn()
55 60 {
56 61 m_chart->zoomIn();
57 62 }
58 63
59 64 void QChartView::zoomIn(const QRect& rect)
60 65 {
61 66 m_chart->zoomIn(rect);
62 67 }
63 68
64 69 void QChartView::zoomOut()
65 70 {
66 71 m_chart->zoomOut();
67 72 }
68 73
69 74 int QChartView::margin() const
70 75 {
71 76 return m_chart->margin();
72 77 }
73 78
74 79 void QChartView::setChartTitle(const QString& title)
75 80 {
76 81 m_chart->setChartTitle(title);
77 82 }
78 83
79 84 void QChartView::setChartTitleFont(const QFont& font)
80 85 {
81 86 m_chart->setChartTitleFont(font);
82 87 }
83 88
84 89 void QChartView::setChartBackgroundBrush(const QBrush& brush)
85 90 {
86 91 m_chart->setChartBackgroundBrush(brush);
87 92 }
88 93 void QChartView::setChartBackgroundPen(const QPen& pen)
89 94 {
90 95 m_chart->setChartBackgroundPen(pen);
91 96 }
92 97
93 98 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
94 99 {
95 100 switch(policy) {
96 101 case VerticalRubberBand:
97 102 m_verticalRubberBand = true;
98 103 m_horizonalRubberBand = false;
99 104 break;
100 105 case HorizonalRubberBand:
101 106 m_verticalRubberBand = false;
102 107 m_horizonalRubberBand = true;
103 108 break;
104 109 case RectangleRubberBand:
105 110 m_verticalRubberBand = true;
106 111 m_horizonalRubberBand = true;
107 112 break;
108 113 case NoRubberBand:
109 114 default:
110 115 delete m_rubberBand;
111 116 m_rubberBand=0;
112 117 m_horizonalRubberBand = false;
113 118 m_verticalRubberBand = false;
114 119 return;
115 120 }
116 121 if(!m_rubberBand) {
117 122 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
118 123 m_rubberBand->setEnabled(true);
119 124 }
120 125 }
121 126
122 127 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
123 128 {
124 129 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
125 130 if(m_horizonalRubberBand) return HorizonalRubberBand;
126 131 if(m_verticalRubberBand) return VerticalRubberBand;
127 132 return NoRubberBand;
128 133 }
129 134
130 135 void QChartView::mousePressEvent(QMouseEvent *event)
131 136 {
132 137 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
133 138
134 139 int margin = m_chart->margin();
135 140 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
136 141
137 142 if (rect.contains(event->pos())) {
138 143 m_rubberBandOrigin = event->pos();
139 144 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
140 145 m_rubberBand->show();
141 146 event->accept();
142 147 }
143 148 }
144 149 }
145 150
146 151 void QChartView::mouseMoveEvent(QMouseEvent *event)
147 152 {
148 153 if(m_rubberBand && m_rubberBand->isVisible()) {
149 154 int margin = m_chart->margin();
150 155 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
151 156 int width = event->pos().x() - m_rubberBandOrigin.x();
152 157 int height = event->pos().y() - m_rubberBandOrigin.y();
153 158 if(!m_verticalRubberBand) {
154 159 m_rubberBandOrigin.setY(rect.top());
155 160 height = rect.height();
156 161 }
157 162 if(!m_horizonalRubberBand) {
158 163 m_rubberBandOrigin.setX(rect.left());
159 164 width= rect.width();
160 165 }
161 166 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
162 167 }
163 168 else {
164 169 QGraphicsView::mouseMoveEvent(event);
165 170 }
166 171 }
167 172
168 173 void QChartView::mouseReleaseEvent(QMouseEvent *event)
169 174 {
170 175 if(m_rubberBand) {
171 176 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
172 177 m_rubberBand->hide();
173 178 QRect rect = m_rubberBand->geometry();
174 179 m_chart->zoomIn(rect);
175 180 event->accept();
176 181 }
177 182
178 183 if(event->button()==Qt::RightButton)
179 184 m_chart->zoomReset();
180 185 }
181 186 else {
182 187 QGraphicsView::mouseReleaseEvent(event);
183 188 }
184 189 }
185 190
186 191 void QChartView::keyPressEvent(QKeyEvent *event)
187 192 {
188 193 switch (event->key()) {
189 194 case Qt::Key_Plus:
190 195 zoomIn();
191 196 break;
192 197 case Qt::Key_Minus:
193 198 zoomOut();
194 199 break;
195 200 default:
196 201 QGraphicsView::keyPressEvent(event);
197 202 break;
198 203 }
199 204 }
200 205
201 206 void QChartView::setChartTheme(QChart::ChartTheme theme)
202 207 {
203 208 m_chart->setChartTheme(theme);
204 209 }
205 210
206 211 QChart::ChartTheme QChartView::chartTheme() const
207 212 {
208 213 return m_chart->chartTheme();
209 214 }
210 215
211 216 QChartAxis* QChartView::axisX() const
212 217 {
213 218 return m_chart->axisX();
214 219 }
215 220
216 221 QChartAxis* QChartView::axisY() const
217 222 {
218 223 return m_chart->axisY();
219 224 }
220 225
221 226 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,70 +1,71
1 1 #ifndef QCHARTWIDGET_H
2 2 #define QCHARTWIDGET_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchartseries.h"
6 6 #include "qchart.h"
7 7 #include <QGraphicsView>
8 8
9 9 class QGraphicsScene;
10 10 class QRubberBand;
11 11
12 12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13 13
14 14 class QChart;
15 15
16 16 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
17 17 {
18 18 public:
19 19 enum RubberBandPolicy { NoRubberBand, VerticalRubberBand, HorizonalRubberBand, RectangleRubberBand };
20 20
21 21 explicit QChartView(QWidget *parent = 0);
22 22 ~QChartView();
23 23
24 24 //implement from QWidget
25 25 void resizeEvent(QResizeEvent *event);
26 26
27 void addSeries(QChartSeries* series,QChartAxis* axisY=0);// takes ownership
28 void removeSeries(QChartSeries* series);
27 void addSeries(QChartSeries* series,QChartAxis* axisY=0);// takes series ownership , takes axis ownership
28 void removeSeries(QChartSeries* series); //returns ownership , deletes axis if no series attached
29 void removeAllSeries(); // deletes series and axis
29 30 int margin() const;
30 31
31 32 void setChartTitle(const QString& title);
32 33 void setChartTitleFont(const QFont& font);
33 34 void setChartBackgroundBrush(const QBrush& brush);
34 35 void setChartBackgroundPen(const QPen& pen);
35 36
36 37 void zoomIn();
37 38 void zoomIn(const QRect& rect);
38 39 void zoomOut();
39 40
40 41 void setRubberBandPolicy(const RubberBandPolicy );
41 42 RubberBandPolicy rubberBandPolicy() const;
42 43
43 44 void setChartTheme(QChart::ChartTheme theme);
44 45 QChart::ChartTheme chartTheme() const;
45 46
46 47 QChartAxis* axisX() const;
47 48 QChartAxis* axisY() const;
48 49
49 50 protected:
50 51 void mousePressEvent(QMouseEvent *event);
51 52 void mouseMoveEvent(QMouseEvent *event);
52 53 void mouseReleaseEvent(QMouseEvent *event);
53 54 void keyPressEvent(QKeyEvent *event);
54 55
55 56
56 57 private:
57 58 QGraphicsScene *m_scene;
58 59 QChart* m_chart;
59 60 QPoint m_rubberBandOrigin;
60 61 QRubberBand* m_rubberBand;
61 62 bool m_verticalRubberBand;
62 63 bool m_horizonalRubberBand;
63 64 Q_DISABLE_COPY(QChartView)
64 65
65 66
66 67 };
67 68
68 69 QTCOMMERCIALCHART_END_NAMESPACE
69 70
70 71 #endif // QCHARTWIDGET_H
@@ -1,528 +1,599
1 1 #include <QtTest/QtTest>
2 2 #include <qchartaxis.h>
3 3 #include <qchartseries.h>
4 4 #include <qlinechartseries.h>
5 5 #include <private/chartdataset_p.h>
6 6 #include <private/domain_p.h>
7 7
8 8 QTCOMMERCIALCHART_USE_NAMESPACE
9 9
10 10 Q_DECLARE_METATYPE(Domain)
11 11 Q_DECLARE_METATYPE(QChartAxis*)
12 12 Q_DECLARE_METATYPE(QChartSeries*)
13 13 Q_DECLARE_METATYPE(QLineChartSeries*)
14 14
15 15 class tst_ChartDataSet: public QObject {
16 16 Q_OBJECT
17 17
18 18 public slots:
19 19 void initTestCase();
20 20 void cleanupTestCase();
21 21 void init();
22 22 void cleanup();
23 23
24 24 private slots:
25 25 void chartdataset_data();
26 26 void chartdataset();
27 27
28 28 void addDomain_data();
29 29 void addDomain();
30 30 void addSeries_data();
31 31 void addSeries();
32 32 void axisY_data();
33 33 void axisY();
34 34 void clearDomains_data();
35 35 void clearDomains();
36 36 void domain_data();
37 37 void domain();
38 38 void nextpreviousDomain_data();
39 39 void nextpreviousDomain();
40 40 void removeSeries_data();
41 41 void removeSeries();
42 void removeAllSeries_data();
43 void removeAllSeries();
42 44 };
43 45
44 46 void tst_ChartDataSet::initTestCase()
45 47 {
46 48 qRegisterMetaType<Domain>("Domain");
47 49 qRegisterMetaType<QChartAxis*>();
48 50 qRegisterMetaType<QChartSeries*>();
49 51 }
50 52
51 53 void tst_ChartDataSet::cleanupTestCase()
52 54 {
53 55 }
54 56
55 57 void tst_ChartDataSet::init()
56 58 {
57 59 }
58 60
59 61 void tst_ChartDataSet::cleanup()
60 62 {
61 63 }
62 64
63 65 void tst_ChartDataSet::chartdataset_data()
64 66 {
65 67 }
66 68
67 69 void tst_ChartDataSet::chartdataset()
68 70 {
69 71 ChartDataSet dataSet;
70 72 QVERIFY2(dataSet.axisX(), "Missing axisX.");
71 73 QVERIFY2(dataSet.axisY(), "Missing axisY.");
72 74 //check if not dangling pointer
73 75 dataSet.axisX()->objectName();
74 76 dataSet.axisY()->objectName();
75 77 QVERIFY(dataSet.domain(dataSet.axisX())==Domain());
76 78 QVERIFY(dataSet.domain(dataSet.axisY())==Domain());
77 79 QCOMPARE(dataSet.domainIndex(), 0);
78 80 }
79 81
80 82 void tst_ChartDataSet::addDomain_data()
81 83 {
82 84 QTest::addColumn<QRectF>("rect");
83 85 QTest::addColumn<QRectF>("viewport");
84 86 QTest::newRow("400x400,1000x1000") << QRectF(200, 200, 600, 600)
85 87 << QRectF(0, 0, 1000, 1000);
86 88 QTest::newRow("600x600,1000x1000") << QRectF(100, 100, 700, 700)
87 89 << QRectF(0, 0, 1000, 1000);
88 90 QTest::newRow("200x200,1000x1000") << QRectF(400, 400, 600, 600)
89 91 << QRectF(0, 0, 1000, 1000);
90 92 }
91 93
92 94 void tst_ChartDataSet::addDomain()
93 95 {
94 96 QFETCH(QRectF, rect);
95 97 QFETCH(QRectF, viewport);
96 98
97 99 ChartDataSet dataSet;
98 100
99 101 Domain domain1(0, 1000, 0, 1000);
100 102 QLineChartSeries series;
101 103 series.add(0, 0);
102 104 series.add(1000, 1000);
103 105
104 106 dataSet.addSeries(&series);
105 107
106 108 QCOMPARE(dataSet.domainIndex(), 0);
107 109
108 110 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
109 111 QSignalSpy spy1(&dataSet,
110 112 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
111 113 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
112 114 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
113 115 QSignalSpy spy4(&dataSet,
114 116 SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
115 117 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
116 118
117 119 Domain domain2 = dataSet.domain(dataSet.axisY());
118 120 QVERIFY(domain1 == domain2);
119 121
120 122 dataSet.addDomain(rect, viewport);
121 123 QCOMPARE(dataSet.domainIndex(), 1);
122 124 Domain domain3 = dataSet.domain(dataSet.axisY());
123 125 Domain domain4 = domain1.subDomain(rect, viewport.width(),
124 126 viewport.height());
125 127 QVERIFY(domain3 == domain4);
126 128
127 129 QCOMPARE(spy0.count(), 0);
128 130 QCOMPARE(spy1.count(), 2);
129 131 QCOMPARE(spy2.count(), 0);
130 132 QCOMPARE(spy3.count(), 0);
131 133 QCOMPARE(spy4.count(), 1);
132 134 QCOMPARE(spy5.count(), 0);
133 135 }
134 136
135 137 void tst_ChartDataSet::addSeries_data()
136 138 {
137 139 QTest::addColumn<int>("seriesCount");
138 140 QTest::addColumn<int>("axisYCount");
139 141 QTest::newRow("2 series, default axis") << 2 << 0;
140 142 QTest::newRow("2 series, 2 new axis") << 2 << 2;
141 143 QTest::newRow("2 series, 1 new axis") << 2 << 2;
142 144 QTest::newRow("3 series, 3 new axis") << 3 << 3;
143 145 QTest::newRow("3 series, 2 new axis") << 3 << 2;
144 146 QTest::newRow("3 series, 1 new axis") << 3 << 1;
145 147 }
146 148
147 149 void tst_ChartDataSet::addSeries()
148 150 {
149 151 QFETCH(int, seriesCount);
150 152 QFETCH(int, axisYCount);
151 153
152 154 ChartDataSet dataSet;
153 155
154 156 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
155 157 QSignalSpy spy1(&dataSet,
156 158 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
157 159 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
158 160 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
159 161 QSignalSpy spy4(&dataSet,
160 162 SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
161 163 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
162 164
163 165 QList<QChartAxis*> axisList;
164 166
165 167 for (int i = 0; i < axisYCount; i++) {
166 168 QChartAxis* axis = new QChartAxis();
167 169 axisList << axis;
168 170 }
169 171
170 172 QList<QChartAxis*>::iterator iterator = axisList.begin();
171 173
172 174 for (int i = 0; i < seriesCount; i++) {
173 175 QChartAxis* axisY = 0;
174 176 QLineChartSeries* series = new QLineChartSeries();
175 177 if (iterator != axisList.end()) {
176 178 axisY = *iterator;
177 179 iterator++;
178 180 } else if (axisList.count() > 0) {
179 181 iterator--;
180 182 axisY = *iterator;
181 183 iterator++;
182 184 }
183 185 dataSet.addSeries(series, axisY);
184 186 }
185 187
186 188 //default axis
187 189 if (axisYCount == 0)
188 190 axisYCount+=2;
189 191 else
190 192 axisYCount++;
191 193
192
193
194 194 QCOMPARE(spy0.count(), axisYCount);
195 195 QCOMPARE(spy1.count(), seriesCount*2);
196 196 QCOMPARE(spy2.count(), 0);
197 197 QCOMPARE(spy3.count(), seriesCount);
198 198 QCOMPARE(spy4.count(), seriesCount);
199 199 QCOMPARE(spy5.count(), 0);
200 200
201 201 QCOMPARE(dataSet.domainIndex(), 0);
202 202 }
203 203
204 204 void tst_ChartDataSet::axisY_data()
205 205 {
206 206 QTest::addColumn<QChartAxis*>("axisY");
207 207 QTest::newRow("axisY1") << new QChartAxis();
208 208 QTest::newRow("axisY2") << new QChartAxis();
209 209 }
210 210
211 211 void tst_ChartDataSet::axisY()
212 212 {
213 213 QFETCH(QChartAxis*, axisY);
214 214
215 215 ChartDataSet dataSet;
216 216
217 217 QChartAxis* defaultAxisY = dataSet.axisY();
218 218
219 219 QVERIFY2(defaultAxisY, "Missing axisY.");
220 220
221 221 QLineChartSeries* series1 = new QLineChartSeries();
222 222 dataSet.addSeries(series1);
223 223
224 224 QLineChartSeries* series2 = new QLineChartSeries();
225 225 dataSet.addSeries(series2, axisY);
226 226
227 227 QVERIFY(dataSet.axisY(series1) == defaultAxisY);
228 228 QVERIFY(dataSet.axisY(series2) == axisY);
229 229
230 230
231 231 }
232 232
233 233 void tst_ChartDataSet::clearDomains_data()
234 234 {
235 235 QTest::addColumn<int>("indexCount");
236 236 QTest::newRow("0") << 0;
237 237 QTest::newRow("1") << 1;
238 238 QTest::newRow("5") << 2;
239 239 QTest::newRow("8") << 3;
240 240 }
241 241
242 242 void tst_ChartDataSet::clearDomains()
243 243 {
244 244 QFETCH(int, indexCount);
245 245
246 246 Domain domain1(0, 100, 0, 100);
247 247 QLineChartSeries* series = new QLineChartSeries();
248 248 series->add(0, 0);
249 249 series->add(100, 100);
250 250
251 251 ChartDataSet dataSet;
252 252
253 253 QCOMPARE(dataSet.domainIndex(), 0);
254 254
255 255 dataSet.addSeries(series);
256 256
257 257 Domain domain2 = dataSet.domain(dataSet.axisY());
258 258
259 259 QVERIFY(domain2 == domain1);
260 260
261 261 QList<Domain> domains;
262 262
263 263 domains << domain1;
264 264
265 265 for (int i = 0; i < indexCount; i++) {
266 266 dataSet.addDomain(QRect(0, 0, 10, 10), QRect(0, 0, 100, 100));
267 267 domains << dataSet.domain(dataSet.axisY());
268 268 }
269 269
270 270 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
271 271 QSignalSpy spy1(&dataSet,
272 272 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
273 273 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
274 274 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
275 275 QSignalSpy spy4(&dataSet,
276 276 SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
277 277 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
278 278
279 279 dataSet.clearDomains(indexCount);
280 280
281 281 QCOMPARE(dataSet.domainIndex(), indexCount);
282 282
283 283 domain2 = dataSet.domain(dataSet.axisY());
284 284
285 285 QVERIFY(domain2 == domains.at(indexCount));
286 286
287 287 QCOMPARE(spy0.count(), 0);
288 288 QCOMPARE(spy1.count(), 0);
289 289 QCOMPARE(spy2.count(), 0);
290 290 QCOMPARE(spy3.count(), 0);
291 291 QCOMPARE(spy4.count(), 0);
292 292 QCOMPARE(spy5.count(), 0);
293 293 }
294 294
295 295 void tst_ChartDataSet::domain_data()
296 296 {
297 297 QTest::addColumn<Domain>("domain1");
298 298 QTest::addColumn<Domain>("domain2");
299 299 QTest::addColumn<Domain>("domain3");
300 300 QTest::addColumn<Domain>("domain");
301 301 QTest::newRow("Domain(0,10,0,10)") << Domain(0, 10, 0, 10)
302 302 << Domain(0, 5, 0, 5) << Domain(0, 3, 0, 3) << Domain(0, 10, 0, 10);
303 303 QTest::newRow("Domain(-1,11,0,11)") << Domain(-1, 10, 0, 10)
304 304 << Domain(0, 11, 0, 11) << Domain(0, 3, 0, 3)
305 305 << Domain(-1, 11, 0, 11);
306 306 QTest::newRow("Domain(-5,5,1,8)") << Domain(-5, 0, 1, 1)
307 307 << Domain(0, 5, 0, 8) << Domain(1, 2, 1, 2) << Domain(-5, 5, 0, 8);
308 308 }
309 309
310 310 void tst_ChartDataSet::domain()
311 311 {
312 312 QFETCH(Domain, domain1);
313 313 QFETCH(Domain, domain2);
314 314 QFETCH(Domain, domain3);
315 315 QFETCH(Domain, domain);
316 316
317 317 ChartDataSet dataSet;
318 318 QLineChartSeries* series1 = new QLineChartSeries();
319 319 series1->add(domain1.m_minX, domain1.m_minY);
320 320 series1->add(domain1.m_maxX, domain1.m_maxY);
321 321 QLineChartSeries* series2 = new QLineChartSeries();
322 322 series2->add(domain2.m_minX, domain2.m_minY);
323 323 series2->add(domain2.m_maxX, domain2.m_maxY);
324 324 QLineChartSeries* series3 = new QLineChartSeries();
325 325 series3->add(domain3.m_minX, domain3.m_minY);
326 326 series3->add(domain3.m_maxX, domain3.m_maxY);
327 327
328 328 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
329 329 QSignalSpy spy1(&dataSet,
330 330 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
331 331 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
332 332 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
333 333 QSignalSpy spy4(&dataSet,
334 334 SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
335 335 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
336 336
337 337 dataSet.addSeries(series1);
338 338 dataSet.addSeries(series2);
339 339 dataSet.addSeries(series3);
340 340
341 341 QCOMPARE(dataSet.domainIndex(), 0);
342 342 QVERIFY2(dataSet.domain(dataSet.axisY()) == domain, "Domain not equal");
343 343
344 344 QCOMPARE(spy0.count(), 2);
345 345 QCOMPARE(spy1.count(), 6);
346 346 QCOMPARE(spy2.count(), 0);
347 347 QCOMPARE(spy3.count(), 3);
348 348 QCOMPARE(spy4.count(), 3);
349 349 QCOMPARE(spy5.count(), 0);
350 350 }
351 351
352 352 void tst_ChartDataSet::nextpreviousDomain_data()
353 353 {
354 354 QTest::addColumn<QRectF>("rect");
355 355 QTest::addColumn<QRectF>("viewport");
356 356 QTest::newRow("400x400,1000x1000") << QRectF(200, 200, 600, 600)
357 357 << QRectF(0, 0, 1000, 1000);
358 358 QTest::newRow("600x600,1000x1000") << QRectF(100, 100, 700, 700)
359 359 << QRectF(0, 0, 1000, 1000);
360 360 QTest::newRow("200x200,1000x1000") << QRectF(400, 400, 600, 600)
361 361 << QRectF(0, 0, 1000, 1000);
362 362 }
363 363
364 364 void tst_ChartDataSet::nextpreviousDomain()
365 365 {
366 366
367 367 QFETCH(QRectF, rect);
368 368 QFETCH(QRectF, viewport);
369 369
370 370 ChartDataSet dataSet;
371 371
372 372 Domain domain1(0, 1000, 0, 1000);
373 373 QLineChartSeries* series = new QLineChartSeries();
374 374 series->add(0, 0);
375 375 series->add(1000, 1000);
376 376
377 377 dataSet.addSeries(series);
378 378
379 379 QCOMPARE(dataSet.domainIndex(), 0);
380 380
381 381 Domain domain2 = dataSet.domain(dataSet.axisY());
382 382 QVERIFY(domain1 == domain2);
383 383
384 384 dataSet.addDomain(rect, viewport);
385 385 QCOMPARE(dataSet.domainIndex(), 1);
386 386 Domain domain3 = dataSet.domain(dataSet.axisY());
387 387 Domain domain4 = domain1.subDomain(rect, viewport.width(),
388 388 viewport.height());
389 389 QVERIFY(domain3 == domain4);
390 390
391 391 dataSet.addDomain(rect, viewport);
392 392 QCOMPARE(dataSet.domainIndex(), 2);
393 393 Domain domain5 = dataSet.domain(dataSet.axisY());
394 394 Domain domain6 = domain3.subDomain(rect, viewport.width(),
395 395 viewport.height());
396 396 QVERIFY(domain5 == domain6);
397 397
398 398 dataSet.addDomain(rect, viewport);
399 399 QCOMPARE(dataSet.domainIndex(), 3);
400 400 Domain domain7 = dataSet.domain(dataSet.axisY());
401 401 Domain domain8 = domain5.subDomain(rect, viewport.width(),
402 402 viewport.height());
403 403 QVERIFY(domain7 == domain8);
404 404
405 405 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
406 406 QSignalSpy spy1(&dataSet,
407 407 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
408 408 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
409 409 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
410 410 QSignalSpy spy4(&dataSet,
411 411 SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
412 412 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
413 413
414 414 Domain domain;
415 415
416 416 bool previous = dataSet.previousDomain();
417 417 QCOMPARE(previous, true);
418 418 QCOMPARE(dataSet.domainIndex(), 2);
419 419 domain = dataSet.domain(dataSet.axisY());
420 420 QVERIFY(domain == domain5);
421 421 previous = dataSet.previousDomain();
422 422 QCOMPARE(previous, true);
423 423 QCOMPARE(dataSet.domainIndex(), 1);
424 424 domain = dataSet.domain(dataSet.axisY());
425 425 QVERIFY(domain == domain3);
426 426 previous = dataSet.previousDomain();
427 427 QCOMPARE(previous, true);
428 428 QCOMPARE(dataSet.domainIndex(), 0);
429 429 domain = dataSet.domain(dataSet.axisY());
430 430 QVERIFY(domain == domain1);
431 431 previous = dataSet.previousDomain();
432 432 QCOMPARE(previous, false);
433 433 QCOMPARE(dataSet.domainIndex(), 0);
434 434 domain = dataSet.domain(dataSet.axisY());
435 435 QVERIFY(domain == domain1);
436 436
437 437 bool next = dataSet.nextDomain();
438 438 QCOMPARE(next, true);
439 439 QCOMPARE(dataSet.domainIndex(),1);
440 440 next = dataSet.nextDomain();
441 441 QCOMPARE(next, true);
442 442 QCOMPARE(dataSet.domainIndex(),2);
443 443 next = dataSet.nextDomain();
444 444 QCOMPARE(next, true);
445 445 QCOMPARE(dataSet.domainIndex(),3);
446 446 next = dataSet.nextDomain();
447 447 QCOMPARE(next, false);
448 448 QCOMPARE(dataSet.domainIndex(),3);
449 449
450 450
451 451 QCOMPARE(spy0.count(), 0);
452 452 QCOMPARE(spy1.count(), 12);
453 453 QCOMPARE(spy2.count(), 0);
454 454 QCOMPARE(spy3.count(), 0);
455 455 QCOMPARE(spy4.count(), 6);
456 456 QCOMPARE(spy5.count(), 0);
457 457 }
458 458
459 459 void tst_ChartDataSet::removeSeries_data()
460 460 {
461 461 QTest::addColumn<int>("seriesCount");
462 462 QTest::addColumn<int>("axisYCount");
463 463 QTest::newRow("2 series, default axis") << 2 << 0;
464 464 QTest::newRow("2 series, 2 new axis") << 2 << 2;
465 465 QTest::newRow("2 series, 1 new axis") << 2 << 2;
466 466 QTest::newRow("3 series, 3 new axis") << 3 << 3;
467 467 QTest::newRow("3 series, 2 new axis") << 3 << 2;
468 468 QTest::newRow("3 series, 1 new axis") << 3 << 1;
469 469 }
470 470
471 471 void tst_ChartDataSet::removeSeries()
472 472 {
473 473 QFETCH(int, seriesCount);
474 474 QFETCH(int, axisYCount);
475 475
476 476 ChartDataSet dataSet;
477 477
478 478 QList<QChartAxis*> axisList;
479 479 QList<QChartSeries*> seriesList;
480 480
481 481 for (int i = 0; i < axisYCount; i++) {
482 482 QChartAxis* axis = new QChartAxis();
483 483 axisList << axis;
484 484 }
485 485
486 486 QList<QChartAxis*>::iterator iterator = axisList.begin();
487 487
488 488 for (int i = 0; i < seriesCount; i++) {
489 489 QChartAxis* axisY = 0;
490 490 QLineChartSeries* series = new QLineChartSeries();
491 491 if (iterator != axisList.end()) {
492 492 axisY = *iterator;
493 493 iterator++;
494 494 } else if (axisList.count() > 0) {
495 495 iterator--;
496 496 axisY = *iterator;
497 497 iterator++;
498 498 }
499 499 dataSet.addSeries(series, axisY);
500 500 seriesList << series;
501 501 }
502 502
503 503 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
504 504 QSignalSpy spy1(&dataSet,
505 505 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
506 506 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
507 507 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
508 508 QSignalSpy spy4(&dataSet,
509 509 SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
510 510 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
511 511
512 512 for (int i = 0; i < seriesCount; i++) {
513 513 dataSet.removeSeries(seriesList.at(i));
514 514 }
515 515
516 //default axis
517 if (axisYCount == 0)
518 axisYCount++;
519
516 520 QCOMPARE(spy0.count(), 0);
517 521 QCOMPARE(spy1.count(), 0);
518 522 QCOMPARE(spy2.count(), axisYCount);
519 523 QCOMPARE(spy3.count(), 0);
520 524 QCOMPARE(spy4.count(), 0);
521 525 QCOMPARE(spy5.count(), seriesCount);
522 526
523 527 QCOMPARE(dataSet.domainIndex(), 0);
528
529 qDeleteAll(seriesList);
524 530 }
525 531
532 void tst_ChartDataSet::removeAllSeries_data()
533 {
534 QTest::addColumn<int>("seriesCount");
535 QTest::addColumn<int>("axisYCount");
536 QTest::newRow("2 series, default axis") << 2 << 0;
537 QTest::newRow("2 series, 2 new axis") << 2 << 2;
538 QTest::newRow("2 series, 1 new axis") << 2 << 2;
539 QTest::newRow("3 series, 3 new axis") << 3 << 3;
540 QTest::newRow("3 series, 2 new axis") << 3 << 2;
541 QTest::newRow("3 series, 1 new axis") << 3 << 1;
542 }
543
544 void tst_ChartDataSet::removeAllSeries()
545 {
546 QFETCH(int, seriesCount);
547 QFETCH(int, axisYCount);
548
549 ChartDataSet dataSet;
550
551 QList<QChartAxis*> axisList;
552
553 for (int i = 0; i < axisYCount; i++) {
554 QChartAxis* axis = new QChartAxis();
555 axisList << axis;
556 }
557
558 QList<QChartAxis*>::iterator iterator = axisList.begin();
559
560 for (int i = 0; i < seriesCount; i++) {
561 QChartAxis* axisY = 0;
562 QLineChartSeries* series = new QLineChartSeries();
563 if (iterator != axisList.end()) {
564 axisY = *iterator;
565 iterator++;
566 } else if (axisList.count() > 0) {
567 iterator--;
568 axisY = *iterator;
569 iterator++;
570 }
571 dataSet.addSeries(series, axisY);
572 }
573
574 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
575 QSignalSpy spy1(&dataSet, SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
576 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
577 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
578 QSignalSpy spy4(&dataSet, SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
579 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
580
581 dataSet.removeAllSeries();
582 //default axis
583 if (axisYCount == 0)
584 axisYCount++;
585
586 QCOMPARE(spy0.count(), 0);
587 QCOMPARE(spy1.count(), 0);
588 QCOMPARE(spy2.count(), axisYCount);
589 QCOMPARE(spy3.count(), 0);
590 QCOMPARE(spy4.count(), 0);
591 QCOMPARE(spy5.count(), seriesCount);
592
593 QCOMPARE(dataSet.domainIndex(), 0);
594 }
595
596
526 597 QTEST_MAIN(tst_ChartDataSet)
527 598 #include "tst_chartdataset.moc"
528 599
General Comments 0
You need to be logged in to leave comments. Login now