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