##// END OF EJS Templates
Added license text to piemodelmapper classes
Marek Rosa -
r1309:62cab2834ea4
parent child
Show More
@@ -1,33 +1,53
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 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
1 21 #include "qhpiemodelmapper.h"
2 22
3 23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 24
5 25 QHPieModelMapper::QHPieModelMapper(QObject *parent) :
6 26 QPieModelMapper(parent)
7 27 {
8 28 QPieModelMapper::setOrientation(Qt::Horizontal);
9 29 }
10 30
11 31 int QHPieModelMapper::valuesRow() const
12 32 {
13 33 return QPieModelMapper::valuesIndex();
14 34 }
15 35
16 36 void QHPieModelMapper::setValuesRow(int valuesRow)
17 37 {
18 38 QPieModelMapper::setValuesIndex(valuesRow);
19 39 }
20 40
21 41 int QHPieModelMapper::labelsRow() const
22 42 {
23 43 return QPieModelMapper::labelsIndex();
24 44 }
25 45
26 46 void QHPieModelMapper::setLabelsRow(int labelsRow)
27 47 {
28 48 QPieModelMapper::setLabelsIndex(labelsRow);
29 49 }
30 50
31 51 #include "moc_qhpiemodelmapper.cpp"
32 52
33 53 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,27 +1,47
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 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
1 21 #ifndef QHPIEMODELMAPPER_H
2 22 #define QHPIEMODELMAPPER_H
3 23
4 24 #include "qpiemodelmapper.h"
5 25
6 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 27
8 28 class QTCOMMERCIALCHART_EXPORT QHPieModelMapper : public QPieModelMapper
9 29 {
10 30 Q_OBJECT
11 31 Q_PROPERTY(int valuesRow READ valuesRow WRITE setValuesRow)
12 32 Q_PROPERTY(int labelsRow READ labelsRow WRITE setLabelsRow)
13 33
14 34 public:
15 35 QHPieModelMapper(QObject *parent = 0);
16 36
17 37 int valuesRow() const;
18 38 void setValuesRow(int valuesRow);
19 39
20 40 int labelsRow() const;
21 41 void setLabelsRow(int labelsRow);
22 42
23 43 };
24 44
25 45 QTCOMMERCIALCHART_END_NAMESPACE
26 46
27 47 #endif // QHPIEMODELMAPPER_H
@@ -1,501 +1,521
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 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
1 21 #include "qpiemodelmapper_p.h"
2 22 #include "qpiemodelmapper.h"
3 23 #include "qpieseries.h"
4 24 #include "qpieslice.h"
5 25 #include <QAbstractItemModel>
6 26
7 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 28
9 29 QPieModelMapper::QPieModelMapper(QObject *parent) :
10 30 QObject(parent),
11 31 d_ptr(new QPieModelMapperPrivate(this))
12 32 {
13 33 }
14 34
15 35 QPieModelMapper::~QPieModelMapper()
16 36 {
17 37 // Q_D(QPieModelMapper);
18 38 // disconnect(d->m_model, 0, d, 0);
19 39 }
20 40
21 41 QAbstractItemModel* QPieModelMapper::model() const
22 42 {
23 43 Q_D(const QPieModelMapper);
24 44 return d->m_model;
25 45 }
26 46
27 47 void QPieModelMapper::setModel(QAbstractItemModel *model)
28 48 {
29 49 if (model == 0)
30 50 return;
31 51
32 52 Q_D(QPieModelMapper);
33 53 if (d->m_model) {
34 54 disconnect(d->m_model, 0, d, 0);
35 55 }
36 56
37 57 d->m_model = model;
38 58 d->initializePieFromModel();
39 59 // connect signals from the model
40 60 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
41 61 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
42 62 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
43 63 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
44 64 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
45 65 }
46 66
47 67 QPieSeries* QPieModelMapper::series() const
48 68 {
49 69 Q_D(const QPieModelMapper);
50 70 return d->m_series;
51 71 }
52 72
53 73 void QPieModelMapper::setSeries(QPieSeries *series)
54 74 {
55 75 Q_D(QPieModelMapper);
56 76 if (d->m_series) {
57 77 disconnect(d->m_series, 0, d, 0);
58 78 }
59 79
60 80 if (series == 0)
61 81 return;
62 82
63 83 d->m_series = series;
64 84 d->initializePieFromModel();
65 85 // connect the signals from the series
66 86 connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>)));
67 87 connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>)));
68 88 }
69 89
70 90 int QPieModelMapper::first() const
71 91 {
72 92 Q_D(const QPieModelMapper);
73 93 return d->m_first;
74 94 }
75 95
76 96 void QPieModelMapper::setFirst(int first)
77 97 {
78 98 Q_D(QPieModelMapper);
79 99 d->m_first = qMax(first, 0);
80 100 d->initializePieFromModel();
81 101 }
82 102
83 103 int QPieModelMapper::count() const
84 104 {
85 105 Q_D(const QPieModelMapper);
86 106 return d->m_count;
87 107 }
88 108
89 109 void QPieModelMapper::setCount(int count)
90 110 {
91 111 Q_D(QPieModelMapper);
92 112 d->m_count = qMax(count, -1);
93 113 d->initializePieFromModel();
94 114 }
95 115
96 116 Qt::Orientation QPieModelMapper::orientation() const
97 117 {
98 118 Q_D(const QPieModelMapper);
99 119 return d->m_orientation;
100 120 }
101 121
102 122 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
103 123 {
104 124 Q_D(QPieModelMapper);
105 125 d->m_orientation = orientation;
106 126 d->initializePieFromModel();
107 127 }
108 128
109 129 int QPieModelMapper::valuesIndex() const
110 130 {
111 131 Q_D(const QPieModelMapper);
112 132 return d->m_valuesIndex;
113 133 }
114 134
115 135 void QPieModelMapper::setValuesIndex(int valuesIndex)
116 136 {
117 137 Q_D(QPieModelMapper);
118 138 d->m_valuesIndex = qMax(-1, valuesIndex);
119 139 d->initializePieFromModel();
120 140 }
121 141
122 142 int QPieModelMapper::labelsIndex() const
123 143 {
124 144 Q_D(const QPieModelMapper);
125 145 return d->m_labelsIndex;
126 146 }
127 147
128 148 void QPieModelMapper::setLabelsIndex(int labelsIndex)
129 149 {
130 150 Q_D(QPieModelMapper);
131 151 d->m_labelsIndex = qMax(-1, labelsIndex);
132 152 d->initializePieFromModel();
133 153 }
134 154
135 155 void QPieModelMapper::reset()
136 156 {
137 157 Q_D(QPieModelMapper);
138 158 d->m_first = 0;
139 159 d->m_count = -1;
140 160 d->m_orientation = Qt::Vertical;
141 161 d->m_valuesIndex = -1;
142 162 d->m_labelsIndex = -1;
143 163 }
144 164
145 165 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
146 166
147 167 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
148 168 m_series(0),
149 169 m_model(0),
150 170 m_first(0),
151 171 m_count(-1),
152 172 m_orientation(Qt::Vertical),
153 173 m_valuesIndex(-1),
154 174 m_labelsIndex(-1),
155 175 m_seriesSignalsBlock(false),
156 176 m_modelSignalsBlock(false),
157 177 q_ptr(q)
158 178 {
159 179 }
160 180
161 181 void QPieModelMapperPrivate::blockModelSignals(bool block)
162 182 {
163 183 m_modelSignalsBlock = block;
164 184 }
165 185
166 186 void QPieModelMapperPrivate::blockSeriesSignals(bool block)
167 187 {
168 188 m_seriesSignalsBlock = block;
169 189 }
170 190
171 191
172 192 QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const
173 193 {
174 194 if (m_orientation == Qt::Vertical && (index.column() == m_valuesIndex || index.column() == m_labelsIndex)) {
175 195 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count))
176 196 return m_series->slices().at(index.row() - m_first);
177 197 } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesIndex || index.row() == m_labelsIndex)) {
178 198 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
179 199 return m_series->slices().at(index.column() - m_first);
180 200 }
181 201 return 0; // This part of model has not been mapped to any slice
182 202 }
183 203
184 204 QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos)
185 205 {
186 206 if (m_count != -1 && slicePos >= m_count)
187 207 return QModelIndex(); // invalid
188 208
189 209 if (m_orientation == Qt::Vertical)
190 210 return m_model->index(slicePos + m_first, m_valuesIndex);
191 211 else
192 212 return m_model->index(m_valuesIndex, slicePos + m_first);
193 213 }
194 214
195 215 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
196 216 {
197 217 if (m_count != -1 && slicePos >= m_count)
198 218 return QModelIndex(); // invalid
199 219
200 220 if (m_orientation == Qt::Vertical)
201 221 return m_model->index(slicePos + m_first, m_labelsIndex);
202 222 else
203 223 return m_model->index(m_labelsIndex, slicePos + m_first);
204 224 }
205 225
206 226 bool QPieModelMapperPrivate::isLabelIndex(QModelIndex index) const
207 227 {
208 228 if (m_orientation == Qt::Vertical && index.column() == m_labelsIndex)
209 229 return true;
210 230 else if (m_orientation == Qt::Horizontal && index.row() == m_labelsIndex)
211 231 return true;
212 232
213 233 return false;
214 234 }
215 235
216 236 bool QPieModelMapperPrivate::isValueIndex(QModelIndex index) const
217 237 {
218 238 if (m_orientation == Qt::Vertical && index.column() == m_valuesIndex)
219 239 return true;
220 240 else if (m_orientation == Qt::Horizontal && index.row() == m_valuesIndex)
221 241 return true;
222 242
223 243 return false;
224 244 }
225 245
226 246 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
227 247 {
228 248 if (m_seriesSignalsBlock)
229 249 return;
230 250
231 251 if (slices.count() == 0)
232 252 return;
233 253
234 254 int firstIndex = m_series->slices().indexOf(slices.at(0));
235 255 if (firstIndex == -1)
236 256 return;
237 257
238 258 if (m_count != -1)
239 259 m_count += slices.count();
240 260
241 261 for (int i = firstIndex; i < firstIndex + slices.count(); i++) {
242 262 m_slices.insert(i, slices.at(i - firstIndex));
243 263 connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
244 264 connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
245 265 }
246 266
247 267 blockModelSignals();
248 268 if (m_orientation == Qt::Vertical)
249 269 m_model->insertRows(firstIndex + m_first, slices.count());
250 270 else
251 271 m_model->insertColumns(firstIndex + m_first, slices.count());
252 272
253 273 for(int i = firstIndex; i < firstIndex + slices.count(); i++) {
254 274 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
255 275 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
256 276 }
257 277 blockModelSignals(false);
258 278 }
259 279
260 280 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
261 281 {
262 282 if (m_seriesSignalsBlock)
263 283 return;
264 284
265 285 if (slices.count() == 0)
266 286 return;
267 287
268 288 int firstIndex = m_slices.indexOf(slices.at(0));
269 289 if (firstIndex == -1)
270 290 return;
271 291
272 292 if (m_count != -1)
273 293 m_count -= slices.count();
274 294
275 295 for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--)
276 296 m_slices.removeAt(i);
277 297
278 298 blockModelSignals();
279 299 if (m_orientation == Qt::Vertical)
280 300 m_model->removeRows(firstIndex + m_first, slices.count());
281 301 else
282 302 m_model->removeColumns(firstIndex + m_first, slices.count());
283 303 blockModelSignals(false);
284 304 }
285 305
286 306 void QPieModelMapperPrivate::sliceLabelChanged()
287 307 {
288 308 if (m_seriesSignalsBlock)
289 309 return;
290 310
291 311 blockModelSignals();
292 312 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
293 313 m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label());
294 314 blockModelSignals(false);
295 315 }
296 316
297 317 void QPieModelMapperPrivate::sliceValueChanged()
298 318 {
299 319 if (m_seriesSignalsBlock)
300 320 return;
301 321
302 322 blockModelSignals();
303 323 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
304 324 m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value());
305 325 blockModelSignals(false);
306 326 }
307 327
308 328 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
309 329 {
310 330 if (m_model == 0 || m_series == 0)
311 331 return;
312 332
313 333 if (m_modelSignalsBlock)
314 334 return;
315 335
316 336 blockSeriesSignals();
317 337 QModelIndex index;
318 338 QPieSlice *slice;
319 339 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
320 340 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
321 341 index = topLeft.sibling(row, column);
322 342 slice = pieSlice(index);
323 343 if (slice) {
324 344 if (isValueIndex(index))
325 345 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
326 346 if (isLabelIndex(index))
327 347 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
328 348 }
329 349 }
330 350 }
331 351 blockSeriesSignals(false);
332 352 }
333 353
334 354
335 355 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
336 356 {
337 357 Q_UNUSED(parent);
338 358 if (m_modelSignalsBlock)
339 359 return;
340 360
341 361 blockSeriesSignals();
342 362 if (m_orientation == Qt::Vertical)
343 363 insertData(start, end);
344 364 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
345 365 initializePieFromModel();
346 366 blockSeriesSignals(false);
347 367 }
348 368
349 369 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
350 370 {
351 371 Q_UNUSED(parent);
352 372 if (m_modelSignalsBlock)
353 373 return;
354 374
355 375 blockSeriesSignals();
356 376 if (m_orientation == Qt::Vertical)
357 377 removeData(start, end);
358 378 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
359 379 initializePieFromModel();
360 380 blockSeriesSignals(false);
361 381 }
362 382
363 383 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
364 384 {
365 385 Q_UNUSED(parent);
366 386 if (m_modelSignalsBlock)
367 387 return;
368 388
369 389 blockSeriesSignals();
370 390 if (m_orientation == Qt::Horizontal)
371 391 insertData(start, end);
372 392 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
373 393 initializePieFromModel();
374 394 blockSeriesSignals(false);
375 395 }
376 396
377 397 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
378 398 {
379 399 Q_UNUSED(parent);
380 400 if (m_modelSignalsBlock)
381 401 return;
382 402
383 403 blockSeriesSignals();
384 404 if (m_orientation == Qt::Horizontal)
385 405 removeData(start, end);
386 406 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
387 407 initializePieFromModel();
388 408 blockSeriesSignals(false);
389 409 }
390 410
391 411 void QPieModelMapperPrivate::insertData(int start, int end)
392 412 {
393 413 if (m_model == 0 || m_series == 0)
394 414 return;
395 415
396 416 if (m_count != -1 && start >= m_first + m_count) {
397 417 return;
398 418 } else {
399 419 int addedCount = end - start + 1;
400 420 if (m_count != -1 && addedCount > m_count)
401 421 addedCount = m_count;
402 422 int first = qMax(start, m_first);
403 423 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
404 424 for (int i = first; i <= last; i++) {
405 425 QPieSlice *slice = new QPieSlice;
406 426 slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble());
407 427 slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString());
408 428 slice->setLabelVisible();
409 429 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
410 430 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
411 431 m_series->insert(i - m_first, slice);
412 432 m_slices.insert(i - m_first, slice);
413 433 }
414 434
415 435 // remove excess of slices (abouve m_count)
416 436 if (m_count != -1 && m_series->slices().size() > m_count)
417 437 for (int i = m_series->slices().size() - 1; i >= m_count; i--) {
418 438 m_series->remove(m_series->slices().at(i));
419 439 m_slices.removeAt(i);
420 440 }
421 441 }
422 442 }
423 443
424 444 void QPieModelMapperPrivate::removeData(int start, int end)
425 445 {
426 446 if (m_model == 0 || m_series == 0)
427 447 return;
428 448
429 449 int removedCount = end - start + 1;
430 450 if (m_count != -1 && start >= m_first + m_count) {
431 451 return;
432 452 } else {
433 453 int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed
434 454 int first = qMax(start, m_first); // get the index of the first item that will be removed.
435 455 int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed.
436 456 for (int i = last; i >= first; i--) {
437 457 m_series->remove(m_series->slices().at(i - m_first));
438 458 m_slices.removeAt(i - m_first);
439 459 }
440 460
441 461 if (m_count != -1) {
442 462 int itemsAvailable; // check how many are available to be added
443 463 if (m_orientation == Qt::Vertical)
444 464 itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size();
445 465 else
446 466 itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size();
447 467 int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled.
448 468 int currentSize = m_series->slices().size();
449 469 if (toBeAdded > 0)
450 470 for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) {
451 471 QPieSlice *slice = new QPieSlice;
452 472 if (m_orientation == Qt::Vertical) {
453 473 slice->setValue(m_model->data(m_model->index(i + m_first, m_valuesIndex), Qt::DisplayRole).toDouble());
454 474 slice->setLabel(m_model->data(m_model->index(i + m_first, m_labelsIndex), Qt::DisplayRole).toString());
455 475 } else {
456 476 slice->setValue(m_model->data(m_model->index(m_valuesIndex, i + m_first), Qt::DisplayRole).toDouble());
457 477 slice->setLabel(m_model->data(m_model->index(m_labelsIndex, i + m_first), Qt::DisplayRole).toString());
458 478 }
459 479 slice->setLabelVisible();
460 480 m_series->insert(i, slice);
461 481 m_slices.insert(i, slice);
462 482 }
463 483 }
464 484 }
465 485 }
466 486
467 487 void QPieModelMapperPrivate::initializePieFromModel()
468 488 {
469 489 if (m_model == 0 || m_series == 0)
470 490 return;
471 491
472 492 blockSeriesSignals();
473 493 // clear current content
474 494 m_series->clear();
475 495 m_slices.clear();
476 496
477 497 // create the initial slices set
478 498 int slicePos = 0;
479 499 QModelIndex valueIndex = valueModelIndex(slicePos);
480 500 QModelIndex labelIndex = labelModelIndex(slicePos);
481 501 while (valueIndex.isValid() && labelIndex.isValid()) {
482 502 QPieSlice *slice = new QPieSlice;
483 503 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
484 504 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
485 505 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
486 506 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
487 507 m_series->append(slice);
488 508 m_slices.append(slice);
489 509 // m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble());
490 510 slicePos++;
491 511 valueIndex = valueModelIndex(slicePos);
492 512 labelIndex = labelModelIndex(slicePos);
493 513 }
494 514 m_series->setLabelsVisible(true);
495 515 blockSeriesSignals(false);
496 516 }
497 517
498 518 #include "moc_qpiemodelmapper_p.cpp"
499 519 #include "moc_qpiemodelmapper.cpp"
500 520
501 521 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,72 +1,101
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 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
1 30 #ifndef QPIEMODELMAPPER_P_H
2 31 #define QPIEMODELMAPPER_P_H
3 32
4 33 #include "qpiemodelmapper.h"
5 34 #include <QObject>
6 35
7 36 class QModelIndex;
8 37 class QAbstractItemModel;
9 38
10 39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 40
12 41 class QPieModelMapper;
13 42 class QPieSeries;
14 43 class QPieSlice;
15 44
16 45 class QPieModelMapperPrivate : public QObject
17 46 {
18 47 Q_OBJECT
19 48
20 49 public:
21 50 QPieModelMapperPrivate(QPieModelMapper *q);
22 51
23 52 public Q_SLOTS:
24 53 // for the model
25 54 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
26 55 void modelRowsAdded(QModelIndex parent, int start, int end);
27 56 void modelRowsRemoved(QModelIndex parent, int start, int end);
28 57 void modelColumnsAdded(QModelIndex parent, int start, int end);
29 58 void modelColumnsRemoved(QModelIndex parent, int start, int end);
30 59
31 60 // for the series
32 61 void slicesAdded(QList<QPieSlice*> slices);
33 62 void slicesRemoved(QList<QPieSlice*> slices);
34 63 void sliceLabelChanged();
35 64 void sliceValueChanged();
36 65
37 66 void initializePieFromModel();
38 67
39 68 private:
40 69 QPieSlice* pieSlice(QModelIndex index) const;
41 70 bool isLabelIndex(QModelIndex index) const;
42 71 bool isValueIndex(QModelIndex index) const;
43 72 QModelIndex valueModelIndex(int slicePos);
44 73 QModelIndex labelModelIndex(int slicePos);
45 74 void insertData(int start, int end);
46 75 void removeData(int start, int end);
47 76
48 77 void blockModelSignals(bool block = true);
49 78 void blockSeriesSignals(bool block = true);
50 79
51 80 private:
52 81 QPieSeries *m_series;
53 82 QList<QPieSlice*> m_slices;
54 83 QAbstractItemModel *m_model;
55 84 int m_first;
56 85 int m_count;
57 86 Qt::Orientation m_orientation;
58 87 int m_valuesIndex;
59 88 int m_labelsIndex;
60 89 bool m_seriesSignalsBlock;
61 90 bool m_modelSignalsBlock;
62 91
63 92 private:
64 93
65 94 QPieModelMapper *q_ptr;
66 95 Q_DECLARE_PUBLIC(QPieModelMapper)
67 96 friend class QPieSeriesPrivate;
68 97 };
69 98
70 99 QTCOMMERCIALCHART_END_NAMESPACE
71 100
72 101 #endif // QPIEMODELMAPPER_P_H
@@ -1,34 +1,54
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 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
1 21 #include "qpiemodelmapper_p.h"
2 22 #include "qvpiemodelmapper.h"
3 23
4 24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 25
6 26 QVPieModelMapper::QVPieModelMapper(QObject *parent) :
7 27 QPieModelMapper(parent)
8 28 {
9 29 QPieModelMapper::setOrientation(Qt::Vertical);
10 30 }
11 31
12 32 int QVPieModelMapper::valuesColumn() const
13 33 {
14 34 return QPieModelMapper::valuesIndex();
15 35 }
16 36
17 37 void QVPieModelMapper::setValuesColumn(int valuesColumn)
18 38 {
19 39 QPieModelMapper::setValuesIndex(valuesColumn);
20 40 }
21 41
22 42 int QVPieModelMapper::labelsColumn() const
23 43 {
24 44 return QPieModelMapper::labelsIndex();
25 45 }
26 46
27 47 void QVPieModelMapper::setLabelsColumn(int labelsColumn)
28 48 {
29 49 QPieModelMapper::setLabelsIndex(labelsColumn);
30 50 }
31 51
32 52 #include "moc_qvpiemodelmapper.cpp"
33 53
34 54 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,26 +1,46
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 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
1 21 #ifndef QVPIEMODELMAPPER_H
2 22 #define QVPIEMODELMAPPER_H
3 23
4 24 #include "qpiemodelmapper.h"
5 25
6 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 27
8 28 class QTCOMMERCIALCHART_EXPORT QVPieModelMapper : public QPieModelMapper
9 29 {
10 30 Q_OBJECT
11 31 Q_PROPERTY(int valuesColumn READ valuesColumn WRITE setValuesColumn)
12 32 Q_PROPERTY(int labelsColumn READ labelsColumn WRITE setLabelsColumn)
13 33
14 34 public:
15 35 explicit QVPieModelMapper(QObject *parent = 0);
16 36
17 37 int valuesColumn() const;
18 38 void setValuesColumn(int valuesColumn);
19 39
20 40 int labelsColumn() const;
21 41 void setLabelsColumn(int labelsColumn);
22 42 };
23 43
24 44 QTCOMMERCIALCHART_END_NAMESPACE
25 45
26 46 #endif // QVPIEMODELMAPPER_H
@@ -1,225 +1,222
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 "qsplineseries.h"
22 22 #include "qsplineseries_p.h"
23 23 #include "splinechartitem_p.h"
24 24 #include "chartdataset_p.h"
25 25 #include "charttheme_p.h"
26 26 #include "chartanimator_p.h"
27 27
28 28 /*!
29 29 \class QSplineSeries
30 30 \brief Series type used to store data needed to draw a spline.
31 31
32 32 QSplineSeries stores the data points along with the segment control points needed by QPainterPath to draw spline
33 33 Control points are automatically calculated when data changes. The algorithm computes the points so that the normal spline can be drawn.
34 34
35 35 \image examples_splinechart.png
36 36
37 37 Creating basic spline chart is simple:
38 38 \code
39 39 QSplineSeries* series = new QSplineSeries();
40 40 series->append(0, 6);
41 41 series->append(2, 4);
42 42 ...
43 43 chart->addSeries(series);
44 44 \endcode
45 45 */
46 46
47 47 /*!
48 48 \fn QSeriesType QSplineSeries::type() const
49 49 Returns the type of the series
50 50 */
51 51
52 52 QTCOMMERCIALCHART_BEGIN_NAMESPACE
53 53
54 54 /*!
55 55 Constructs empty series object which is a child of \a parent.
56 56 When series object is added to QChartView or QChart instance then the ownerships is transferred.
57 57 */
58 58
59 59 QSplineSeries::QSplineSeries(QObject *parent) :
60 60 QLineSeries(*new QSplineSeriesPrivate(this),parent)
61 61 {
62 62 Q_D(QSplineSeries);
63 63 QObject::connect(this,SIGNAL(pointAdded(int)), d, SLOT(updateControlPoints()));
64 64 QObject::connect(this,SIGNAL(pointRemoved(int)), d, SLOT(updateControlPoints()));
65 65 QObject::connect(this,SIGNAL(pointReplaced(int)), d, SLOT(updateControlPoints()));
66 66 }
67 67
68 68 QSplineSeries::~QSplineSeries()
69 69 {
70 70 Q_D(QSplineSeries);
71 71 if(d->m_dataset){
72 72 d->m_dataset->removeSeries(this);
73 73 }
74 74 }
75 75
76 76 QAbstractSeries::SeriesType QSplineSeries::type() const
77 77 {
78 78 return QAbstractSeries::SeriesTypeSpline;
79 79 }
80 80
81 81 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
82 82
83 83 QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries* q):QLineSeriesPrivate(q)
84 84 {
85 // QObject::connect(this,SIGNAL(pointAdded(int)), this, SLOT(updateControlPoints()));
86 // QObject::connect(this,SIGNAL(pointRemoved(int)), this, SLOT(updateControlPoints()));
87 // QObject::connect(this,SIGNAL(pointReplaced(int)), this, SLOT(updateControlPoints()));
88 };
85 }
89 86
90 87 /*!
91 88 Calculates control points which are needed by QPainterPath.cubicTo function to draw the cubic Bezier cureve between two points.
92 89 */
93 90 void QSplineSeriesPrivate::calculateControlPoints()
94 91 {
95 92 Q_Q(QSplineSeries);
96 93
97 94 const QList<QPointF>& points = q->points();
98 95
99 96 int n = points.count() - 1;
100 97
101 98 if (n == 1)
102 99 {
103 100 //for n==1
104 101 m_controlPoints[0].setX((2 * points[0].x() + points[1].x()) / 3);
105 102 m_controlPoints[0].setY((2 * points[0].y() + points[1].y()) / 3);
106 103 m_controlPoints[1].setX(2 * m_controlPoints[0].x() - points[0].x());
107 104 m_controlPoints[1].setY(2 * m_controlPoints[0].y() - points[0].y());
108 105 return;
109 106 }
110 107
111 108 // Calculate first Bezier control points
112 109 // Right hand side vector
113 110 // Set of equations for P0 to Pn points.
114 111 //
115 112 // | 2 1 0 0 ... 0 0 0 ... 0 0 0 | | P1_1 | | P0 + 2 * P1 |
116 113 // | 1 4 1 0 ... 0 0 0 ... 0 0 0 | | P1_2 | | 4 * P1 + 2 * P2 |
117 114 // | 0 1 4 1 ... 0 0 0 ... 0 0 0 | | P1_3 | | 4 * P2 + 2 * P3 |
118 115 // | . . . . . . . . . . . . | | ... | | ... |
119 116 // | 0 0 0 0 ... 1 4 1 ... 0 0 0 | * | P1_i | = | 4 * P(i-1) + 2 * Pi |
120 117 // | . . . . . . . . . . . . | | ... | | ... |
121 118 // | 0 0 0 0 0 0 0 0 ... 1 4 1 | | P1_(n-1)| | 4 * P(n-2) + 2 * P(n-1) |
122 119 // | 0 0 0 0 0 0 0 0 ... 0 2 7 | | P1_n | | 8 * P(n-1) + Pn |
123 120 //
124 121 QVector<qreal> vector;
125 122 vector.resize(n);
126 123
127 124 vector[0] = points[0].x() + 2 * points[1].x();
128 125
129 126
130 127 for (int i = 1; i < n - 1; ++i){
131 128 vector[i] = 4 * points[i].x() + 2 * points[i + 1].x();
132 129 }
133 130
134 131 vector[n - 1] = (8 * points[n-1].x() + points[n].x()) / 2.0;
135 132
136 133 QVector<qreal> xControl = firstControlPoints(vector);
137 134
138 135 vector[0] = points[0].y() + 2 * points[1].y();
139 136
140 137 for (int i = 1; i < n - 1; ++i) {
141 138 vector[i] = 4 * points[i].y() + 2 * points[i + 1].y();
142 139 }
143 140
144 141 vector[n - 1] = (8 * points[n-1].y() + points[n].y()) / 2.0;
145 142
146 143 QVector<qreal> yControl = firstControlPoints(vector);
147 144
148 145 for (int i = 0,j =0; i < n; ++i, ++j) {
149 146
150 147 m_controlPoints[j].setX(xControl[i]);
151 148 m_controlPoints[j].setY(yControl[i]);
152 149
153 150 j++;
154 151
155 152 if (i < n - 1){
156 153 m_controlPoints[j].setX(2 * points[i+1].x() - xControl[i + 1]);
157 154 m_controlPoints[j].setY(2 * points[i+1].y() - yControl[i + 1]);
158 155 }else{
159 156 m_controlPoints[j].setX((points[n].x() + xControl[n - 1]) / 2);
160 157 m_controlPoints[j].setY((points[n].y() + yControl[n - 1]) / 2);
161 158 }
162 159 }
163 160 }
164 161
165 162 QVector<qreal> QSplineSeriesPrivate::firstControlPoints(const QVector<qreal>& vector)
166 163 {
167 164 QVector<qreal> result;
168 165
169 166 int count = vector.count();
170 167 result.resize(count);
171 168 result[0] = vector[0] / 2.0;
172 169
173 170 QVector<qreal> temp;
174 171 temp.resize(count);
175 172 temp[0] = 0;
176 173
177 174 qreal b = 2.0;
178 175
179 176 for (int i = 1; i < count; i++) {
180 177 temp[i] = 1 / b;
181 178 b = (i < count - 1 ? 4.0 : 3.5) - temp[i];
182 179 result[i]=(vector[i] - result[i - 1]) / b;
183 180 }
184 181 for (int i = 1; i < count; i++)
185 182 result[count - i - 1] -= temp[count - i] * result[count - i];
186 183
187 184 return result;
188 185 }
189 186
190 187 QPointF QSplineSeriesPrivate::controlPoint(int index) const
191 188 {
192 189 // Q_D(const QSplineSeries);
193 190 // return d->m_controlPoints[index];
194 191 return m_controlPoints[index];
195 192 }
196 193
197 194 /*!
198 195 Updates the control points, besed on currently avaiable knots.
199 196 */
200 197 void QSplineSeriesPrivate::updateControlPoints()
201 198 {
202 199 Q_Q(QSplineSeries);
203 200 if (q->count() > 1) {
204 201 m_controlPoints.resize(2*q->count()-2);
205 202 calculateControlPoints();
206 203 }
207 204 }
208 205
209 206 Chart* QSplineSeriesPrivate::createGraphics(ChartPresenter* presenter)
210 207 {
211 208 Q_Q(QSplineSeries);
212 209 SplineChartItem* spline = new SplineChartItem(q,presenter);
213 210 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
214 211 spline->setAnimator(presenter->animator());
215 212 spline->setAnimation(new SplineAnimation(spline));
216 213 }
217 214
218 215 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
219 216 return spline;
220 217 }
221 218
222 219 #include "moc_qsplineseries.cpp"
223 220 #include "moc_qsplineseries_p.cpp"
224 221
225 222 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now