##// END OF EJS Templates
Small fixes to piemodelmapper and one more test
Marek Rosa -
r1251:3e42cdbd748d
parent child
Show More
@@ -1,465 +1,493
1 1 #include "qpiemodelmapper_p.h"
2 2 #include "qpiemodelmapper.h"
3 3 #include "qpieseries.h"
4 4 #include "qpieslice.h"
5 5 #include <QAbstractItemModel>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 QPieModelMapper::QPieModelMapper(QObject *parent) :
10 10 QObject(parent),
11 11 d_ptr(new QPieModelMapperPrivate(this))
12 12 {
13 13 }
14 14
15 15 QAbstractItemModel* QPieModelMapper::model() const
16 16 {
17 17 Q_D(const QPieModelMapper);
18 18 return d->m_model;
19 19 }
20 20
21 21 void QPieModelMapper::setModel(QAbstractItemModel *model)
22 22 {
23 23 if (model == 0)
24 24 return;
25 25
26 26 Q_D(QPieModelMapper);
27 27 if (d->m_model) {
28 28 disconnect(d->m_model, 0, d, 0);
29 29 }
30 30
31 31 d->m_model = model;
32 32 d->initializePieFromModel();
33 33 // connect signals from the model
34 34 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
35 35 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
36 36 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
37 37 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
38 38 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
39 39 }
40 40
41 41 QPieSeries* QPieModelMapper::series() const
42 42 {
43 43 Q_D(const QPieModelMapper);
44 44 return d->m_series;
45 45 }
46 46
47 47 void QPieModelMapper::setSeries(QPieSeries *series)
48 48 {
49 49 if (series == 0)
50 50 return;
51 51
52 52 Q_D(QPieModelMapper);
53 53 if (d->m_series) {
54 54 disconnect(d->m_series, 0, d, 0);
55 55 }
56 56
57 57 d->m_series = series;
58 58 d->initializePieFromModel();
59 59 // connect the signals from the series
60 60 connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>)));
61 61 connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>)));
62 62 // connect(d->m_model, SIGNAL(), d, SLOT());
63 63 }
64 64
65 65 int QPieModelMapper::first() const
66 66 {
67 67 Q_D(const QPieModelMapper);
68 68 return d->m_first;
69 69 }
70 70
71 71 void QPieModelMapper::setFirst(int first)
72 72 {
73 73 Q_D(QPieModelMapper);
74 74 d->m_first = qMax(first, 0);
75 75 d->initializePieFromModel();
76 76 }
77 77
78 78 int QPieModelMapper::count() const
79 79 {
80 80 Q_D(const QPieModelMapper);
81 81 return d->m_count;
82 82 }
83 83
84 84 void QPieModelMapper::setCount(int count)
85 85 {
86 86 Q_D(QPieModelMapper);
87 87 d->m_count = qMax(count, -1);
88 88 d->initializePieFromModel();
89 89 }
90 90
91 91 Qt::Orientation QPieModelMapper::orientation() const
92 92 {
93 93 Q_D(const QPieModelMapper);
94 94 return d->m_orientation;
95 95 }
96 96
97 97 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
98 98 {
99 99 Q_D(QPieModelMapper);
100 100 d->m_orientation = orientation;
101 101 d->initializePieFromModel();
102 102 }
103 103
104 104 int QPieModelMapper::valuesIndex() const
105 105 {
106 106 Q_D(const QPieModelMapper);
107 107 return d->m_valuesIndex;
108 108 }
109 109
110 110 void QPieModelMapper::setValuesIndex(int valuesIndex)
111 111 {
112 112 Q_D(QPieModelMapper);
113 113 d->m_valuesIndex = qMax(-1, valuesIndex);
114 114 d->initializePieFromModel();
115 115 }
116 116
117 117 int QPieModelMapper::labelsIndex() const
118 118 {
119 119 Q_D(const QPieModelMapper);
120 120 return d->m_labelsIndex;
121 121 }
122 122
123 123 void QPieModelMapper::setLabelsIndex(int labelsIndex)
124 124 {
125 125 Q_D(QPieModelMapper);
126 126 d->m_labelsIndex = qMax(-1, labelsIndex);
127 127 d->initializePieFromModel();
128 128 }
129 129
130 130 void QPieModelMapper::reset()
131 131 {
132 132 Q_D(QPieModelMapper);
133 133 d->m_first = 0;
134 134 d->m_count = -1;
135 135 d->m_orientation = Qt::Vertical;
136 136 d->m_valuesIndex = -1;
137 137 d->m_labelsIndex = -1;
138 138 }
139 139
140 140 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
141 141
142 142 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
143 143 m_series(0),
144 144 m_model(0),
145 145 m_first(0),
146 146 m_count(-1),
147 147 m_orientation(Qt::Vertical),
148 148 m_valuesIndex(-1),
149 149 m_labelsIndex(-1),
150 150 m_seriesSignalsBlock(false),
151 151 m_modelSignalsBlock(false),
152 152 q_ptr(q)
153 153 {
154 154 }
155 155
156 156 void QPieModelMapperPrivate::blockModelSignals(bool block)
157 157 {
158 158 m_modelSignalsBlock = block;
159 159 }
160 160
161 161 void QPieModelMapperPrivate::blockSeriesSignals(bool block)
162 162 {
163 163 m_seriesSignalsBlock = block;
164 164 }
165 165
166 166
167 167 QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const
168 168 {
169 169 if (m_orientation == Qt::Vertical && (index.column() == m_valuesIndex || index.column() == m_labelsIndex)) {
170 170 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count))
171 171 return m_series->slices().at(index.row() - m_first);
172 172 } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesIndex || index.row() == m_labelsIndex)) {
173 173 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
174 174 return m_series->slices().at(index.column() - m_first);
175 175 }
176 176 return 0; // This part of model has not been mapped to any slice
177 177 }
178 178
179 179 QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos)
180 180 {
181 181 if (m_count != -1 && slicePos >= m_count)
182 182 return QModelIndex(); // invalid
183 183
184 184 if (m_orientation == Qt::Vertical)
185 185 return m_model->index(slicePos + m_first, m_valuesIndex);
186 186 else
187 187 return m_model->index(m_valuesIndex, slicePos + m_first);
188 188 }
189 189
190 190 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
191 191 {
192 192 if (m_count != -1 && slicePos >= m_count)
193 193 return QModelIndex(); // invalid
194 194
195 195 if (m_orientation == Qt::Vertical)
196 196 return m_model->index(slicePos + m_first, m_labelsIndex);
197 197 else
198 198 return m_model->index(m_labelsIndex, slicePos + m_first);
199 199 }
200 200
201 bool QPieModelMapperPrivate::isLabelIndex(QModelIndex index) const
202 {
203 if (m_orientation == Qt::Vertical && index.column() == m_labelsIndex)
204 return true;
205 else if (m_orientation == Qt::Horizontal && index.row() == m_labelsIndex)
206 return true;
207
208 return false;
209 }
210
211 bool QPieModelMapperPrivate::isValueIndex(QModelIndex index) const
212 {
213 if (m_orientation == Qt::Vertical && index.column() == m_valuesIndex)
214 return true;
215 else if (m_orientation == Qt::Horizontal && index.row() == m_valuesIndex)
216 return true;
217
218 return false;
219 }
220
201 221 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
202 222 {
203 223 if (m_seriesSignalsBlock)
204 224 return;
205 225
206 226 if (slices.count() == 0)
207 227 return;
208 228
209 229 int firstIndex = m_series->slices().indexOf(slices.at(0));
210 230 if (firstIndex == -1)
211 231 return;
212 232
213 233 if (m_count != -1)
214 234 m_count += slices.count();
215 235
216 236 for (int i = firstIndex; i < firstIndex + slices.count(); i++) {
217 237 m_slices.insert(i, slices.at(i - firstIndex));
218 238 connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
219 239 connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
220 240 }
221 241
222 242 blockModelSignals();
223 243 if (m_orientation == Qt::Vertical)
224 244 m_model->insertRows(firstIndex + m_first, slices.count());
225 245 else
226 246 m_model->insertColumns(firstIndex + m_first, slices.count());
227 247
228 248 for(int i = firstIndex; i < firstIndex + slices.count(); i++) {
229 249 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
230 250 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
231 251 }
232 252 blockModelSignals(false);
233 253 }
234 254
235 255 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
236 256 {
237 257 if (m_seriesSignalsBlock)
238 258 return;
239 259
240 260 if (slices.count() == 0)
241 261 return;
242 262
243 263 int firstIndex = m_slices.indexOf(slices.at(0));
244 264 if (firstIndex == -1)
245 265 return;
246 266
247 267 if (m_count != -1)
248 268 m_count -= slices.count();
249 269
250 270 for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--)
251 271 m_slices.removeAt(i);
252 272
253 273 blockModelSignals();
254 274 if (m_orientation == Qt::Vertical)
255 275 m_model->removeRows(firstIndex + m_first, slices.count());
256 276 else
257 277 m_model->removeColumns(firstIndex + m_first, slices.count());
258 278 blockModelSignals(false);
259 279 }
260 280
261 281 void QPieModelMapperPrivate::sliceLabelChanged()
262 282 {
263 283 if (m_seriesSignalsBlock)
264 284 return;
265 285
266 286 blockModelSignals();
267 287 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
268 288 m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label());
269 289 blockModelSignals(false);
270 290 }
271 291
272 292 void QPieModelMapperPrivate::sliceValueChanged()
273 293 {
274 294 if (m_seriesSignalsBlock)
275 295 return;
276 296
277 297 blockModelSignals();
278 298 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
279 299 m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value());
280 300 blockModelSignals(false);
281 301 }
282 302
283 303 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
284 304 {
285 305 if (m_modelSignalsBlock)
286 306 return;
287 307
288 308 blockSeriesSignals();
289 309 QModelIndex index;
290 310 QPieSlice *slice;
291 311 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
292 312 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
293 313 index = topLeft.sibling(row, column);
294 314 slice = pieSlice(index);
295 315 if (slice) {
296 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
297 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
316 if (isValueIndex(index))
317 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
318 if (isLabelIndex(index))
319 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
298 320 }
299 321 }
300 322 }
301 323 blockSeriesSignals(false);
302 324 }
303 325
304 326
305 327 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
306 328 {
307 329 Q_UNUSED(parent);
308 330 if (m_modelSignalsBlock)
309 331 return;
310 332
311 333 blockSeriesSignals();
312 334 if (m_orientation == Qt::Vertical)
313 335 insertData(start, end);
314 336 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
315 337 initializePieFromModel();
316 338 blockSeriesSignals(false);
317 339 }
318 340
319 341 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
320 342 {
321 343 Q_UNUSED(parent);
322 344 if (m_modelSignalsBlock)
323 345 return;
324 346
325 347 blockSeriesSignals();
326 348 if (m_orientation == Qt::Vertical)
327 349 removeData(start, end);
328 350 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
329 351 initializePieFromModel();
330 352 blockSeriesSignals(false);
331 353 }
332 354
333 355 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
334 356 {
335 357 Q_UNUSED(parent);
336 358 if (m_modelSignalsBlock)
337 359 return;
338 360
339 361 blockSeriesSignals();
340 362 if (m_orientation == Qt::Horizontal)
341 363 insertData(start, end);
342 364 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
343 365 initializePieFromModel();
344 366 blockSeriesSignals(false);
345 367 }
346 368
347 369 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
348 370 {
349 371 Q_UNUSED(parent);
350 372 if (m_modelSignalsBlock)
351 373 return;
352 374
353 375 blockSeriesSignals();
354 376 if (m_orientation == Qt::Horizontal)
355 377 removeData(start, end);
356 378 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
357 379 initializePieFromModel();
358 380 blockSeriesSignals(false);
359 381 }
360 382
361 383 void QPieModelMapperPrivate::insertData(int start, int end)
362 384 {
385 if (m_model == 0 || m_series == 0)
386 return;
387
363 388 if (m_count != -1 && start >= m_first + m_count) {
364 389 return;
365 390 } else {
366 391 int addedCount = end - start + 1;
367 392 if (m_count != -1 && addedCount > m_count)
368 393 addedCount = m_count;
369 394 int first = qMax(start, m_first);
370 395 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
371 396 for (int i = first; i <= last; i++) {
372 397 QPieSlice *slice = new QPieSlice;
373 398 slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble());
374 399 slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString());
375 400 slice->setLabelVisible();
376 401 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
377 402 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
378 403 m_series->insert(i - m_first, slice);
379 404 m_slices.insert(i - m_first, slice);
380 405 }
381 406
382 407 // remove excess of slices (abouve m_count)
383 408 if (m_count != -1 && m_series->slices().size() > m_count)
384 409 for (int i = m_series->slices().size() - 1; i >= m_count; i--) {
385 410 m_series->remove(m_series->slices().at(i));
386 411 m_slices.removeAt(i);
387 412 }
388 413 }
389 414 }
390 415
391 416 void QPieModelMapperPrivate::removeData(int start, int end)
392 417 {
418 if (m_model == 0 || m_series == 0)
419 return;
420
393 421 int removedCount = end - start + 1;
394 422 if (m_count != -1 && start >= m_first + m_count) {
395 423 return;
396 424 } else {
397 425 int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed
398 426 int first = qMax(start, m_first); // get the index of the first item that will be removed.
399 427 int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed.
400 428 for (int i = last; i >= first; i--) {
401 429 m_series->remove(m_series->slices().at(i - m_first));
402 430 m_slices.removeAt(i - m_first);
403 431 }
404 432
405 433 if (m_count != -1) {
406 434 int itemsAvailable; // check how many are available to be added
407 435 if (m_orientation == Qt::Vertical)
408 436 itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size();
409 437 else
410 438 itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size();
411 439 int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled.
412 440 int currentSize = m_series->slices().size();
413 441 if (toBeAdded > 0)
414 442 for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) {
415 443 QPieSlice *slice = new QPieSlice;
416 444 if (m_orientation == Qt::Vertical) {
417 445 slice->setValue(m_model->data(m_model->index(i + m_first, m_valuesIndex), Qt::DisplayRole).toDouble());
418 446 slice->setLabel(m_model->data(m_model->index(i + m_first, m_labelsIndex), Qt::DisplayRole).toString());
419 447 } else {
420 448 slice->setValue(m_model->data(m_model->index(m_valuesIndex, i + m_first), Qt::DisplayRole).toDouble());
421 449 slice->setLabel(m_model->data(m_model->index(m_labelsIndex, i + m_first), Qt::DisplayRole).toString());
422 450 }
423 451 slice->setLabelVisible();
424 452 m_series->insert(i, slice);
425 453 m_slices.insert(i, slice);
426 454 }
427 455 }
428 456 }
429 457 }
430 458
431 459 void QPieModelMapperPrivate::initializePieFromModel()
432 460 {
433 461 if (m_model == 0 || m_series == 0)
434 462 return;
435 463
436 464 blockSeriesSignals();
437 465 // clear current content
438 466 m_series->clear();
439 467 m_slices.clear();
440 468
441 469 // create the initial slices set
442 470 int slicePos = 0;
443 471 QModelIndex valueIndex = valueModelIndex(slicePos);
444 472 QModelIndex labelIndex = labelModelIndex(slicePos);
445 473 while (valueIndex.isValid() && labelIndex.isValid()) {
446 474 QPieSlice *slice = new QPieSlice;
447 475 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
448 476 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
449 477 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
450 478 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
451 479 m_series->append(slice);
452 480 m_slices.append(slice);
453 // m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble());
481 // m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble());
454 482 slicePos++;
455 483 valueIndex = valueModelIndex(slicePos);
456 484 labelIndex = labelModelIndex(slicePos);
457 485 }
458 486 m_series->setLabelsVisible(true);
459 487 blockSeriesSignals(false);
460 488 }
461 489
462 490 #include "moc_qpiemodelmapper_p.cpp"
463 491 #include "moc_qpiemodelmapper.cpp"
464 492
465 493 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,70 +1,72
1 1 #ifndef QPIEMODELMAPPER_P_H
2 2 #define QPIEMODELMAPPER_P_H
3 3
4 4 #include "qpiemodelmapper.h"
5 5 #include <QObject>
6 6
7 7 class QModelIndex;
8 8 class QAbstractItemModel;
9 9
10 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 11
12 12 class QPieModelMapper;
13 13 class QPieSeries;
14 14 class QPieSlice;
15 15
16 16 class QPieModelMapperPrivate : public QObject
17 17 {
18 18 Q_OBJECT
19 19
20 20 public:
21 21 QPieModelMapperPrivate(QPieModelMapper *q);
22 22
23 23 public Q_SLOTS:
24 24 // for the model
25 25 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
26 26 void modelRowsAdded(QModelIndex parent, int start, int end);
27 27 void modelRowsRemoved(QModelIndex parent, int start, int end);
28 28 void modelColumnsAdded(QModelIndex parent, int start, int end);
29 29 void modelColumnsRemoved(QModelIndex parent, int start, int end);
30 30
31 31 // for the series
32 32 void slicesAdded(QList<QPieSlice*> slices);
33 33 void slicesRemoved(QList<QPieSlice*> slices);
34 34 void sliceLabelChanged();
35 35 void sliceValueChanged();
36 36
37 37 void initializePieFromModel();
38 38
39 39 private:
40 40 QPieSlice* pieSlice(QModelIndex index) const;
41 bool isLabelIndex(QModelIndex index) const;
42 bool isValueIndex(QModelIndex index) const;
41 43 QModelIndex valueModelIndex(int slicePos);
42 44 QModelIndex labelModelIndex(int slicePos);
43 45 void insertData(int start, int end);
44 46 void removeData(int start, int end);
45 47
46 48 void blockModelSignals(bool block = true);
47 49 void blockSeriesSignals(bool block = true);
48 50
49 51 private:
50 52 QPieSeries *m_series;
51 53 QList<QPieSlice*> m_slices;
52 54 QAbstractItemModel *m_model;
53 55 int m_first;
54 56 int m_count;
55 57 Qt::Orientation m_orientation;
56 58 int m_valuesIndex;
57 59 int m_labelsIndex;
58 60 bool m_seriesSignalsBlock;
59 61 bool m_modelSignalsBlock;
60 62
61 63 private:
62 64
63 65 QPieModelMapper *q_ptr;
64 66 Q_DECLARE_PUBLIC(QPieModelMapper)
65 67 friend class QPieSeriesPrivate;
66 68 };
67 69
68 70 QTCOMMERCIALCHART_END_NAMESPACE
69 71
70 72 #endif // QPIEMODELMAPPER_P_H
@@ -1,241 +1,270
1 1 #include <QtCore/QString>
2 2 #include <QtTest/QtTest>
3 3
4 4 #include <qchart.h>
5 5 #include <qchartview.h>
6 6 #include <qpieseries.h>
7 7 #include <qvpiemodelmapper.h>
8 8 #include <qhpiemodelmapper.h>
9 9 #include <QStandardItemModel>
10 10
11 11 QTCOMMERCIALCHART_USE_NAMESPACE
12 12
13 13 class tst_piemodelmapper : public QObject
14 14 {
15 15 Q_OBJECT
16 16
17 17 public:
18 18 tst_piemodelmapper();
19 19
20 20 private Q_SLOTS:
21 21 void initTestCase();
22 22 void cleanupTestCase();
23 23 void init();
24 24 void cleanup();
25 25 void verticalMapper_data();
26 26 void verticalMapper();
27 27 void verticalMapperCustomMapping_data();
28 28 void verticalMapperCustomMapping();
29 29 void horizontalMapper_data();
30 30 void horizontalMapper();
31 31 void horizontalMapperCustomMapping_data();
32 32 void horizontalMapperCustomMapping();
33 void seriesUpdated();
33 34
34 35
35 36 private:
36 37 QStandardItemModel *m_model;
37 38 int m_modelRowCount;
38 39 int m_modelColumnCount;
39 40
40 41 QPieSeries *m_series;
41 42 QChart *m_chart;
42 43 };
43 44
44 45 tst_piemodelmapper::tst_piemodelmapper():
45 46 m_model(0),
46 47 m_modelRowCount(10),
47 48 m_modelColumnCount(8)
48 49 {
49 50 }
50 51
51 52 void tst_piemodelmapper::init()
52 53 {
53 54 m_series = new QPieSeries;
54 55 m_chart->addSeries(m_series);
55 56 }
56 57
57 58 void tst_piemodelmapper::cleanup()
58 59 {
59 60 m_chart->removeSeries(m_series);
60 61 delete m_series;
61 62 m_series = 0;
62 63 }
63 64
64 65 void tst_piemodelmapper::initTestCase()
65 66 {
66 67 m_chart = new QChart;
67 68 QChartView *chartView = new QChartView(m_chart);
68 69 chartView->show();
69 70
70 71 m_model = new QStandardItemModel(this);
71 72 for (int row = 0; row < m_modelRowCount; ++row) {
72 73 for (int column = 0; column < m_modelColumnCount; column++) {
73 74 QStandardItem *item = new QStandardItem(row * column);
74 75 m_model->setItem(row, column, item);
75 76 }
76 77 }
77 78 }
78 79
79 80 void tst_piemodelmapper::cleanupTestCase()
80 81 {
81 82 m_model->clear();
82 83 }
83 84
84 85 void tst_piemodelmapper::verticalMapper_data()
85 86 {
86 87 QTest::addColumn<int>("valuesColumn");
87 88 QTest::addColumn<int>("labelsColumn");
88 89 QTest::addColumn<int>("expectedCount");
89 90 QTest::newRow("different values and labels columns") << 0 << 1 << m_modelRowCount;
90 91 QTest::newRow("same values and labels columns") << 1 << 1 << m_modelRowCount;
91 92 QTest::newRow("invalid values column and correct labels column") << -3 << 1 << 0;
92 93 QTest::newRow("values column beyond the size of model and correct labels column") << m_modelColumnCount << 1 << 0;
93 94 QTest::newRow("values column beyond the size of model and correct labels column") << m_modelColumnCount << -1 << 0;
94 95 }
95 96
96 97 void tst_piemodelmapper::verticalMapper()
97 98 {
98 99 QFETCH(int, valuesColumn);
99 100 QFETCH(int, labelsColumn);
100 101 QFETCH(int, expectedCount);
101 102
102 103 QVPieModelMapper *mapper = new QVPieModelMapper;
103 104 mapper->setValuesColumn(valuesColumn);
104 105 mapper->setLabelsColumn(labelsColumn);
105 106 mapper->setModel(m_model);
106 107 mapper->setSeries(m_series);
107 108
108 109 QCOMPARE(m_series->count(), expectedCount);
109 110 QCOMPARE(mapper->valuesColumn(), qMax(-1, valuesColumn));
110 111 QCOMPARE(mapper->labelsColumn(), qMax(-1, labelsColumn));
111 112
112 113 delete mapper;
113 114 mapper = 0;
114 115 }
115 116
116 117 void tst_piemodelmapper::verticalMapperCustomMapping_data()
117 118 {
118 119 QTest::addColumn<int>("first");
119 120 QTest::addColumn<int>("countLimit");
120 121 QTest::addColumn<int>("expectedCount");
121 122 QTest::newRow("first: 0, unlimited count") << 0 << -1 << m_modelRowCount;
122 123 QTest::newRow("first: 3, unlimited count") << 3 << -1 << m_modelRowCount - 3;
123 124 QTest::newRow("first: 0, count: 5") << 0 << 5 << qMin(5, m_modelRowCount);
124 125 QTest::newRow("first: 3, count: 5") << 3 << 5 << qMin(5, m_modelRowCount - 3);
125 126 QTest::newRow("first: +1 greater then the number of rows in the model, unlimited count") << m_modelRowCount + 1 << -1 << 0;
126 127 QTest::newRow("first: +1 greater then the number of rows in the model, count: 5") << m_modelRowCount + 1 << 5 << 0;
127 128 QTest::newRow("first: 0, count: +3 greater than the number of rows in the model (should limit to the size of model)") << 0 << m_modelRowCount + 3 << m_modelRowCount;
128 129 QTest::newRow("first: -3(invalid - should default to 0), unlimited count") << -3 << -1 << m_modelRowCount;
129 130 QTest::newRow("first: 0, count: -3 (invalid - shlould default to -1)") << 0 << -3 << m_modelRowCount;
130 131 QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << m_modelRowCount;
131 132
132 133 }
133 134
134 135 void tst_piemodelmapper::verticalMapperCustomMapping()
135 136 {
136 137 QFETCH(int, first);
137 138 QFETCH(int, countLimit);
138 139 QFETCH(int, expectedCount);
139 140
140 141 QCOMPARE(m_series->count(), 0);
141 142
142 143 QVPieModelMapper *mapper = new QVPieModelMapper;
143 144 mapper->setValuesColumn(0);
144 145 mapper->setLabelsColumn(1);
145 146 mapper->setModel(m_model);
146 147 mapper->setSeries(m_series);
147 148 mapper->setFirst(first);
148 149 mapper->setCount(countLimit);
149 150
150 151 QCOMPARE(m_series->count(), expectedCount);
151 152
152 // change values column mappings to invalid
153 // change values column mapping to invalid
153 154 mapper->setValuesColumn(-1);
154 155 mapper->setLabelsColumn(1);
155 156
156 157 QCOMPARE(m_series->count(), 0);
157 158
158 159 delete mapper;
159 160 mapper = 0;
160 161 }
161 162
162 163 void tst_piemodelmapper::horizontalMapper_data()
163 164 {
164 165 QTest::addColumn<int>("valuesRow");
165 166 QTest::addColumn<int>("labelsRow");
166 167 QTest::addColumn<int>("expectedCount");
167 168 QTest::newRow("different values and labels rows") << 0 << 1 << m_modelColumnCount;
168 169 QTest::newRow("same values and labels rows") << 1 << 1 << m_modelColumnCount;
169 170 QTest::newRow("invalid values row and correct labels row") << -3 << 1 << 0;
170 171 QTest::newRow("values row beyond the size of model and correct labels row") << m_modelRowCount << 1 << 0;
171 172 QTest::newRow("values row beyond the size of model and invalid labels row") << m_modelRowCount << -1 << 0;
172 173 }
173 174
174 175 void tst_piemodelmapper::horizontalMapper()
175 176 {
176 177 QFETCH(int, valuesRow);
177 178 QFETCH(int, labelsRow);
178 179 QFETCH(int, expectedCount);
179 180
180 181 QHPieModelMapper *mapper = new QHPieModelMapper;
181 182 mapper->setValuesRow(valuesRow);
182 183 mapper->setLabelsRow(labelsRow);
183 184 mapper->setModel(m_model);
184 185 mapper->setSeries(m_series);
185 186
186 187 QCOMPARE(m_series->count(), expectedCount);
187 188 QCOMPARE(mapper->valuesRow(), qMax(-1, valuesRow));
188 189 QCOMPARE(mapper->labelsRow(), qMax(-1, labelsRow));
189 190
190 191 delete mapper;
191 192 mapper = 0;
192 193 }
193 194
194 195 void tst_piemodelmapper::horizontalMapperCustomMapping_data()
195 196 {
196 197 QTest::addColumn<int>("first");
197 198 QTest::addColumn<int>("countLimit");
198 199 QTest::addColumn<int>("expectedCount");
199 200 QTest::newRow("first: 0, unlimited count") << 0 << -1 << m_modelColumnCount;
200 201 QTest::newRow("first: 3, unlimited count") << 3 << -1 << m_modelColumnCount - 3;
201 202 QTest::newRow("first: 0, count: 5") << 0 << 5 << qMin(5, m_modelColumnCount);
202 203 QTest::newRow("first: 3, count: 5") << 3 << 5 << qMin(5, m_modelColumnCount - 3);
203 204 QTest::newRow("first: +1 greater then the number of columns in the model, unlimited count") << m_modelColumnCount + 1 << -1 << 0;
204 205 QTest::newRow("first: +1 greater then the number of columns in the model, count: 5") << m_modelColumnCount + 1 << 5 << 0;
205 206 QTest::newRow("first: 0, count: +3 greater than the number of columns in the model (should limit to the size of model)") << 0 << m_modelColumnCount + 3 << m_modelColumnCount;
206 207 QTest::newRow("first: -3(invalid - should default to 0), unlimited count") << -3 << -1 << m_modelColumnCount;
207 208 QTest::newRow("first: 0, count: -3 (invalid - shlould default to -1)") << 0 << -3 << m_modelColumnCount;
208 209 QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << m_modelColumnCount;
209 210 }
210 211
211 212 void tst_piemodelmapper::horizontalMapperCustomMapping()
212 213 {
213 214 QFETCH(int, first);
214 215 QFETCH(int, countLimit);
215 216 QFETCH(int, expectedCount);
216 217
217 218 QCOMPARE(m_series->count(), 0);
218 219
219 220 QHPieModelMapper *mapper = new QHPieModelMapper;
220 221 mapper->setValuesRow(0);
221 222 mapper->setLabelsRow(1);
222 223 mapper->setModel(m_model);
223 224 mapper->setSeries(m_series);
224 225 mapper->setFirst(first);
225 226 mapper->setCount(countLimit);
226 227
227 228 QCOMPARE(m_series->count(), expectedCount);
228 229
229 // change values column mappings to invalid
230 // change values row mapping to invalid
230 231 mapper->setValuesRow(-1);
231 232 mapper->setLabelsRow(1);
232 233
233 234 QCOMPARE(m_series->count(), 0);
234 235
235 236 delete mapper;
236 237 mapper = 0;
237 238 }
238 239
240 void tst_piemodelmapper::seriesUpdated()
241 {
242 QStandardItemModel *otherModel = new QStandardItemModel;
243 for (int row = 0; row < m_modelRowCount; ++row) {
244 for (int column = 0; column < m_modelColumnCount; column++) {
245 QStandardItem *item = new QStandardItem(row * column);
246 otherModel->setItem(row, column, item);
247 }
248 }
249
250 QVPieModelMapper *mapper = new QVPieModelMapper;
251 mapper->setValuesColumn(0);
252 mapper->setLabelsColumn(1);
253 mapper->setModel(otherModel);
254 mapper->setSeries(m_series);
255 QCOMPARE(m_series->count(), m_modelRowCount);
256
257 m_series->append("1000", 1000);
258 QCOMPARE(m_series->count(), m_modelRowCount + 1);
259
260 m_series->remove(m_series->slices().last());
261 QCOMPARE(m_series->count(), m_modelRowCount);
262
263 otherModel->clear();
264 delete otherModel;
265 otherModel = 0;
266 }
267
239 268 QTEST_MAIN(tst_piemodelmapper)
240 269
241 270 #include "tst_qpiemodelmapper.moc"
@@ -1,519 +1,523
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 <QVPieModelMapper>
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 QPushButton* specialPieButton = new QPushButton("Add slices from series");
84 QPushButton* specialPieButton = new QPushButton("Add slices using series API");
85 85 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
86 86
87 QPushButton* specialPieButton2 = new QPushButton("Remove slices from series");
87 QPushButton* specialPieButton2 = new QPushButton("Remove slices using series API");
88 88 connect(specialPieButton2, SIGNAL(clicked()), this, SLOT(testPie2()));
89 89
90 QPushButton* specialPieButton3 = new QPushButton("Remove slices from series");
90 QPushButton* specialPieButton3 = new QPushButton("Modify slices using series API");
91 91 connect(specialPieButton3, SIGNAL(clicked()), this, SLOT(testPie3()));
92 92
93 93
94 94 // QLabel *spinBoxLabel = new QLabel("Rows affected:");
95 95
96 96 // spin box for setting number of affected items (add, remove)
97 97 m_linesCountSpinBox = new QSpinBox;
98 98 m_linesCountSpinBox->setRange(1, 10);
99 99 m_linesCountSpinBox->setValue(1);
100 100
101 101 // buttons layout
102 102 QVBoxLayout* buttonsLayout = new QVBoxLayout;
103 103 // buttonsLayout->addWidget(spinBoxLabel);
104 104 // buttonsLayout->addWidget(m_linesCountSpinBox);
105 105 // buttonsLayout->addWidget(addRowAboveButton);
106 106 buttonsLayout->addWidget(addRowBelowButton);
107 107 buttonsLayout->addWidget(removeRowButton);
108 108 // buttonsLayout->addWidget(addColumnRightButton);
109 109 // buttonsLayout->addWidget(removeColumnButton);
110 110 buttonsLayout->addWidget(specialPieButton);
111 111 buttonsLayout->addWidget(specialPieButton2);
112 112 buttonsLayout->addWidget(specialPieButton3);
113 113 buttonsLayout->addStretch();
114 114
115 115 // chart type radio buttons
116 116 m_lineRadioButton = new QRadioButton("Line");
117 117 m_splineRadioButton = new QRadioButton("Spline");
118 118 m_scatterRadioButton = new QRadioButton("Scatter");
119 119 m_pieRadioButton = new QRadioButton("Pie");
120 120 m_areaRadioButton = new QRadioButton("Area");
121 121 m_barRadioButton = new QRadioButton("Bar");
122 122
123 123 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
124 124 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
125 125 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
126 126 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
127 127 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
128 128 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
129 129 m_lineRadioButton->setChecked(true);
130 130
131 131 // radio buttons layout
132 132 QVBoxLayout* radioLayout = new QVBoxLayout;
133 133 radioLayout->addWidget(m_lineRadioButton);
134 134 radioLayout->addWidget(m_splineRadioButton);
135 135 // radioLayout->addWidget(m_scatterRadioButton);
136 136 radioLayout->addWidget(m_pieRadioButton);
137 137 // radioLayout->addWidget(m_areaRadioButton);
138 138 radioLayout->addWidget(m_barRadioButton);
139 139 radioLayout->addStretch();
140 140
141 141 // create main layout
142 142 QGridLayout* mainLayout = new QGridLayout;
143 143 mainLayout->addLayout(buttonsLayout, 2, 0);
144 144 mainLayout->addLayout(radioLayout, 3, 0);
145 145 mainLayout->addWidget(m_tableView, 1, 0);
146 146 mainLayout->addWidget(m_chartView, 1, 1, 2, 1);
147 147 setLayout(mainLayout);
148 148 m_lineRadioButton->setFocus();
149 149 }
150 150
151 151 void TableWidget::addRowAbove()
152 152 {
153 153 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
154 154
155 155 }
156 156
157 157 void TableWidget::addRowBelow()
158 158 {
159 159 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
160 160
161 161 }
162 162
163 163 void TableWidget::removeRow()
164 164 {
165 165 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
166 166 }
167 167
168 168 void TableWidget::addColumnRight()
169 169 {
170 170 m_model->insertColumns(m_tableView->currentIndex().column() + 1, m_linesCountSpinBox->value());
171 171 }
172 172
173 173 void TableWidget::removeColumn()
174 174 {
175 175 m_model->removeColumns(m_tableView->currentIndex().column(), qMin(m_model->columnCount() - m_tableView->currentIndex().column(), m_linesCountSpinBox->value()));
176 176 }
177 177
178 178 void TableWidget::updateChartType(bool toggle)
179 179 {
180 180 // this if is needed, so that the function is only called once.
181 181 // For the radioButton that was enabled.
182 182 if (toggle) {
183 183 // specialPie = 0;
184 184 m_chart->removeAllSeries();
185 185 // m_chart->axisX()->setNiceNumbersEnabled(false);
186 186 // m_chart->axisY()->setNiceNumbersEnabled(false);
187 187
188 188 // // renable axes of the chart (pie hides them)
189 189 // // x axis
190 190 // QAxis *axis = m_chart->axisX();
191 191 // axis->setAxisVisible(true);
192 192 // axis->setGridLineVisible(true);
193 193 // axis->setLabelsVisible(true);
194 194
195 195 // // y axis
196 196 // axis = m_chart->axisY();
197 197 // axis->setAxisVisible(true);
198 198 // axis->setGridLineVisible(true);
199 199 // axis->setLabelsVisible(true);
200 200
201 201 // m_model->clearMapping();
202 202
203 203 QString seriesColorHex = "#000000";
204 204 // QPen pen;
205 205 // pen.setWidth(2);
206 206
207 207 if (m_lineRadioButton->isChecked())
208 208 {
209 209 // m_chart->setAnimationOptions(QChart::NoAnimation);
210 210
211 211 // // series 1
212 212 // m_series = new QLineSeries;
213 213 // m_series->setModel(m_model);
214 214
215 215 // QXYModelMapper *mapper = new QXYModelMapper;
216 216 // mapper->setMapX(0);
217 217 // mapper->setMapY(1);
218 218 // mapper->setFirst(3);
219 219 // mapper->setCount(4);
220 220 // m_series->setModelMapper(mapper);
221 221 // // m_series->setModelMapping(0,1, Qt::Vertical);
222 222 // // m_series->setModelMappingRange(3, 4);
223 223 // m_chart->addSeries(m_series);
224 224 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
225 225 // m_model->addMapping(seriesColorHex, QRect(0, 3, 2, 4));
226 226
227 227 // // series 2
228 228 // m_series = new QLineSeries;
229 229 // m_series->setModel(m_model);
230 230
231 231 // mapper = new QXYModelMapper;
232 232 // mapper->setMapX(3);
233 233 // mapper->setMapY(4);
234 234 // // mapper->setFirst(3);
235 235 // // mapper->setCount(4);
236 236 // m_series->setModelMapper(mapper);
237 237 // // m_series->setModelMapping(2,3, Qt::Vertical);
238 238 // m_chart->addSeries(m_series);
239 239 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
240 240 // m_model->addMapping(seriesColorHex, QRect(3, 0, 2, 1000));
241 241
242 242 // // series 3
243 243 // m_series = new QLineSeries;
244 244 // m_series->setModel(m_model);
245 245
246 246 // mapper = new QXYModelMapper;
247 247 // mapper->setMapX(5);
248 248 // mapper->setMapY(6);
249 249 // mapper->setFirst(2);
250 250 // mapper->setCount(-1);
251 251 // m_series->setModelMapper(mapper);
252 252 // // m_series->setModelMapping(4,5, Qt::Vertical);
253 253 // // m_series->setModelMappingRange(2, -1);
254 254 // m_chart->addSeries(m_series);
255 255 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
256 256 // m_model->addMapping(seriesColorHex, QRect(5, 2, 2, 1000));
257 257 }
258 258 else if (m_splineRadioButton->isChecked())
259 259 {
260 260 // m_chart->setAnimationOptions(QChart::NoAnimation);
261 261
262 262 // // series 1
263 263 // m_series = new QSplineSeries;
264 264 // m_series->setModel(m_model);
265 265
266 266 // QXYModelMapper *mapper = new QXYModelMapper;
267 267 // mapper->setMapX(0);
268 268 // mapper->setMapY(1);
269 269 // mapper->setFirst(0);
270 270 // mapper->setCount(-1);
271 271
272 272 // m_series->setModelMapper(mapper);
273 273
274 274 // m_chart->addSeries(m_series);
275 275 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
276 276 // m_model->addMapping(seriesColorHex, QRect(0, 0, 2, 1000));
277 277
278 278 // // series 2
279 279 // m_series = new QSplineSeries;
280 280 // m_series->setModel(m_model);
281 281
282 282 // mapper = new QXYModelMapper;
283 283 // mapper->setMapX(2);
284 284 // mapper->setMapY(3);
285 285 // mapper->setFirst(2);
286 286 // mapper->setCount(4);
287 287
288 288 // m_series->setModelMapper(mapper);
289 289
290 290 // m_chart->addSeries(m_series);
291 291 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
292 292 // m_model->addMapping(seriesColorHex, QRect(2, 2, 2, 4));
293 293
294 294 // // series 3
295 295 // m_series = new QSplineSeries;
296 296 // m_series->setModel(m_model);
297 297
298 298 // mapper = new QXYModelMapper;
299 299 // mapper->setMapX(4);
300 300 // mapper->setMapY(5);
301 301 // mapper->setFirst(2);
302 302 // mapper->setCount(-1);
303 303
304 304 // m_series->setModelMapper(mapper);
305 305
306 306 // m_chart->addSeries(m_series);
307 307 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
308 308 // m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
309 309 }
310 310 // else if (m_scatterRadioButton->isChecked())
311 311 // {
312 312 // m_chart->setAnimationOptions(QChart::NoAnimation);
313 313
314 314 // // series 1
315 315 // m_series = new QScatterSeries;
316 316 // m_series->setModel(m_model);
317 317 // m_series->setModelMapping(0,1, Qt::Vertical);
318 318 // // m_series->setModelMappingRange(2, 0);
319 319 // // series->setModelMapping(0,1, Qt::Horizontal);
320 320 // m_chart->addSeries(m_series);
321 321
322 322 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
323 323 // m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
324 324
325 325 // // series 2
326 326 // m_series = new QScatterSeries;
327 327 // m_series->setModel(m_model);
328 328 // m_series->setModelMapping(2,3, Qt::Vertical);
329 329 // // m_series->setModelMappingRange(1, 6);
330 330 // // series->setModelMapping(2,3, Qt::Horizontal);
331 331 // m_chart->addSeries(m_series);
332 332
333 333 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
334 334 // m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
335 335
336 336 // // series 3
337 337 // m_series = new QScatterSeries;
338 338 // m_series->setModel(m_model);
339 339 // m_series->setModelMapping(4,5, Qt::Vertical);
340 340 // // series->setModelMapping(4,5, Qt::Horizontal);
341 341 // m_chart->addSeries(m_series);
342 342 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
343 343 // m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
344 344 // }
345 345 else if (m_pieRadioButton->isChecked())
346 346 {
347 347 m_chart->setAnimationOptions(QChart::SeriesAnimations);
348 348
349 349 // pie 1
350 350 m_pieSeries = new QPieSeries;
351 351
352 352 m_pieMapper = new QVPieModelMapper;
353 353 m_pieMapper->setValuesColumn(1);
354 354 m_pieMapper->setLabelsColumn(7);
355 355 m_pieMapper->setSeries(m_pieSeries);
356 356 m_pieMapper->setModel(m_model);
357 357 m_pieMapper->setFirst(2);
358 // m_pieMapper->setCount(5);
358 // m_pieMapper->setCount(5);
359 359 // pieSeries->setModelMapper(mapper);
360 360
361 361 m_pieSeries->setLabelsVisible(true);
362 m_pieSeries->setPieSize(0.75);
363 // pieSeries->setHorizontalPosition(0.2);
364 // pieSeries->setVerticalPosition(0.3);
362 m_pieSeries->setPieSize(0.35);
363 m_pieSeries->setHorizontalPosition(0.25);
364 m_pieSeries->setVerticalPosition(0.35);
365 365
366 366 m_chart->addSeries(m_pieSeries);
367 367 seriesColorHex = "#" + QString::number(m_pieSeries->slices().at(m_pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
368 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 5));
368 m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 50));
369 369
370 370
371 371 // pieSeries->slices().at(0)->setValue(400);
372 372 // pieSeries->slices().at(0)->setLabel(QString("36"));
373 373
374 // // pie 2
375 // pieSeries = new QPieSeries;
376 // pieSeries->setModel(m_model);
374 // pie 2
375 m_pieSeries2 = new QPieSeries;
377 376
378 // pieSeries->setModelMapping(1,1, Qt::Vertical);
379 // pieSeries->setModelMappingRange(2, -1);
380 // pieSeries->setLabelsVisible(true);
381 // pieSeries->setPieSize(0.35);
382 // pieSeries->setHorizontalPosition(0.8);
383 // pieSeries->setVerticalPosition(0.3);
384 // m_chart->addSeries(pieSeries);
385 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
386 // m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 1000));
377 m_pieMapper = new QVPieModelMapper;
378 m_pieMapper->setValuesColumn(0);
379 m_pieMapper->setLabelsColumn(7);
380 m_pieMapper->setSeries(m_pieSeries2);
381 m_pieMapper->setModel(m_model);
382 m_pieMapper->setFirst(2);
383
384 m_pieSeries2->setLabelsVisible(true);
385 m_pieSeries2->setPieSize(0.35);
386 m_pieSeries2->setHorizontalPosition(0.75);
387 m_pieSeries2->setVerticalPosition(0.65);
388 m_chart->addSeries(m_pieSeries2);
389 seriesColorHex = "#" + QString::number(m_pieSeries2->slices().at(m_pieSeries2->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
390 m_model->addMapping(seriesColorHex, QRect(0, 2, 1, 1000));
387 391
388 392 // // pie 3
389 393 // pieSeries = new QPieSeries;
390 394 // pieSeries->setModel(m_model);
391 395 // pieSeries->setModelMapping(2,2, Qt::Vertical);
392 396 // pieSeries->setLabelsVisible(true);
393 397 // pieSeries->setPieSize(0.35);
394 398 // pieSeries->setHorizontalPosition(0.5);
395 399 // pieSeries->setVerticalPosition(0.75);
396 400 // m_chart->addSeries(pieSeries);
397 401 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
398 402 // m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
399 403
400 404 // // special pie
401 405 // specialPie = new QPieSeries;
402 406 // specialPie->append(17, "1");
403 407 // specialPie->append(45, "2");
404 408 // specialPie->append(77, "3");
405 409 // specialPie->append(37, "4");
406 410 // specialPie->append(27, "5");
407 411 // specialPie->append(47, "6");
408 412 // specialPie->setPieSize(0.35);
409 413 // specialPie->setHorizontalPosition(0.8);
410 414 // specialPie->setVerticalPosition(0.75);
411 415 // specialPie->setLabelsVisible(true);
412 416 // m_chart->addSeries(specialPie);
413 417 }
414 418 // else if (m_areaRadioButton->isChecked())
415 419 // {
416 420 // m_chart->setAnimationOptions(QChart::NoAnimation);
417 421
418 422 // QLineSeries* upperLineSeries = new QLineSeries;
419 423 // upperLineSeries->setModel(m_model);
420 424 // upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
421 425 // // upperLineSeries->setModelMappingRange(1, 5);
422 426 // QLineSeries* lowerLineSeries = new QLineSeries;
423 427 // lowerLineSeries->setModel(m_model);
424 428 // lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
425 429 // QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
426 430 // m_chart->addSeries(areaSeries);
427 431 // seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
428 432 // m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
429 433 // m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
430 434 // }
431 435 else if (m_barRadioButton->isChecked())
432 436 {
433 437 // m_chart->setAnimationOptions(QChart::SeriesAnimations);
434 438
435 439 // QGroupedBarSeries* barSeries = new QGroupedBarSeries();
436 440 // barSeries->setCategories(QStringList());
437 441 // barSeries->setModel(m_model);
438 442 // // barSeries->setModelMappingRange(2, 5);
439 443 //// barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
440 444
441 445 // QBarModelMapper *mapper = new QBarModelMapper;
442 446 // mapper->setMapCategories(5);
443 447 // mapper->setMapBarBottom(2);
444 448 // mapper->setMapBarTop(4);
445 449 // barSeries->setModelMapper(mapper);
446 450 // m_chart->addSeries(barSeries);
447 451 // QList<QBarSet*> barsets = barSeries->barSets();
448 452 // for (int i = 0; i < barsets.count(); i++) {
449 453 // seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
450 454 // m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
451 455 // }
452 456 }
453 457
454 458
455 459 if (!m_barRadioButton->isChecked()) {
456 460 m_chart->axisX()->setRange(0, 500);
457 461 m_chart->axisY()->setRange(0, 220);
458 462 }
459 463 m_chart->legend()->setVisible(true);
460 464
461 465 // repaint table view colors
462 466 m_tableView->repaint();
463 467 m_tableView->setFocus();
464 468 }
465 469 }
466 470
467 471 void TableWidget::testPie()
468 472 {
469 473 // m_pieMapper->setCount(-1);
470 474 QPieSlice *slice = new QPieSlice("Hehe", 145);
471 475 slice->setLabelVisible();
472 476 m_pieSeries->append(slice);
473 477
474 478 slice = new QPieSlice("Hoho", 34);
475 479 slice->setLabelVisible();
476 480 m_pieSeries->append(slice);
477 481 // m_series->modelMapper()->setMapX(4);
478 482 // m_tableView->setColumnWidth(10, 250);
479 483 // if (specialPie) {
480 484 // specialPie->remove(specialPie->slices().at(2));
481 485 // // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2));
482 486 // specialPie->append(4, "heloo");
483 487 // }
484 488 }
485 489
486 490 void TableWidget::testPie2()
487 491 {
488 492 QPieSlice *slice;
489 493 if (m_pieSeries->count() > 0) {
490 494 slice = m_pieSeries->slices().last();
491 495 m_pieSeries->remove(slice);
492 496 }
493 497
494 498 if (m_pieSeries->count() > 0) {
495 499 slice = m_pieSeries->slices().first();
496 500 m_pieSeries->remove(slice);
497 501 }
498 502 }
499 503
500 504 void TableWidget::testPie3()
501 505 {
502 506 QPieSlice *slice;
503 507 if (m_pieSeries->count() > 0) {
504 508 slice = m_pieSeries->slices().last();
505 509 slice->setLabel("Dalej");
506 510 slice->setValue(222);
507 511 }
508 512
509 513 if (m_pieSeries->count() > 0) {
510 514 slice = m_pieSeries->slices().first();
511 515 slice->setLabel("Prawie");
512 516 slice->setValue(111);
513 517 }
514 518 }
515 519
516 520 TableWidget::~TableWidget()
517 521 {
518 522
519 523 }
@@ -1,76 +1,78
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 #ifndef TABLEWIDGET_H
22 22 #define TABLEWIDGET_H
23 23
24 24 #include <QtGui/QWidget>
25 25 //#include <QChartGlobal>
26 26 #include "qchartview.h"
27 27 //#include "qxyseries.h"
28 28 #include <QPieSeries>
29 29 #include <QVPieModelMapper>
30 30
31 31 class CustomTableModel;
32 32 class QTableView;
33 33 class QRadioButton;
34 34 class QSpinBox;
35 35
36 36 QTCOMMERCIALCHART_USE_NAMESPACE
37 37
38 38 class TableWidget : public QWidget
39 39 {
40 40 Q_OBJECT
41 41
42 42 public:
43 43 TableWidget(QWidget *parent = 0);
44 44 ~TableWidget();
45 45
46 46
47 47 public slots:
48 48 void addRowAbove();
49 49 void addRowBelow();
50 50 void removeRow();
51 51 void addColumnRight();
52 52 void removeColumn();
53 53 void updateChartType(bool toggle);
54 54 void testPie();
55 55 void testPie2();
56 56 void testPie3();
57 57
58 58 private:
59 59 QChartView* m_chartView;
60 60 QChart* m_chart;
61 61 QXYSeries* m_series;
62 62 CustomTableModel* m_model;
63 63 QTableView* m_tableView;
64 64 QRadioButton* m_lineRadioButton;
65 65 QRadioButton* m_splineRadioButton;
66 66 QRadioButton* m_scatterRadioButton;
67 67 QRadioButton* m_pieRadioButton;
68 68 QRadioButton* m_areaRadioButton;
69 69 QRadioButton* m_barRadioButton;
70 70 QSpinBox* m_linesCountSpinBox;
71 71 QVPieModelMapper *m_pieMapper;
72 72 QPieSeries* m_pieSeries;
73 QVPieModelMapper *m_pieMapper2;
74 QPieSeries* m_pieSeries2;
73 75 // QPieSeries* specialPie;
74 76 };
75 77
76 78 #endif // TABLEWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now