##// END OF EJS Templates
More model related tests for Pie
Marek Rosa -
r1182:b3b206fa2a47
parent child
Show More
@@ -1,398 +1,477
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include <QtTest/QtTest>
21 #include <QtTest/QtTest>
22 #include <qchartview.h>
22 #include <qchartview.h>
23 #include <qchart.h>
23 #include <qchart.h>
24 #include <qpieseries.h>
24 #include <qpieseries.h>
25 #include <qpieslice.h>
25 #include <qpieslice.h>
26 #include <qpiemodelmapper.h>
26 #include <qpiemodelmapper.h>
27 #include <QStandardItemModel>
27 #include <QStandardItemModel>
28 #include <tst_definitions.h>
28 #include <tst_definitions.h>
29
29
30 QTCOMMERCIALCHART_USE_NAMESPACE
30 QTCOMMERCIALCHART_USE_NAMESPACE
31
31
32 Q_DECLARE_METATYPE(QPieSlice*)
32 Q_DECLARE_METATYPE(QPieSlice*)
33
33
34 class tst_qpieseries : public QObject
34 class tst_qpieseries : public QObject
35 {
35 {
36 Q_OBJECT
36 Q_OBJECT
37
37
38 public slots:
38 public slots:
39 void initTestCase();
39 void initTestCase();
40 void cleanupTestCase();
40 void cleanupTestCase();
41 void init();
41 void init();
42 void cleanup();
42 void cleanup();
43
43
44 private slots:
44 private slots:
45 void construction();
45 void construction();
46 void append();
46 void append();
47 void insert();
47 void insert();
48 void remove();
48 void remove();
49 void calculatedValues();
49 void calculatedValues();
50 void clickedSignal();
50 void clickedSignal();
51 void hoverSignal();
51 void hoverSignal();
52 void model();
52 void model();
53 void modelCustomMap();
53 void modelCustomMap();
54 void modelUpdate();
54
55
55 private:
56 private:
56 void verifyCalculatedData(const QPieSeries &series, bool *ok);
57 void verifyCalculatedData(const QPieSeries &series, bool *ok);
57
58
58 private:
59 private:
59
60
60 };
61 };
61
62
62 void tst_qpieseries::initTestCase()
63 void tst_qpieseries::initTestCase()
63 {
64 {
64 qRegisterMetaType<QPieSlice*>("QPieSlice*");
65 qRegisterMetaType<QPieSlice*>("QPieSlice*");
65 }
66 }
66
67
67 void tst_qpieseries::cleanupTestCase()
68 void tst_qpieseries::cleanupTestCase()
68 {
69 {
69 }
70 }
70
71
71 void tst_qpieseries::init()
72 void tst_qpieseries::init()
72 {
73 {
73
74
74 }
75 }
75
76
76 void tst_qpieseries::cleanup()
77 void tst_qpieseries::cleanup()
77 {
78 {
78
79
79 }
80 }
80
81
81 void tst_qpieseries::construction()
82 void tst_qpieseries::construction()
82 {
83 {
83 // verify default values
84 // verify default values
84 QPieSeries s;
85 QPieSeries s;
85 QVERIFY(s.type() == QAbstractSeries::SeriesTypePie);
86 QVERIFY(s.type() == QAbstractSeries::SeriesTypePie);
86 QVERIFY(s.count() == 0);
87 QVERIFY(s.count() == 0);
87 QVERIFY(s.isEmpty());
88 QVERIFY(s.isEmpty());
88 QCOMPARE(s.sum(), 0.0);
89 QCOMPARE(s.sum(), 0.0);
89 QCOMPARE(s.horizontalPosition(), 0.5);
90 QCOMPARE(s.horizontalPosition(), 0.5);
90 QCOMPARE(s.verticalPosition(), 0.5);
91 QCOMPARE(s.verticalPosition(), 0.5);
91 QCOMPARE(s.pieSize(), 0.7);
92 QCOMPARE(s.pieSize(), 0.7);
92 QCOMPARE(s.pieStartAngle(), 0.0);
93 QCOMPARE(s.pieStartAngle(), 0.0);
93 QCOMPARE(s.pieEndAngle(), 360.0);
94 QCOMPARE(s.pieEndAngle(), 360.0);
94 }
95 }
95
96
96 void tst_qpieseries::append()
97 void tst_qpieseries::append()
97 {
98 {
98 QPieSeries s;
99 QPieSeries s;
99
100
100 // append pointer
101 // append pointer
101 QPieSlice *slice1 = 0;
102 QPieSlice *slice1 = 0;
102 QVERIFY(!s.append(slice1));
103 QVERIFY(!s.append(slice1));
103 slice1 = new QPieSlice(1, "slice 1");
104 slice1 = new QPieSlice(1, "slice 1");
104 QVERIFY(s.append(slice1));
105 QVERIFY(s.append(slice1));
105 QVERIFY(!s.append(slice1));
106 QVERIFY(!s.append(slice1));
106 QCOMPARE(s.count(), 1);
107 QCOMPARE(s.count(), 1);
107
108
108 // append pointer list
109 // append pointer list
109 QList<QPieSlice *> list;
110 QList<QPieSlice *> list;
110 QVERIFY(!s.append(list));
111 QVERIFY(!s.append(list));
111 list << (QPieSlice *) 0;
112 list << (QPieSlice *) 0;
112 QVERIFY(!s.append(list));
113 QVERIFY(!s.append(list));
113 list.clear();
114 list.clear();
114 list << new QPieSlice(2, "slice 2");
115 list << new QPieSlice(2, "slice 2");
115 list << new QPieSlice(3, "slice 3");
116 list << new QPieSlice(3, "slice 3");
116 QVERIFY(s.append(list));
117 QVERIFY(s.append(list));
117 QVERIFY(!s.append(list));
118 QVERIFY(!s.append(list));
118 QCOMPARE(s.count(), 3);
119 QCOMPARE(s.count(), 3);
119
120
120 // append operator
121 // append operator
121 s << new QPieSlice(4, "slice 4");
122 s << new QPieSlice(4, "slice 4");
122 s << slice1; // fails because already added
123 s << slice1; // fails because already added
123 QCOMPARE(s.count(), 4);
124 QCOMPARE(s.count(), 4);
124
125
125 // append with params
126 // append with params
126 QPieSlice *slice5 = s.append(5, "slice 5");
127 QPieSlice *slice5 = s.append(5, "slice 5");
127 QVERIFY(slice5 != 0);
128 QVERIFY(slice5 != 0);
128 QCOMPARE(slice5->value(), 5.0);
129 QCOMPARE(slice5->value(), 5.0);
129 QCOMPARE(slice5->label(), QString("slice 5"));
130 QCOMPARE(slice5->label(), QString("slice 5"));
130 QCOMPARE(s.count(), 5);
131 QCOMPARE(s.count(), 5);
131
132
132 // check slices
133 // check slices
133 QVERIFY(!s.isEmpty());
134 QVERIFY(!s.isEmpty());
134 for (int i=0; i<s.count(); i++) {
135 for (int i=0; i<s.count(); i++) {
135 QCOMPARE(s.slices().at(i)->value(), (qreal) i+1);
136 QCOMPARE(s.slices().at(i)->value(), (qreal) i+1);
136 QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1));
137 QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1));
137 }
138 }
138 }
139 }
139
140
140 void tst_qpieseries::insert()
141 void tst_qpieseries::insert()
141 {
142 {
142 QPieSeries s;
143 QPieSeries s;
143
144
144 // insert one slice
145 // insert one slice
145 QPieSlice *slice1 = 0;
146 QPieSlice *slice1 = 0;
146 QVERIFY(!s.insert(0, slice1));
147 QVERIFY(!s.insert(0, slice1));
147 slice1 = new QPieSlice(1, "slice 1");
148 slice1 = new QPieSlice(1, "slice 1");
148 QVERIFY(!s.insert(-1, slice1));
149 QVERIFY(!s.insert(-1, slice1));
149 QVERIFY(!s.insert(5, slice1));
150 QVERIFY(!s.insert(5, slice1));
150 QVERIFY(s.insert(0, slice1));
151 QVERIFY(s.insert(0, slice1));
151 QVERIFY(!s.insert(0, slice1));
152 QVERIFY(!s.insert(0, slice1));
152 QCOMPARE(s.count(), 1);
153 QCOMPARE(s.count(), 1);
153
154
154 // add some more slices
155 // add some more slices
155 s.append(2, "slice 2");
156 s.append(2, "slice 2");
156 s.append(4, "slice 4");
157 s.append(4, "slice 4");
157 QCOMPARE(s.count(), 3);
158 QCOMPARE(s.count(), 3);
158
159
159 // insert between slices
160 // insert between slices
160 s.insert(2, new QPieSlice(3, "slice 3"));
161 s.insert(2, new QPieSlice(3, "slice 3"));
161 QCOMPARE(s.count(), 4);
162 QCOMPARE(s.count(), 4);
162
163
163 // check slices
164 // check slices
164 for (int i=0; i<s.count(); i++) {
165 for (int i=0; i<s.count(); i++) {
165 QCOMPARE(s.slices().at(i)->value(), (qreal) i+1);
166 QCOMPARE(s.slices().at(i)->value(), (qreal) i+1);
166 QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1));
167 QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1));
167 }
168 }
168 }
169 }
169
170
170 void tst_qpieseries::remove()
171 void tst_qpieseries::remove()
171 {
172 {
172 QPieSeries s;
173 QPieSeries s;
173
174
174 // add some slices
175 // add some slices
175 QPieSlice *slice1 = s.append(1, "slice 1");
176 QPieSlice *slice1 = s.append(1, "slice 1");
176 QPieSlice *slice2 = s.append(2, "slice 2");
177 QPieSlice *slice2 = s.append(2, "slice 2");
177 QPieSlice *slice3 = s.append(3, "slice 3");
178 QPieSlice *slice3 = s.append(3, "slice 3");
178 QSignalSpy spy1(slice1, SIGNAL(destroyed()));
179 QSignalSpy spy1(slice1, SIGNAL(destroyed()));
179 QSignalSpy spy2(slice2, SIGNAL(destroyed()));
180 QSignalSpy spy2(slice2, SIGNAL(destroyed()));
180 QSignalSpy spy3(slice3, SIGNAL(destroyed()));
181 QSignalSpy spy3(slice3, SIGNAL(destroyed()));
181 QCOMPARE(s.count(), 3);
182 QCOMPARE(s.count(), 3);
182
183
183 // null pointer remove
184 // null pointer remove
184 QVERIFY(!s.remove(0));
185 QVERIFY(!s.remove(0));
185
186
186 // remove first
187 // remove first
187 QVERIFY(s.remove(slice1));
188 QVERIFY(s.remove(slice1));
188 QVERIFY(!s.remove(slice1));
189 QVERIFY(!s.remove(slice1));
189 QCOMPARE(s.count(), 2);
190 QCOMPARE(s.count(), 2);
190 QCOMPARE(s.slices().at(0)->label(), slice2->label());
191 QCOMPARE(s.slices().at(0)->label(), slice2->label());
191
192
192 // remove all
193 // remove all
193 s.clear();
194 s.clear();
194 QVERIFY(s.isEmpty());
195 QVERIFY(s.isEmpty());
195 QVERIFY(s.slices().isEmpty());
196 QVERIFY(s.slices().isEmpty());
196 QCOMPARE(s.count(), 0);
197 QCOMPARE(s.count(), 0);
197
198
198 // check that slices were actually destroyed
199 // check that slices were actually destroyed
199 TRY_COMPARE(spy1.count(), 1);
200 TRY_COMPARE(spy1.count(), 1);
200 TRY_COMPARE(spy2.count(), 1);
201 TRY_COMPARE(spy2.count(), 1);
201 TRY_COMPARE(spy3.count(), 1);
202 TRY_COMPARE(spy3.count(), 1);
202 }
203 }
203
204
204 void tst_qpieseries::calculatedValues()
205 void tst_qpieseries::calculatedValues()
205 {
206 {
206 bool ok;
207 bool ok;
207 QPieSeries s;
208 QPieSeries s;
208
209
209 // add a slice
210 // add a slice
210 QPieSlice *slice1 = s.append(1, "slice 1");
211 QPieSlice *slice1 = s.append(1, "slice 1");
211 verifyCalculatedData(s, &ok);
212 verifyCalculatedData(s, &ok);
212 if (!ok)
213 if (!ok)
213 return;
214 return;
214
215
215 // add some more slices
216 // add some more slices
216 QList<QPieSlice *> list;
217 QList<QPieSlice *> list;
217 list << new QPieSlice(2, "slice 2");
218 list << new QPieSlice(2, "slice 2");
218 list << new QPieSlice(3, "slice 3");
219 list << new QPieSlice(3, "slice 3");
219 s.append(list);
220 s.append(list);
220 verifyCalculatedData(s, &ok);
221 verifyCalculatedData(s, &ok);
221 if (!ok)
222 if (!ok)
222 return;
223 return;
223
224
224 // remove a slice
225 // remove a slice
225 s.remove(slice1);
226 s.remove(slice1);
226 verifyCalculatedData(s, &ok);
227 verifyCalculatedData(s, &ok);
227 if (!ok)
228 if (!ok)
228 return;
229 return;
229
230
230 // insert a slice
231 // insert a slice
231 s.insert(0, new QPieSlice(1, "Slice 4"));
232 s.insert(0, new QPieSlice(1, "Slice 4"));
232 verifyCalculatedData(s, &ok);
233 verifyCalculatedData(s, &ok);
233 if (!ok)
234 if (!ok)
234 return;
235 return;
235
236
236 // clear all
237 // clear all
237 s.clear();
238 s.clear();
238 verifyCalculatedData(s, &ok);
239 verifyCalculatedData(s, &ok);
239 }
240 }
240
241
241 void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok)
242 void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok)
242 {
243 {
243 *ok = false;
244 *ok = false;
244
245
245 qreal sum = 0;
246 qreal sum = 0;
246 foreach (const QPieSlice *slice, series.slices())
247 foreach (const QPieSlice *slice, series.slices())
247 sum += slice->value();
248 sum += slice->value();
248 QCOMPARE(series.sum(), sum);
249 QCOMPARE(series.sum(), sum);
249
250
250 qreal startAngle = series.pieStartAngle();
251 qreal startAngle = series.pieStartAngle();
251 qreal pieAngleSpan = series.pieEndAngle() - series.pieStartAngle();
252 qreal pieAngleSpan = series.pieEndAngle() - series.pieStartAngle();
252 foreach (const QPieSlice *slice, series.slices()) {
253 foreach (const QPieSlice *slice, series.slices()) {
253 qreal ratio = slice->value() / sum;
254 qreal ratio = slice->value() / sum;
254 qreal sliceSpan = pieAngleSpan * ratio;
255 qreal sliceSpan = pieAngleSpan * ratio;
255 QCOMPARE(slice->startAngle(), startAngle);
256 QCOMPARE(slice->startAngle(), startAngle);
256 QCOMPARE(slice->endAngle(), startAngle + sliceSpan);
257 QCOMPARE(slice->endAngle(), startAngle + sliceSpan);
257 QCOMPARE(slice->percentage(), ratio);
258 QCOMPARE(slice->percentage(), ratio);
258 startAngle += sliceSpan;
259 startAngle += sliceSpan;
259 }
260 }
260
261
261 if (!series.isEmpty())
262 if (!series.isEmpty())
262 QCOMPARE(series.slices().last()->endAngle(), series.pieEndAngle());
263 QCOMPARE(series.slices().last()->endAngle(), series.pieEndAngle());
263
264
264 *ok = true;
265 *ok = true;
265 }
266 }
266
267
267
268
268 void tst_qpieseries::clickedSignal()
269 void tst_qpieseries::clickedSignal()
269 {
270 {
270 // create a pie series
271 // create a pie series
271 QPieSeries *series = new QPieSeries();
272 QPieSeries *series = new QPieSeries();
272 series->setPieSize(1.0);
273 series->setPieSize(1.0);
273 QPieSlice *s1 = series->append(1, "slice 1");
274 QPieSlice *s1 = series->append(1, "slice 1");
274 series->append(2, "slice 2");
275 series->append(2, "slice 2");
275 series->append(3, "slice 3");
276 series->append(3, "slice 3");
276 QSignalSpy clickSpy1(series, SIGNAL(clicked(QPieSlice*)));
277 QSignalSpy clickSpy1(series, SIGNAL(clicked(QPieSlice*)));
277
278
278 // add series to the chart
279 // add series to the chart
279 QChartView view(new QChart());
280 QChartView view(new QChart());
280 view.resize(200, 200);
281 view.resize(200, 200);
281 view.chart()->addSeries(series);
282 view.chart()->addSeries(series);
282 view.show();
283 view.show();
283 QTest::qWaitForWindowShown(&view);
284 QTest::qWaitForWindowShown(&view);
284
285
285 // simulate clicks
286 // simulate clicks
286 // pie rectangle: QRectF(60,60 121x121)
287 // pie rectangle: QRectF(60,60 121x121)
287 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(139, 85)); // inside slice 1
288 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(139, 85)); // inside slice 1
288 TRY_COMPARE(clickSpy1.count(), 1);
289 TRY_COMPARE(clickSpy1.count(), 1);
289 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy1.at(0).at(0)), s1);
290 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy1.at(0).at(0)), s1);
290 }
291 }
291
292
292 void tst_qpieseries::hoverSignal()
293 void tst_qpieseries::hoverSignal()
293 {
294 {
294 // create a pie series
295 // create a pie series
295 QPieSeries *series = new QPieSeries();
296 QPieSeries *series = new QPieSeries();
296 series->setPieSize(1.0);
297 series->setPieSize(1.0);
297 QPieSlice *s1 = series->append(1, "slice 1");
298 QPieSlice *s1 = series->append(1, "slice 1");
298 series->append(2, "slice 2");
299 series->append(2, "slice 2");
299 series->append(3, "slice 3");
300 series->append(3, "slice 3");
300
301
301 // add series to the chart
302 // add series to the chart
302 QChartView view(new QChart());
303 QChartView view(new QChart());
303 view.resize(200, 200);
304 view.resize(200, 200);
304 view.chart()->addSeries(series);
305 view.chart()->addSeries(series);
305 view.show();
306 view.show();
306 QTest::qWaitForWindowShown(&view);
307 QTest::qWaitForWindowShown(&view);
307
308
308 // first move to right top corner
309 // first move to right top corner
309 QTest::mouseMove(view.viewport(), QPoint(200, 0));
310 QTest::mouseMove(view.viewport(), QPoint(200, 0));
310 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
311 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
311
312
312 // move inside the slice
313 // move inside the slice
313 // pie rectangle: QRectF(60,60 121x121)
314 // pie rectangle: QRectF(60,60 121x121)
314 QSignalSpy hoverSpy(series, SIGNAL(hovered(QPieSlice*,bool)));
315 QSignalSpy hoverSpy(series, SIGNAL(hovered(QPieSlice*,bool)));
315 QTest::mouseMove(view.viewport(), QPoint(139, 85));
316 QTest::mouseMove(view.viewport(), QPoint(139, 85));
316 TRY_COMPARE(hoverSpy.count(), 1);
317 TRY_COMPARE(hoverSpy.count(), 1);
317 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(0).at(0)), s1);
318 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(0).at(0)), s1);
318 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(1)), true);
319 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(1)), true);
319
320
320 // move outside the slice
321 // move outside the slice
321 QTest::mouseMove(view.viewport(), QPoint(200, 0));
322 QTest::mouseMove(view.viewport(), QPoint(200, 0));
322 TRY_COMPARE(hoverSpy.count(), 2);
323 TRY_COMPARE(hoverSpy.count(), 2);
323 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(1).at(0)), s1);
324 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(1).at(0)), s1);
324 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(1)), false);
325 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(1)), false);
325 }
326 }
326
327
327 void tst_qpieseries::model()
328 void tst_qpieseries::model()
328 {
329 {
329 QPieSeries *series = new QPieSeries;
330 QPieSeries *series = new QPieSeries;
331 QChart *chart = new QChart;
332 chart->addSeries(series);
333 QChartView *chartView = new QChartView(chart);
334 chartView->show();
335
330 QStandardItemModel *stdModel = new QStandardItemModel(0, 2);
336 QStandardItemModel *stdModel = new QStandardItemModel(0, 2);
331 series->setModel(stdModel);
337 series->setModel(stdModel);
332 QVERIFY2((series->model()) == stdModel, "Model should be stdModel");
338 QVERIFY2((series->model()) == stdModel, "Model should be stdModel");
333
339
334 int rowCount = 3;
340 int rowCount = 3;
335 for (int row = 0; row < rowCount; ++row) {
341 for (int row = 0; row < rowCount; ++row) {
336 for (int column = 0; column < 2; column++) {
342 for (int column = 0; column < 2; column++) {
337 QStandardItem *item = new QStandardItem(row * column);
343 QStandardItem *item = new QStandardItem(row * column);
338 stdModel->setItem(row, column, item);
344 stdModel->setItem(row, column, item);
339 }
345 }
340 }
346 }
341
347
342 // data has been added to the model, but mapper is not set the number of slices should still be 0
348 // data has been added to the model, but mapper is not set the number of slices should still be 0
343 QVERIFY2(series->slices().count() == 0, "Mapper has not been set, so the number of slices should be 0");
349 QVERIFY2(series->slices().count() == 0, "Mapper has not been set, so the number of slices should be 0");
344
350
345 // set the mapper
351 // set the mapper
346 QPieModelMapper *mapper = new QPieModelMapper;
352 QPieModelMapper *mapper = new QPieModelMapper;
347 mapper->setMapValues(0);
353 mapper->setMapValues(0);
348 mapper->setMapLabels(0);
354 mapper->setMapLabels(0);
349 series->setModelMapper(mapper); // this should cause the Pie to get initialized from the model, since there is now both the model and the mapper defined
355 series->setModelMapper(mapper); // this should cause the Pie to get initialized from the model, since there is now both the model and the mapper defined
350 QCOMPARE(series->slices().count(), rowCount);
356 QCOMPARE(series->slices().count(), rowCount);
351
357
358 // set the mappings to be outside of the model
359 mapper->setMapLabels(5);
360 mapper->setMapValues(4);
361 QCOMPARE(series->slices().count(), 0); // Mappings are invalid, so the number of slices should be 0
362
363 // set back to correct ones
364 mapper->setMapValues(0);
365 mapper->setMapLabels(0);
366 QCOMPARE(series->slices().count(), rowCount);
367
352 // reset the mappings
368 // reset the mappings
353 mapper->reset();
369 mapper->reset();
354 QCOMPARE(series->slices().count(), 0); // Mappings have been reset and are invalid, so the number of slices should be 0
370 QCOMPARE(series->slices().count(), 0); // Mappings have been reset and are invalid, so the number of slices should be 0
355
371
356 // unset the model and the mapper
372 // unset the model and the mapper
357 series->setModel(0);
373 series->setModel(0);
358 series->setModelMapper(0);
374 series->setModelMapper(0);
359 QVERIFY(series->model() == 0); // Model should be unset
375 QVERIFY(series->model() == 0); // Model should be unset
360 QVERIFY(series->modelMapper() == 0); // Model mapper should be unset
376 QVERIFY(series->modelMapper() == 0); // Model mapper should be unset
361 }
377 }
362
378
363 void tst_qpieseries::modelCustomMap()
379 void tst_qpieseries::modelCustomMap()
364 {
380 {
365 int rowCount = 12;
381 int rowCount = 12;
366 QStandardItemModel *stdModel = new QStandardItemModel(0, 2);
382 int columnCount = 3;
383 QStandardItemModel *stdModel = new QStandardItemModel(0, 3);
367 for (int row = 0; row < rowCount; ++row) {
384 for (int row = 0; row < rowCount; ++row) {
368 for (int column = 0; column < 2; column++) {
385 for (int column = 0; column < 2; column++) {
369 QStandardItem *item = new QStandardItem(row * column);
386 QStandardItem *item = new QStandardItem(row * column);
370 stdModel->setItem(row, column, item);
387 stdModel->setItem(row, column, item);
371 }
388 }
372 }
389 }
373
390
374 QPieSeries *series = new QPieSeries;
391 QPieSeries *series = new QPieSeries;
392 QChart *chart = new QChart;
393 chart->addSeries(series);
394 QChartView *chartView = new QChartView(chart);
395 chartView->show();
375 series->setModel(stdModel);
396 series->setModel(stdModel);
376
397
377 QPieModelMapper *mapper = new QPieModelMapper;
398 QPieModelMapper *mapper = new QPieModelMapper;
378 mapper->setMapValues(0);
399 mapper->setMapValues(0);
379 mapper->setMapLabels(0);
400 mapper->setMapLabels(0);
380 series->setModelMapper(mapper);
401 series->setModelMapper(mapper);
381 QCOMPARE(series->slices().count(), rowCount);
402 QCOMPARE(series->slices().count(), rowCount);
382
403
404 // lets change the orientation to horizontal
405 mapper->setOrientation(Qt::Horizontal);
406 QCOMPARE(series->slices().count(), columnCount);
407
408 // change it back to vertical
409 mapper->setOrientation(Qt::Vertical);
410 QCOMPARE(series->slices().count(), rowCount);
411
383 // lets customize the mapping
412 // lets customize the mapping
384 int first = 3;
413 int first = 3;
385 mapper->setFirst(first);
414 mapper->setFirst(first);
386 QCOMPARE(series->slices().count(), rowCount - first);
415 QCOMPARE(series->slices().count(), rowCount - first);
387 int count = 7;
416 int count = 7;
388 mapper->setCount(count);
417 mapper->setCount(count);
389 QCOMPARE(series->slices().count(), count);
418 QCOMPARE(series->slices().count(), count);
390 first = 9;
419 first = 9;
391 mapper->setFirst(first);
420 mapper->setFirst(first);
392 QCOMPARE(series->slices().count(), qMin(count, rowCount - first));
421 QCOMPARE(series->slices().count(), qMin(count, rowCount - first));
393 }
422 }
394
423
424 void tst_qpieseries::modelUpdate()
425 {
426 int rowCount = 12;
427 int columnCount = 7;
428 QStandardItemModel *stdModel = new QStandardItemModel(0, 3);
429 for (int row = 0; row < rowCount; ++row) {
430 for (int column = 0; column < 2; column++) {
431 QStandardItem *item = new QStandardItem(row * column);
432 stdModel->setItem(row, column, item);
433 }
434 }
435
436 QPieSeries *series = new QPieSeries;
437 QChart *chart = new QChart;
438 chart->addSeries(series);
439 QChartView *chartView = new QChartView(chart);
440 chartView->show();
441 series->setModel(stdModel);
442
443 QPieModelMapper *mapper = new QPieModelMapper;
444 mapper->setMapValues(0);
445 mapper->setMapLabels(0);
446 series->setModelMapper(mapper);
447
448 stdModel->insertRows(3, 5);
449 QCOMPARE(series->slices().count(), rowCount + 5);
450
451 stdModel->removeRows(10, 5);
452 QCOMPARE(series->slices().count(), rowCount);
453
454 // limit the number of slices taken from the model to 12
455 mapper->setCount(rowCount);
456 stdModel->insertRows(3, 5);
457 QCOMPARE(series->slices().count(), rowCount);
458
459 stdModel->removeRows(0, 10);
460 QCOMPARE(series->slices().count(), rowCount - 5);
461
462 // change the orientation to horizontal
463 mapper->setOrientation(Qt::Horizontal);
464 QCOMPARE(series->slices().count(), columnCount);
465
466 stdModel->insertColumns(3, 10);
467 QCOMPARE(series->slices().count(), rowCount); // count is limited to rowCount (12)
468
469 stdModel->removeColumns(5, 10);
470 QCOMPARE(series->slices().count(), columnCount);
471
472 }
473
395 QTEST_MAIN(tst_qpieseries)
474 QTEST_MAIN(tst_qpieseries)
396
475
397 #include "tst_qpieseries.moc"
476 #include "tst_qpieseries.moc"
398
477
@@ -1,468 +1,468
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "tablewidget.h"
21 #include "tablewidget.h"
22 #include <QGridLayout>
22 #include <QGridLayout>
23 #include <QTableView>
23 #include <QTableView>
24 #include <QChart>
24 #include <QChart>
25 #include <QStyledItemDelegate>
25 #include <QStyledItemDelegate>
26 #include <QLineSeries>
26 #include <QLineSeries>
27 #include <QSplineSeries>
27 #include <QSplineSeries>
28 #include <QScatterSeries>
28 #include <QScatterSeries>
29 #include <QXYModelMapper>
29 #include <QXYModelMapper>
30 #include "customtablemodel.h"
30 #include "customtablemodel.h"
31 #include <QPieSeries>
31 #include <QPieSeries>
32 #include <QPieModelMapper>
32 #include <QPieModelMapper>
33 #include <QPieSlice>
33 #include <QPieSlice>
34 #include <QAreaSeries>
34 #include <QAreaSeries>
35 #include <QBarSeries>
35 #include <QBarSeries>
36 #include <QGroupedBarSeries>
36 #include <QGroupedBarSeries>
37 #include <QBarSet>
37 #include <QBarSet>
38 #include <QBarModelMapper>
38 #include <QBarModelMapper>
39 #include <QPushButton>
39 #include <QPushButton>
40 #include <QRadioButton>
40 #include <QRadioButton>
41 #include <QLabel>
41 #include <QLabel>
42 #include <QSpinBox>
42 #include <QSpinBox>
43 #include <QTime>
43 #include <QTime>
44 #include <QHeaderView>
44 #include <QHeaderView>
45
45
46 TableWidget::TableWidget(QWidget *parent)
46 TableWidget::TableWidget(QWidget *parent)
47 : QWidget(parent)
47 : QWidget(parent)
48 // specialPie(0)
48 // specialPie(0)
49 {
49 {
50 setGeometry(1900, 100, 1000, 600);
50 setGeometry(1900, 100, 1000, 600);
51 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
51 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
52 // create simple model for storing data
52 // create simple model for storing data
53 // user's table data model
53 // user's table data model
54 m_model = new CustomTableModel;
54 m_model = new CustomTableModel;
55 m_tableView = new QTableView;
55 m_tableView = new QTableView;
56 m_tableView->setModel(m_model);
56 m_tableView->setModel(m_model);
57 // m_tableView->setMinimumHeight(300);
57 // m_tableView->setMinimumHeight(300);
58 m_tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
58 m_tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
59 m_tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
59 m_tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
60
60
61 m_chart = new QChart;
61 m_chart = new QChart;
62 m_chart->legend()->setVisible(true);
62 m_chart->legend()->setVisible(true);
63 m_chart->setAnimationOptions(QChart::SeriesAnimations);
63 m_chart->setAnimationOptions(QChart::SeriesAnimations);
64 m_chartView = new QChartView(m_chart);
64 m_chartView = new QChartView(m_chart);
65 m_chartView->setRenderHint(QPainter::Antialiasing);
65 m_chartView->setRenderHint(QPainter::Antialiasing);
66 m_chartView->setMinimumSize(640, 480);
66 m_chartView->setMinimumSize(640, 480);
67
67
68 // add, remove data buttons
68 // add, remove data buttons
69 QPushButton* addRowAboveButton = new QPushButton("Add row above");
69 QPushButton* addRowAboveButton = new QPushButton("Add row above");
70 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
70 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
71
71
72 QPushButton* addRowBelowButton = new QPushButton("Add row below");
72 QPushButton* addRowBelowButton = new QPushButton("Add row below");
73 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
73 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
74
74
75 QPushButton* removeRowButton = new QPushButton("Remove row");
75 QPushButton* removeRowButton = new QPushButton("Remove row");
76 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
76 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
77
77
78 QPushButton* addColumnRightButton = new QPushButton("Add column to the right");
78 QPushButton* addColumnRightButton = new QPushButton("Add column to the right");
79 connect(addColumnRightButton, SIGNAL(clicked()), this, SLOT(addColumnRight()));
79 connect(addColumnRightButton, SIGNAL(clicked()), this, SLOT(addColumnRight()));
80
80
81 QPushButton* removeColumnButton = new QPushButton("Remove column");
81 QPushButton* removeColumnButton = new QPushButton("Remove column");
82 connect(removeColumnButton, SIGNAL(clicked()), this, SLOT(removeColumn()));
82 connect(removeColumnButton, SIGNAL(clicked()), this, SLOT(removeColumn()));
83
83
84 QPushButton* specialPieButton = new QPushButton("Test pie");
84 QPushButton* specialPieButton = new QPushButton("Test pie");
85 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
85 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
86
86
87
87
88 // QLabel *spinBoxLabel = new QLabel("Rows affected:");
88 // QLabel *spinBoxLabel = new QLabel("Rows affected:");
89
89
90 // spin box for setting number of affected items (add, remove)
90 // spin box for setting number of affected items (add, remove)
91 m_linesCountSpinBox = new QSpinBox;
91 m_linesCountSpinBox = new QSpinBox;
92 m_linesCountSpinBox->setRange(1, 10);
92 m_linesCountSpinBox->setRange(1, 10);
93 m_linesCountSpinBox->setValue(1);
93 m_linesCountSpinBox->setValue(1);
94
94
95 // buttons layout
95 // buttons layout
96 QVBoxLayout* buttonsLayout = new QVBoxLayout;
96 QVBoxLayout* buttonsLayout = new QVBoxLayout;
97 // buttonsLayout->addWidget(spinBoxLabel);
97 // buttonsLayout->addWidget(spinBoxLabel);
98 // buttonsLayout->addWidget(m_linesCountSpinBox);
98 // buttonsLayout->addWidget(m_linesCountSpinBox);
99 // buttonsLayout->addWidget(addRowAboveButton);
99 // buttonsLayout->addWidget(addRowAboveButton);
100 buttonsLayout->addWidget(addRowBelowButton);
100 buttonsLayout->addWidget(addRowBelowButton);
101 buttonsLayout->addWidget(removeRowButton);
101 buttonsLayout->addWidget(removeRowButton);
102 // buttonsLayout->addWidget(addColumnRightButton);
102 // buttonsLayout->addWidget(addColumnRightButton);
103 // buttonsLayout->addWidget(removeColumnButton);
103 // buttonsLayout->addWidget(removeColumnButton);
104 buttonsLayout->addWidget(specialPieButton);
104 buttonsLayout->addWidget(specialPieButton);
105 buttonsLayout->addStretch();
105 buttonsLayout->addStretch();
106
106
107 // chart type radio buttons
107 // chart type radio buttons
108 m_lineRadioButton = new QRadioButton("Line");
108 m_lineRadioButton = new QRadioButton("Line");
109 m_splineRadioButton = new QRadioButton("Spline");
109 m_splineRadioButton = new QRadioButton("Spline");
110 m_scatterRadioButton = new QRadioButton("Scatter");
110 m_scatterRadioButton = new QRadioButton("Scatter");
111 m_pieRadioButton = new QRadioButton("Pie");
111 m_pieRadioButton = new QRadioButton("Pie");
112 m_areaRadioButton = new QRadioButton("Area");
112 m_areaRadioButton = new QRadioButton("Area");
113 m_barRadioButton = new QRadioButton("Bar");
113 m_barRadioButton = new QRadioButton("Bar");
114
114
115 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
115 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
116 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
116 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
117 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
117 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
118 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
118 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
119 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
119 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
120 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
120 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
121 m_lineRadioButton->setChecked(true);
121 m_lineRadioButton->setChecked(true);
122
122
123 // radio buttons layout
123 // radio buttons layout
124 QVBoxLayout* radioLayout = new QVBoxLayout;
124 QVBoxLayout* radioLayout = new QVBoxLayout;
125 radioLayout->addWidget(m_lineRadioButton);
125 radioLayout->addWidget(m_lineRadioButton);
126 radioLayout->addWidget(m_splineRadioButton);
126 radioLayout->addWidget(m_splineRadioButton);
127 // radioLayout->addWidget(m_scatterRadioButton);
127 // radioLayout->addWidget(m_scatterRadioButton);
128 radioLayout->addWidget(m_pieRadioButton);
128 radioLayout->addWidget(m_pieRadioButton);
129 // radioLayout->addWidget(m_areaRadioButton);
129 // radioLayout->addWidget(m_areaRadioButton);
130 radioLayout->addWidget(m_barRadioButton);
130 radioLayout->addWidget(m_barRadioButton);
131 radioLayout->addStretch();
131 radioLayout->addStretch();
132
132
133 // create main layout
133 // create main layout
134 QGridLayout* mainLayout = new QGridLayout;
134 QGridLayout* mainLayout = new QGridLayout;
135 mainLayout->addLayout(buttonsLayout, 2, 0);
135 mainLayout->addLayout(buttonsLayout, 2, 0);
136 mainLayout->addLayout(radioLayout, 3, 0);
136 mainLayout->addLayout(radioLayout, 3, 0);
137 mainLayout->addWidget(m_tableView, 1, 0);
137 mainLayout->addWidget(m_tableView, 1, 0);
138 mainLayout->addWidget(m_chartView, 1, 1, 2, 1);
138 mainLayout->addWidget(m_chartView, 1, 1, 2, 1);
139 setLayout(mainLayout);
139 setLayout(mainLayout);
140 m_lineRadioButton->setFocus();
140 m_lineRadioButton->setFocus();
141 }
141 }
142
142
143 void TableWidget::addRowAbove()
143 void TableWidget::addRowAbove()
144 {
144 {
145 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
145 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
146
146
147 }
147 }
148
148
149 void TableWidget::addRowBelow()
149 void TableWidget::addRowBelow()
150 {
150 {
151 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
151 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
152
152
153 }
153 }
154
154
155 void TableWidget::removeRow()
155 void TableWidget::removeRow()
156 {
156 {
157 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
157 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
158 }
158 }
159
159
160 void TableWidget::addColumnRight()
160 void TableWidget::addColumnRight()
161 {
161 {
162 m_model->insertColumns(m_tableView->currentIndex().column() + 1, m_linesCountSpinBox->value());
162 m_model->insertColumns(m_tableView->currentIndex().column() + 1, m_linesCountSpinBox->value());
163 }
163 }
164
164
165 void TableWidget::removeColumn()
165 void TableWidget::removeColumn()
166 {
166 {
167 m_model->removeColumns(m_tableView->currentIndex().column(), qMin(m_model->columnCount() - m_tableView->currentIndex().column(), m_linesCountSpinBox->value()));
167 m_model->removeColumns(m_tableView->currentIndex().column(), qMin(m_model->columnCount() - m_tableView->currentIndex().column(), m_linesCountSpinBox->value()));
168 }
168 }
169
169
170 void TableWidget::updateChartType(bool toggle)
170 void TableWidget::updateChartType(bool toggle)
171 {
171 {
172 // this if is needed, so that the function is only called once.
172 // this if is needed, so that the function is only called once.
173 // For the radioButton that was enabled.
173 // For the radioButton that was enabled.
174 if (toggle) {
174 if (toggle) {
175 // specialPie = 0;
175 // specialPie = 0;
176 m_chart->removeAllSeries();
176 m_chart->removeAllSeries();
177 m_chart->axisX()->setNiceNumbersEnabled(false);
177 m_chart->axisX()->setNiceNumbersEnabled(false);
178 m_chart->axisY()->setNiceNumbersEnabled(false);
178 m_chart->axisY()->setNiceNumbersEnabled(false);
179
179
180 // renable axes of the chart (pie hides them)
180 // renable axes of the chart (pie hides them)
181 // x axis
181 // x axis
182 QAxis *axis = m_chart->axisX();
182 QAxis *axis = m_chart->axisX();
183 axis->setAxisVisible(true);
183 axis->setAxisVisible(true);
184 axis->setGridLineVisible(true);
184 axis->setGridLineVisible(true);
185 axis->setLabelsVisible(true);
185 axis->setLabelsVisible(true);
186
186
187 // y axis
187 // y axis
188 axis = m_chart->axisY();
188 axis = m_chart->axisY();
189 axis->setAxisVisible(true);
189 axis->setAxisVisible(true);
190 axis->setGridLineVisible(true);
190 axis->setGridLineVisible(true);
191 axis->setLabelsVisible(true);
191 axis->setLabelsVisible(true);
192
192
193 m_model->clearMapping();
193 m_model->clearMapping();
194
194
195 QString seriesColorHex = "#000000";
195 QString seriesColorHex = "#000000";
196 QPen pen;
196 QPen pen;
197 pen.setWidth(2);
197 pen.setWidth(2);
198
198
199 if (m_lineRadioButton->isChecked())
199 if (m_lineRadioButton->isChecked())
200 {
200 {
201 m_chart->setAnimationOptions(QChart::NoAnimation);
201 m_chart->setAnimationOptions(QChart::NoAnimation);
202
202
203 // series 1
203 // series 1
204 m_series = new QLineSeries;
204 m_series = new QLineSeries;
205 m_series->setModel(m_model);
205 m_series->setModel(m_model);
206
206
207 QXYModelMapper *mapper = new QXYModelMapper;
207 QXYModelMapper *mapper = new QXYModelMapper;
208 mapper->setMapX(0);
208 mapper->setMapX(0);
209 mapper->setMapY(1);
209 mapper->setMapY(1);
210 mapper->setFirst(3);
210 mapper->setFirst(3);
211 mapper->setCount(4);
211 mapper->setCount(4);
212 m_series->setModelMapper(mapper);
212 m_series->setModelMapper(mapper);
213 // m_series->setModelMapping(0,1, Qt::Vertical);
213 // m_series->setModelMapping(0,1, Qt::Vertical);
214 // m_series->setModelMappingRange(3, 4);
214 // m_series->setModelMappingRange(3, 4);
215 m_chart->addSeries(m_series);
215 m_chart->addSeries(m_series);
216 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
216 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
217 m_model->addMapping(seriesColorHex, QRect(0, 3, 2, 4));
217 m_model->addMapping(seriesColorHex, QRect(0, 3, 2, 4));
218
218
219 // series 2
219 // series 2
220 m_series = new QLineSeries;
220 m_series = new QLineSeries;
221 m_series->setModel(m_model);
221 m_series->setModel(m_model);
222
222
223 mapper = new QXYModelMapper;
223 mapper = new QXYModelMapper;
224 mapper->setMapX(3);
224 mapper->setMapX(3);
225 mapper->setMapY(4);
225 mapper->setMapY(4);
226 // mapper->setFirst(3);
226 // mapper->setFirst(3);
227 // mapper->setCount(4);
227 // mapper->setCount(4);
228 m_series->setModelMapper(mapper);
228 m_series->setModelMapper(mapper);
229 // m_series->setModelMapping(2,3, Qt::Vertical);
229 // m_series->setModelMapping(2,3, Qt::Vertical);
230 m_chart->addSeries(m_series);
230 m_chart->addSeries(m_series);
231 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
231 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
232 m_model->addMapping(seriesColorHex, QRect(3, 0, 2, 1000));
232 m_model->addMapping(seriesColorHex, QRect(3, 0, 2, 1000));
233
233
234 // series 3
234 // series 3
235 m_series = new QLineSeries;
235 m_series = new QLineSeries;
236 m_series->setModel(m_model);
236 m_series->setModel(m_model);
237
237
238 mapper = new QXYModelMapper;
238 mapper = new QXYModelMapper;
239 mapper->setMapX(5);
239 mapper->setMapX(5);
240 mapper->setMapY(6);
240 mapper->setMapY(6);
241 mapper->setFirst(2);
241 mapper->setFirst(2);
242 mapper->setCount(-1);
242 mapper->setCount(-1);
243 m_series->setModelMapper(mapper);
243 m_series->setModelMapper(mapper);
244 // m_series->setModelMapping(4,5, Qt::Vertical);
244 // m_series->setModelMapping(4,5, Qt::Vertical);
245 // m_series->setModelMappingRange(2, -1);
245 // m_series->setModelMappingRange(2, -1);
246 m_chart->addSeries(m_series);
246 m_chart->addSeries(m_series);
247 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
247 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
248 m_model->addMapping(seriesColorHex, QRect(5, 2, 2, 1000));
248 m_model->addMapping(seriesColorHex, QRect(5, 2, 2, 1000));
249 }
249 }
250 else if (m_splineRadioButton->isChecked())
250 else if (m_splineRadioButton->isChecked())
251 {
251 {
252 m_chart->setAnimationOptions(QChart::NoAnimation);
252 m_chart->setAnimationOptions(QChart::NoAnimation);
253
253
254 // series 1
254 // series 1
255 m_series = new QSplineSeries;
255 m_series = new QSplineSeries;
256 m_series->setModel(m_model);
256 m_series->setModel(m_model);
257
257
258 QXYModelMapper *mapper = new QXYModelMapper;
258 QXYModelMapper *mapper = new QXYModelMapper;
259 mapper->setMapX(0);
259 mapper->setMapX(0);
260 mapper->setMapY(1);
260 mapper->setMapY(1);
261 mapper->setFirst(0);
261 mapper->setFirst(0);
262 mapper->setCount(-1);
262 mapper->setCount(-1);
263
263
264 m_series->setModelMapper(mapper);
264 m_series->setModelMapper(mapper);
265
265
266 m_chart->addSeries(m_series);
266 m_chart->addSeries(m_series);
267 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
267 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
268 m_model->addMapping(seriesColorHex, QRect(0, 0, 2, 1000));
268 m_model->addMapping(seriesColorHex, QRect(0, 0, 2, 1000));
269
269
270 // series 2
270 // series 2
271 m_series = new QSplineSeries;
271 m_series = new QSplineSeries;
272 m_series->setModel(m_model);
272 m_series->setModel(m_model);
273
273
274 mapper = new QXYModelMapper;
274 mapper = new QXYModelMapper;
275 mapper->setMapX(2);
275 mapper->setMapX(2);
276 mapper->setMapY(3);
276 mapper->setMapY(3);
277 mapper->setFirst(2);
277 mapper->setFirst(2);
278 mapper->setCount(4);
278 mapper->setCount(4);
279
279
280 m_series->setModelMapper(mapper);
280 m_series->setModelMapper(mapper);
281
281
282 m_chart->addSeries(m_series);
282 m_chart->addSeries(m_series);
283 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
283 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
284 m_model->addMapping(seriesColorHex, QRect(2, 2, 2, 4));
284 m_model->addMapping(seriesColorHex, QRect(2, 2, 2, 4));
285
285
286 // series 3
286 // series 3
287 m_series = new QSplineSeries;
287 m_series = new QSplineSeries;
288 m_series->setModel(m_model);
288 m_series->setModel(m_model);
289
289
290 mapper = new QXYModelMapper;
290 mapper = new QXYModelMapper;
291 mapper->setMapX(4);
291 mapper->setMapX(4);
292 mapper->setMapY(5);
292 mapper->setMapY(5);
293 mapper->setFirst(2);
293 mapper->setFirst(2);
294 mapper->setCount(-1);
294 mapper->setCount(-1);
295
295
296 m_series->setModelMapper(mapper);
296 m_series->setModelMapper(mapper);
297
297
298 m_chart->addSeries(m_series);
298 m_chart->addSeries(m_series);
299 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
299 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
300 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
300 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
301 }
301 }
302 // else if (m_scatterRadioButton->isChecked())
302 // else if (m_scatterRadioButton->isChecked())
303 // {
303 // {
304 // m_chart->setAnimationOptions(QChart::NoAnimation);
304 // m_chart->setAnimationOptions(QChart::NoAnimation);
305
305
306 // // series 1
306 // // series 1
307 // m_series = new QScatterSeries;
307 // m_series = new QScatterSeries;
308 // m_series->setModel(m_model);
308 // m_series->setModel(m_model);
309 // m_series->setModelMapping(0,1, Qt::Vertical);
309 // m_series->setModelMapping(0,1, Qt::Vertical);
310 // // m_series->setModelMappingRange(2, 0);
310 // // m_series->setModelMappingRange(2, 0);
311 // // series->setModelMapping(0,1, Qt::Horizontal);
311 // // series->setModelMapping(0,1, Qt::Horizontal);
312 // m_chart->addSeries(m_series);
312 // m_chart->addSeries(m_series);
313
313
314 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
314 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
315 // m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
315 // m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
316
316
317 // // series 2
317 // // series 2
318 // m_series = new QScatterSeries;
318 // m_series = new QScatterSeries;
319 // m_series->setModel(m_model);
319 // m_series->setModel(m_model);
320 // m_series->setModelMapping(2,3, Qt::Vertical);
320 // m_series->setModelMapping(2,3, Qt::Vertical);
321 // // m_series->setModelMappingRange(1, 6);
321 // // m_series->setModelMappingRange(1, 6);
322 // // series->setModelMapping(2,3, Qt::Horizontal);
322 // // series->setModelMapping(2,3, Qt::Horizontal);
323 // m_chart->addSeries(m_series);
323 // m_chart->addSeries(m_series);
324
324
325 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
325 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
326 // m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
326 // m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
327
327
328 // // series 3
328 // // series 3
329 // m_series = new QScatterSeries;
329 // m_series = new QScatterSeries;
330 // m_series->setModel(m_model);
330 // m_series->setModel(m_model);
331 // m_series->setModelMapping(4,5, Qt::Vertical);
331 // m_series->setModelMapping(4,5, Qt::Vertical);
332 // // series->setModelMapping(4,5, Qt::Horizontal);
332 // // series->setModelMapping(4,5, Qt::Horizontal);
333 // m_chart->addSeries(m_series);
333 // m_chart->addSeries(m_series);
334 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
334 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
335 // m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
335 // m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
336 // }
336 // }
337 else if (m_pieRadioButton->isChecked())
337 else if (m_pieRadioButton->isChecked())
338 {
338 {
339 m_chart->setAnimationOptions(QChart::SeriesAnimations);
339 m_chart->setAnimationOptions(QChart::SeriesAnimations);
340
340
341 // pie 1
341 // pie 1
342 QPieSeries* pieSeries = new QPieSeries;
342 QPieSeries* pieSeries = new QPieSeries;
343 pieSeries->setModel(m_model);
343 pieSeries->setModel(m_model);
344
344
345 QPieModelMapper *mapper = new QPieModelMapper;
345 QPieModelMapper *mapper = new QPieModelMapper;
346 mapper->setMapValues(1);
346 mapper->setMapValues(-1);
347 mapper->setMapLabels(1);
347 mapper->setMapLabels(-1);
348 mapper->setFirst(2);
348 mapper->setFirst(2);
349 mapper->setCount(5);
349 mapper->setCount(5);
350 pieSeries->setModelMapper(mapper);
350 pieSeries->setModelMapper(mapper);
351
351
352 pieSeries->setLabelsVisible(true);
352 pieSeries->setLabelsVisible(true);
353 pieSeries->setPieSize(0.75);
353 pieSeries->setPieSize(0.75);
354 // pieSeries->setHorizontalPosition(0.2);
354 // pieSeries->setHorizontalPosition(0.2);
355 // pieSeries->setVerticalPosition(0.3);
355 // pieSeries->setVerticalPosition(0.3);
356
356
357 m_chart->addSeries(pieSeries);
357 m_chart->addSeries(pieSeries);
358 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
358 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
359 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 5));
359 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 5));
360
360
361 // // pie 2
361 // // pie 2
362 // pieSeries = new QPieSeries;
362 // pieSeries = new QPieSeries;
363 // pieSeries->setModel(m_model);
363 // pieSeries->setModel(m_model);
364
364
365 // pieSeries->setModelMapping(1,1, Qt::Vertical);
365 // pieSeries->setModelMapping(1,1, Qt::Vertical);
366 // pieSeries->setModelMappingRange(2, -1);
366 // pieSeries->setModelMappingRange(2, -1);
367 // pieSeries->setLabelsVisible(true);
367 // pieSeries->setLabelsVisible(true);
368 // pieSeries->setPieSize(0.35);
368 // pieSeries->setPieSize(0.35);
369 // pieSeries->setHorizontalPosition(0.8);
369 // pieSeries->setHorizontalPosition(0.8);
370 // pieSeries->setVerticalPosition(0.3);
370 // pieSeries->setVerticalPosition(0.3);
371 // m_chart->addSeries(pieSeries);
371 // m_chart->addSeries(pieSeries);
372 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
372 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
373 // m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 1000));
373 // m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 1000));
374
374
375 // // pie 3
375 // // pie 3
376 // pieSeries = new QPieSeries;
376 // pieSeries = new QPieSeries;
377 // pieSeries->setModel(m_model);
377 // pieSeries->setModel(m_model);
378 // pieSeries->setModelMapping(2,2, Qt::Vertical);
378 // pieSeries->setModelMapping(2,2, Qt::Vertical);
379 // pieSeries->setLabelsVisible(true);
379 // pieSeries->setLabelsVisible(true);
380 // pieSeries->setPieSize(0.35);
380 // pieSeries->setPieSize(0.35);
381 // pieSeries->setHorizontalPosition(0.5);
381 // pieSeries->setHorizontalPosition(0.5);
382 // pieSeries->setVerticalPosition(0.75);
382 // pieSeries->setVerticalPosition(0.75);
383 // m_chart->addSeries(pieSeries);
383 // m_chart->addSeries(pieSeries);
384 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
384 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
385 // m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
385 // m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
386
386
387 // // special pie
387 // // special pie
388 // specialPie = new QPieSeries;
388 // specialPie = new QPieSeries;
389 // specialPie->append(17, "1");
389 // specialPie->append(17, "1");
390 // specialPie->append(45, "2");
390 // specialPie->append(45, "2");
391 // specialPie->append(77, "3");
391 // specialPie->append(77, "3");
392 // specialPie->append(37, "4");
392 // specialPie->append(37, "4");
393 // specialPie->append(27, "5");
393 // specialPie->append(27, "5");
394 // specialPie->append(47, "6");
394 // specialPie->append(47, "6");
395 // specialPie->setPieSize(0.35);
395 // specialPie->setPieSize(0.35);
396 // specialPie->setHorizontalPosition(0.8);
396 // specialPie->setHorizontalPosition(0.8);
397 // specialPie->setVerticalPosition(0.75);
397 // specialPie->setVerticalPosition(0.75);
398 // specialPie->setLabelsVisible(true);
398 // specialPie->setLabelsVisible(true);
399 // m_chart->addSeries(specialPie);
399 // m_chart->addSeries(specialPie);
400 }
400 }
401 // else if (m_areaRadioButton->isChecked())
401 // else if (m_areaRadioButton->isChecked())
402 // {
402 // {
403 // m_chart->setAnimationOptions(QChart::NoAnimation);
403 // m_chart->setAnimationOptions(QChart::NoAnimation);
404
404
405 // QLineSeries* upperLineSeries = new QLineSeries;
405 // QLineSeries* upperLineSeries = new QLineSeries;
406 // upperLineSeries->setModel(m_model);
406 // upperLineSeries->setModel(m_model);
407 // upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
407 // upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
408 // // upperLineSeries->setModelMappingRange(1, 5);
408 // // upperLineSeries->setModelMappingRange(1, 5);
409 // QLineSeries* lowerLineSeries = new QLineSeries;
409 // QLineSeries* lowerLineSeries = new QLineSeries;
410 // lowerLineSeries->setModel(m_model);
410 // lowerLineSeries->setModel(m_model);
411 // lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
411 // lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
412 // QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
412 // QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
413 // m_chart->addSeries(areaSeries);
413 // m_chart->addSeries(areaSeries);
414 // seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
414 // seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
415 // m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
415 // m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
416 // m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
416 // m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
417 // }
417 // }
418 else if (m_barRadioButton->isChecked())
418 else if (m_barRadioButton->isChecked())
419 {
419 {
420 m_chart->setAnimationOptions(QChart::SeriesAnimations);
420 m_chart->setAnimationOptions(QChart::SeriesAnimations);
421
421
422 QGroupedBarSeries* barSeries = new QGroupedBarSeries();
422 QGroupedBarSeries* barSeries = new QGroupedBarSeries();
423 barSeries->setCategories(QStringList());
423 barSeries->setCategories(QStringList());
424 barSeries->setModel(m_model);
424 barSeries->setModel(m_model);
425 // barSeries->setModelMappingRange(2, 5);
425 // barSeries->setModelMappingRange(2, 5);
426 // barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
426 // barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
427
427
428 QBarModelMapper *mapper = new QBarModelMapper;
428 QBarModelMapper *mapper = new QBarModelMapper;
429 mapper->setMapCategories(5);
429 mapper->setMapCategories(5);
430 mapper->setMapBarBottom(2);
430 mapper->setMapBarBottom(2);
431 mapper->setMapBarTop(4);
431 mapper->setMapBarTop(4);
432 barSeries->setModelMapper(mapper);
432 barSeries->setModelMapper(mapper);
433 m_chart->addSeries(barSeries);
433 m_chart->addSeries(barSeries);
434 QList<QBarSet*> barsets = barSeries->barSets();
434 QList<QBarSet*> barsets = barSeries->barSets();
435 for (int i = 0; i < barsets.count(); i++) {
435 for (int i = 0; i < barsets.count(); i++) {
436 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
436 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
437 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
437 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
438 }
438 }
439 }
439 }
440
440
441
441
442 if (!m_barRadioButton->isChecked()) {
442 if (!m_barRadioButton->isChecked()) {
443 m_chart->axisX()->setRange(0, 500);
443 m_chart->axisX()->setRange(0, 500);
444 m_chart->axisY()->setRange(0, 220);
444 m_chart->axisY()->setRange(0, 220);
445 }
445 }
446 m_chart->legend()->setVisible(true);
446 m_chart->legend()->setVisible(true);
447
447
448 // repaint table view colors
448 // repaint table view colors
449 m_tableView->repaint();
449 m_tableView->repaint();
450 m_tableView->setFocus();
450 m_tableView->setFocus();
451 }
451 }
452 }
452 }
453
453
454 void TableWidget::testPie()
454 void TableWidget::testPie()
455 {
455 {
456 m_series->modelMapper()->setMapX(4);
456 m_series->modelMapper()->setMapX(4);
457 // m_tableView->setColumnWidth(10, 250);
457 // m_tableView->setColumnWidth(10, 250);
458 // if (specialPie) {
458 // if (specialPie) {
459 // specialPie->remove(specialPie->slices().at(2));
459 // specialPie->remove(specialPie->slices().at(2));
460 // // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2));
460 // // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2));
461 // specialPie->append(4, "heloo");
461 // specialPie->append(4, "heloo");
462 // }
462 // }
463 }
463 }
464
464
465 TableWidget::~TableWidget()
465 TableWidget::~TableWidget()
466 {
466 {
467
467
468 }
468 }
General Comments 0
You need to be logged in to leave comments. Login now