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