##// END OF EJS Templates
QML bar model mapper documentation
Tero Ahola -
r1497:c1097d2a6ae8
parent child
Show More
@@ -1,630 +1,612
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 "qbarmodelmapper.h"
22 22 #include "qbarmodelmapper_p.h"
23 23 #include "qbarseries.h"
24 24 #include "qbarset.h"
25 25 #include "qchart.h"
26 26 #include <QAbstractItemModel>
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 30 /*!
31 31 \class QBarModelMapper
32 32 \brief part of QtCommercial chart API.
33 33 \mainclass
34 34
35 35 Model mappers allow you to use QAbstractItemModel derived models as a data source for a chart series.
36 36 The instance of this class cannot be created directly. QHBarModelMapper of QVBarModelMapper should be used instead.
37 37 This class is used to create a connection between QBarSeries and QAbstractItemModel derived model object.
38 38 Model mapper maintains equal size of all the BarSets.
39 39 Adding/removing value from the BarSet causes the the same change in the rest of the BarSets added to the same series.
40 40 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
41 41 */
42 /*!
43 \qmlclass BarModelMapper
44 Cannot be created by the user. Base for HBarModelMapper and VBarModelMapper. Model mappers allow you to use
45 QAbstractItemModel derived models as a data source for a chart series. Adding/removing value from the BarSet causes
46 the the same change in the rest of the BarSets added to the same series.
47 */
48 42
49 43 /*!
50 44 \property QBarModelMapper::series
51 45 \brief Defines the QPieSeries object that is used by the mapper.
52 46 All the data in the series is discarded when it is set to the mapper.
53 47 When new series is specified the old series is disconnected (it preserves its data)
54 48 */
55 /*!
56 \qmlproperty BarSeries BarModelMapper::series
57 Defines the BarSeries based object that is used by the mapper. All the data in the series is
58 discarded when it is set to the mapper. When new series is specified the old series is
59 disconnected (it preserves its data).
60 */
61 49
62 50 /*!
63 51 \property QBarModelMapper::model
64 52 \brief Defines the model that is used by the mapper.
65 53 */
66 /*!
67 \qmlproperty Model BarModelMapper::model
68 The QAbstractItemModel based model that is used by the mapper. You need to implement the model and expose it to
69 QML as shown in \l {QML Custom Model} demo application. NOTE: the model has to support adding/removing rows/columns
70 and modifying the data of the cells.
71 */
72 54
73 55 /*!
74 56 \fn void QBarModelMapper::seriesReplaced()
75 57 Emitted when the series to which mapper is connected to has changed.
76 58 */
77 59
78 60 /*!
79 61 \fn void QBarModelMapper::modelReplaced()
80 62 Emitted when the model to which mapper is connected to has changed.
81 63 */
82 64
83 65 /*!
84 66 Constructs a mapper object which is a child of \a parent.
85 67 */
86 68 QBarModelMapper::QBarModelMapper(QObject *parent) :
87 69 QObject(parent),
88 70 d_ptr(new QBarModelMapperPrivate(this))
89 71 {
90 72 }
91 73
92 74 QAbstractItemModel* QBarModelMapper::model() const
93 75 {
94 76 Q_D(const QBarModelMapper);
95 77 return d->m_model;
96 78 }
97 79
98 80 void QBarModelMapper::setModel(QAbstractItemModel *model)
99 81 {
100 82 if (model == 0)
101 83 return;
102 84
103 85 Q_D(QBarModelMapper);
104 86 if (d->m_model) {
105 87 disconnect(d->m_model, 0, d, 0);
106 88 }
107 89
108 90 d->m_model = model;
109 91 d->initializeBarFromModel();
110 92 // connect signals from the model
111 93 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
112 94 connect(d->m_model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)), d, SLOT(modelHeaderDataUpdated(Qt::Orientation,int,int)));
113 95 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
114 96 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
115 97 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
116 98 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
117 99
118 100 emit modelReplaced();
119 101 }
120 102
121 103 QBarSeries* QBarModelMapper::series() const
122 104 {
123 105 Q_D(const QBarModelMapper);
124 106 return d->m_series;
125 107 }
126 108
127 109 void QBarModelMapper::setSeries(QBarSeries *series)
128 110 {
129 111 Q_D(QBarModelMapper);
130 112 if (d->m_series) {
131 113 disconnect(d->m_series, 0, d, 0);
132 114 }
133 115
134 116 if (series == 0)
135 117 return;
136 118
137 119 d->m_series = series;
138 120 d->initializeBarFromModel();
139 121 // connect the signals from the series
140 122 connect(d->m_series, SIGNAL(barsetsAdded(QList<QBarSet*>)), d, SLOT(barSetsAdded(QList<QBarSet*>)));
141 123 connect(d->m_series, SIGNAL(barsetsRemoved(QList<QBarSet*>)), d, SLOT(barSetsRemoved(QList<QBarSet*>)));
142 124
143 125 emit seriesReplaced();
144 126 }
145 127
146 128 /*!
147 129 Returns which row/column of the model contains the first values of the QBarSets in the series.
148 130 The default value is 0.
149 131 */
150 132 int QBarModelMapper::first() const
151 133 {
152 134 Q_D(const QBarModelMapper);
153 135 return d->m_first;
154 136 }
155 137
156 138 /*!
157 139 Sets which row of the model contains the \a first values of the QBarSets in the series.
158 140 The default value is 0.
159 141 */
160 142 void QBarModelMapper::setFirst(int first)
161 143 {
162 144 Q_D(QBarModelMapper);
163 145 d->m_first = qMax(first, 0);
164 146 d->initializeBarFromModel();
165 147 }
166 148
167 149 /*!
168 150 Returns the number of rows/columns of the model that are mapped as the data for QBarSeries
169 151 Minimal and default value is: -1 (count limited by the number of rows/columns in the model)
170 152 */
171 153 int QBarModelMapper::count() const
172 154 {
173 155 Q_D(const QBarModelMapper);
174 156 return d->m_count;
175 157 }
176 158
177 159 /*!
178 160 Sets the \a count of rows/columns of the model that are mapped as the data for QBarSeries
179 161 Minimal and default value is: -1 (count limited by the number of rows/columns in the model)
180 162 */
181 163 void QBarModelMapper::setCount(int count)
182 164 {
183 165 Q_D(QBarModelMapper);
184 166 d->m_count = qMax(count, -1);
185 167 d->initializeBarFromModel();
186 168 }
187 169
188 170 /*!
189 171 Returns the orientation that is used when QBarModelMapper accesses the model.
190 172 This mean whether the consecutive values of the bar set are read from row (Qt::Horizontal)
191 173 or from columns (Qt::Vertical)
192 174 */
193 175 Qt::Orientation QBarModelMapper::orientation() const
194 176 {
195 177 Q_D(const QBarModelMapper);
196 178 return d->m_orientation;
197 179 }
198 180
199 181 /*!
200 182 Returns the \a orientation that is used when QBarModelMapper accesses the model.
201 183 This mean whether the consecutive values of the pie are read from row (Qt::Horizontal)
202 184 or from columns (Qt::Vertical)
203 185 */
204 186 void QBarModelMapper::setOrientation(Qt::Orientation orientation)
205 187 {
206 188 Q_D(QBarModelMapper);
207 189 d->m_orientation = orientation;
208 190 d->initializeBarFromModel();
209 191 }
210 192
211 193 /*!
212 194 Returns which section of the model is used as the data source for the first bar set
213 195 */
214 196 int QBarModelMapper::firstBarSetSection() const
215 197 {
216 198 Q_D(const QBarModelMapper);
217 199 return d->m_firstBarSetSection;
218 200 }
219 201
220 202 /*!
221 203 Sets the model section that is used as the data source for the first bar set
222 204 Parameter \a firstBarSetSection specifies the section of the model.
223 205 */
224 206 void QBarModelMapper::setFirstBarSetSection(int firstBarSetSection)
225 207 {
226 208 Q_D(QBarModelMapper);
227 209 d->m_firstBarSetSection = qMax(-1, firstBarSetSection);
228 210 d->initializeBarFromModel();
229 211 }
230 212
231 213 /*!
232 214 Returns which section of the model is used as the data source for the last bar set
233 215 */
234 216 int QBarModelMapper::lastBarSetSection() const
235 217 {
236 218 Q_D(const QBarModelMapper);
237 219 return d->m_lastBarSetSection;
238 220 }
239 221
240 222 /*!
241 223 Sets the model section that is used as the data source for the last bar set
242 224 Parameter \a lastBarSetSection specifies the section of the model.
243 225 */
244 226 void QBarModelMapper::setLastBarSetSection(int lastBarSetSection)
245 227 {
246 228 Q_D(QBarModelMapper);
247 229 d->m_lastBarSetSection = qMax(-1, lastBarSetSection);
248 230 d->initializeBarFromModel();
249 231 }
250 232
251 233 /*!
252 234 Resets the QBarModelMapper to the default state.
253 235 first: 0; count: -1; firstBarSetSection: -1; lastBarSetSection: -1; categoriesSection: -1
254 236 */
255 237 void QBarModelMapper::reset()
256 238 {
257 239 Q_D(QBarModelMapper);
258 240 d->m_first = 0;
259 241 d->m_count = -1;
260 242 d->m_firstBarSetSection = -1;
261 243 d->m_lastBarSetSection = -1;
262 244 d->initializeBarFromModel();
263 245 }
264 246
265 247 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
266 248
267 249 QBarModelMapperPrivate::QBarModelMapperPrivate(QBarModelMapper *q) :
268 250 m_series(0),
269 251 m_model(0),
270 252 m_first(0),
271 253 m_count(-1),
272 254 m_orientation(Qt::Vertical),
273 255 m_firstBarSetSection(-1),
274 256 m_lastBarSetSection(-1),
275 257 m_seriesSignalsBlock(false),
276 258 m_modelSignalsBlock(false),
277 259 q_ptr(q)
278 260 {
279 261 }
280 262
281 263 void QBarModelMapperPrivate::blockModelSignals(bool block)
282 264 {
283 265 m_modelSignalsBlock = block;
284 266 }
285 267
286 268 void QBarModelMapperPrivate::blockSeriesSignals(bool block)
287 269 {
288 270 m_seriesSignalsBlock = block;
289 271 }
290 272
291 273 QBarSet* QBarModelMapperPrivate::barSet(QModelIndex index)
292 274 {
293 275 if (!index.isValid())
294 276 return 0;
295 277
296 278 if (m_orientation == Qt::Vertical && index.column() >= m_firstBarSetSection && index.column() <= m_lastBarSetSection) {
297 279 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) {
298 280 // if (m_model->index(index.row(), m_valuesSection).isValid() && m_model->index(index.row(), m_labelsSection).isValid())
299 281 return m_series->barSets().at(index.column() - m_firstBarSetSection);
300 282 // else
301 283 // return 0;
302 284 }
303 285 } else if (m_orientation == Qt::Horizontal && index.row() >= m_firstBarSetSection && index.row() <= m_lastBarSetSection) {
304 286 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
305 287 return m_series->barSets().at(index.row() - m_firstBarSetSection);
306 288 }
307 289 return 0; // This part of model has not been mapped to any slice
308 290 }
309 291
310 292 QModelIndex QBarModelMapperPrivate::barModelIndex(int barSection, int posInBar)
311 293 {
312 294 if (m_count != -1 && posInBar >= m_count)
313 295 return QModelIndex(); // invalid
314 296
315 297 if (barSection < m_firstBarSetSection || barSection > m_lastBarSetSection)
316 298 return QModelIndex(); // invalid
317 299
318 300 if (m_orientation == Qt::Vertical)
319 301 return m_model->index(posInBar + m_first, barSection);
320 302 else
321 303 return m_model->index(barSection, posInBar + m_first);
322 304 }
323 305
324 306 void QBarModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
325 307 {
326 308 Q_UNUSED(topLeft)
327 309 Q_UNUSED(bottomRight)
328 310
329 311 if (m_model == 0 || m_series == 0)
330 312 return;
331 313
332 314 if (m_modelSignalsBlock)
333 315 return;
334 316
335 317 blockSeriesSignals();
336 318 QModelIndex index;
337 319 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
338 320 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
339 321 index = topLeft.sibling(row, column);
340 322 QBarSet* bar = barSet(index);
341 323 if (bar) {
342 324 if (m_orientation == Qt::Vertical)
343 325 bar->replace(row - m_first, m_model->data(index).toReal());
344 326 else
345 327 bar->replace(column - m_first, m_model->data(index).toReal());
346 328 }
347 329 }
348 330 }
349 331 blockSeriesSignals(false);
350 332 }
351 333
352 334 void QBarModelMapperPrivate::modelHeaderDataUpdated(Qt::Orientation orientation, int first, int last)
353 335 {
354 336 if (m_model == 0 || m_series == 0)
355 337 return;
356 338
357 339 if (m_modelSignalsBlock)
358 340 return;
359 341
360 342 blockSeriesSignals();
361 343 if (orientation != m_orientation) {
362 344 for (int section = first; section <= last; section++) {
363 345 if (section >= m_firstBarSetSection && section <= m_lastBarSetSection) {
364 346 QBarSet* bar = m_series->barSets().at(section - m_firstBarSetSection);
365 347 if (bar)
366 348 bar->setLabel(m_model->headerData(section, orientation).toString());
367 349 }
368 350 }
369 351 }
370 352 blockSeriesSignals(false);
371 353 }
372 354
373 355 void QBarModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
374 356 {
375 357 Q_UNUSED(parent);
376 358 Q_UNUSED(end)
377 359 if (m_modelSignalsBlock)
378 360 return;
379 361
380 362 blockSeriesSignals();
381 363 if (m_orientation == Qt::Vertical)
382 364 // insertData(start, end);
383 365 initializeBarFromModel();
384 366 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
385 367 initializeBarFromModel();
386 368 blockSeriesSignals(false);
387 369 }
388 370
389 371 void QBarModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
390 372 {
391 373 Q_UNUSED(parent);
392 374 Q_UNUSED(end)
393 375 if (m_modelSignalsBlock)
394 376 return;
395 377
396 378 blockSeriesSignals();
397 379 if (m_orientation == Qt::Vertical)
398 380 // removeData(start, end);
399 381 initializeBarFromModel();
400 382 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
401 383 initializeBarFromModel();
402 384 blockSeriesSignals(false);
403 385 }
404 386
405 387 void QBarModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
406 388 {
407 389 Q_UNUSED(parent);
408 390 Q_UNUSED(end)
409 391 if (m_modelSignalsBlock)
410 392 return;
411 393
412 394 blockSeriesSignals();
413 395 if (m_orientation == Qt::Horizontal)
414 396 // insertData(start, end);
415 397 initializeBarFromModel();
416 398 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
417 399 initializeBarFromModel();
418 400 blockSeriesSignals(false);
419 401 }
420 402
421 403 void QBarModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
422 404 {
423 405 Q_UNUSED(parent);
424 406 Q_UNUSED(end)
425 407 if (m_modelSignalsBlock)
426 408 return;
427 409
428 410 blockSeriesSignals();
429 411 if (m_orientation == Qt::Horizontal)
430 412 // removeData(start, end);
431 413 initializeBarFromModel();
432 414 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
433 415 initializeBarFromModel();
434 416 blockSeriesSignals(false);
435 417 }
436 418
437 419 void QBarModelMapperPrivate::insertData(int start, int end)
438 420 {
439 421 Q_UNUSED(end)
440 422 Q_UNUSED(start)
441 423 Q_UNUSED(end)
442 424 // To be implemented
443 425 }
444 426
445 427 void QBarModelMapperPrivate::removeData(int start, int end)
446 428 {
447 429 Q_UNUSED(end)
448 430 Q_UNUSED(start)
449 431 Q_UNUSED(end)
450 432 // To be implemented
451 433 }
452 434
453 435 void QBarModelMapperPrivate::barSetsAdded(QList<QBarSet*> sets)
454 436 {
455 437 if (m_seriesSignalsBlock)
456 438 return;
457 439
458 440 if (sets.count() == 0)
459 441 return;
460 442
461 443 int firstIndex = m_series->barSets().indexOf(sets.at(0));
462 444 if (firstIndex == -1)
463 445 return;
464 446
465 447 int maxCount = 0;
466 448 for(int i = 0; i < sets.count(); i++)
467 449 if (sets.at(i)->count() > m_count)
468 450 maxCount = sets.at(i)->count();
469 451
470 452 if (m_count != -1 && m_count < maxCount)
471 453 m_count = maxCount;
472 454
473 455 m_lastBarSetSection += sets.count();
474 456
475 457 blockModelSignals();
476 458 int modelCapacity = m_orientation == Qt::Vertical ? m_model->rowCount() - m_first : m_model->columnCount() - m_first;
477 459 if (maxCount > modelCapacity) {
478 460 if (m_orientation == Qt::Vertical)
479 461 m_model->insertRows(m_model->rowCount(), maxCount - modelCapacity);
480 462 else
481 463 m_model->insertColumns(m_model->columnCount(), maxCount - modelCapacity);
482 464 }
483 465
484 466 if (m_orientation == Qt::Vertical)
485 467 m_model->insertColumns(firstIndex + m_firstBarSetSection, sets.count());
486 468 else
487 469 m_model->insertRows(firstIndex + m_firstBarSetSection, sets.count());
488 470
489 471
490 472 for(int i = firstIndex + m_firstBarSetSection; i < firstIndex + m_firstBarSetSection + sets.count(); i++) {
491 473 m_model->setHeaderData(i, m_orientation == Qt::Vertical ? Qt::Horizontal : Qt::Vertical, sets.at(i - firstIndex - m_firstBarSetSection)->label());
492 474 for (int j = 0; j < sets.at(i - firstIndex - m_firstBarSetSection)->count(); j++)
493 475 m_model->setData(barModelIndex(i, j), sets.at(i - firstIndex - m_firstBarSetSection)->at(j).y());
494 476 }
495 477 blockModelSignals(false);
496 478 initializeBarFromModel();
497 479 }
498 480
499 481 void QBarModelMapperPrivate::barSetsRemoved(QList<QBarSet*> sets)
500 482 {
501 483 if (m_seriesSignalsBlock)
502 484 return;
503 485
504 486 if (sets.count() == 0)
505 487 return;
506 488
507 489 int firstIndex = m_barSets.indexOf(sets.at(0));
508 490 if (firstIndex == -1)
509 491 return;
510 492
511 493 m_lastBarSetSection -= sets.count();
512 494
513 495 for (int i = firstIndex + sets.count() - 1; i >= firstIndex; i--)
514 496 m_barSets.removeAt(i);
515 497
516 498 blockModelSignals();
517 499 if (m_orientation == Qt::Vertical)
518 500 m_model->removeColumns(firstIndex + m_firstBarSetSection, sets.count());
519 501 else
520 502 m_model->removeRows(firstIndex + m_firstBarSetSection, sets.count());
521 503 blockModelSignals(false);
522 504 initializeBarFromModel();
523 505 }
524 506
525 507 void QBarModelMapperPrivate::valuesAdded(int index, int count)
526 508 {
527 509 if (m_seriesSignalsBlock)
528 510 return;
529 511
530 512 if (m_count != -1)
531 513 m_count += count;
532 514
533 515 int barSetIndex = m_barSets.indexOf(qobject_cast<QBarSet *>(QObject::sender()));
534 516
535 517 blockModelSignals();
536 518 if (m_orientation == Qt::Vertical)
537 519 m_model->insertRows(index + m_first, count);
538 520 else
539 521 m_model->insertColumns(index + m_first, count);
540 522
541 523 for (int j = index; j < index + count; j++)
542 524 m_model->setData(barModelIndex(barSetIndex + m_firstBarSetSection, j), m_barSets.at(barSetIndex)->at(j).y());
543 525
544 526 blockModelSignals(false);
545 527 initializeBarFromModel();
546 528 }
547 529
548 530 void QBarModelMapperPrivate::valuesRemoved(int index, int count)
549 531 {
550 532 if (m_seriesSignalsBlock)
551 533 return;
552 534
553 535 if (m_count != -1)
554 536 m_count -= count;
555 537
556 538 blockModelSignals();
557 539 if (m_orientation == Qt::Vertical)
558 540 m_model->removeRows(index + m_first, count);
559 541 else
560 542 m_model->removeColumns(index + m_first, count);
561 543
562 544 blockModelSignals(false);
563 545 initializeBarFromModel();
564 546 }
565 547
566 548 void QBarModelMapperPrivate::barLabelChanged()
567 549 {
568 550 if (m_seriesSignalsBlock)
569 551 return;
570 552
571 553 int barSetIndex = m_barSets.indexOf(qobject_cast<QBarSet *>(QObject::sender()));
572 554
573 555 blockModelSignals();
574 556 m_model->setHeaderData(barSetIndex + m_firstBarSetSection, m_orientation == Qt::Vertical ? Qt::Horizontal : Qt::Vertical, m_barSets.at(barSetIndex)->label());
575 557 blockModelSignals(false);
576 558 initializeBarFromModel();
577 559 }
578 560
579 561 void QBarModelMapperPrivate::barValueChanged(int index)
580 562 {
581 563 if (m_seriesSignalsBlock)
582 564 return;
583 565
584 566 int barSetIndex = m_barSets.indexOf(qobject_cast<QBarSet *>(QObject::sender()));
585 567
586 568 blockModelSignals();
587 569 m_model->setData(barModelIndex(barSetIndex + m_firstBarSetSection, index), m_barSets.at(barSetIndex)->at(index).y());
588 570 blockModelSignals(false);
589 571 initializeBarFromModel();
590 572 }
591 573
592 574 void QBarModelMapperPrivate::initializeBarFromModel()
593 575 {
594 576 if (m_model == 0 || m_series == 0)
595 577 return;
596 578
597 579 blockSeriesSignals();
598 580 // clear current content
599 581 m_series->clear();
600 582 m_barSets.clear();
601 583
602 584 // create the initial bar sets
603 585 for (int i = m_firstBarSetSection; i <= m_lastBarSetSection; i++) {
604 586 int posInBar = 0;
605 587 QModelIndex barIndex = barModelIndex(i, posInBar);
606 588 // check if there is such model index
607 589 if (barIndex.isValid()) {
608 590 QBarSet *barSet = new QBarSet(m_model->headerData(i, m_orientation == Qt::Vertical ? Qt::Horizontal : Qt::Vertical).toString());
609 591 while (barIndex.isValid()) {
610 592 barSet->append(m_model->data(barIndex, Qt::DisplayRole).toDouble());
611 593 posInBar++;
612 594 barIndex = barModelIndex(i, posInBar);
613 595 }
614 596 connect(barSet, SIGNAL(valuesAdded(int, int)), this, SLOT(valuesAdded(int, int)));
615 597 connect(barSet, SIGNAL(valuesRemoved(int, int)), this, SLOT(valuesRemoved(int, int)));
616 598 connect(barSet, SIGNAL(valueChanged(int)), this, SLOT(barValueChanged(int)));
617 599 connect(barSet, SIGNAL(labelChanged()), this, SLOT(barLabelChanged()));
618 600 m_series->append(barSet);
619 601 m_barSets.append(barSet);
620 602 } else {
621 603 break;
622 604 }
623 605 }
624 606 blockSeriesSignals(false);
625 607 }
626 608
627 609 #include "moc_qbarmodelmapper.cpp"
628 610 #include "moc_qbarmodelmapper_p.cpp"
629 611
630 612 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,178 +1,199
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 "qhbarmodelmapper.h"
22 22
23 23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 24
25 25 /*!
26 26 \class QHBarModelMapper
27 27 \brief part of QtCommercial chart API.
28 28 \mainclass
29 29
30 30 Model mappers allow you to use QAbstractItemModel derived models as a data source for a chart series.
31 31 Horizontal model mapper is used to create a connection between QBarSeries and QAbstractItemModel derived model object.
32 32 Model mapper maintains equal size of all the BarSets.
33 33 Adding/removing value from the BarSet causes the the same change in the rest of the BarSets added to the same series.
34 34 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
35 35 */
36 36 /*!
37 \qmlclass HBarModelMapper
38 \inherits BarModelMapper
37 \qmlclass HBarModelMapper QHBarModelMapper
39 38 \mainclass
40 39
41 40 HBarModelMapper allows you to use your own QAbstractItemModel derived model with data in rows as a data source
42 for any bar series. The following QML example would create a bar series with three bar sets (assuming the model has
43 at least four rows). Each bar set would contain data starting from column 1. The name of a set would be defined
44 by the vertical header (of the row).
41 for any bar series. Adding/removing value from the BarSet causes the the same change in the rest of the BarSets
42 added to the same series. The following QML example would create a bar series with three bar sets (assuming the
43 model has at least four rows). Each bar set would contain data starting from column 1. The name of a set would be
44 defined by the vertical header (of the row).
45 45 \code
46 46 BarSeries {
47 47 HBarModelMapper {
48 model: myCustomModel
48 model: myCustomModel // QAbstractItemModel derived implementation
49 49 firstBarSetRow: 1
50 50 lastBarSetRow: 3
51 51 firstColumn: 1
52 52 }
53 53 }
54 54 \endcode
55 55 */
56 56
57 57 /*!
58 \qmlproperty BarSeries HBarModelMapper::series
59 Defines the BarSeries based object that is used by the mapper. All the data in the series is discarded when it is
60 set to the mapper. When new series is specified the old series is disconnected (it preserves its data).
61 */
62
63 /*!
64 \qmlproperty Model HBarModelMapper::model
65 The QAbstractItemModel based model that is used by the mapper. You need to implement the model and expose it to
66 QML as shown in \l {QML Custom Model} demo application. NOTE: the model has to support adding/removing rows/columns
67 and modifying the data of the cells.
68 */
69
70 /*!
58 71 \property QHBarModelMapper::firstBarSetRow
59 72 \brief Defines which column of the model is used as the data source for the first bar set
60
61 73 Default value is: -1 (invalid mapping)
62 74 */
75 /*!
76 \qmlproperty int HBarModelMapper::firstBarSetRow
77 Defines which column of the model is used as the data source for the first bar set. The default value is -1
78 (invalid mapping).
79 */
63 80
64 81 /*!
65 82 \property QHBarModelMapper::lastBarSetRow
66 83 \brief Defines which column of the model is used as the data source for the last bar set
67
68 84 Default value is: -1 (invalid mapping)
69 85 */
86 /*!
87 \qmlproperty int HBarModelMapper::lastBarSetRow
88 Defines which column of the model is used as the data source for the last bar set. The default value is -1
89 (invalid mapping).
90 */
70 91
71 92 /*!
72 93 \property QHBarModelMapper::firstColumn
73 94 \brief Defines which column of the model contains the first values of the QBarSets in the series.
74 95 Minimal and default value is: 0
75 96 */
76 97 /*!
77 \qmlproperty int BarModelMapper::firstColumn
98 \qmlproperty int HBarModelMapper::firstColumn
78 99 Defines which column of the model contains the first values of the QBarSets in the series.
79 100 The default value is 0.
80 101 */
81 102
82 103 /*!
83 104 \property QHBarModelMapper::columnCount
84 \brief Defines the number of rows of the model that are mapped as the data for QBarSeries
85 Minimal and default value is: -1 (count limited by the number of rows in the model)
105 \brief Defines the number of columns of the model that are mapped as the data for QBarSeries
106 Minimal and default value is: -1 (count limited by the number of columns in the model)
86 107 */
87 108 /*!
88 \qmlproperty int BarModelMapper::columnCount
89 Defines the number of rows of the model that are mapped as the data for QBarSeries. The default value is
90 -1 (count limited by the number of rows in the model)
109 \qmlproperty int HBarModelMapper::columnCount
110 Defines the number of columns of the model that are mapped as the data for QBarSeries. The default value is
111 -1 (count limited by the number of columns in the model)
91 112 */
92 113
93 114 /*!
94 115 \fn void QHBarModelMapper::firstBarSetRowChanged()
95 116
96 117 Emitted when the firstBarSetRow has changed.
97 118 */
98 119
99 120 /*!
100 121 \fn void QHBarModelMapper::lastBarSetRowChanged()
101 122
102 123 Emitted when the lastBarSetRow has changed.
103 124 */
104 125
105 126 /*!
106 127 \fn void QHBarModelMapper::firstColumnChanged()
107 128 Emitted when the firstColumn has changed.
108 129 */
109 130
110 131 /*!
111 132 \fn void QHBarModelMapper::columnCountChanged()
112 133 Emitted when the columnCount has changed.
113 134 */
114 135
115 136 /*!
116 137 Constructs a mapper object which is a child of \a parent.
117 138 */
118 139 QHBarModelMapper::QHBarModelMapper(QObject *parent) :
119 140 QBarModelMapper(parent)
120 141 {
121 142 QBarModelMapper::setOrientation(Qt::Horizontal);
122 143 }
123 144
124 145 int QHBarModelMapper::firstBarSetRow() const
125 146 {
126 147 return QBarModelMapper::firstBarSetSection();
127 148 }
128 149
129 150 void QHBarModelMapper::setFirstBarSetRow(int firstBarSetRow)
130 151 {
131 152 if (firstBarSetRow != firstBarSetSection()) {
132 153 QBarModelMapper::setFirstBarSetSection(firstBarSetRow);
133 154 emit firstBarSetRowChanged();
134 155 }
135 156 }
136 157
137 158 int QHBarModelMapper::lastBarSetRow() const
138 159 {
139 160 return QBarModelMapper::lastBarSetSection();
140 161 }
141 162
142 163 void QHBarModelMapper::setLastBarSetRow(int lastBarSetRow)
143 164 {
144 165 if (lastBarSetRow != lastBarSetSection()) {
145 166 QBarModelMapper::setLastBarSetSection(lastBarSetRow);
146 167 emit lastBarSetRowChanged();
147 168 }
148 169 }
149 170
150 171 int QHBarModelMapper::firstColumn() const
151 172 {
152 173 return QBarModelMapper::first();
153 174 }
154 175
155 176 void QHBarModelMapper::setFirstColumn(int firstColumn)
156 177 {
157 178 if (firstColumn != first()) {
158 179 QBarModelMapper::setFirst(firstColumn);
159 180 emit firstColumnChanged();
160 181 }
161 182 }
162 183
163 184 int QHBarModelMapper::columnCount() const
164 185 {
165 186 return QBarModelMapper::count();
166 187 }
167 188
168 189 void QHBarModelMapper::setColumnCount(int columnCount)
169 190 {
170 191 if (columnCount != count()) {
171 192 QBarModelMapper::setCount(columnCount);
172 193 emit firstColumnChanged();
173 194 }
174 195 }
175 196
176 197 #include "moc_qhbarmodelmapper.cpp"
177 198
178 199 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,184 +1,197
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 "qvbarmodelmapper.h"
22 22
23 23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 24
25 25 /*!
26 26 \class QVBarModelMapper
27 27 \brief part of QtCommercial chart API.
28 28 \mainclass
29 29
30 30 Model mappers allow you to use QAbstractItemModel derived models as a data source for a chart series.
31 31 Vertical model mapper is used to create a connection between QBarSeries and QAbstractItemModel derived model object.
32 32 Model mapper maintains equal size of all the BarSets.
33 33 Adding/removing value from the BarSet causes the the same change in the rest of the BarSets added to the same series.
34 34 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
35 35 */
36 36 /*!
37 37 \qmlclass VBarModelMapper
38 \inherits BarModelMapper
39 38 \mainclass
40 39
41 40 VBarModelMapper allows you to use your own QAbstractItemModel derived model with data in columns as a data source
42 for any bar series. The following QML example would create a bar series with three bar sets (assuming the model has
43 at least four columns). Each bar set would contain data starting from row 1. The name of a set would be defined
44 by the horizontal header (of the column).
41 for any bar series. Adding/removing value from the BarSet causes the the same change in the rest of the BarSets
42 added to the same series. The following QML example would create a bar series with three bar sets (assuming the
43 model has at least four columns). Each bar set would contain data starting from row 1. The name of a set would be
44 defined by the horizontal header (of the column).
45 45 \code
46 46 GroupedBarSeries {
47 47 VBarModelMapper {
48 model: myCustomModel
48 model: myCustomModel // QAbstractItemModel derived implementation
49 49 firstBarSetColumn: 1
50 50 lastBarSetColumn: 3
51 51 firstRow: 1
52 52 }
53 53 }
54 54 \endcode
55 55 */
56 56
57 57 /*!
58 \qmlproperty BarSeries VBarModelMapper::series
59 Defines the BarSeries based object that is used by the mapper. All the data in the series is discarded when it is
60 set to the mapper. When new series is specified the old series is disconnected (it preserves its data).
61 */
62
63 /*!
64 \qmlproperty Model VBarModelMapper::model
65 The QAbstractItemModel based model that is used by the mapper. You need to implement the model and expose it to
66 QML as shown in \l {QML Custom Model} demo application. NOTE: the model has to support adding/removing rows/columns
67 and modifying the data of the cells.
68 */
69
70 /*!
58 71 \property QVBarModelMapper::firstBarSetColumn
59 72 \brief Defines which column of the model is used as the data source for the first bar set
60 73 Default value is: -1 (invalid mapping)
61 74 */
62 75 /*!
63 76 \qmlproperty int VBarModelMapper::firstBarSetColumn
64 77 Defines which column of the model is used as the data source for the first bar set. Default value
65 78 is: -1 (invalid mapping).
66 79 */
67 80
68 81 /*!
69 82 \property QVBarModelMapper::lastBarSetColumn
70 83 \brief Defines which column of the model is used as the data source for the last bar set
71 84 Default value is: -1 (invalid mapping)
72 85 */
73 86 /*!
74 87 \qmlproperty int VBarModelMapper::lastBarSetColumn
75 88 Defines which column of the model is used as the data source for the last bar set. Default
76 89 value is: -1 (invalid mapping).
77 90 */
78 91
79 92 /*!
80 93 \property QVBarModelMapper::firstRow
81 94 \brief Defines which row of the model contains the first values of the QBarSets in the series.
82 95 Minimal and default value is: 0
83 96 */
84 97 /*!
85 \qmlproperty int BarModelMapper::firstRow
98 \qmlproperty int VBarModelMapper::firstRow
86 99 Defines which row of the model contains the first values of the QBarSets in the series.
87 100 The default value is 0.
88 101 */
89 102
90 103 /*!
91 104 \property QVBarModelMapper::rowCount
92 105 \brief Defines the number of rows of the model that are mapped as the data for QBarSeries
93 106 Minimal and default value is: -1 (count limited by the number of rows in the model)
94 107 */
95 108 /*!
96 \qmlproperty int BarModelMapper::rowCount
109 \qmlproperty int VBarModelMapper::rowCount
97 110 Defines the number of rows of the model that are mapped as the data for QBarSeries. The default value is
98 111 -1 (count limited by the number of rows in the model)
99 112 */
100 113
101 114 /*!
102 115 \fn void QVBarModelMapper::firstBarSetColumnChanged()
103 116 Emitted when the firstBarSetColumn has changed.
104 117 */
105 118
106 119 /*!
107 120 \fn void QVBarModelMapper::lastBarSetColumnChanged()
108 121 Emitted when the lastBarSetColumn has changed.
109 122 */
110 123
111 124 /*!
112 125 \fn void QVBarModelMapper::firstRowChanged()
113 126 Emitted when the firstRow has changed.
114 127 */
115 128
116 129 /*!
117 130 \fn void QVBarModelMapper::rowCountChanged()
118 131 Emitted when the rowCount has changed.
119 132 */
120 133
121 134 /*!
122 135 Constructs a mapper object which is a child of \a parent.
123 136 */
124 137 QVBarModelMapper::QVBarModelMapper(QObject *parent) :
125 138 QBarModelMapper(parent)
126 139 {
127 140 QBarModelMapper::setOrientation(Qt::Vertical);
128 141 }
129 142
130 143 int QVBarModelMapper::firstBarSetColumn() const
131 144 {
132 145 return QBarModelMapper::firstBarSetSection();
133 146 }
134 147
135 148 void QVBarModelMapper::setFirstBarSetColumn(int firstBarSetColumn)
136 149 {
137 150 if (firstBarSetColumn != firstBarSetSection()) {
138 151 QBarModelMapper::setFirstBarSetSection(firstBarSetColumn);
139 152 emit firstBarSetColumnChanged();
140 153 }
141 154 }
142 155
143 156 int QVBarModelMapper::lastBarSetColumn() const
144 157 {
145 158 return QBarModelMapper::lastBarSetSection();
146 159 }
147 160
148 161 void QVBarModelMapper::setLastBarSetColumn(int lastBarSetColumn)
149 162 {
150 163 if (lastBarSetColumn != lastBarSetSection()) {
151 164 QBarModelMapper::setLastBarSetSection(lastBarSetColumn);
152 165 emit lastBarSetColumnChanged();
153 166 }
154 167 }
155 168
156 169 int QVBarModelMapper::firstRow() const
157 170 {
158 171 return QBarModelMapper::first();
159 172 }
160 173
161 174 void QVBarModelMapper::setFirstRow(int firstRow)
162 175 {
163 176 if (firstRow != first()) {
164 177 QBarModelMapper::setFirst(firstRow);
165 178 emit firstRowChanged();
166 179 }
167 180 }
168 181
169 182 int QVBarModelMapper::rowCount() const
170 183 {
171 184 return QBarModelMapper::count();
172 185 }
173 186
174 187 void QVBarModelMapper::setRowCount(int rowCount)
175 188 {
176 189 if (rowCount != count()) {
177 190 QBarModelMapper::setCount(rowCount);
178 191 emit firstRowChanged();
179 192 }
180 193 }
181 194
182 195 #include "moc_qvbarmodelmapper.cpp"
183 196
184 197 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now