##// END OF EJS Templates
Removed commented out code and unnecessary lines from model mapper classes
Marek Rosa -
r1379:e28c452cfa1d
parent child
Show More
@@ -1,482 +1,428
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 The instance of this class cannot be created directly. QHBarModelMapper of QVBarModelMapper should be used instead. This class is used to create a connection between QBarSeries and QAbstractItemModel derived model object.
36 36 Curently it is NOT possible to use both QAbstractItemModel and QBarSeries model API.
37 37 When the series is set to the mapper the QBarSeries and QBarSet API that affect the data (append, setValue, remove) should not be used.
38 38 The model and the QBarSeries won't be kept in sync. Model API should be used to insert,remove,modify BarSets.
39 39 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
40 40 */
41 41
42 42 /*!
43 43 \property QBarModelMapper::series
44 44 \brief Defines the QPieSeries object that is used by the mapper.
45 45
46 46 All the data in the series is discarded when it is set to the mapper.
47 47 When new series is specified the old series is disconnected (it preserves its data)
48 48 */
49 49
50 50 /*!
51 51 \property QBarModelMapper::model
52 52 \brief Defines the model that is used by the mapper.
53 53 */
54 54
55 55 /*!
56 56 \property QBarModelMapper::first
57 57 \brief Defines which item of the model's row/column should be mapped as the value of the first QBarSet in the series.
58 58
59 59 Minimal and default value is: 0
60 60 */
61 61
62 62 /*!
63 63 \property QBarModelMapper::count
64 64 \brief Defines the number of rows/columns of the model that are mapped as the data for QBarSeries
65 65
66 66 Minimal and default value is: -1 (count limited by the number of rows/columns in the model)
67 67 */
68 68
69 69 /*!
70 70 Constructs a mapper object which is a child of \a parent.
71 71 */
72 72 QBarModelMapper::QBarModelMapper(QObject *parent) :
73 73 QObject(parent),
74 74 d_ptr(new QBarModelMapperPrivate(this))
75 75 {
76 76 }
77 77
78 78 QAbstractItemModel* QBarModelMapper::model() const
79 79 {
80 80 Q_D(const QBarModelMapper);
81 81 return d->m_model;
82 82 }
83 83
84 84 void QBarModelMapper::setModel(QAbstractItemModel *model)
85 85 {
86 86 if (model == 0)
87 87 return;
88 88
89 89 Q_D(QBarModelMapper);
90 90 if (d->m_model) {
91 91 disconnect(d->m_model, 0, d, 0);
92 92 }
93 93
94 94 d->m_model = model;
95 95 d->initializeBarFromModel();
96 96 // connect signals from the model
97 97 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
98 98 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
99 99 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
100 100 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
101 101 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
102 102 }
103 103
104 104 QBarSeries* QBarModelMapper::series() const
105 105 {
106 106 Q_D(const QBarModelMapper);
107 107 return d->m_series;
108 108 }
109 109
110 110 void QBarModelMapper::setSeries(QBarSeries *series)
111 111 {
112 112 Q_D(QBarModelMapper);
113 113 if (d->m_series) {
114 114 disconnect(d->m_series, 0, d, 0);
115 115 }
116 116
117 117 if (series == 0)
118 118 return;
119 119
120 120 d->m_series = series;
121 121 d->initializeBarFromModel();
122 122 // connect the signals from the series
123 // connect(d->m_series, SIGNAL(pointAdded(int)), d, SLOT(handlePointAdded(int)));
124 // connect(d->m_series, SIGNAL(pointRemoved(int)), d, SLOT(handlePointRemoved(int)));
125 // connect(d->m_series, SIGNAL(pointReplaced(int)), d, SLOT(handlePointReplaced(int)));
123 // TODO: TO be implemented
126 124 }
127 125
128 126 int QBarModelMapper::first() const
129 127 {
130 128 Q_D(const QBarModelMapper);
131 129 return d->m_first;
132 130 }
133 131
134 132 void QBarModelMapper::setFirst(int first)
135 133 {
136 134 Q_D(QBarModelMapper);
137 135 d->m_first = qMax(first, 0);
138 136 d->initializeBarFromModel();
139 137 }
140 138
141 139 int QBarModelMapper::count() const
142 140 {
143 141 Q_D(const QBarModelMapper);
144 142 return d->m_count;
145 143 }
146 144
147 145 void QBarModelMapper::setCount(int count)
148 146 {
149 147 Q_D(QBarModelMapper);
150 148 d->m_count = qMax(count, -1);
151 149 d->initializeBarFromModel();
152 150 }
153 151
154 152 /*!
155 153 Returns the orientation that is used when QBarModelMapper accesses the model.
156 154 This mean whether the consecutive values of the bar set are read from row (Qt::Horizontal)
157 155 or from columns (Qt::Vertical)
158 156 */
159 157 Qt::Orientation QBarModelMapper::orientation() const
160 158 {
161 159 Q_D(const QBarModelMapper);
162 160 return d->m_orientation;
163 161 }
164 162
165 163 /*!
166 164 Returns the \a orientation that is used when QBarModelMapper accesses the model.
167 165 This mean whether the consecutive values of the pie are read from row (Qt::Horizontal)
168 166 or from columns (Qt::Vertical)
169 167 */
170 168 void QBarModelMapper::setOrientation(Qt::Orientation orientation)
171 169 {
172 170 Q_D(QBarModelMapper);
173 171 d->m_orientation = orientation;
174 172 d->initializeBarFromModel();
175 173 }
176 174
177 175 /*!
178 176 Returns which section of the model is used as the data source for the first bar set
179 177 */
180 178 int QBarModelMapper::firstBarSetSection() const
181 179 {
182 180 Q_D(const QBarModelMapper);
183 181 return d->m_firstBarSetSection;
184 182 }
185 183
186 184 /*!
187 185 Sets the model section that is used as the data source for the first bar set
188 186 Parameter \a firstBarSetSection specifies the section of the model.
189 187 */
190 188 void QBarModelMapper::setFirstBarSetSection(int firstBarSetSection)
191 189 {
192 190 Q_D(QBarModelMapper);
193 191 d->m_firstBarSetSection = qMax(-1, firstBarSetSection);
194 192 d->initializeBarFromModel();
195 193 }
196 194
197 195 /*!
198 196 Returns which section of the model is used as the data source for the last bar set
199 197 */
200 198 int QBarModelMapper::lastBarSetSection() const
201 199 {
202 200 Q_D(const QBarModelMapper);
203 201 return d->m_lastBarSetSection;
204 202 }
205 203
206 204 /*!
207 205 Sets the model section that is used as the data source for the last bar set
208 206 Parameter \a lastBarSetSection specifies the section of the model.
209 207 */
210 208 void QBarModelMapper::setLastBarSetSection(int lastBarSetSection)
211 209 {
212 210 Q_D(QBarModelMapper);
213 211 d->m_lastBarSetSection = qMax(-1, lastBarSetSection);
214 212 d->initializeBarFromModel();
215 213 }
216 214
217 215 /*!
218 216 Resets the QBarModelMapper to the default state.
219 217 first: 0; count: -1; firstBarSetSection: -1; lastBarSetSection: -1; categoriesSection: -1
220 218 */
221 219 void QBarModelMapper::reset()
222 220 {
223 221 Q_D(QBarModelMapper);
224 222 d->m_first = 0;
225 223 d->m_count = -1;
226 224 d->m_firstBarSetSection = -1;
227 225 d->m_lastBarSetSection = -1;
228 226 d->initializeBarFromModel();
229 227 }
230 228
231 229 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
232 230
233 231 QBarModelMapperPrivate::QBarModelMapperPrivate(QBarModelMapper *q) :
234 232 m_series(0),
235 233 m_model(0),
236 234 m_first(0),
237 235 m_count(-1),
238 236 m_orientation(Qt::Vertical),
239 237 m_firstBarSetSection(-1),
240 238 m_lastBarSetSection(-1),
241 239 m_seriesSignalsBlock(false),
242 240 m_modelSignalsBlock(false),
243 241 q_ptr(q)
244 242 {
245 243 }
246 244
247 245 void QBarModelMapperPrivate::blockModelSignals(bool block)
248 246 {
249 247 m_modelSignalsBlock = block;
250 248 }
251 249
252 250 void QBarModelMapperPrivate::blockSeriesSignals(bool block)
253 251 {
254 252 m_seriesSignalsBlock = block;
255 253 }
256 254
257 255 QBarSet* QBarModelMapperPrivate::barSet(QModelIndex index)
258 256 {
259 257 if (!index.isValid())
260 258 return 0;
261 259
262 260 if (m_orientation == Qt::Vertical && index.column() >= m_firstBarSetSection && index.column() <= m_lastBarSetSection) {
263 261 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) {
264 262 // if (m_model->index(index.row(), m_valuesSection).isValid() && m_model->index(index.row(), m_labelsSection).isValid())
265 263 return m_series->barSets().at(index.column() - m_firstBarSetSection);
266 264 // else
267 265 // return 0;
268 266 }
269 267 } else if (m_orientation == Qt::Horizontal && index.row() >= m_firstBarSetSection && index.row() <= m_lastBarSetSection) {
270 268 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
271 269 return m_series->barSets().at(index.row() - m_firstBarSetSection);
272 270 }
273 271 return 0; // This part of model has not been mapped to any slice
274 272 }
275 273
276 274 QModelIndex QBarModelMapperPrivate::barModelIndex(int barSection, int posInBar)
277 275 {
278 276 if (m_count != -1 && posInBar >= m_count)
279 277 return QModelIndex(); // invalid
280 278
281 279 if (barSection < m_firstBarSetSection || barSection > m_lastBarSetSection)
282 280 return QModelIndex(); // invalid
283 281
284 282 if (m_orientation == Qt::Vertical)
285 283 return m_model->index(posInBar + m_first, barSection);
286 284 else
287 285 return m_model->index(barSection, posInBar + m_first);
288 286 }
289 287
290 288 void QBarModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
291 289 {
292 290 Q_UNUSED(topLeft)
293 291 Q_UNUSED(bottomRight)
294 292
295 293 if (m_model == 0 || m_series == 0)
296 294 return;
297 295
298 296 if (m_modelSignalsBlock)
299 297 return;
300 298
301 299 blockSeriesSignals();
302 300 QModelIndex index;
303 301 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
304 302 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
305 303 index = topLeft.sibling(row, column);
306 304 QBarSet* bar = barSet(index);
307 305 if (bar) {
308 306 if (m_orientation == Qt::Vertical)
309 307 bar->replace(row - m_first, m_model->data(index).toReal());
310 308 else
311 309 bar->replace(column - m_first, m_model->data(index).toReal());
312 310 }
313 311 }
314 312 }
315 313 blockSeriesSignals(false);
316 314 }
317 315
318 316 void QBarModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
319 317 {
320 318 Q_UNUSED(parent);
321 319 Q_UNUSED(end)
322 320 if (m_modelSignalsBlock)
323 321 return;
324 322
325 323 blockSeriesSignals();
326 324 if (m_orientation == Qt::Vertical)
327 325 // insertData(start, end);
328 326 initializeBarFromModel();
329 327 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
330 328 initializeBarFromModel();
331 329 blockSeriesSignals(false);
332 330 }
333 331
334 332 void QBarModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
335 333 {
336 334 Q_UNUSED(parent);
337 335 Q_UNUSED(end)
338 336 if (m_modelSignalsBlock)
339 337 return;
340 338
341 339 blockSeriesSignals();
342 340 if (m_orientation == Qt::Vertical)
343 341 // removeData(start, end);
344 342 initializeBarFromModel();
345 343 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
346 344 initializeBarFromModel();
347 345 blockSeriesSignals(false);
348 346 }
349 347
350 348 void QBarModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
351 349 {
352 350 Q_UNUSED(parent);
353 351 Q_UNUSED(end)
354 352 if (m_modelSignalsBlock)
355 353 return;
356 354
357 355 blockSeriesSignals();
358 356 if (m_orientation == Qt::Horizontal)
359 357 // insertData(start, end);
360 358 initializeBarFromModel();
361 359 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
362 360 initializeBarFromModel();
363 361 blockSeriesSignals(false);
364 362 }
365 363
366 364 void QBarModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
367 365 {
368 366 Q_UNUSED(parent);
369 367 Q_UNUSED(end)
370 368 if (m_modelSignalsBlock)
371 369 return;
372 370
373 371 blockSeriesSignals();
374 372 if (m_orientation == Qt::Horizontal)
375 373 // removeData(start, end);
376 374 initializeBarFromModel();
377 375 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
378 376 initializeBarFromModel();
379 377 blockSeriesSignals(false);
380 378 }
381 379
382 380 void QBarModelMapperPrivate::insertData(int start, int end)
383 381 {
384 382 Q_UNUSED(end)
385 if (m_model == 0 || m_series == 0)
386 return;
387
388 if (m_count != -1 && start >= m_first + m_count) {
389 return;
390 } /*else {
391 int addedCount = end - start + 1;
392 if (m_count != -1 && addedCount > m_count)
393 addedCount = m_count;
394 int first = qMax(start, m_first);
395 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
396 for (int k = 0; k < m_series->barSets().count(); k++) {
397 for (int i = first; i <= last; i++) {
398 QBar point;
399 point.setX(m_model->data(xModelIndex(i - m_first), Qt::DisplayRole).toDouble());
400 point.setY(m_model->data(yModelIndex(i - m_first), Qt::DisplayRole).toDouble());
401 m_series->insert(i - m_first, point);
402 }
403 >>>>>>> Stashed changes
404 }
405
406 // remove excess of slices (abouve m_count)
407 if (m_count != -1 && m_series->points().size() > m_count)
408 for (int i = m_series->points().size() - 1; i >= m_count; i--) {
409 m_series->remove(m_series->points().at(i));
410 }
411 }*/
383 Q_UNUSED(start)
384 Q_UNUSED(end)
385 // To be implemented
412 386 }
413 387
414 388 void QBarModelMapperPrivate::removeData(int start, int end)
415 389 {
416 390 Q_UNUSED(end)
417 if (m_model == 0 || m_series == 0)
418 return;
419
420 // int removedCount = end - start + 1;
421 if (m_count != -1 && start >= m_first + m_count) {
422 return;
423 } /*else {
424 int toRemove = qMin(m_series->count(), removedCount); // first find how many items can actually be removed
425 int first = qMax(start, m_first); // get the index of the first item that will be removed.
426 int last = qMin(first + toRemove - 1, m_series->count() + m_first - 1); // get the index of the last item that will be removed.
427 for (int i = last; i >= first; i--) {
428 m_series->remove(m_series->points().at(i - m_first));
429 }
430
431 if (m_count != -1) {
432 int itemsAvailable; // check how many are available to be added
433 if (m_orientation == Qt::Vertical)
434 itemsAvailable = m_model->rowCount() - m_first - m_series->count();
435 else
436 itemsAvailable = m_model->columnCount() - m_first - m_series->count();
437 int toBeAdded = qMin(itemsAvailable, m_count - m_series->count()); // add not more items than there is space left to be filled.
438 int currentSize = m_series->count();
439 if (toBeAdded > 0)
440 for (int i = m_series->count(); i < currentSize + toBeAdded; i++) {
441 QPointF point;
442 point.setX(m_model->data(xModelIndex(i), Qt::DisplayRole).toDouble());
443 point.setY(m_model->data(yModelIndex(i), Qt::DisplayRole).toDouble());
444 m_series->insert(i, point);
445 }
446 }
447 }*/
391 Q_UNUSED(start)
392 Q_UNUSED(end)
393 // To be implemented
448 394 }
449 395
450 396 void QBarModelMapperPrivate::initializeBarFromModel()
451 397 {
452 398 if (m_model == 0 || m_series == 0)
453 399 return;
454 400
455 401 blockSeriesSignals();
456 402 // clear current content
457 403 m_series->clear();
458 404
459 405 // create the initial bar sets
460 406 for (int i = m_firstBarSetSection; i <= m_lastBarSetSection; i++) {
461 407 int posInBar = 0;
462 408 QModelIndex barIndex = barModelIndex(i, posInBar);
463 409 // check if there is such model index
464 410 if (barIndex.isValid()) {
465 411 QBarSet *barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal).toString());
466 412 while (barIndex.isValid()) {
467 413 barSet->append(m_model->data(barIndex, Qt::DisplayRole).toDouble());
468 414 posInBar++;
469 415 barIndex = barModelIndex(i, posInBar);
470 416 }
471 417 m_series->append(barSet);
472 418 } else {
473 419 break;
474 420 }
475 421 }
476 422 blockSeriesSignals(false);
477 423 }
478 424
479 425 #include "moc_qbarmodelmapper.cpp"
480 426 #include "moc_qbarmodelmapper_p.cpp"
481 427
482 428 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,90 +1,85
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 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef QBARMODELMAPPER_P_H
31 31 #define QBARMODELMAPPER_P_H
32 32
33 33 #include "qchartglobal.h"
34 34 #include <QObject>
35 35 #include "qbarmodelmapper.h"
36 36
37 37 class QModelIndex;
38 38
39 39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40 40
41 41 class QBarSet;
42 42
43 43 class QBarModelMapperPrivate : public QObject
44 44 {
45 45 Q_OBJECT
46 46 public:
47 47 explicit QBarModelMapperPrivate(QBarModelMapper *q);
48 48
49 49 public Q_SLOTS:
50 50 // for the model
51 51 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
52 52 void modelRowsAdded(QModelIndex parent, int start, int end);
53 53 void modelRowsRemoved(QModelIndex parent, int start, int end);
54 54 void modelColumnsAdded(QModelIndex parent, int start, int end);
55 55 void modelColumnsRemoved(QModelIndex parent, int start, int end);
56 56
57 // // for the series
58 // void handlePointAdded(int pointPos);
59 // void handlePointRemoved(int pointPos);
60 // void handlePointReplaced(int pointPos);
61
62 57 void initializeBarFromModel();
63 58
64 59 private:
65 60 QBarSet* barSet(QModelIndex index);
66 61 QModelIndex barModelIndex(int barSection, int posInBar);
67 62 void insertData(int start, int end);
68 63 void removeData(int start, int end);
69 64 void blockModelSignals(bool block = true);
70 65 void blockSeriesSignals(bool block = true);
71 66
72 67 private:
73 68 QBarSeries *m_series;
74 69 QAbstractItemModel *m_model;
75 70 int m_first;
76 71 int m_count;
77 72 Qt::Orientation m_orientation;
78 73 int m_firstBarSetSection;
79 74 int m_lastBarSetSection;
80 75 bool m_seriesSignalsBlock;
81 76 bool m_modelSignalsBlock;
82 77
83 78 private:
84 79 QBarModelMapper *q_ptr;
85 80 Q_DECLARE_PUBLIC(QBarModelMapper)
86 81 };
87 82
88 83 QTCOMMERCIALCHART_END_NAMESPACE
89 84
90 85 #endif // QBARMODELMAPPER_P_H
@@ -1,47 +1,46
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 QHPIEMODELMAPPER_H
22 22 #define QHPIEMODELMAPPER_H
23 23
24 24 #include "qpiemodelmapper.h"
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 class QTCOMMERCIALCHART_EXPORT QHPieModelMapper : public QPieModelMapper
29 29 {
30 30 Q_OBJECT
31 31 Q_PROPERTY(int valuesRow READ valuesRow WRITE setValuesRow)
32 32 Q_PROPERTY(int labelsRow READ labelsRow WRITE setLabelsRow)
33 33
34 34 public:
35 35 explicit QHPieModelMapper(QObject *parent = 0);
36 36
37 37 int valuesRow() const;
38 38 void setValuesRow(int valuesRow);
39 39
40 40 int labelsRow() const;
41 41 void setLabelsRow(int labelsRow);
42
43 42 };
44 43
45 44 QTCOMMERCIALCHART_END_NAMESPACE
46 45
47 46 #endif // QHPIEMODELMAPPER_H
@@ -1,596 +1,595
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 "qpiemodelmapper_p.h"
22 22 #include "qpiemodelmapper.h"
23 23 #include "qpieseries.h"
24 24 #include "qpieslice.h"
25 25 #include <QAbstractItemModel>
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 /*!
30 \class QPieModelMapper
31 \brief part of QtCommercial chart API.
32 \mainclass
33
34 The instance of this class cannot be created directly. QHPieModelMapper of QVPieModelMapper should be used instead. This class is used to create a connection between QPieSeries and QAbstractItemModel derived model object.
35 It is possible to use both QAbstractItemModel and QPieSeries model API. QPieModelMapper makes sure that Pie and the model are kept in sync.
36 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
37 */
38
39 /*!
30 40 \property QPieModelMapper::series
31 41 \brief Defines the QPieSeries object that is used by the mapper.
32 42
33 43 All the data in the series is discarded when it is set to the mapper.
34 44 When new series is specified the old series is disconnected (it preserves its data)
35 45 */
36 46
37 47 /*!
38 48 \property QPieModelMapper::model
39 49 \brief Defines the model that is used by the mapper.
40 50 */
41 51
42 52 /*!
43 53 \property QPieModelMapper::first
44 54 \brief Defines which item of the model's row/column should be mapped as the value/label of the first slice of the pie
45 55
46 56 Minimal and default value is: 0
47 57 */
48 58
49 59 /*!
50 60 \property QPieModelMapper::count
51 61 \brief Defines the number of rows/columns of the model that are mapped as the data for the pie.
52 62
53 63 Minimal and default value is: -1 (count limited by the number of rows/columns in the model)
54 64 */
55 65
56 66 /*!
57 \class QPieModelMapper
58 \brief part of QtCommercial chart API.
59 \mainclass
60
61 The instance of this class cannot be created directly. QHPieModelMapper of QVPieModelMapper should be used instead. This class is used to create a connection between QPieSeries and QAbstractItemModel derived model object.
62 It is possible to use both QAbstractItemModel and QPieSeries model API. QPieModelMapper makes sure that Pie and the model are kept in sync.
63 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
64 */
65
66 /*!
67 67 Constructs a mapper object which is a child of \a parent.
68 68 */
69 69 QPieModelMapper::QPieModelMapper(QObject *parent) :
70 70 QObject(parent),
71 71 d_ptr(new QPieModelMapperPrivate(this))
72 72 {
73 73 }
74 74
75 75 QAbstractItemModel* QPieModelMapper::model() const
76 76 {
77 77 Q_D(const QPieModelMapper);
78 78 return d->m_model;
79 79 }
80 80
81 81 void QPieModelMapper::setModel(QAbstractItemModel *model)
82 82 {
83 83 if (model == 0)
84 84 return;
85 85
86 86 Q_D(QPieModelMapper);
87 87 if (d->m_model) {
88 88 disconnect(d->m_model, 0, d, 0);
89 89 }
90 90
91 91 d->m_model = model;
92 92 d->initializePieFromModel();
93 93 // connect signals from the model
94 94 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
95 95 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
96 96 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
97 97 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
98 98 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
99 99 }
100 100
101 101 QPieSeries* QPieModelMapper::series() const
102 102 {
103 103 Q_D(const QPieModelMapper);
104 104 return d->m_series;
105 105 }
106 106
107 107 void QPieModelMapper::setSeries(QPieSeries *series)
108 108 {
109 109 Q_D(QPieModelMapper);
110 110 if (d->m_series) {
111 111 disconnect(d->m_series, 0, d, 0);
112 112 }
113 113
114 114 if (series == 0)
115 115 return;
116 116
117 117 d->m_series = series;
118 118 d->initializePieFromModel();
119 119 // connect the signals from the series
120 120 connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>)));
121 121 connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>)));
122 122 }
123 123
124 124 int QPieModelMapper::first() const
125 125 {
126 126 Q_D(const QPieModelMapper);
127 127 return d->m_first;
128 128 }
129 129
130 130 void QPieModelMapper::setFirst(int first)
131 131 {
132 132 Q_D(QPieModelMapper);
133 133 d->m_first = qMax(first, 0);
134 134 d->initializePieFromModel();
135 135 }
136 136
137 137 int QPieModelMapper::count() const
138 138 {
139 139 Q_D(const QPieModelMapper);
140 140 return d->m_count;
141 141 }
142 142
143 143 void QPieModelMapper::setCount(int count)
144 144 {
145 145 Q_D(QPieModelMapper);
146 146 d->m_count = qMax(count, -1);
147 147 d->initializePieFromModel();
148 148 }
149 149
150 150 /*!
151 151 Returns the orientation that is used when QPieModelMapper accesses the model.
152 152 This mean whether the consecutive values/labels of the pie are read from row (Qt::Horizontal)
153 153 or from columns (Qt::Vertical)
154 154 */
155 155 Qt::Orientation QPieModelMapper::orientation() const
156 156 {
157 157 Q_D(const QPieModelMapper);
158 158 return d->m_orientation;
159 159 }
160 160
161 161 /*!
162 162 Returns the \a orientation that is used when QPieModelMapper accesses the model.
163 163 This mean whether the consecutive values/labels of the pie are read from row (Qt::Horizontal)
164 164 or from columns (Qt::Vertical)
165 165 */
166 166 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
167 167 {
168 168 Q_D(QPieModelMapper);
169 169 d->m_orientation = orientation;
170 170 d->initializePieFromModel();
171 171 }
172 172
173 173 /*!
174 174 Returns which section of the model is kept in sync with the values of the pie's slices
175 175 */
176 176 int QPieModelMapper::valuesSection() const
177 177 {
178 178 Q_D(const QPieModelMapper);
179 179 return d->m_valuesSection;
180 180 }
181 181
182 182 /*!
183 183 Sets the model section that is kept in sync with the pie slices values.
184 184 Parameter \a valuesSection specifies the section of the model.
185 185 */
186 186 void QPieModelMapper::setValuesSection(int valuesSection)
187 187 {
188 188 Q_D(QPieModelMapper);
189 189 d->m_valuesSection = qMax(-1, valuesSection);
190 190 d->initializePieFromModel();
191 191 }
192 192
193 193 /*!
194 194 Returns which section of the model is kept in sync with the labels of the pie's slices
195 195 */
196 196 int QPieModelMapper::labelsSection() const
197 197 {
198 198 Q_D(const QPieModelMapper);
199 199 return d->m_labelsSection;
200 200 }
201 201
202 202 /*!
203 203 Sets the model section that is kept in sync with the pie slices labels.
204 204 Parameter \a labelsSection specifies the section of the model.
205 205 */
206 206 void QPieModelMapper::setLabelsSection(int labelsSection)
207 207 {
208 208 Q_D(QPieModelMapper);
209 209 d->m_labelsSection = qMax(-1, labelsSection);
210 210 d->initializePieFromModel();
211 211 }
212 212
213 213 /*!
214 214 Resets the QPieModelMapper to the default state.
215 215 first: 0; count: -1; valuesSection: -1; labelsSection: -1;
216 216 */
217 217 void QPieModelMapper::reset()
218 218 {
219 219 Q_D(QPieModelMapper);
220 220 d->m_first = 0;
221 221 d->m_count = -1;
222 222 d->m_valuesSection = -1;
223 223 d->m_labelsSection = -1;
224 224 }
225 225
226 226 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
227 227
228 228 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
229 229 m_series(0),
230 230 m_model(0),
231 231 m_first(0),
232 232 m_count(-1),
233 233 m_orientation(Qt::Vertical),
234 234 m_valuesSection(-1),
235 235 m_labelsSection(-1),
236 236 m_seriesSignalsBlock(false),
237 237 m_modelSignalsBlock(false),
238 238 q_ptr(q)
239 239 {
240 240 }
241 241
242 242 void QPieModelMapperPrivate::blockModelSignals(bool block)
243 243 {
244 244 m_modelSignalsBlock = block;
245 245 }
246 246
247 247 void QPieModelMapperPrivate::blockSeriesSignals(bool block)
248 248 {
249 249 m_seriesSignalsBlock = block;
250 250 }
251 251
252 252
253 253 QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const
254 254 {
255 255 if (!index.isValid())
256 256 return 0; // index is invalid
257 257
258 258 if (m_orientation == Qt::Vertical && (index.column() == m_valuesSection || index.column() == m_labelsSection)) {
259 259 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) {
260 260 if (m_model->index(index.row(), m_valuesSection).isValid() && m_model->index(index.row(), m_labelsSection).isValid())
261 261 return m_series->slices().at(index.row() - m_first);
262 262 else
263 263 return 0;
264 264 }
265 265 } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesSection || index.row() == m_labelsSection)) {
266 266 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count)) {
267 267 if (m_model->index(m_valuesSection, index.column()).isValid() && m_model->index(m_labelsSection, index.column()).isValid())
268 268 return m_series->slices().at(index.column() - m_first);
269 269 else
270 270 return 0;
271 271 }
272 272 }
273 273 return 0; // This part of model has not been mapped to any slice
274 274 }
275 275
276 276 QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos)
277 277 {
278 278 if (m_count != -1 && slicePos >= m_count)
279 279 return QModelIndex(); // invalid
280 280
281 281 if (m_orientation == Qt::Vertical)
282 282 return m_model->index(slicePos + m_first, m_valuesSection);
283 283 else
284 284 return m_model->index(m_valuesSection, slicePos + m_first);
285 285 }
286 286
287 287 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
288 288 {
289 289 if (m_count != -1 && slicePos >= m_count)
290 290 return QModelIndex(); // invalid
291 291
292 292 if (m_orientation == Qt::Vertical)
293 293 return m_model->index(slicePos + m_first, m_labelsSection);
294 294 else
295 295 return m_model->index(m_labelsSection, slicePos + m_first);
296 296 }
297 297
298 298 bool QPieModelMapperPrivate::isLabelIndex(QModelIndex index) const
299 299 {
300 300 if (m_orientation == Qt::Vertical && index.column() == m_labelsSection)
301 301 return true;
302 302 else if (m_orientation == Qt::Horizontal && index.row() == m_labelsSection)
303 303 return true;
304 304
305 305 return false;
306 306 }
307 307
308 308 bool QPieModelMapperPrivate::isValueIndex(QModelIndex index) const
309 309 {
310 310 if (m_orientation == Qt::Vertical && index.column() == m_valuesSection)
311 311 return true;
312 312 else if (m_orientation == Qt::Horizontal && index.row() == m_valuesSection)
313 313 return true;
314 314
315 315 return false;
316 316 }
317 317
318 318 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
319 319 {
320 320 if (m_seriesSignalsBlock)
321 321 return;
322 322
323 323 if (slices.count() == 0)
324 324 return;
325 325
326 326 int firstIndex = m_series->slices().indexOf(slices.at(0));
327 327 if (firstIndex == -1)
328 328 return;
329 329
330 330 if (m_count != -1)
331 331 m_count += slices.count();
332 332
333 333 for (int i = firstIndex; i < firstIndex + slices.count(); i++) {
334 334 m_slices.insert(i, slices.at(i - firstIndex));
335 335 connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
336 336 connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
337 337 }
338 338
339 339 blockModelSignals();
340 340 if (m_orientation == Qt::Vertical)
341 341 m_model->insertRows(firstIndex + m_first, slices.count());
342 342 else
343 343 m_model->insertColumns(firstIndex + m_first, slices.count());
344 344
345 345 for(int i = firstIndex; i < firstIndex + slices.count(); i++) {
346 346 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
347 347 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
348 348 }
349 349 blockModelSignals(false);
350 350 }
351 351
352 352 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
353 353 {
354 354 if (m_seriesSignalsBlock)
355 355 return;
356 356
357 357 if (slices.count() == 0)
358 358 return;
359 359
360 360 int firstIndex = m_slices.indexOf(slices.at(0));
361 361 if (firstIndex == -1)
362 362 return;
363 363
364 364 if (m_count != -1)
365 365 m_count -= slices.count();
366 366
367 367 for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--)
368 368 m_slices.removeAt(i);
369 369
370 370 blockModelSignals();
371 371 if (m_orientation == Qt::Vertical)
372 372 m_model->removeRows(firstIndex + m_first, slices.count());
373 373 else
374 374 m_model->removeColumns(firstIndex + m_first, slices.count());
375 375 blockModelSignals(false);
376 376 }
377 377
378 378 void QPieModelMapperPrivate::sliceLabelChanged()
379 379 {
380 380 if (m_seriesSignalsBlock)
381 381 return;
382 382
383 383 blockModelSignals();
384 384 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
385 385 m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label());
386 386 blockModelSignals(false);
387 387 }
388 388
389 389 void QPieModelMapperPrivate::sliceValueChanged()
390 390 {
391 391 if (m_seriesSignalsBlock)
392 392 return;
393 393
394 394 blockModelSignals();
395 395 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
396 396 m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value());
397 397 blockModelSignals(false);
398 398 }
399 399
400 400 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
401 401 {
402 402 if (m_model == 0 || m_series == 0)
403 403 return;
404 404
405 405 if (m_modelSignalsBlock)
406 406 return;
407 407
408 408 blockSeriesSignals();
409 409 QModelIndex index;
410 410 QPieSlice *slice;
411 411 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
412 412 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
413 413 index = topLeft.sibling(row, column);
414 414 slice = pieSlice(index);
415 415 if (slice) {
416 416 if (isValueIndex(index))
417 417 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
418 418 if (isLabelIndex(index))
419 419 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
420 420 }
421 421 }
422 422 }
423 423 blockSeriesSignals(false);
424 424 }
425 425
426 426
427 427 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
428 428 {
429 429 Q_UNUSED(parent);
430 430 if (m_modelSignalsBlock)
431 431 return;
432 432
433 433 blockSeriesSignals();
434 434 if (m_orientation == Qt::Vertical)
435 435 insertData(start, end);
436 436 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
437 437 initializePieFromModel();
438 438 blockSeriesSignals(false);
439 439 }
440 440
441 441 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
442 442 {
443 443 Q_UNUSED(parent);
444 444 if (m_modelSignalsBlock)
445 445 return;
446 446
447 447 blockSeriesSignals();
448 448 if (m_orientation == Qt::Vertical)
449 449 removeData(start, end);
450 450 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
451 451 initializePieFromModel();
452 452 blockSeriesSignals(false);
453 453 }
454 454
455 455 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
456 456 {
457 457 Q_UNUSED(parent);
458 458 if (m_modelSignalsBlock)
459 459 return;
460 460
461 461 blockSeriesSignals();
462 462 if (m_orientation == Qt::Horizontal)
463 463 insertData(start, end);
464 464 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
465 465 initializePieFromModel();
466 466 blockSeriesSignals(false);
467 467 }
468 468
469 469 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
470 470 {
471 471 Q_UNUSED(parent);
472 472 if (m_modelSignalsBlock)
473 473 return;
474 474
475 475 blockSeriesSignals();
476 476 if (m_orientation == Qt::Horizontal)
477 477 removeData(start, end);
478 478 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
479 479 initializePieFromModel();
480 480 blockSeriesSignals(false);
481 481 }
482 482
483 483 void QPieModelMapperPrivate::insertData(int start, int end)
484 484 {
485 485 if (m_model == 0 || m_series == 0)
486 486 return;
487 487
488 488 if (m_count != -1 && start >= m_first + m_count) {
489 489 return;
490 490 } else {
491 491 int addedCount = end - start + 1;
492 492 if (m_count != -1 && addedCount > m_count)
493 493 addedCount = m_count;
494 494 int first = qMax(start, m_first);
495 495 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
496 496 for (int i = first; i <= last; i++) {
497 497 QModelIndex valueIndex = valueModelIndex(i - m_first);
498 498 QModelIndex labelIndex = labelModelIndex(i - m_first);
499 499 if (valueIndex.isValid() && labelIndex.isValid()) {
500 500 QPieSlice *slice = new QPieSlice;
501 501 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
502 502 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
503 503 slice->setLabelVisible();
504 504 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
505 505 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
506 506 m_series->insert(i - m_first, slice);
507 507 m_slices.insert(i - m_first, slice);
508 508 }
509 509 }
510 510
511 511 // remove excess of slices (abouve m_count)
512 512 if (m_count != -1 && m_series->slices().size() > m_count)
513 513 for (int i = m_series->slices().size() - 1; i >= m_count; i--) {
514 514 m_series->remove(m_series->slices().at(i));
515 515 m_slices.removeAt(i);
516 516 }
517 517 }
518 518 }
519 519
520 520 void QPieModelMapperPrivate::removeData(int start, int end)
521 521 {
522 522 if (m_model == 0 || m_series == 0)
523 523 return;
524 524
525 525 int removedCount = end - start + 1;
526 526 if (m_count != -1 && start >= m_first + m_count) {
527 527 return;
528 528 } else {
529 529 int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed
530 530 int first = qMax(start, m_first); // get the index of the first item that will be removed.
531 531 int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed.
532 532 for (int i = last; i >= first; i--) {
533 533 m_series->remove(m_series->slices().at(i - m_first));
534 534 m_slices.removeAt(i - m_first);
535 535 }
536 536
537 537 if (m_count != -1) {
538 538 int itemsAvailable; // check how many are available to be added
539 539 if (m_orientation == Qt::Vertical)
540 540 itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size();
541 541 else
542 542 itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size();
543 543 int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled.
544 544 int currentSize = m_series->slices().size();
545 545 if (toBeAdded > 0)
546 546 for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) {
547 547 QModelIndex valueIndex = valueModelIndex(i - m_first);
548 548 QModelIndex labelIndex = labelModelIndex(i - m_first);
549 549 if (valueIndex.isValid() && labelIndex.isValid()) {
550 550 QPieSlice *slice = new QPieSlice;
551 551 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
552 552 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
553 553 slice->setLabelVisible();
554 554 m_series->insert(i, slice);
555 555 m_slices.insert(i, slice);
556 556 }
557 557 }
558 558 }
559 559 }
560 560 }
561 561
562 562 void QPieModelMapperPrivate::initializePieFromModel()
563 563 {
564 564 if (m_model == 0 || m_series == 0)
565 565 return;
566 566
567 567 blockSeriesSignals();
568 568 // clear current content
569 569 m_series->clear();
570 570 m_slices.clear();
571 571
572 572 // create the initial slices set
573 573 int slicePos = 0;
574 574 QModelIndex valueIndex = valueModelIndex(slicePos);
575 575 QModelIndex labelIndex = labelModelIndex(slicePos);
576 576 while (valueIndex.isValid() && labelIndex.isValid()) {
577 577 QPieSlice *slice = new QPieSlice;
578 578 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
579 579 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
580 580 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
581 581 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
582 582 m_series->append(slice);
583 583 m_slices.append(slice);
584 // m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble());
585 584 slicePos++;
586 585 valueIndex = valueModelIndex(slicePos);
587 586 labelIndex = labelModelIndex(slicePos);
588 587 }
589 588 m_series->setLabelsVisible(true);
590 589 blockSeriesSignals(false);
591 590 }
592 591
593 592 #include "moc_qpiemodelmapper_p.cpp"
594 593 #include "moc_qpiemodelmapper.cpp"
595 594
596 595 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,47 +1,46
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 QHXYMODELMAPPER_H
22 22 #define QHXYMODELMAPPER_H
23 23
24 24 #include <QXYModelMapper>
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 class QTCOMMERCIALCHART_EXPORT QHXYModelMapper : public QXYModelMapper
29 29 {
30 30 Q_OBJECT
31 31 Q_PROPERTY(int xRow READ xRow WRITE setXRow)
32 32 Q_PROPERTY(int yRow READ yRow WRITE setYRow)
33 33
34 34 public:
35 35 explicit QHXYModelMapper(QObject *parent = 0);
36 36
37 37 int xRow() const;
38 38 void setXRow(int xRow);
39 39
40 40 int yRow() const;
41 void setYRow(int yRow);
42
41 void setYRow(int yRow);
43 42 };
44 43
45 44 QTCOMMERCIALCHART_END_NAMESPACE
46 45
47 46 #endif // QHXYMODELMAPPER_H
@@ -1,78 +1,80
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 "qvxymodelmapper.h"
22 22
23 23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 24
25 25 /*!
26 26 \class QVXYModelMapper
27 27 \brief part of QtCommercial chart API.
28 28 \mainclass
29 29
30 Nothing here yet
30 Vertical model mapper is used to create a connection between QXYSeries and QAbstractItemModel derived model object.
31 It is possible to use both QAbstractItemModel and QXYSeries model API. QXYModelMapper makes sure that QXYSeries and the model are kept in sync.
32 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
31 33 */
32 34
33 35 /*!
34 36 \property QVXYModelMapper::xColumn
35 37 \brief Defines which column of the model is kept in sync with the x values of QXYSeries
36 38
37 39 Default value is: -1 (invalid mapping)
38 40 */
39 41
40 42 /*!
41 43 \property QVXYModelMapper::yColumn
42 44 \brief Defines which column of the model is kept in sync with the y values of QXYSeries
43 45
44 46 Default value is: -1 (invalid mapping)
45 47 */
46 48
47 49 /*!
48 50 Constructs a mapper object which is a child of \a parent.
49 51 */
50 52 QVXYModelMapper::QVXYModelMapper(QObject *parent) :
51 53 QXYModelMapper(parent)
52 54 {
53 55 QXYModelMapper::setOrientation(Qt::Vertical);
54 56 }
55 57
56 58 int QVXYModelMapper::xColumn() const
57 59 {
58 60 return QXYModelMapper::xSection();
59 61 }
60 62
61 63 void QVXYModelMapper::setXColumn(int xColumn)
62 64 {
63 65 return QXYModelMapper::setXSection(xColumn);
64 66 }
65 67
66 68 int QVXYModelMapper::yColumn() const
67 69 {
68 70 return QXYModelMapper::ySection();
69 71 }
70 72
71 73 void QVXYModelMapper::setYColumn(int yColumn)
72 74 {
73 75 return QXYModelMapper::setYSection(yColumn);
74 76 }
75 77
76 78 #include "moc_qvxymodelmapper.cpp"
77 79
78 80 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,47 +1,46
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 QVXYMODELMAPPER_H
22 22 #define QVXYMODELMAPPER_H
23 23
24 24 #include <QXYModelMapper>
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 class QTCOMMERCIALCHART_EXPORT QVXYModelMapper : public QXYModelMapper
29 29 {
30 30 Q_OBJECT
31 31 Q_PROPERTY(int xColumn READ xColumn WRITE setXColumn)
32 32 Q_PROPERTY(int yColumn READ yColumn WRITE setYColumn)
33 33
34 34 public:
35 35 explicit QVXYModelMapper(QObject *parent = 0);
36 36
37 37 int xColumn() const;
38 38 void setXColumn(int xColumn);
39 39
40 40 int yColumn() const;
41 void setYColumn(int yColumn);
42
41 void setYColumn(int yColumn);
43 42 };
44 43
45 44 QTCOMMERCIALCHART_END_NAMESPACE
46 45
47 46 #endif // QVXYMODELMAPPER_H
General Comments 0
You need to be logged in to leave comments. Login now