This diff has been collapsed as it changes many lines, (538 lines changed) Show them Hide them | |||
@@ -0,0 +1,538 | |||
|
1 | /**************************************************************************** | |
|
2 | ** | |
|
3 | ** Copyright (C) 2013 Digia Plc | |
|
4 | ** All rights reserved. | |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
|
6 | ** | |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
|
8 | ** | |
|
9 | ** $QT_BEGIN_LICENSE$ | |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
|
13 | ** a written agreement between you and Digia. | |
|
14 | ** | |
|
15 | ** If you have questions regarding the use of this file, please use | |
|
16 | ** contact form at http://qt.digia.com | |
|
17 | ** $QT_END_LICENSE$ | |
|
18 | ** | |
|
19 | ****************************************************************************/ | |
|
20 | ||
|
21 | #include "qboxplotmodelmapper.h" | |
|
22 | #include "qboxplotmodelmapper_p.h" | |
|
23 | #include "qboxplotseries.h" | |
|
24 | #include "qboxset.h" | |
|
25 | #include "qchart.h" | |
|
26 | #include <QAbstractItemModel> | |
|
27 | ||
|
28 | #include <QDebug> | |
|
29 | ||
|
30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
|
31 | ||
|
32 | QBoxPlotModelMapper::QBoxPlotModelMapper(QObject *parent) : | |
|
33 | QObject(parent), | |
|
34 | d_ptr(new QBoxPlotModelMapperPrivate(this)) | |
|
35 | { | |
|
36 | } | |
|
37 | ||
|
38 | QAbstractItemModel *QBoxPlotModelMapper::model() const | |
|
39 | { | |
|
40 | Q_D(const QBoxPlotModelMapper); | |
|
41 | return d->m_model; | |
|
42 | } | |
|
43 | ||
|
44 | void QBoxPlotModelMapper::setModel(QAbstractItemModel *model) | |
|
45 | { | |
|
46 | if (model == 0) | |
|
47 | return; | |
|
48 | ||
|
49 | Q_D(QBoxPlotModelMapper); | |
|
50 | if (d->m_model) | |
|
51 | disconnect(d->m_model, 0, d, 0); | |
|
52 | ||
|
53 | d->m_model = model; | |
|
54 | d->initializeBoxFromModel(); | |
|
55 | // connect signals from the model | |
|
56 | connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex))); | |
|
57 | connect(d->m_model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)), d, SLOT(modelHeaderDataUpdated(Qt::Orientation,int,int))); | |
|
58 | connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int))); | |
|
59 | connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int))); | |
|
60 | connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int))); | |
|
61 | connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int))); | |
|
62 | connect(d->m_model, SIGNAL(destroyed()), d, SLOT(handleModelDestroyed())); | |
|
63 | } | |
|
64 | ||
|
65 | QBoxPlotSeries *QBoxPlotModelMapper::series() const | |
|
66 | { | |
|
67 | Q_D(const QBoxPlotModelMapper); | |
|
68 | return d->m_series; | |
|
69 | } | |
|
70 | ||
|
71 | void QBoxPlotModelMapper::setSeries(QBoxPlotSeries *series) | |
|
72 | { | |
|
73 | Q_D(QBoxPlotModelMapper); | |
|
74 | if (d->m_series) | |
|
75 | disconnect(d->m_series, 0, d, 0); | |
|
76 | ||
|
77 | if (series == 0) | |
|
78 | return; | |
|
79 | ||
|
80 | d->m_series = series; | |
|
81 | d->initializeBoxFromModel(); | |
|
82 | // connect the signals from the series | |
|
83 | connect(d->m_series, SIGNAL(boxsetsAdded(QList<QBoxSet*>)), d, SLOT(boxSetsAdded(QList<QBoxSet*>))); | |
|
84 | connect(d->m_series, SIGNAL(boxsetsRemoved(QList<QBoxSet*>)), d, SLOT(boxSetsRemoved(QList<QBoxSet*>))); | |
|
85 | connect(d->m_series, SIGNAL(destroyed()), d, SLOT(handleSeriesDestroyed())); | |
|
86 | } | |
|
87 | ||
|
88 | /*! | |
|
89 | Returns which row/column of the model contains the first values of the QBoxSets in the series. | |
|
90 | The default value is 0. | |
|
91 | */ | |
|
92 | int QBoxPlotModelMapper::first() const | |
|
93 | { | |
|
94 | Q_D(const QBoxPlotModelMapper); | |
|
95 | return d->m_first; | |
|
96 | } | |
|
97 | ||
|
98 | /*! | |
|
99 | Sets which row of the model contains the \a first values of the QBoxSets in the series. | |
|
100 | The default value is 0. | |
|
101 | */ | |
|
102 | void QBoxPlotModelMapper::setFirst(int first) | |
|
103 | { | |
|
104 | Q_D(QBoxPlotModelMapper); | |
|
105 | d->m_first = qMax(first, 0); | |
|
106 | d->initializeBoxFromModel(); | |
|
107 | } | |
|
108 | ||
|
109 | /*! | |
|
110 | Returns the number of rows/columns of the model that are mapped as the data for QBoxPlotSeries | |
|
111 | Minimal and default value is: -1 (count limited by the number of rows/columns in the model) | |
|
112 | */ | |
|
113 | int QBoxPlotModelMapper::count() const | |
|
114 | { | |
|
115 | Q_D(const QBoxPlotModelMapper); | |
|
116 | return d->m_count; | |
|
117 | } | |
|
118 | ||
|
119 | /*! | |
|
120 | Sets the \a count of rows/columns of the model that are mapped as the data for QBoxPlotSeries | |
|
121 | Minimal and default value is: -1 (count limited by the number of rows/columns in the model) | |
|
122 | */ | |
|
123 | void QBoxPlotModelMapper::setCount(int count) | |
|
124 | { | |
|
125 | Q_D(QBoxPlotModelMapper); | |
|
126 | d->m_count = qMax(count, -1); | |
|
127 | d->initializeBoxFromModel(); | |
|
128 | } | |
|
129 | ||
|
130 | /*! | |
|
131 | Returns the orientation that is used when QBoxPlotModelMapper accesses the model. | |
|
132 | This mean whether the consecutive values of the bar set are read from row (Qt::Horizontal) | |
|
133 | or from columns (Qt::Vertical) | |
|
134 | */ | |
|
135 | Qt::Orientation QBoxPlotModelMapper::orientation() const | |
|
136 | { | |
|
137 | Q_D(const QBoxPlotModelMapper); | |
|
138 | return d->m_orientation; | |
|
139 | } | |
|
140 | ||
|
141 | /*! | |
|
142 | Returns the \a orientation that is used when QBoxPlotModelMapper accesses the model. | |
|
143 | This mean whether the consecutive values of the pie are read from row (Qt::Horizontal) | |
|
144 | or from columns (Qt::Vertical) | |
|
145 | */ | |
|
146 | void QBoxPlotModelMapper::setOrientation(Qt::Orientation orientation) | |
|
147 | { | |
|
148 | Q_D(QBoxPlotModelMapper); | |
|
149 | d->m_orientation = orientation; | |
|
150 | d->initializeBoxFromModel(); | |
|
151 | } | |
|
152 | ||
|
153 | /*! | |
|
154 | Returns which section of the model is used as the data source for the first box set | |
|
155 | */ | |
|
156 | int QBoxPlotModelMapper::firstBoxSetSection() const | |
|
157 | { | |
|
158 | Q_D(const QBoxPlotModelMapper); | |
|
159 | return d->m_firstBoxSetSection; | |
|
160 | } | |
|
161 | ||
|
162 | /*! | |
|
163 | Sets the model section that is used as the data source for the first box set | |
|
164 | Parameter \a firstBoxSetSection specifies the section of the model. | |
|
165 | */ | |
|
166 | void QBoxPlotModelMapper::setFirstBoxSetSection(int firstBoxSetSection) | |
|
167 | { | |
|
168 | Q_D(QBoxPlotModelMapper); | |
|
169 | d->m_firstBoxSetSection = qMax(-1, firstBoxSetSection); | |
|
170 | d->initializeBoxFromModel(); | |
|
171 | } | |
|
172 | ||
|
173 | /*! | |
|
174 | Returns which section of the model is used as the data source for the last box set | |
|
175 | */ | |
|
176 | int QBoxPlotModelMapper::lastBoxSetSection() const | |
|
177 | { | |
|
178 | Q_D(const QBoxPlotModelMapper); | |
|
179 | return d->m_lastBoxSetSection; | |
|
180 | } | |
|
181 | ||
|
182 | /*! | |
|
183 | Sets the model section that is used as the data source for the last box set | |
|
184 | Parameter \a lastBoxSetSection specifies the section of the model. | |
|
185 | */ | |
|
186 | void QBoxPlotModelMapper::setLastBoxSetSection(int lastBoxSetSection) | |
|
187 | { | |
|
188 | Q_D(QBoxPlotModelMapper); | |
|
189 | d->m_lastBoxSetSection = qMax(-1, lastBoxSetSection); | |
|
190 | d->initializeBoxFromModel(); | |
|
191 | } | |
|
192 | ||
|
193 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
|
194 | ||
|
195 | QBoxPlotModelMapperPrivate::QBoxPlotModelMapperPrivate(QBoxPlotModelMapper *q) : | |
|
196 | QObject(q), | |
|
197 | m_series(0), | |
|
198 | m_model(0), | |
|
199 | m_first(0), | |
|
200 | m_count(-1), | |
|
201 | m_orientation(Qt::Vertical), | |
|
202 | m_firstBoxSetSection(-1), | |
|
203 | m_lastBoxSetSection(-1), | |
|
204 | m_seriesSignalsBlock(false), | |
|
205 | m_modelSignalsBlock(false), | |
|
206 | q_ptr(q) | |
|
207 | { | |
|
208 | } | |
|
209 | ||
|
210 | void QBoxPlotModelMapperPrivate::blockModelSignals(bool block) | |
|
211 | { | |
|
212 | m_modelSignalsBlock = block; | |
|
213 | } | |
|
214 | ||
|
215 | void QBoxPlotModelMapperPrivate::blockSeriesSignals(bool block) | |
|
216 | { | |
|
217 | m_seriesSignalsBlock = block; | |
|
218 | } | |
|
219 | ||
|
220 | QBoxSet *QBoxPlotModelMapperPrivate::boxSet(QModelIndex index) | |
|
221 | { | |
|
222 | if (!index.isValid()) | |
|
223 | return 0; | |
|
224 | ||
|
225 | if (m_orientation == Qt::Vertical && index.column() >= m_firstBoxSetSection && index.column() <= m_lastBoxSetSection) { | |
|
226 | if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) { | |
|
227 | return m_series->boxSets().at(index.column() - m_firstBoxSetSection); | |
|
228 | } | |
|
229 | } else if (m_orientation == Qt::Horizontal && index.row() >= m_firstBoxSetSection && index.row() <= m_lastBoxSetSection) { | |
|
230 | if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count)) | |
|
231 | return m_series->boxSets().at(index.row() - m_firstBoxSetSection); | |
|
232 | } | |
|
233 | return 0; // This part of model has not been mapped to any slice | |
|
234 | } | |
|
235 | ||
|
236 | QModelIndex QBoxPlotModelMapperPrivate::boxModelIndex(int boxSection, int posInBar) | |
|
237 | { | |
|
238 | if (m_count != -1 && posInBar >= m_count) | |
|
239 | return QModelIndex(); // invalid | |
|
240 | ||
|
241 | if (boxSection < m_firstBoxSetSection || boxSection > m_lastBoxSetSection) | |
|
242 | return QModelIndex(); // invalid | |
|
243 | ||
|
244 | if (m_orientation == Qt::Vertical) | |
|
245 | return m_model->index(posInBar + m_first, boxSection); | |
|
246 | else | |
|
247 | return m_model->index(boxSection, posInBar + m_first); | |
|
248 | } | |
|
249 | ||
|
250 | void QBoxPlotModelMapperPrivate::handleSeriesDestroyed() | |
|
251 | { | |
|
252 | m_series = 0; | |
|
253 | } | |
|
254 | ||
|
255 | void QBoxPlotModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) | |
|
256 | { | |
|
257 | Q_UNUSED(topLeft) | |
|
258 | Q_UNUSED(bottomRight) | |
|
259 | ||
|
260 | if (m_model == 0 || m_series == 0) | |
|
261 | return; | |
|
262 | ||
|
263 | if (m_modelSignalsBlock) | |
|
264 | return; | |
|
265 | ||
|
266 | blockSeriesSignals(); | |
|
267 | QModelIndex index; | |
|
268 | for (int row = topLeft.row(); row <= bottomRight.row(); row++) { | |
|
269 | for (int column = topLeft.column(); column <= bottomRight.column(); column++) { | |
|
270 | index = topLeft.sibling(row, column); | |
|
271 | QBoxSet *box = boxSet(index); | |
|
272 | if (box) { | |
|
273 | if (m_orientation == Qt::Vertical) | |
|
274 | box->replace(row - m_first, m_model->data(index).toReal()); | |
|
275 | else | |
|
276 | box->replace(column - m_first, m_model->data(index).toReal()); | |
|
277 | } | |
|
278 | } | |
|
279 | } | |
|
280 | blockSeriesSignals(false); | |
|
281 | } | |
|
282 | ||
|
283 | void QBoxPlotModelMapperPrivate::modelHeaderDataUpdated(Qt::Orientation orientation, int first, int last) | |
|
284 | { | |
|
285 | Q_UNUSED(orientation); | |
|
286 | Q_UNUSED(first); | |
|
287 | Q_UNUSED(last); | |
|
288 | ||
|
289 | qDebug() << "ALERT: QBoxPlotModelMapperPrivate::modelHeaderDataUpdated implement"; | |
|
290 | } | |
|
291 | ||
|
292 | void QBoxPlotModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end) | |
|
293 | { | |
|
294 | Q_UNUSED(parent) | |
|
295 | if (m_modelSignalsBlock) | |
|
296 | return; | |
|
297 | ||
|
298 | blockSeriesSignals(); | |
|
299 | if (m_orientation == Qt::Vertical) | |
|
300 | insertData(start, end); | |
|
301 | else if (start <= m_firstBoxSetSection || start <= m_lastBoxSetSection) // if the changes affect the map - reinitialize | |
|
302 | initializeBoxFromModel(); | |
|
303 | blockSeriesSignals(false); | |
|
304 | } | |
|
305 | ||
|
306 | void QBoxPlotModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end) | |
|
307 | { | |
|
308 | Q_UNUSED(parent) | |
|
309 | if (m_modelSignalsBlock) | |
|
310 | return; | |
|
311 | ||
|
312 | blockSeriesSignals(); | |
|
313 | if (m_orientation == Qt::Vertical) | |
|
314 | removeData(start, end); | |
|
315 | else if (start <= m_firstBoxSetSection || start <= m_lastBoxSetSection) // if the changes affect the map - reinitialize | |
|
316 | initializeBoxFromModel(); | |
|
317 | blockSeriesSignals(false); | |
|
318 | } | |
|
319 | ||
|
320 | void QBoxPlotModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end) | |
|
321 | { | |
|
322 | Q_UNUSED(parent) | |
|
323 | if (m_modelSignalsBlock) | |
|
324 | return; | |
|
325 | ||
|
326 | blockSeriesSignals(); | |
|
327 | if (m_orientation == Qt::Horizontal) | |
|
328 | insertData(start, end); | |
|
329 | else if (start <= m_firstBoxSetSection || start <= m_lastBoxSetSection) // if the changes affect the map - reinitialize | |
|
330 | initializeBoxFromModel(); | |
|
331 | blockSeriesSignals(false); | |
|
332 | } | |
|
333 | ||
|
334 | void QBoxPlotModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end) | |
|
335 | { | |
|
336 | Q_UNUSED(parent) | |
|
337 | if (m_modelSignalsBlock) | |
|
338 | return; | |
|
339 | ||
|
340 | blockSeriesSignals(); | |
|
341 | if (m_orientation == Qt::Horizontal) | |
|
342 | removeData(start, end); | |
|
343 | else if (start <= m_firstBoxSetSection || start <= m_lastBoxSetSection) // if the changes affect the map - reinitialize | |
|
344 | initializeBoxFromModel(); | |
|
345 | blockSeriesSignals(false); | |
|
346 | } | |
|
347 | ||
|
348 | void QBoxPlotModelMapperPrivate::handleModelDestroyed() | |
|
349 | { | |
|
350 | m_model = 0; | |
|
351 | } | |
|
352 | ||
|
353 | void QBoxPlotModelMapperPrivate::insertData(int start, int end) | |
|
354 | { | |
|
355 | Q_UNUSED(end) | |
|
356 | Q_UNUSED(start) | |
|
357 | Q_UNUSED(end) | |
|
358 | // Currently barchart needs to be fully recalculated when change is made. | |
|
359 | // Re-initialize | |
|
360 | initializeBoxFromModel(); | |
|
361 | } | |
|
362 | ||
|
363 | void QBoxPlotModelMapperPrivate::removeData(int start, int end) | |
|
364 | { | |
|
365 | Q_UNUSED(end) | |
|
366 | Q_UNUSED(start) | |
|
367 | Q_UNUSED(end) | |
|
368 | // Currently barchart needs to be fully recalculated when change is made. | |
|
369 | // Re-initialize | |
|
370 | initializeBoxFromModel(); | |
|
371 | } | |
|
372 | ||
|
373 | void QBoxPlotModelMapperPrivate::boxSetsAdded(QList<QBoxSet *> sets) | |
|
374 | { | |
|
375 | if (m_seriesSignalsBlock) | |
|
376 | return; | |
|
377 | ||
|
378 | if (sets.count() == 0) | |
|
379 | return; | |
|
380 | ||
|
381 | int firstIndex = m_series->boxSets().indexOf(sets.at(0)); | |
|
382 | if (firstIndex == -1) | |
|
383 | return; | |
|
384 | ||
|
385 | int maxCount = 0; | |
|
386 | for (int i = 0; i < sets.count(); i++) { | |
|
387 | if (sets.at(i)->count() > m_count) | |
|
388 | maxCount = sets.at(i)->count(); | |
|
389 | } | |
|
390 | ||
|
391 | if (m_count != -1 && m_count < maxCount) | |
|
392 | m_count = maxCount; | |
|
393 | ||
|
394 | m_lastBoxSetSection += sets.count(); | |
|
395 | ||
|
396 | blockModelSignals(); | |
|
397 | int modelCapacity = m_orientation == Qt::Vertical ? m_model->rowCount() - m_first : m_model->columnCount() - m_first; | |
|
398 | if (maxCount > modelCapacity) { | |
|
399 | if (m_orientation == Qt::Vertical) | |
|
400 | m_model->insertRows(m_model->rowCount(), maxCount - modelCapacity); | |
|
401 | else | |
|
402 | m_model->insertColumns(m_model->columnCount(), maxCount - modelCapacity); | |
|
403 | } | |
|
404 | ||
|
405 | if (m_orientation == Qt::Vertical) | |
|
406 | m_model->insertColumns(firstIndex + m_firstBoxSetSection, sets.count()); | |
|
407 | else | |
|
408 | m_model->insertRows(firstIndex + m_firstBoxSetSection, sets.count()); | |
|
409 | ||
|
410 | ||
|
411 | for (int i = firstIndex + m_firstBoxSetSection; i < firstIndex + m_firstBoxSetSection + sets.count(); i++) { | |
|
412 | //m_model->setHeaderData(i, m_orientation == Qt::Vertical ? Qt::Horizontal : Qt::Vertical, sets.at(i - firstIndex - m_firstBoxSetSection)->label()); | |
|
413 | for (int j = 0; j < sets.at(i - firstIndex - m_firstBoxSetSection)->count(); j++) | |
|
414 | m_model->setData(boxModelIndex(i, j), sets.at(i - firstIndex - m_firstBoxSetSection)->at(j)); | |
|
415 | } | |
|
416 | blockModelSignals(false); | |
|
417 | initializeBoxFromModel(); | |
|
418 | } | |
|
419 | ||
|
420 | void QBoxPlotModelMapperPrivate::boxSetsRemoved(QList<QBoxSet *> sets) | |
|
421 | { | |
|
422 | if (m_seriesSignalsBlock) | |
|
423 | return; | |
|
424 | ||
|
425 | if (sets.count() == 0) | |
|
426 | return; | |
|
427 | ||
|
428 | int firstIndex = m_boxSets.indexOf(sets.at(0)); | |
|
429 | if (firstIndex == -1) | |
|
430 | return; | |
|
431 | ||
|
432 | m_lastBoxSetSection -= sets.count(); | |
|
433 | ||
|
434 | for (int i = firstIndex + sets.count() - 1; i >= firstIndex; i--) | |
|
435 | m_boxSets.removeAt(i); | |
|
436 | ||
|
437 | blockModelSignals(); | |
|
438 | if (m_orientation == Qt::Vertical) | |
|
439 | m_model->removeColumns(firstIndex + m_firstBoxSetSection, sets.count()); | |
|
440 | else | |
|
441 | m_model->removeRows(firstIndex + m_firstBoxSetSection, sets.count()); | |
|
442 | blockModelSignals(false); | |
|
443 | initializeBoxFromModel(); | |
|
444 | } | |
|
445 | ||
|
446 | void QBoxPlotModelMapperPrivate::valuesAdded(int index, int count) | |
|
447 | { | |
|
448 | if (m_seriesSignalsBlock) | |
|
449 | return; | |
|
450 | ||
|
451 | if (m_count != -1) | |
|
452 | m_count += count; | |
|
453 | ||
|
454 | int boxSetIndex = m_boxSets.indexOf(qobject_cast<QBoxSet *>(QObject::sender())); | |
|
455 | ||
|
456 | blockModelSignals(); | |
|
457 | if (m_orientation == Qt::Vertical) | |
|
458 | m_model->insertRows(index + m_first, count); | |
|
459 | else | |
|
460 | m_model->insertColumns(index + m_first, count); | |
|
461 | ||
|
462 | for (int j = index; j < index + count; j++) | |
|
463 | m_model->setData(boxModelIndex(boxSetIndex + m_firstBoxSetSection, j), m_boxSets.at(boxSetIndex)->at(j)); | |
|
464 | ||
|
465 | blockModelSignals(false); | |
|
466 | initializeBoxFromModel(); | |
|
467 | } | |
|
468 | ||
|
469 | void QBoxPlotModelMapperPrivate::valuesRemoved(int index, int count) | |
|
470 | { | |
|
471 | if (m_seriesSignalsBlock) | |
|
472 | return; | |
|
473 | ||
|
474 | if (m_count != -1) | |
|
475 | m_count -= count; | |
|
476 | ||
|
477 | blockModelSignals(); | |
|
478 | if (m_orientation == Qt::Vertical) | |
|
479 | m_model->removeRows(index + m_first, count); | |
|
480 | else | |
|
481 | m_model->removeColumns(index + m_first, count); | |
|
482 | ||
|
483 | blockModelSignals(false); | |
|
484 | initializeBoxFromModel(); | |
|
485 | } | |
|
486 | ||
|
487 | void QBoxPlotModelMapperPrivate::boxValueChanged(int index) | |
|
488 | { | |
|
489 | if (m_seriesSignalsBlock) | |
|
490 | return; | |
|
491 | ||
|
492 | int boxSetIndex = m_boxSets.indexOf(qobject_cast<QBoxSet *>(QObject::sender())); | |
|
493 | ||
|
494 | blockModelSignals(); | |
|
495 | m_model->setData(boxModelIndex(boxSetIndex + m_firstBoxSetSection, index), m_boxSets.at(boxSetIndex)->at(index)); | |
|
496 | blockModelSignals(false); | |
|
497 | initializeBoxFromModel(); | |
|
498 | } | |
|
499 | ||
|
500 | void QBoxPlotModelMapperPrivate::initializeBoxFromModel() | |
|
501 | { | |
|
502 | if (m_model == 0 || m_series == 0) | |
|
503 | return; | |
|
504 | ||
|
505 | blockSeriesSignals(); | |
|
506 | // clear current content | |
|
507 | m_series->clear(); | |
|
508 | m_boxSets.clear(); | |
|
509 | ||
|
510 | // create the initial bar sets | |
|
511 | for (int i = m_firstBoxSetSection; i <= m_lastBoxSetSection; i++) { | |
|
512 | int posInBar = 0; | |
|
513 | QModelIndex boxIndex = boxModelIndex(i, posInBar); | |
|
514 | // check if there is such model index | |
|
515 | if (boxIndex.isValid()) { | |
|
516 | QBoxSet *boxSet = new QBoxSet(); | |
|
517 | while (boxIndex.isValid()) { | |
|
518 | boxSet->append(m_model->data(boxIndex, Qt::DisplayRole).toDouble()); | |
|
519 | posInBar++; | |
|
520 | boxIndex = boxModelIndex(i, posInBar); | |
|
521 | } | |
|
522 | connect(boxSet, SIGNAL(valuesAdded(int,int)), this, SLOT(valuesAdded(int,int))); | |
|
523 | connect(boxSet, SIGNAL(valuesRemoved(int,int)), this, SLOT(valuesRemoved(int,int))); | |
|
524 | connect(boxSet, SIGNAL(valueChanged(int)), this, SLOT(boxValueChanged(int))); | |
|
525 | m_series->append(boxSet); | |
|
526 | m_boxSets.append(boxSet); | |
|
527 | } else { | |
|
528 | break; | |
|
529 | } | |
|
530 | } | |
|
531 | blockSeriesSignals(false); | |
|
532 | } | |
|
533 | ||
|
534 | #include "moc_qboxplotmodelmapper.cpp" | |
|
535 | #include "moc_qboxplotmodelmapper_p.cpp" | |
|
536 | ||
|
537 | QTCOMMERCIALCHART_END_NAMESPACE | |
|
538 |
@@ -0,0 +1,69 | |||
|
1 | /**************************************************************************** | |
|
2 | ** | |
|
3 | ** Copyright (C) 2013 Digia Plc | |
|
4 | ** All rights reserved. | |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
|
6 | ** | |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
|
8 | ** | |
|
9 | ** $QT_BEGIN_LICENSE$ | |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
|
13 | ** a written agreement between you and Digia. | |
|
14 | ** | |
|
15 | ** If you have questions regarding the use of this file, please use | |
|
16 | ** contact form at http://qt.digia.com | |
|
17 | ** $QT_END_LICENSE$ | |
|
18 | ** | |
|
19 | ****************************************************************************/ | |
|
20 | ||
|
21 | #ifndef QBOXPLOTMODELMAPPER_H | |
|
22 | #define QBOXPLOTMODELMAPPER_H | |
|
23 | ||
|
24 | #include "qchartglobal.h" | |
|
25 | #include <QObject> | |
|
26 | ||
|
27 | class QAbstractItemModel; | |
|
28 | ||
|
29 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
|
30 | ||
|
31 | class QBoxPlotModelMapperPrivate; | |
|
32 | class QBoxPlotSeries; | |
|
33 | ||
|
34 | class QTCOMMERCIALCHART_EXPORT QBoxPlotModelMapper : public QObject | |
|
35 | { | |
|
36 | Q_OBJECT | |
|
37 | ||
|
38 | protected: | |
|
39 | explicit QBoxPlotModelMapper(QObject *parent = 0); | |
|
40 | ||
|
41 | QAbstractItemModel *model() const; | |
|
42 | void setModel(QAbstractItemModel *model); | |
|
43 | ||
|
44 | QBoxPlotSeries *series() const; | |
|
45 | void setSeries(QBoxPlotSeries *series); | |
|
46 | ||
|
47 | int first() const; | |
|
48 | void setFirst(int first); | |
|
49 | ||
|
50 | int count() const; | |
|
51 | void setCount(int count); | |
|
52 | ||
|
53 | int firstBoxSetSection() const; | |
|
54 | void setFirstBoxSetSection(int firstBoxSetSection); | |
|
55 | ||
|
56 | int lastBoxSetSection() const; | |
|
57 | void setLastBoxSetSection(int lastBoxSetSection); | |
|
58 | ||
|
59 | Qt::Orientation orientation() const; | |
|
60 | void setOrientation(Qt::Orientation orientation); | |
|
61 | ||
|
62 | protected: | |
|
63 | QBoxPlotModelMapperPrivate * const d_ptr; | |
|
64 | Q_DECLARE_PRIVATE(QBoxPlotModelMapper) | |
|
65 | }; | |
|
66 | ||
|
67 | QTCOMMERCIALCHART_END_NAMESPACE | |
|
68 | ||
|
69 | #endif // QBOXPLOTMODELMAPPER_H |
@@ -0,0 +1,95 | |||
|
1 | /**************************************************************************** | |
|
2 | ** | |
|
3 | ** Copyright (C) 2013 Digia Plc | |
|
4 | ** All rights reserved. | |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
|
6 | ** | |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
|
8 | ** | |
|
9 | ** $QT_BEGIN_LICENSE$ | |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
|
13 | ** a written agreement between you and Digia. | |
|
14 | ** | |
|
15 | ** If you have questions regarding the use of this file, please use | |
|
16 | ** contact form at http://qt.digia.com | |
|
17 | ** $QT_END_LICENSE$ | |
|
18 | ** | |
|
19 | ****************************************************************************/ | |
|
20 | ||
|
21 | // W A R N I N G | |
|
22 | // ------------- | |
|
23 | // | |
|
24 | // This file is not part of the QtCommercial Chart API. It exists purely as an | |
|
25 | // implementation detail. This header file may change from version to | |
|
26 | // version without notice, or even be removed. | |
|
27 | // | |
|
28 | // We mean it. | |
|
29 | ||
|
30 | #ifndef QBOXPLOTMODELMAPPER_P_H | |
|
31 | #define QBOXPLOTMODELMAPPER_P_H | |
|
32 | ||
|
33 | #include <QObject> | |
|
34 | #include "qboxplotmodelmapper.h" | |
|
35 | ||
|
36 | class QModelIndex; | |
|
37 | ||
|
38 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
|
39 | ||
|
40 | class QBoxSet; | |
|
41 | ||
|
42 | class QBoxPlotModelMapperPrivate : public QObject | |
|
43 | { | |
|
44 | Q_OBJECT | |
|
45 | public: | |
|
46 | explicit QBoxPlotModelMapperPrivate(QBoxPlotModelMapper *q); | |
|
47 | ||
|
48 | public Q_SLOTS: | |
|
49 | // for the model | |
|
50 | void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); | |
|
51 | void modelHeaderDataUpdated(Qt::Orientation orientation, int first, int last); | |
|
52 | void modelRowsAdded(QModelIndex parent, int start, int end); | |
|
53 | void modelRowsRemoved(QModelIndex parent, int start, int end); | |
|
54 | void modelColumnsAdded(QModelIndex parent, int start, int end); | |
|
55 | void modelColumnsRemoved(QModelIndex parent, int start, int end); | |
|
56 | void handleModelDestroyed(); | |
|
57 | ||
|
58 | // for the series | |
|
59 | void boxSetsAdded(QList<QBoxSet *> sets); | |
|
60 | void boxSetsRemoved(QList<QBoxSet *> sets); | |
|
61 | void valuesAdded(int index, int count); | |
|
62 | void valuesRemoved(int index, int count); | |
|
63 | void boxValueChanged(int index); | |
|
64 | void handleSeriesDestroyed(); | |
|
65 | ||
|
66 | void initializeBoxFromModel(); | |
|
67 | ||
|
68 | private: | |
|
69 | QBoxSet *boxSet(QModelIndex index); | |
|
70 | QModelIndex boxModelIndex(int boxSection, int posInBox); | |
|
71 | void insertData(int start, int end); | |
|
72 | void removeData(int start, int end); | |
|
73 | void blockModelSignals(bool block = true); | |
|
74 | void blockSeriesSignals(bool block = true); | |
|
75 | ||
|
76 | private: | |
|
77 | QBoxPlotSeries *m_series; | |
|
78 | QList<QBoxSet *> m_boxSets; | |
|
79 | QAbstractItemModel *m_model; | |
|
80 | int m_first; | |
|
81 | int m_count; | |
|
82 | Qt::Orientation m_orientation; | |
|
83 | int m_firstBoxSetSection; | |
|
84 | int m_lastBoxSetSection; | |
|
85 | bool m_seriesSignalsBlock; | |
|
86 | bool m_modelSignalsBlock; | |
|
87 | ||
|
88 | private: | |
|
89 | QBoxPlotModelMapper *q_ptr; | |
|
90 | Q_DECLARE_PUBLIC(QBoxPlotModelMapper) | |
|
91 | }; | |
|
92 | ||
|
93 | QTCOMMERCIALCHART_END_NAMESPACE | |
|
94 | ||
|
95 | #endif // QBOXPLOTMODELMAPPER_P_H |
@@ -0,0 +1,249 | |||
|
1 | /**************************************************************************** | |
|
2 | ** | |
|
3 | ** Copyright (C) 2013 Digia Plc | |
|
4 | ** All rights reserved. | |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
|
6 | ** | |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
|
8 | ** | |
|
9 | ** $QT_BEGIN_LICENSE$ | |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
|
13 | ** a written agreement between you and Digia. | |
|
14 | ** | |
|
15 | ** If you have questions regarding the use of this file, please use | |
|
16 | ** contact form at http://qt.digia.com | |
|
17 | ** $QT_END_LICENSE$ | |
|
18 | ** | |
|
19 | ****************************************************************************/ | |
|
20 | ||
|
21 | #include "qvboxplotmodelmapper.h" | |
|
22 | ||
|
23 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
|
24 | ||
|
25 | /*! | |
|
26 | \class QVBoxPlotModelMapper | |
|
27 | \brief Vertical model mapper for bar series | |
|
28 | \mainclass | |
|
29 | ||
|
30 | Model mappers allow you to use QAbstractItemModel derived models as a data source for a chart series. | |
|
31 | Vertical model mapper is used to create a connection between QAbstractBarSeries and QAbstractItemModel derived model object. | |
|
32 | Model mapper maintains equal size of all the BarSets. | |
|
33 | Adding/removing value from the BarSet causes the the same change in the rest of the BarSets added to the same series. | |
|
34 | Note: used model has to support adding/removing rows/columns and modifying the data of the cells. | |
|
35 | */ | |
|
36 | /*! | |
|
37 | \qmlclass VBarModelMapper | |
|
38 | \mainclass | |
|
39 | ||
|
40 | VBarModelMapper allows you to use your own QAbstractItemModel derived model with data in columns as a data source | |
|
41 | for any bar series. It is possible to use both QAbstractItemModel and bar series data API to manipulate data. | |
|
42 | VBarModelMapper keeps the series and the model in sync. | |
|
43 | ||
|
44 | The following QML example would create a bar series with three bar sets (assuming the | |
|
45 | model has at least four columns). Each bar set would contain data starting from row 1. The name of a set would be | |
|
46 | defined by the horizontal header (of the column). | |
|
47 | \code | |
|
48 | BarSeries { | |
|
49 | VBarModelMapper { | |
|
50 | model: myCustomModel // QAbstractItemModel derived implementation | |
|
51 | firstBarSetColumn: 1 | |
|
52 | lastBarSetColumn: 3 | |
|
53 | firstRow: 1 | |
|
54 | } | |
|
55 | } | |
|
56 | \endcode | |
|
57 | */ | |
|
58 | ||
|
59 | /*! | |
|
60 | \property QVBoxPlotModelMapper::series | |
|
61 | \brief Defines the QPieSeries object that is used by the mapper. | |
|
62 | ||
|
63 | All the data in the series is discarded when it is set to the mapper. | |
|
64 | When new series is specified the old series is disconnected (it preserves its data) | |
|
65 | */ | |
|
66 | /*! | |
|
67 | \qmlproperty AbstractBarSeries VBarModelMapper::series | |
|
68 | Defines the AbstractBarSeries based object that is used by the mapper. All the data in the series is discarded when it is | |
|
69 | set to the mapper. When new series is specified the old series is disconnected (it preserves its data). | |
|
70 | */ | |
|
71 | ||
|
72 | /*! | |
|
73 | \property QVBoxPlotModelMapper::model | |
|
74 | \brief Defines the model that is used by the mapper. | |
|
75 | */ | |
|
76 | /*! | |
|
77 | \qmlproperty SomeModel VBarModelMapper::model | |
|
78 | The QAbstractItemModel based model that is used by the mapper. You need to implement the model and expose it to | |
|
79 | QML as shown in \l {QML Custom Model} demo application. Note: the model has to support adding/removing rows/columns | |
|
80 | and modifying the data of the cells. | |
|
81 | */ | |
|
82 | ||
|
83 | /*! | |
|
84 | \property QVBoxPlotModelMapper::firstBarSetColumn | |
|
85 | \brief Defines which column of the model is used as the data source for the first bar set | |
|
86 | Default value is: -1 (invalid mapping) | |
|
87 | */ | |
|
88 | /*! | |
|
89 | \qmlproperty int VBarModelMapper::firstBarSetColumn | |
|
90 | Defines which column of the model is used as the data source for the first bar set. Default value | |
|
91 | is: -1 (invalid mapping). | |
|
92 | */ | |
|
93 | ||
|
94 | /*! | |
|
95 | \property QVBoxPlotModelMapper::lastBarSetColumn | |
|
96 | \brief Defines which column of the model is used as the data source for the last bar set | |
|
97 | Default value is: -1 (invalid mapping) | |
|
98 | */ | |
|
99 | /*! | |
|
100 | \qmlproperty int VBarModelMapper::lastBarSetColumn | |
|
101 | Defines which column of the model is used as the data source for the last bar set. Default | |
|
102 | value is: -1 (invalid mapping). | |
|
103 | */ | |
|
104 | ||
|
105 | /*! | |
|
106 | \property QVBoxPlotModelMapper::firstRow | |
|
107 | \brief Defines which row of the model contains the first values of the QBarSets in the series. | |
|
108 | Minimal and default value is: 0 | |
|
109 | */ | |
|
110 | /*! | |
|
111 | \qmlproperty int VBarModelMapper::firstRow | |
|
112 | Defines which row of the model contains the first values of the QBarSets in the series. | |
|
113 | The default value is 0. | |
|
114 | */ | |
|
115 | ||
|
116 | /*! | |
|
117 | \property QVBoxPlotModelMapper::rowCount | |
|
118 | \brief Defines the number of rows of the model that are mapped as the data for QAbstractBarSeries | |
|
119 | Minimal and default value is: -1 (count limited by the number of rows in the model) | |
|
120 | */ | |
|
121 | /*! | |
|
122 | \qmlproperty int VBarModelMapper::rowCount | |
|
123 | Defines the number of rows of the model that are mapped as the data for QAbstractBarSeries. The default value is | |
|
124 | -1 (count limited by the number of rows in the model) | |
|
125 | */ | |
|
126 | ||
|
127 | /*! | |
|
128 | \fn void QVBoxPlotModelMapper::seriesReplaced() | |
|
129 | ||
|
130 | Emitted when the series to which mapper is connected to has changed. | |
|
131 | */ | |
|
132 | ||
|
133 | /*! | |
|
134 | \fn void QVBoxPlotModelMapper::modelReplaced() | |
|
135 | ||
|
136 | Emitted when the model to which mapper is connected to has changed. | |
|
137 | */ | |
|
138 | ||
|
139 | /*! | |
|
140 | \fn void QVBoxPlotModelMapper::firstBarSetColumnChanged() | |
|
141 | Emitted when the firstBarSetColumn has changed. | |
|
142 | */ | |
|
143 | ||
|
144 | /*! | |
|
145 | \fn void QVBoxPlotModelMapper::lastBarSetColumnChanged() | |
|
146 | Emitted when the lastBarSetColumn has changed. | |
|
147 | */ | |
|
148 | ||
|
149 | /*! | |
|
150 | \fn void QVBoxPlotModelMapper::firstRowChanged() | |
|
151 | Emitted when the firstRow has changed. | |
|
152 | */ | |
|
153 | ||
|
154 | /*! | |
|
155 | \fn void QVBoxPlotModelMapper::rowCountChanged() | |
|
156 | Emitted when the rowCount has changed. | |
|
157 | */ | |
|
158 | ||
|
159 | /*! | |
|
160 | Constructs a mapper object which is a child of \a parent. | |
|
161 | */ | |
|
162 | QVBoxPlotModelMapper::QVBoxPlotModelMapper(QObject *parent) : | |
|
163 | QBoxPlotModelMapper(parent) | |
|
164 | { | |
|
165 | QBoxPlotModelMapper::setOrientation(Qt::Vertical); | |
|
166 | } | |
|
167 | ||
|
168 | QAbstractItemModel *QVBoxPlotModelMapper::model() const | |
|
169 | { | |
|
170 | return QBoxPlotModelMapper::model(); | |
|
171 | } | |
|
172 | ||
|
173 | void QVBoxPlotModelMapper::setModel(QAbstractItemModel *model) | |
|
174 | { | |
|
175 | if (model != QBoxPlotModelMapper::model()) { | |
|
176 | QBoxPlotModelMapper::setModel(model); | |
|
177 | emit modelReplaced(); | |
|
178 | } | |
|
179 | } | |
|
180 | ||
|
181 | QBoxPlotSeries *QVBoxPlotModelMapper::series() const | |
|
182 | { | |
|
183 | return QBoxPlotModelMapper::series(); | |
|
184 | } | |
|
185 | ||
|
186 | void QVBoxPlotModelMapper::setSeries(QBoxPlotSeries *series) | |
|
187 | { | |
|
188 | if (series != QBoxPlotModelMapper::series()) { | |
|
189 | QBoxPlotModelMapper::setSeries(series); | |
|
190 | emit seriesReplaced(); | |
|
191 | } | |
|
192 | } | |
|
193 | ||
|
194 | int QVBoxPlotModelMapper::firstBoxSetColumn() const | |
|
195 | { | |
|
196 | return QBoxPlotModelMapper::firstBoxSetSection(); | |
|
197 | } | |
|
198 | ||
|
199 | void QVBoxPlotModelMapper::setFirstBoxSetColumn(int firstBoxSetColumn) | |
|
200 | { | |
|
201 | if (firstBoxSetColumn != firstBoxSetSection()) { | |
|
202 | QBoxPlotModelMapper::setFirstBoxSetSection(firstBoxSetColumn); | |
|
203 | emit firstBoxSetColumnChanged(); | |
|
204 | } | |
|
205 | } | |
|
206 | ||
|
207 | int QVBoxPlotModelMapper::lastBoxSetColumn() const | |
|
208 | { | |
|
209 | return QBoxPlotModelMapper::lastBoxSetSection(); | |
|
210 | } | |
|
211 | ||
|
212 | void QVBoxPlotModelMapper::setLastBoxSetColumn(int lastBoxSetColumn) | |
|
213 | { | |
|
214 | if (lastBoxSetColumn != lastBoxSetSection()) { | |
|
215 | QBoxPlotModelMapper::setLastBoxSetSection(lastBoxSetColumn); | |
|
216 | emit lastBoxSetColumnChanged(); | |
|
217 | } | |
|
218 | } | |
|
219 | ||
|
220 | int QVBoxPlotModelMapper::firstRow() const | |
|
221 | { | |
|
222 | return QBoxPlotModelMapper::first(); | |
|
223 | } | |
|
224 | ||
|
225 | void QVBoxPlotModelMapper::setFirstRow(int firstRow) | |
|
226 | { | |
|
227 | if (firstRow != first()) { | |
|
228 | QBoxPlotModelMapper::setFirst(firstRow); | |
|
229 | emit firstRowChanged(); | |
|
230 | } | |
|
231 | } | |
|
232 | ||
|
233 | int QVBoxPlotModelMapper::rowCount() const | |
|
234 | { | |
|
235 | return QBoxPlotModelMapper::count(); | |
|
236 | } | |
|
237 | ||
|
238 | void QVBoxPlotModelMapper::setRowCount(int rowCount) | |
|
239 | { | |
|
240 | if (rowCount != count()) { | |
|
241 | QBoxPlotModelMapper::setCount(rowCount); | |
|
242 | emit rowCountChanged(); | |
|
243 | } | |
|
244 | } | |
|
245 | ||
|
246 | #include "moc_qvboxplotmodelmapper.cpp" | |
|
247 | ||
|
248 | QTCOMMERCIALCHART_END_NAMESPACE | |
|
249 |
@@ -0,0 +1,70 | |||
|
1 | /**************************************************************************** | |
|
2 | ** | |
|
3 | ** Copyright (C) 2013 Digia Plc | |
|
4 | ** All rights reserved. | |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
|
6 | ** | |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
|
8 | ** | |
|
9 | ** $QT_BEGIN_LICENSE$ | |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
|
13 | ** a written agreement between you and Digia. | |
|
14 | ** | |
|
15 | ** If you have questions regarding the use of this file, please use | |
|
16 | ** contact form at http://qt.digia.com | |
|
17 | ** $QT_END_LICENSE$ | |
|
18 | ** | |
|
19 | ****************************************************************************/ | |
|
20 | ||
|
21 | #ifndef QVBOXPLOTMODELMAPPER_H | |
|
22 | #define QVBOXPLOTMODELMAPPER_H | |
|
23 | ||
|
24 | #include <QBoxPlotModelMapper> | |
|
25 | ||
|
26 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
|
27 | ||
|
28 | class QTCOMMERCIALCHART_EXPORT QVBoxPlotModelMapper : public QBoxPlotModelMapper | |
|
29 | { | |
|
30 | Q_OBJECT | |
|
31 | Q_PROPERTY(QBoxPlotSeries *series READ series WRITE setSeries NOTIFY seriesReplaced) | |
|
32 | Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelReplaced) | |
|
33 | Q_PROPERTY(int firstBoxSetColumn READ firstBoxSetColumn WRITE setFirstBoxSetColumn NOTIFY firstBoxSetColumnChanged) | |
|
34 | Q_PROPERTY(int lastBoxSetColumn READ lastBoxSetColumn WRITE setLastBoxSetColumn NOTIFY lastBoxSetColumnChanged) | |
|
35 | Q_PROPERTY(int firstRow READ firstRow WRITE setFirstRow NOTIFY firstRowChanged) | |
|
36 | Q_PROPERTY(int rowCount READ rowCount WRITE setRowCount NOTIFY rowCountChanged) | |
|
37 | ||
|
38 | public: | |
|
39 | explicit QVBoxPlotModelMapper(QObject *parent = 0); | |
|
40 | ||
|
41 | QAbstractItemModel *model() const; | |
|
42 | void setModel(QAbstractItemModel *model); | |
|
43 | ||
|
44 | QBoxPlotSeries *series() const; | |
|
45 | void setSeries(QBoxPlotSeries *series); | |
|
46 | ||
|
47 | int firstBoxSetColumn() const; | |
|
48 | void setFirstBoxSetColumn(int firstBoxSetColumn); | |
|
49 | ||
|
50 | int lastBoxSetColumn() const; | |
|
51 | void setLastBoxSetColumn(int lastBoxSetColumn); | |
|
52 | ||
|
53 | int firstRow() const; | |
|
54 | void setFirstRow(int firstRow); | |
|
55 | ||
|
56 | int rowCount() const; | |
|
57 | void setRowCount(int rowCount); | |
|
58 | ||
|
59 | Q_SIGNALS: | |
|
60 | void seriesReplaced(); | |
|
61 | void modelReplaced(); | |
|
62 | void firstBoxSetColumnChanged(); | |
|
63 | void lastBoxSetColumnChanged(); | |
|
64 | void firstRowChanged(); | |
|
65 | void rowCountChanged(); | |
|
66 | }; | |
|
67 | ||
|
68 | QTCOMMERCIALCHART_END_NAMESPACE | |
|
69 | ||
|
70 | #endif // QVBOXPLOTMODELMAPPER_H |
@@ -1,19 +1,25 | |||
|
1 | 1 | INCLUDEPATH += $$PWD |
|
2 | 2 | DEPENDPATH += $$PWD |
|
3 | 3 | |
|
4 | 4 | SOURCES += \ |
|
5 | 5 | $$PWD/boxplotchartitem.cpp \ |
|
6 | 6 | $$PWD/qboxplotseries.cpp \ |
|
7 | 7 | $$PWD/boxwhiskers.cpp \ |
|
8 | $$PWD/qboxset.cpp | |
|
8 | $$PWD/qboxset.cpp \ | |
|
9 | $$PWD/qboxplotmodelmapper.cpp \ | |
|
10 | $$PWD/qvboxplotmodelmapper.cpp | |
|
9 | 11 | |
|
10 | 12 | PRIVATE_HEADERS += \ |
|
11 | 13 | $$PWD/boxplotchartitem_p.h \ |
|
12 | 14 | $$PWD/qboxplotseries_p.h \ |
|
13 | 15 | $$PWD/boxwhiskers_p.h \ |
|
14 | 16 | $$PWD/boxwhiskersdata_p.h \ |
|
15 | $$PWD/qboxset_p.h | |
|
17 | $$PWD/qboxset_p.h \ | |
|
18 | $$PWD/qboxplotmodelmapper_p.h | |
|
16 | 19 | |
|
17 | 20 | PUBLIC_HEADERS += \ |
|
18 | 21 | $$PWD/qboxplotseries.h \ |
|
19 | $$PWD/qboxset.h | |
|
22 | $$PWD/qboxset.h \ | |
|
23 | $$PWD/qboxplotmodelmapper.h \ | |
|
24 | $$PWD/qvboxplotmodelmapper.h | |
|
25 |
@@ -1,358 +1,358 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2013 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 "mainwidget.h" |
|
22 | 22 | #include "customtablemodel.h" |
|
23 |
#include <QVB |
|
|
23 | #include <QVBoxPlotModelMapper> | |
|
24 | 24 | #include <QTableView> |
|
25 | 25 | #include <QHeaderView> |
|
26 | 26 | #include <QChartView> |
|
27 | 27 | #include <QBoxPlotSeries> |
|
28 | 28 | #include <QBoxSet> |
|
29 | 29 | #include <QLegend> |
|
30 | 30 | #include <QBarCategoryAxis> |
|
31 | 31 | #include <QBrush> |
|
32 | 32 | #include <QColor> |
|
33 | 33 | #include <QPushButton> |
|
34 | 34 | #include <QComboBox> |
|
35 | 35 | #include <QSpinBox> |
|
36 | 36 | #include <QCheckBox> |
|
37 | 37 | #include <QGridLayout> |
|
38 | 38 | #include <QHBoxLayout> |
|
39 | 39 | #include <QLabel> |
|
40 | 40 | #include <QSpacerItem> |
|
41 | 41 | #include <QMessageBox> |
|
42 | 42 | #include <cmath> |
|
43 | 43 | #include <QDebug> |
|
44 | 44 | #include <QStandardItemModel> |
|
45 | 45 | #include <QBarCategoryAxis> |
|
46 | 46 | |
|
47 | 47 | |
|
48 | 48 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
49 | 49 | |
|
50 | 50 | QString addCategories[] = {"Jul", "Aug", "Sep", "Nov", "Dec"}; |
|
51 | 51 | |
|
52 | 52 | MainWidget::MainWidget(QWidget *parent) : |
|
53 | 53 | QWidget(parent), |
|
54 | 54 | m_chart(0), |
|
55 | 55 | rowPos(0), |
|
56 | 56 | nSeries(0), |
|
57 | 57 | nNewBoxes(0) |
|
58 | 58 | { |
|
59 | 59 | m_chart = new QChart(); |
|
60 | 60 | |
|
61 | 61 | // Grid layout for the controls for configuring the chart widget |
|
62 | 62 | QGridLayout *grid = new QGridLayout(); |
|
63 | 63 | |
|
64 | 64 | // Create add a series button |
|
65 | 65 | QPushButton *addSeriesButton = new QPushButton("Add a series"); |
|
66 | 66 | connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries())); |
|
67 | 67 | grid->addWidget(addSeriesButton, rowPos++, 1); |
|
68 | 68 | |
|
69 | 69 | // Create remove a series button |
|
70 | 70 | QPushButton *removeSeriesButton = new QPushButton("Remove a series"); |
|
71 | 71 | connect(removeSeriesButton, SIGNAL(clicked()), this, SLOT(removeSeries())); |
|
72 | 72 | grid->addWidget(removeSeriesButton, rowPos++, 1); |
|
73 | 73 | |
|
74 | 74 | // Create add a single box button |
|
75 | 75 | QPushButton *addBoxButton = new QPushButton("Add a box"); |
|
76 | 76 | connect(addBoxButton, SIGNAL(clicked()), this, SLOT(addBox())); |
|
77 | 77 | grid->addWidget(addBoxButton, rowPos++, 1); |
|
78 | 78 | |
|
79 | 79 | // Create insert a box button |
|
80 | 80 | QPushButton *insertBoxButton = new QPushButton("Insert a box"); |
|
81 | 81 | connect(insertBoxButton, SIGNAL(clicked()), this, SLOT(insertBox())); |
|
82 | 82 | grid->addWidget(insertBoxButton, rowPos++, 1); |
|
83 | 83 | |
|
84 | 84 | // Create add a single box button |
|
85 | 85 | QPushButton *removeBoxButton = new QPushButton("Remove a box"); |
|
86 | 86 | connect(removeBoxButton, SIGNAL(clicked()), this, SLOT(removeBox())); |
|
87 | 87 | grid->addWidget(removeBoxButton, rowPos++, 1); |
|
88 | 88 | |
|
89 | 89 | // Create clear button |
|
90 | 90 | QPushButton *clearButton = new QPushButton("Clear"); |
|
91 | 91 | connect(clearButton, SIGNAL(clicked()), this, SLOT(clear())); |
|
92 | 92 | grid->addWidget(clearButton, rowPos++, 1); |
|
93 | 93 | |
|
94 | 94 | // Create set brush button |
|
95 | 95 | QPushButton *setBrushButton = new QPushButton("Set brush"); |
|
96 | 96 | connect(setBrushButton, SIGNAL(clicked()), this, SLOT(setBrush())); |
|
97 | 97 | grid->addWidget(setBrushButton, rowPos++, 1); |
|
98 | 98 | |
|
99 | 99 | initThemeCombo(grid); |
|
100 | 100 | initCheckboxes(grid); |
|
101 | 101 | |
|
102 | 102 | m_model = new CustomTableModel; |
|
103 | 103 | QTableView *tableView = new QTableView; |
|
104 | 104 | tableView->setModel(m_model); |
|
105 | 105 | tableView->setMaximumWidth(200); |
|
106 | 106 | grid->addWidget(tableView, rowPos++, 0, 3, 2, Qt::AlignLeft); |
|
107 | 107 | #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) |
|
108 | 108 | tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); |
|
109 | 109 | tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch); |
|
110 | 110 | #else |
|
111 | 111 | tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch); |
|
112 | 112 | tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch); |
|
113 | 113 | #endif |
|
114 | 114 | |
|
115 | 115 | // add row with empty label to make all the other rows static |
|
116 | 116 | grid->addWidget(new QLabel(""), grid->rowCount(), 0); |
|
117 | 117 | grid->setRowStretch(grid->rowCount() - 1, 1); |
|
118 | 118 | |
|
119 | 119 | // Create chart view with the chart |
|
120 | 120 | m_chartView = new QChartView(m_chart, this); |
|
121 | 121 | //m_chartView->setRubberBand(QChartView::HorizonalRubberBand); |
|
122 | 122 | |
|
123 | 123 | // Another grid layout as a main layout |
|
124 | 124 | QGridLayout *mainLayout = new QGridLayout(); |
|
125 | 125 | mainLayout->addLayout(grid, 0, 0); |
|
126 | 126 | mainLayout->addWidget(m_chartView, 0, 1, 3, 1); |
|
127 | 127 | setLayout(mainLayout); |
|
128 | 128 | |
|
129 | 129 | legendToggled(false); |
|
130 | 130 | animationToggled(false); |
|
131 | 131 | } |
|
132 | 132 | |
|
133 | 133 | // Combo box for selecting theme |
|
134 | 134 | void MainWidget::initThemeCombo(QGridLayout *grid) |
|
135 | 135 | { |
|
136 | 136 | QComboBox *chartTheme = new QComboBox(); |
|
137 | 137 | chartTheme->addItem("Default"); |
|
138 | 138 | chartTheme->addItem("Light"); |
|
139 | 139 | chartTheme->addItem("Blue Cerulean"); |
|
140 | 140 | chartTheme->addItem("Dark"); |
|
141 | 141 | chartTheme->addItem("Brown Sand"); |
|
142 | 142 | chartTheme->addItem("Blue NCS"); |
|
143 | 143 | chartTheme->addItem("High Contrast"); |
|
144 | 144 | chartTheme->addItem("Blue Icy"); |
|
145 | 145 | connect(chartTheme, SIGNAL(currentIndexChanged(int)), |
|
146 | 146 | this, SLOT(changeChartTheme(int))); |
|
147 | 147 | grid->addWidget(new QLabel("Chart theme:"), rowPos, 0); |
|
148 | 148 | grid->addWidget(chartTheme, rowPos++, 1); |
|
149 | 149 | } |
|
150 | 150 | |
|
151 | 151 | // Different check boxes for customizing chart |
|
152 | 152 | void MainWidget::initCheckboxes(QGridLayout *grid) |
|
153 | 153 | { |
|
154 | 154 | QCheckBox *animationCheckBox = new QCheckBox("Animation"); |
|
155 | 155 | connect(animationCheckBox, SIGNAL(toggled(bool)), this, SLOT(animationToggled(bool))); |
|
156 | 156 | animationCheckBox->setChecked(false); |
|
157 | 157 | grid->addWidget(animationCheckBox, rowPos++, 0); |
|
158 | 158 | |
|
159 | 159 | QCheckBox *legendCheckBox = new QCheckBox("Legend"); |
|
160 | 160 | connect(legendCheckBox, SIGNAL(toggled(bool)), this, SLOT(legendToggled(bool))); |
|
161 | 161 | legendCheckBox->setChecked(false); |
|
162 | 162 | grid->addWidget(legendCheckBox, rowPos++, 0); |
|
163 | 163 | |
|
164 | 164 | QCheckBox *titleCheckBox = new QCheckBox("Title"); |
|
165 | 165 | connect(titleCheckBox, SIGNAL(toggled(bool)), this, SLOT(titleToggled(bool))); |
|
166 | 166 | titleCheckBox->setChecked(false); |
|
167 | 167 | grid->addWidget(titleCheckBox, rowPos++, 0); |
|
168 | 168 | |
|
169 | 169 | QCheckBox *modelMapperCheckBox = new QCheckBox("Use model mapper"); |
|
170 | 170 | connect(modelMapperCheckBox, SIGNAL(toggled(bool)), this, SLOT(modelMapperToggled(bool))); |
|
171 | 171 | modelMapperCheckBox->setChecked(false); |
|
172 | 172 | grid->addWidget(modelMapperCheckBox, rowPos++, 0); |
|
173 | 173 | |
|
174 | 174 | } |
|
175 | 175 | |
|
176 | 176 | void MainWidget::addSeries() |
|
177 | 177 | { |
|
178 | 178 | qDebug() << "BoxPlotTester::MainWidget::addSeries()"; |
|
179 | 179 | |
|
180 | 180 | if (nSeries > 9) |
|
181 | 181 | return; |
|
182 | 182 | |
|
183 | 183 | // Initial data |
|
184 | 184 | //![1] |
|
185 | 185 | QBoxSet *set0 = new QBoxSet(); |
|
186 | 186 | QBoxSet *set1 = new QBoxSet(); |
|
187 | 187 | QBoxSet *set2 = new QBoxSet(); |
|
188 | 188 | QBoxSet *set3 = new QBoxSet(); |
|
189 | 189 | QBoxSet *set4 = new QBoxSet(); |
|
190 | 190 | QBoxSet *set5 = new QBoxSet(); |
|
191 | 191 | |
|
192 | 192 | // low bot med top upp |
|
193 | 193 | *set0 << 3 << 4 << 4.4 << 6 << 7; |
|
194 | 194 | *set1 << 5 << 6 << 7.5 << 8 << 12; |
|
195 | 195 | *set2 << 3 << 5 << 5.7 << 8 << 9; |
|
196 | 196 | *set3 << 5 << 6 << 6.8 << 7 << 8; |
|
197 | 197 | *set4 << 4 << 5 << 5.2 << 6 << 7; |
|
198 | 198 | *set5 << 4 << 7 << 8.2 << 9 << 10; |
|
199 | 199 | |
|
200 | 200 | m_series[nSeries] = new QBoxPlotSeries(); |
|
201 | 201 | m_series[nSeries]->append(set0); |
|
202 | 202 | m_series[nSeries]->append(set1); |
|
203 | 203 | m_series[nSeries]->append(set2); |
|
204 | 204 | m_series[nSeries]->append(set3); |
|
205 | 205 | m_series[nSeries]->append(set4); |
|
206 | 206 | m_series[nSeries]->append(set5); |
|
207 | 207 | m_series[nSeries]->setName("Box & Whiskers"); |
|
208 | 208 | |
|
209 | 209 | m_chart->addSeries(m_series[nSeries]); |
|
210 | 210 | |
|
211 | 211 | if (nSeries == 0) { |
|
212 | 212 | QStringList categories; |
|
213 | 213 | categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; |
|
214 | 214 | m_axis = new QBarCategoryAxis(); |
|
215 | 215 | m_axis->append(categories); |
|
216 | 216 | m_chart->createDefaultAxes(); |
|
217 |
|
|
|
217 | m_chart->setAxisX(m_axis, m_series[nSeries]); | |
|
218 | 218 | } |
|
219 | 219 | |
|
220 | 220 | nSeries++; |
|
221 | 221 | } |
|
222 | 222 | |
|
223 | 223 | void MainWidget::removeSeries() |
|
224 | 224 | { |
|
225 | 225 | qDebug() << "BoxPlotTester::MainWidget::removeSeries()"; |
|
226 | 226 | |
|
227 | 227 | if (nSeries > 0) { |
|
228 | 228 | nSeries--; |
|
229 | 229 | m_chart->removeSeries(m_series[nSeries]); |
|
230 | 230 | delete m_series[nSeries]; |
|
231 | 231 | } else { |
|
232 | 232 | qDebug() << "Create a series first"; |
|
233 | 233 | } |
|
234 | 234 | } |
|
235 | 235 | |
|
236 | 236 | void MainWidget::addBox() |
|
237 | 237 | { |
|
238 | 238 | qDebug() << "BoxPlotTester::MainWidget::addBox()"; |
|
239 | 239 | |
|
240 | 240 | if (nSeries > 0) { |
|
241 | 241 | QBoxSet *newSet = new QBoxSet(); |
|
242 | 242 | *newSet << 5 << 6 << 6.8 << 7 << 8; |
|
243 | 243 | |
|
244 | 244 | m_series[0]->append(newSet); |
|
245 | 245 | |
|
246 | 246 | m_axis->append(addCategories[nNewBoxes]); |
|
247 | 247 | |
|
248 | 248 | nNewBoxes++; |
|
249 | 249 | } |
|
250 | 250 | } |
|
251 | 251 | |
|
252 | 252 | void MainWidget::insertBox() |
|
253 | 253 | { |
|
254 | 254 | qDebug() << "BoxPlotTester::MainWidget::insertBox()"; |
|
255 | 255 | |
|
256 | 256 | if (nSeries > 0) { |
|
257 | 257 | QBoxSet *newSet = new QBoxSet(); |
|
258 | 258 | *newSet << 2 << 6 << 6.8 << 7 << 10; |
|
259 | 259 | |
|
260 | 260 | m_series[0]->insert(1, newSet); |
|
261 | 261 | |
|
262 | 262 | m_axis->append(addCategories[nNewBoxes]); |
|
263 | 263 | |
|
264 | 264 | nNewBoxes++; |
|
265 | 265 | } |
|
266 | 266 | } |
|
267 | 267 | |
|
268 | 268 | void MainWidget::removeBox() |
|
269 | 269 | { |
|
270 | 270 | qDebug() << "BoxPlotTester::MainWidget::removeBox"; |
|
271 | 271 | |
|
272 | 272 | if (nSeries > 0) { |
|
273 | 273 | QList<QBoxSet *> sets = m_series[0]->boxSets(); |
|
274 | 274 | m_series[0]->remove(sets.at(m_series[0]->count() - 3)); |
|
275 | 275 | } else { |
|
276 | 276 | qDebug() << "Create a series first"; |
|
277 | 277 | } |
|
278 | 278 | } |
|
279 | 279 | |
|
280 | 280 | void MainWidget::clear() |
|
281 | 281 | { |
|
282 | 282 | qDebug() << "BoxPlotTester::MainWidget::clear"; |
|
283 | 283 | |
|
284 | 284 | if (nSeries > 0) { |
|
285 | 285 | m_series[0]->clear(); |
|
286 | 286 | } else { |
|
287 | 287 | qDebug() << "Create a series first"; |
|
288 | 288 | } |
|
289 | 289 | } |
|
290 | 290 | |
|
291 | 291 | void MainWidget::setBrush() |
|
292 | 292 | { |
|
293 | 293 | qDebug() << "BoxPlotTester::MainWidget::setBrush"; |
|
294 | 294 | |
|
295 | 295 | if (nSeries > 0) { |
|
296 | 296 | QList<QBoxSet *> sets = m_series[0]->boxSets(); |
|
297 | 297 | sets.at(1)->setBrush(QBrush(QColor(Qt::yellow))); |
|
298 | 298 | } else { |
|
299 | 299 | qDebug() << "Create a series first"; |
|
300 | 300 | } |
|
301 | 301 | } |
|
302 | 302 | |
|
303 | 303 | void MainWidget::animationToggled(bool enabled) |
|
304 | 304 | { |
|
305 | 305 | qDebug() << "BoxPlotTester::Animation toggled to " << enabled; |
|
306 | 306 | if (enabled) |
|
307 | 307 | m_chart->setAnimationOptions(QChart::SeriesAnimations); |
|
308 | 308 | else |
|
309 | 309 | m_chart->setAnimationOptions(QChart::NoAnimation); |
|
310 | 310 | } |
|
311 | 311 | |
|
312 | 312 | void MainWidget::legendToggled(bool enabled) |
|
313 | 313 | { |
|
314 | 314 | qDebug() << "BoxPlotTester::Legend toggled to " << enabled; |
|
315 | 315 | m_chart->legend()->setVisible(enabled); |
|
316 | 316 | if (enabled) |
|
317 | 317 | m_chart->legend()->setAlignment(Qt::AlignBottom); |
|
318 | 318 | } |
|
319 | 319 | |
|
320 | 320 | void MainWidget::titleToggled(bool enabled) |
|
321 | 321 | { |
|
322 | 322 | qDebug() << "BoxPlotTester::Title toggled to " << enabled; |
|
323 | 323 | if (enabled) |
|
324 | 324 | m_chart->setTitle("Simple boxplotchart example"); |
|
325 | 325 | else |
|
326 | 326 | m_chart->setTitle(""); |
|
327 | 327 | } |
|
328 | 328 | |
|
329 | 329 | void MainWidget::modelMapperToggled(bool enabled) |
|
330 | 330 | { |
|
331 | 331 | if (enabled) { |
|
332 | 332 | m_series[nSeries] = new QBoxPlotSeries(); |
|
333 | 333 | |
|
334 | 334 | int first = 0; |
|
335 | 335 | int count = 5; |
|
336 |
QVB |
|
|
337 |
mapper->setFirstB |
|
|
338 |
mapper->setLastB |
|
|
336 | QVBoxPlotModelMapper *mapper = new QVBoxPlotModelMapper(this); | |
|
337 | mapper->setFirstBoxSetColumn(0); | |
|
338 | mapper->setLastBoxSetColumn(5); | |
|
339 | 339 | mapper->setFirstRow(first); |
|
340 | 340 | mapper->setRowCount(count); |
|
341 |
|
|
|
341 | mapper->setSeries(m_series[nSeries]); | |
|
342 | 342 | mapper->setModel(m_model); |
|
343 | 343 | m_chart->addSeries(m_series[nSeries]); |
|
344 | 344 | |
|
345 | 345 | nSeries++; |
|
346 | 346 | } else { |
|
347 | 347 | removeSeries(); |
|
348 | 348 | } |
|
349 | 349 | } |
|
350 | 350 | |
|
351 | 351 | void MainWidget::changeChartTheme(int themeIndex) |
|
352 | 352 | { |
|
353 | 353 | qDebug() << "BoxPlotTester::changeChartTheme: " << themeIndex; |
|
354 | 354 | if (themeIndex == 0) |
|
355 | 355 | m_chart->setTheme(QChart::ChartThemeLight); |
|
356 | 356 | else |
|
357 | 357 | m_chart->setTheme((QChart::ChartTheme) (themeIndex - 1)); |
|
358 | 358 | } |
General Comments 0
You need to be logged in to leave comments.
Login now