##// END OF EJS Templates
ModelMapper proposal
Marek Rosa -
r1229:d938c059b23b
parent child
Show More
@@ -0,0 +1,58
1 #ifndef QPIEMODELMAPPER_P_H
2 #define QPIEMODELMAPPER_P_H
3
4 #include "qpiemodelmapper.h"
5 #include <QObject>
6
7 class QModelIndex;
8 class QAbstractItemModel;
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
12 class QPieModelMapper;
13 class QPieSeries;
14
15 class QPieModelMapperPrivate : public QObject
16 {
17 Q_OBJECT
18
19 public:
20 QPieModelMapperPrivate(QPieModelMapper *q);
21
22 public Q_SLOTS:
23 // for the model
24 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
25 // void modelRowsAdded(QModelIndex parent, int start, int end);
26 // void modelRowsRemoved(QModelIndex parent, int start, int end);
27 // void modelColumnsAdded(QModelIndex parent, int start, int end);
28 // void modelColumnsRemoved(QModelIndex parent, int start, int end);
29
30 // for the series
31 void slicesAdded();
32 void slicesRemoved();
33 void sliceChanged();
34
35 private:
36 void setModel(QAbstractItemModel *model);
37
38 private:
39 int getSliceIndex();
40
41 private:
42 int m_first;
43 int m_count;
44 Qt::Orientation m_orientation;
45 int m_mapValues;
46 int m_mapLabels;
47
48 private:
49 QPieSeries *m_series;
50 QAbstractItemModel *m_model;
51 QPieModelMapper *q_ptr;
52 Q_DECLARE_PUBLIC(QPieModelMapper)
53 friend class QPieSeriesPrivate;
54 };
55
56 QTCOMMERCIALCHART_END_NAMESPACE
57
58 #endif // QPIEMODELMAPPER_P_H
@@ -12,7 +12,8 PRIVATE_HEADERS += \
12 $$PWD/pieslicedata_p.h \
12 $$PWD/pieslicedata_p.h \
13 $$PWD/piechartitem_p.h \
13 $$PWD/piechartitem_p.h \
14 $$PWD/piesliceitem_p.h \
14 $$PWD/piesliceitem_p.h \
15 $$PWD/qpieseries_p.h
15 $$PWD/qpieseries_p.h \
16 $$PWD/qpiemodelmapper_p.h
16
17
17 PUBLIC_HEADERS += \
18 PUBLIC_HEADERS += \
18 $$PWD/qpieseries.h \
19 $$PWD/qpieseries.h \
@@ -1,82 +1,319
1 #include "qpiemodelmapper_p.h"
1 #include "qpiemodelmapper.h"
2 #include "qpiemodelmapper.h"
3 #include "qpieseries.h"
4 #include "qpieslice.h"
5 #include <QAbstractItemModel>
2
6
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
8
5 QPieModelMapper::QPieModelMapper(QObject *parent) :
9 QPieModelMapper::QPieModelMapper(QObject *parent) :
6 QObject(parent),
10 QObject(parent),
7 m_first(0),
11 d_ptr(new QPieModelMapperPrivate(this))
8 m_count(-1),
9 m_orientation(Qt::Vertical),
10 m_mapValues(-1),
11 m_mapLabels(-1)
12 {
12 {
13 }
13 }
14
14
15 int QPieModelMapper::first() const
15 int QPieModelMapper::first() const
16 {
16 {
17 return m_first;
17 Q_D(const QPieModelMapper);
18 return d->m_first;
18 }
19 }
19
20
20 void QPieModelMapper::setFirst(int first)
21 void QPieModelMapper::setFirst(int first)
21 {
22 {
22 m_first = qMax(first, 0);
23 Q_D(QPieModelMapper);
23 emit updated();
24 d->m_first = qMax(first, 0);
25 // emit updated();
24 }
26 }
25
27
26 int QPieModelMapper::count() const
28 int QPieModelMapper::count() const
27 {
29 {
28 return m_count;
30 Q_D(const QPieModelMapper);
31 return d->m_count;
29 }
32 }
30
33
31 void QPieModelMapper::setCount(int count)
34 void QPieModelMapper::setCount(int count)
32 {
35 {
33 m_count = qMax(count, -1);
36 Q_D(QPieModelMapper);
34 emit updated();
37 d->m_count = qMax(count, -1);
38 // emit updated();
35 }
39 }
36
40
37 Qt::Orientation QPieModelMapper::orientation() const
41 Qt::Orientation QPieModelMapper::orientation() const
38 {
42 {
39 return m_orientation;
43 Q_D(const QPieModelMapper);
44 return d->m_orientation;
40 }
45 }
41
46
42 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
47 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
43 {
48 {
44 m_orientation = orientation;
49 Q_D(QPieModelMapper);
45 emit updated();
50 d->m_orientation = orientation;
51 // emit updated();
46 }
52 }
47
53
48 int QPieModelMapper::mapValues() const
54 int QPieModelMapper::mapValues() const
49 {
55 {
50 return m_mapValues;
56 Q_D(const QPieModelMapper);
57 return d->m_mapValues;
51 }
58 }
52
59
53 void QPieModelMapper::setMapValues(int mapValues)
60 void QPieModelMapper::setMapValues(int mapValues)
54 {
61 {
55 m_mapValues = mapValues;
62 Q_D(QPieModelMapper);
56 emit updated();
63 d->m_mapValues = mapValues;
64 // emit updated();
57 }
65 }
58
66
59 int QPieModelMapper::mapLabels() const
67 int QPieModelMapper::mapLabels() const
60 {
68 {
61 return m_mapLabels;
69 Q_D(const QPieModelMapper);
70 return d->m_mapLabels;
62 }
71 }
63
72
64 void QPieModelMapper::setMapLabels(int mapLabels)
73 void QPieModelMapper::setMapLabels(int mapLabels)
65 {
74 {
66 m_mapLabels = mapLabels;
75 Q_D(QPieModelMapper);
67 emit updated();
76 d->m_mapLabels = mapLabels;
77 // emit updated();
68 }
78 }
69
79
70 void QPieModelMapper::reset()
80 void QPieModelMapper::reset()
71 {
81 {
82 Q_D(QPieModelMapper);
83 d->m_first = 0;
84 d->m_count = -1;
85 d->m_orientation = Qt::Vertical;
86 d->m_mapValues = -1;
87 d->m_mapLabels = -1;
88 // emit updated();
89 }
90
91 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
92
93 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
94 q_ptr(q)
95 {
72 m_first = 0;
96 m_first = 0;
73 m_count = -1;
97 m_count = -1;
74 m_orientation = Qt::Vertical;
98 m_orientation = Qt::Vertical;
75 m_mapValues = -1;
99 m_mapValues = -1;
76 m_mapLabels = -1;
100 m_mapLabels = -1;
77 emit updated();
78 }
101 }
79
102
103 int QPieModelMapperPrivate::getSliceIndex()
104 {
105
106 // invalid
107 return -1;
108 }
109
110 void QPieModelMapperPrivate::setModel(QAbstractItemModel *model)
111 {
112 if (model == 0)
113 return;
114
115 if(m_model)
116 {
117 // disconnect signals from old model
118 disconnect(m_model, 0, this, 0);
119 }
120
121 m_model = model;
122 // connect signals from the model
123 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
124 connect(m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelRowsAdded(QModelIndex,int,int)));
125 connect(m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelRowsRemoved(QModelIndex,int,int)));
126 connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelColumnsAdded(QModelIndex,int,int)));
127 connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
128 }
129
130 void QPieModelMapperPrivate::slicesAdded()
131 {
132 //
133 }
134
135 void QPieModelMapperPrivate::slicesRemoved()
136 {
137 //
138 }
139
140 void QPieModelMapperPrivate::sliceChanged()
141 {
142 //
143 }
144
145 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
146 {
147 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
148 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
149 if (m_orientation == Qt::Vertical)
150 {
151 if ( topLeft.row() >= m_first && (m_count == - 1 || topLeft.row() < m_first + m_count)) {
152 if (topLeft.column() == m_mapValues)
153 m_series->slices().at(topLeft.row() - m_first)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
154 if (topLeft.column() == m_mapLabels)
155 m_series->slices().at(topLeft.row() - m_first)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
156 }
157 }
158 else
159 {
160 if (topLeft.column() >= m_first && (m_count == - 1 || topLeft.column() < m_first + m_count)) {
161 if (topLeft.row() == m_mapValues)
162 m_series->slices().at(topLeft.column() - m_first)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
163 if (topLeft.row() == m_mapLabels)
164 m_series->slices().at(topLeft.column() - m_first)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
165 }
166 }
167 }
168 }
169 }
170
171
172 //void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
173 //{
174 // Q_UNUSED(parent);
175 // if (m_mapper->orientation() == Qt::Vertical)
176 // insertData(start, end);
177 // else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie
178 // initializePieFromModel();
179 //}
180
181 //void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
182 //{
183 // Q_UNUSED(parent);
184 // if (m_mapper->orientation() == Qt::Vertical)
185 // removeData(start, end);
186 // else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie
187 // initializePieFromModel();
188 //}
189
190 //void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
191 //{
192 // Q_UNUSED(parent);
193 // if (m_mapper->orientation() == Qt::Horizontal)
194 // insertData(start, end);
195 // else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie
196 // initializePieFromModel();
197 //}
198
199 //void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
200 //{
201 // Q_UNUSED(parent);
202 // if (m_mapper->orientation() == Qt::Horizontal)
203 // removeData(start, end);
204 // else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie
205 // initializePieFromModel();
206 //}
207
208 //void QPieModelMapperPrivate::insertData(int start, int end)
209 //{
210 // Q_Q(QPieSeries);
211 // if (m_count != -1 && start >= m_first + m_count) {
212 // return;
213 // } else {
214 // int addedCount = end - start + 1;
215 // if (m_count != -1 && addedCount > m_count)
216 // addedCount = m_count;
217 // int first = qMax(start, m_first);
218 // int last = qMin(first + addedCount - 1, m_mapper->orientation() == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
219 // for (int i = first; i <= last; i++) {
220 // QPieSlice *slice = new QPieSlice;
221 // if (m_mapper->orientation() == Qt::Vertical) {
222 // slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
223 // slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
224 // } else {
225 // slice->setValue(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble());
226 // slice->setLabel(m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString());
227 // }
228 // slice->setLabelVisible();
229 // q->insert(i - m_first, slice);
230 // }
231 // if (m_count != -1 && m_series->slices().size() > m_count)
232 // for (int i = m_series->slices().size() - 1; i >= m_count; i--)
233 // q->remove(q->slices().at(i));
234 // }
235 //}
236
237 //void QPieModelMapperPrivate::removeData(int start, int end)
238 //{
239 // Q_Q(QPieSeries);
240 // int removedCount = end - start + 1;
241 // if (m_count != -1 && start >= m_first + m_count) {
242 // return;
243 // } else {
244 // int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed
245 // int first = qMax(start, m_first); // get the index of the first item that will be removed.
246 // int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed.
247 // for (int i = last; i >= first; i--)
248 // q->remove(q->slices().at(i - m_first));
249
250 // if (m_count != -1) {
251 // int itemsAvailable; // check how many are available to be added
252 // if (m_mapper->orientation() == Qt::Vertical)
253 // itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size();
254 // else
255 // itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size();
256 // int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled.
257 // int currentSize = m_series->slices().size();
258 // if (toBeAdded > 0)
259 // for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) {
260 // QPieSlice *slice = new QPieSlice;
261 // if (m_mapper->orientation() == Qt::Vertical) {
262 // slice->setValue(m_model->data(m_model->index(i + m_first, m_mapValues), Qt::DisplayRole).toDouble());
263 // slice->setLabel(m_model->data(m_model->index(i + m_first, m_mapLabels), Qt::DisplayRole).toString());
264 // } else {
265 // slice->setValue(m_model->data(m_model->index(m_mapValues, i + m_first), Qt::DisplayRole).toDouble());
266 // slice->setLabel(m_model->data(m_model->index(m_mapLabels, i + m_first), Qt::DisplayRole).toString());
267 // }
268 // slice->setLabelVisible();
269 // q->insert(i, slice);
270 // }
271 // }
272 // }
273 //}
274
275 //void QPieModelMapperPrivate::initializePieFromModel()
276 //{
277 // Q_Q(QPieSeries);
278
279 // // clear current content
280 // q->clear();
281
282 // if (m_model == 0 || m_mapper == 0)
283 // return;
284
285 // // check if mappings are set
286 // if (m_mapValues == -1 || m_mapLabels == -1)
287 // return;
288
289 // // create the initial slices set
290 // if (m_mapper->orientation() == Qt::Vertical) {
291 // if (m_mapValues >= m_model->columnCount() || m_mapLabels >= m_model->columnCount())
292 // return; // mapped columns are not existing
293
294 // int sliceCount = 0;
295 // if(m_count == -1)
296 // sliceCount = m_model->rowCount() - m_first;
297 // else
298 // sliceCount = qMin(m_count, m_model->rowCount() - m_first);
299 // for (int i = m_first; i < m_first + sliceCount; i++)
300 // q->append(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
301 // } else {
302 // if (m_mapValues >= m_model->rowCount() || m_mapLabels >= m_model->rowCount())
303 // return; // mapped columns are not existing
304
305 // int sliceCount = 0;
306 // if(m_count == -1)
307 // sliceCount = m_model->columnCount() - m_first;
308 // else
309 // sliceCount = qMin(m_count, m_model->columnCount() - m_first);
310 // for (int i = m_first; i < m_first + sliceCount; i++)
311 // q->append(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString());
312 // }
313 // q->setLabelsVisible(true);
314 //}
315
316 #include "moc_qpiemodelmapper_p.cpp"
80 #include "moc_qpiemodelmapper.cpp"
317 #include "moc_qpiemodelmapper.cpp"
81
318
82 QTCOMMERCIALCHART_END_NAMESPACE
319 QTCOMMERCIALCHART_END_NAMESPACE
@@ -6,6 +6,8
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 class QPieModelMapperPrivate;
10
9 class QTCOMMERCIALCHART_EXPORT QPieModelMapper : public QObject
11 class QTCOMMERCIALCHART_EXPORT QPieModelMapper : public QObject
10 {
12 {
11 Q_OBJECT
13 Q_OBJECT
@@ -17,7 +19,10 class QTCOMMERCIALCHART_EXPORT QPieModelMapper : public QObject
17 Q_ENUMS(Qt::Orientation)
19 Q_ENUMS(Qt::Orientation)
18
20
19 public:
21 public:
20 explicit QPieModelMapper(QObject *parent = 0);
22 QPieModelMapper(QObject *parent = 0);
23
24 // QAbstractItemModel* model() const;
25 // void setModel(QAbstractItemModel *model);
21
26
22 int first() const;
27 int first() const;
23 void setFirst(int first);
28 void setFirst(int first);
@@ -36,16 +41,10 public:
36
41
37 void reset();
42 void reset();
38
43
39 Q_SIGNALS:
40 void updated();
41
42 private:
44 private:
43 int m_first;
45 QScopedPointer<QPieModelMapperPrivate> d_ptr;
44 int m_count;
46 Q_DECLARE_PRIVATE(QPieModelMapper)
45 Qt::Orientation m_orientation;
47 friend class QPieSeriesPrivate;
46 int m_mapValues;
47 int m_mapLabels;
48
49 };
48 };
50
49
51 QTCOMMERCIALCHART_END_NAMESPACE
50 QTCOMMERCIALCHART_END_NAMESPACE
@@ -28,6 +28,7
28 #include "legendmarker_p.h"
28 #include "legendmarker_p.h"
29 #include <QAbstractItemModel>
29 #include <QAbstractItemModel>
30 #include "qpiemodelmapper.h"
30 #include "qpiemodelmapper.h"
31 #include "qpiemodelmapper_p.h"
31
32
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
34
@@ -469,30 +470,31 qreal QPieSeries::sum() const
469 void QPieSeries::setModel(QAbstractItemModel* model)
470 void QPieSeries::setModel(QAbstractItemModel* model)
470 {
471 {
471 Q_D(QPieSeries);
472 Q_D(QPieSeries);
472 // disconnect signals from old model
473 d->setModel(model);
473 if(d->m_model)
474 // // disconnect signals from old model
474 {
475 // if(d->m_model)
475 disconnect(d->m_model, 0, this, 0);
476 // {
476 }
477 // disconnect(d->m_model, 0, this, 0);
477
478 // }
478 // set new model
479
479 if(model)
480 // // set new model
480 {
481 // if(model)
481 d->m_model = model;
482 // {
482 // connect signals from the model
483 // d->m_model = model;
483 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
484 // // connect signals from the model
484 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
485 //// connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
485 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
486 //// connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
486 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
487 //// connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
487 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
488 //// connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
488
489 //// connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
489 if (d->m_mapper)
490
490 d->initializePieFromModel();
491 //// if (d->m_mapper)
491 }
492 //// d->initializePieFromModel();
492 else
493 // }
493 {
494 // else
494 d->m_model = 0;
495 // {
495 }
496 // d->m_model = 0;
497 // }
496 }
498 }
497
499
498 void QPieSeries::setModelMapper(QPieModelMapper *mapper)
500 void QPieSeries::setModelMapper(QPieModelMapper *mapper)
@@ -506,10 +508,10 void QPieSeries::setModelMapper(QPieModelMapper *mapper)
506 if (mapper) {
508 if (mapper) {
507 d->m_mapper = mapper;
509 d->m_mapper = mapper;
508 // connect the signal from the mapper
510 // connect the signal from the mapper
509 connect(d->m_mapper, SIGNAL(updated()), d, SLOT(initializePieFromModel()));
511 // connect(d->m_mapper, SIGNAL(updated()), d, SLOT(initializePieFromModel()));
510
512
511 if (d->m_model)
513 // if (d->m_model)
512 d->initializePieFromModel();
514 // d->initializePieFromModel();
513 } else {
515 } else {
514 d->m_mapper = 0;
516 d->m_mapper = 0;
515 }
517 }
@@ -532,6 +534,7 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
532 m_pieStartAngle(0),
534 m_pieStartAngle(0),
533 m_pieEndAngle(360),
535 m_pieEndAngle(360),
534 m_sum(0),
536 m_sum(0),
537 m_model(0),
535 m_mapper(0)
538 m_mapper(0)
536 {
539 {
537
540
@@ -608,191 +611,6 void QPieSeriesPrivate::sliceHovered(bool state)
608 emit q->hovered(slice, state);
611 emit q->hovered(slice, state);
609 }
612 }
610
613
611 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
612 {
613 if (m_mapper) {
614 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
615 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
616 if (m_mapper->orientation() == Qt::Vertical)
617 {
618 if ( topLeft.row() >= m_mapper->first() && (m_mapper->count() == - 1 || topLeft.row() < m_mapper->first() + m_mapper->count())) {
619 if (topLeft.column() == m_mapper->mapValues())
620 m_slices.at(topLeft.row() - m_mapper->first())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
621 if (topLeft.column() == m_mapper->mapLabels())
622 m_slices.at(topLeft.row() - m_mapper->first())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
623 }
624 }
625 else
626 {
627 if (topLeft.column() >= m_mapper->first() && (m_mapper->count() == - 1 || topLeft.column() < m_mapper->first() + m_mapper->count())) {
628 if (topLeft.row() == m_mapper->mapValues())
629 m_slices.at(topLeft.column() - m_mapper->first())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
630 if (topLeft.row() == m_mapper->mapLabels())
631 m_slices.at(topLeft.column() - m_mapper->first())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
632 }
633 }
634 }
635 }
636 }
637 }
638
639
640 void QPieSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
641 {
642 Q_UNUSED(parent);
643 if (m_mapper) {
644 if (m_mapper->orientation() == Qt::Vertical)
645 insertData(start, end);
646 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
647 initializePieFromModel();
648 }
649 }
650
651 void QPieSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
652 {
653 Q_UNUSED(parent);
654 if (m_mapper) {
655 if (m_mapper->orientation() == Qt::Vertical)
656 removeData(start, end);
657 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
658 initializePieFromModel();
659 }
660 }
661
662 void QPieSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
663 {
664 Q_UNUSED(parent);
665 if (m_mapper) {
666 if (m_mapper->orientation() == Qt::Horizontal)
667 insertData(start, end);
668 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
669 initializePieFromModel();
670 }
671 }
672
673 void QPieSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
674 {
675 Q_UNUSED(parent);
676 if (m_mapper) {
677 if (m_mapper->orientation() == Qt::Horizontal)
678 removeData(start, end);
679 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
680 initializePieFromModel();
681 }
682 }
683
684 void QPieSeriesPrivate::insertData(int start, int end)
685 {
686 Q_Q(QPieSeries);
687 if (m_mapper) {
688 if (m_mapper->count() != -1 && start >= m_mapper->first() + m_mapper->count()) {
689 return;
690 } else {
691 int addedCount = end - start + 1;
692 if (m_mapper->count() != -1 && addedCount > m_mapper->count())
693 addedCount = m_mapper->count();
694 int first = qMax(start, m_mapper->first());
695 int last = qMin(first + addedCount - 1, m_mapper->orientation() == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
696 for (int i = first; i <= last; i++) {
697 QPieSlice *slice = new QPieSlice;
698 if (m_mapper->orientation() == Qt::Vertical) {
699 slice->setValue(m_model->data(m_model->index(i, m_mapper->mapValues()), Qt::DisplayRole).toDouble());
700 slice->setLabel(m_model->data(m_model->index(i, m_mapper->mapLabels()), Qt::DisplayRole).toString());
701 } else {
702 slice->setValue(m_model->data(m_model->index(m_mapper->mapValues(), i), Qt::DisplayRole).toDouble());
703 slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString());
704 }
705 slice->setLabelVisible();
706 q->insert(i - m_mapper->first(), slice);
707 }
708 if (m_mapper->count() != -1 && m_slices.size() > m_mapper->count())
709 for (int i = m_slices.size() - 1; i >= m_mapper->count(); i--)
710 q->remove(q->slices().at(i));
711 }
712 }
713 }
714
715 void QPieSeriesPrivate::removeData(int start, int end)
716 {
717 Q_Q(QPieSeries);
718 if (m_mapper) {
719 int removedCount = end - start + 1;
720 if (m_mapper->count() != -1 && start >= m_mapper->first() + m_mapper->count()) {
721 return;
722 } else {
723 int toRemove = qMin(m_slices.size(), removedCount); // first find how many items can actually be removed
724 int first = qMax(start, m_mapper->first()); // get the index of the first item that will be removed.
725 int last = qMin(first + toRemove - 1, m_slices.size() + m_mapper->first() - 1); // get the index of the last item that will be removed.
726 for (int i = last; i >= first; i--)
727 q->remove(q->slices().at(i - m_mapper->first()));
728
729 if (m_mapper->count() != -1) {
730 int itemsAvailable; // check how many are available to be added
731 if (m_mapper->orientation() == Qt::Vertical)
732 itemsAvailable = m_model->rowCount() - m_mapper->first() - m_slices.size();
733 else
734 itemsAvailable = m_model->columnCount() - m_mapper->first() - m_slices.size();
735 int toBeAdded = qMin(itemsAvailable, m_mapper->count() - m_slices.size()); // add not more items than there is space left to be filled.
736 int currentSize = m_slices.size();
737 if (toBeAdded > 0)
738 for (int i = m_slices.size(); i < currentSize + toBeAdded; i++) {
739 QPieSlice *slice = new QPieSlice;
740 if (m_mapper->orientation() == Qt::Vertical) {
741 slice->setValue(m_model->data(m_model->index(i + m_mapper->first(), m_mapper->mapValues()), Qt::DisplayRole).toDouble());
742 slice->setLabel(m_model->data(m_model->index(i + m_mapper->first(), m_mapper->mapLabels()), Qt::DisplayRole).toString());
743 } else {
744 slice->setValue(m_model->data(m_model->index(m_mapper->mapValues(), i + m_mapper->first()), Qt::DisplayRole).toDouble());
745 slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i + m_mapper->first()), Qt::DisplayRole).toString());
746 }
747 slice->setLabelVisible();
748 q->insert(i, slice);
749 }
750 }
751 }
752 }
753 }
754
755 void QPieSeriesPrivate::initializePieFromModel()
756 {
757 Q_Q(QPieSeries);
758
759 // clear current content
760 q->clear();
761
762 if (m_model == 0 || m_mapper == 0)
763 return;
764
765 // check if mappings are set
766 if (m_mapper->mapValues() == -1 || m_mapper->mapLabels() == -1)
767 return;
768
769 // create the initial slices set
770 if (m_mapper->orientation() == Qt::Vertical) {
771 if (m_mapper->mapValues() >= m_model->columnCount() || m_mapper->mapLabels() >= m_model->columnCount())
772 return; // mapped columns are not existing
773
774 int sliceCount = 0;
775 if(m_mapper->count() == -1)
776 sliceCount = m_model->rowCount() - m_mapper->first();
777 else
778 sliceCount = qMin(m_mapper->count(), m_model->rowCount() - m_mapper->first());
779 for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++)
780 q->append(m_model->data(m_model->index(i, m_mapper->mapLabels()), Qt::DisplayRole).toString(), m_model->data(m_model->index(i, m_mapper->mapValues()), Qt::DisplayRole).toDouble());
781 } else {
782 if (m_mapper->mapValues() >= m_model->rowCount() || m_mapper->mapLabels() >= m_model->rowCount())
783 return; // mapped columns are not existing
784
785 int sliceCount = 0;
786 if(m_mapper->count() == -1)
787 sliceCount = m_model->columnCount() - m_mapper->first();
788 else
789 sliceCount = qMin(m_mapper->count(), m_model->columnCount() - m_mapper->first());
790 for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++)
791 q->append(m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString(), m_model->data(m_model->index(m_mapper->mapValues(), i), Qt::DisplayRole).toDouble());
792 }
793 q->setLabelsVisible(true);
794 }
795
796 bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min)
614 bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min)
797 {
615 {
798 // Remove rounding errors
616 // Remove rounding errors
@@ -847,6 +665,21 QList<LegendMarker*> QPieSeriesPrivate::createLegendMarker(QLegend* legend)
847 return markers;
665 return markers;
848 }
666 }
849
667
668 void QPieSeriesPrivate::setModel(QAbstractItemModel* model)
669 {
670 Q_Q(QPieSeries);
671 QPieModelMapperPrivate *mapperPrivate = m_mapper->d_func();
672 mapperPrivate->setModel(model);
673 if(mapperPrivate->m_series != q)
674 {
675 disconnect(mapperPrivate->m_series, 0, mapperPrivate, 0);
676 mapperPrivate->m_series = q;
677 connect(this, SIGNAL(added(QList<QPieSlice*>)), mapperPrivate, SLOT(slicesAdded()));
678 connect(this, SIGNAL(removed(QList<QPieSlice*>)), mapperPrivate, SLOT(slicesRemoved()));
679 connect(this, SIGNAL(modified()), mapperPrivate, SLOT(sliceChanged()));
680 }
681 }
682
850 #include "moc_qpieseries.cpp"
683 #include "moc_qpieseries.cpp"
851 #include "moc_qpieseries_p.cpp"
684 #include "moc_qpieseries_p.cpp"
852
685
@@ -54,20 +54,18 public Q_SLOTS:
54 void sliceChanged();
54 void sliceChanged();
55 void sliceClicked();
55 void sliceClicked();
56 void sliceHovered(bool state);
56 void sliceHovered(bool state);
57 void initializePieFromModel();
57 // void initializePieFromModel();
58 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
58 // void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
59 void modelRowsAdded(QModelIndex parent, int start, int end);
59 // void modelRowsAdded(QModelIndex parent, int start, int end);
60 void modelRowsRemoved(QModelIndex parent, int start, int end);
60 // void modelRowsRemoved(QModelIndex parent, int start, int end);
61 void modelColumnsAdded(QModelIndex parent, int start, int end);
61 // void modelColumnsAdded(QModelIndex parent, int start, int end);
62 void modelColumnsRemoved(QModelIndex parent, int start, int end);
62 // void modelColumnsRemoved(QModelIndex parent, int start, int end);
63 bool setRealValue(qreal &value, qreal newValue, qreal max, qreal min = 0.0);
63 bool setRealValue(qreal &value, qreal newValue, qreal max, qreal min = 0.0);
64
64
65 private:
65 private:
66 void doClear();
66 void setModel(QAbstractItemModel *model);
67 void doRemove(QPieSlice* slice);
67 // void insertData(int start, int end);
68 void doInsert(int index, QPieSlice* slice);
68 // void removeData(int start, int end);
69 void insertData(int start, int end);
70 void removeData(int start, int end);
71
69
72 public:
70 public:
73 QList<QPieSlice*> m_slices;
71 QList<QPieSlice*> m_slices;
General Comments 0
You need to be logged in to leave comments. Login now