##// END OF EJS Templates
Fixed problem with PieModelMapper
Marek Rosa -
r1320:42dbf474caea
parent child
Show More
@@ -74,12 +74,19 Rectangle {
74 74 verticalPosition: 0.3
75 75 }
76 76
77 VPieModelMapper {
77 // VPieModelMapper {
78 // series: pieSeries
79 // model: customModel
80 // labelsColumn: 1
81 // valuesColumn: 2
82 // first: 1
83 // }
84 HPieModelMapper {
78 85 series: pieSeries
79 86 model: customModel
80 labelsColumn: 1
81 valuesColumn: 2
82 first: 1
87 labelsRow: 1
88 valuesRow: 2
89 first: 2
83 90 }
84 91
85 92 AreaSeries {
@@ -30,22 +30,22 QHPieModelMapper::QHPieModelMapper(QObject *parent) :
30 30
31 31 int QHPieModelMapper::valuesRow() const
32 32 {
33 return QPieModelMapper::valuesIndex();
33 return QPieModelMapper::valuesSection();
34 34 }
35 35
36 36 void QHPieModelMapper::setValuesRow(int valuesRow)
37 37 {
38 QPieModelMapper::setValuesIndex(valuesRow);
38 QPieModelMapper::setValuesSection(valuesRow);
39 39 }
40 40
41 41 int QHPieModelMapper::labelsRow() const
42 42 {
43 return QPieModelMapper::labelsIndex();
43 return QPieModelMapper::labelsSection();
44 44 }
45 45
46 46 void QHPieModelMapper::setLabelsRow(int labelsRow)
47 47 {
48 QPieModelMapper::setLabelsIndex(labelsRow);
48 QPieModelMapper::setLabelsSection(labelsRow);
49 49 }
50 50
51 51 #include "moc_qhpiemodelmapper.cpp"
@@ -34,8 +34,8 QPieModelMapper::QPieModelMapper(QObject *parent) :
34 34
35 35 QPieModelMapper::~QPieModelMapper()
36 36 {
37 // Q_D(QPieModelMapper);
38 // disconnect(d->m_model, 0, d, 0);
37 // Q_D(QPieModelMapper);
38 // disconnect(d->m_model, 0, d, 0);
39 39 }
40 40
41 41 QAbstractItemModel* QPieModelMapper::model() const
@@ -126,29 +126,29 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
126 126 d->initializePieFromModel();
127 127 }
128 128
129 int QPieModelMapper::valuesIndex() const
129 int QPieModelMapper::valuesSection() const
130 130 {
131 131 Q_D(const QPieModelMapper);
132 return d->m_valuesIndex;
132 return d->m_valuesSection;
133 133 }
134 134
135 void QPieModelMapper::setValuesIndex(int valuesIndex)
135 void QPieModelMapper::setValuesSection(int valuesSection)
136 136 {
137 137 Q_D(QPieModelMapper);
138 d->m_valuesIndex = qMax(-1, valuesIndex);
138 d->m_valuesSection = qMax(-1, valuesSection);
139 139 d->initializePieFromModel();
140 140 }
141 141
142 int QPieModelMapper::labelsIndex() const
142 int QPieModelMapper::labelsSection() const
143 143 {
144 144 Q_D(const QPieModelMapper);
145 return d->m_labelsIndex;
145 return d->m_labelsSection;
146 146 }
147 147
148 void QPieModelMapper::setLabelsIndex(int labelsIndex)
148 void QPieModelMapper::setLabelsSection(int labelsSection)
149 149 {
150 150 Q_D(QPieModelMapper);
151 d->m_labelsIndex = qMax(-1, labelsIndex);
151 d->m_labelsSection = qMax(-1, labelsSection);
152 152 d->initializePieFromModel();
153 153 }
154 154
@@ -158,8 +158,8 void QPieModelMapper::reset()
158 158 d->m_first = 0;
159 159 d->m_count = -1;
160 160 d->m_orientation = Qt::Vertical;
161 d->m_valuesIndex = -1;
162 d->m_labelsIndex = -1;
161 d->m_valuesSection = -1;
162 d->m_labelsSection = -1;
163 163 }
164 164
165 165 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -170,8 +170,8 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
170 170 m_first(0),
171 171 m_count(-1),
172 172 m_orientation(Qt::Vertical),
173 m_valuesIndex(-1),
174 m_labelsIndex(-1),
173 m_valuesSection(-1),
174 m_labelsSection(-1),
175 175 m_seriesSignalsBlock(false),
176 176 m_modelSignalsBlock(false),
177 177 q_ptr(q)
@@ -191,12 +191,23 void QPieModelMapperPrivate::blockSeriesSignals(bool block)
191 191
192 192 QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const
193 193 {
194 if (m_orientation == Qt::Vertical && (index.column() == m_valuesIndex || index.column() == m_labelsIndex)) {
195 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count))
196 return m_series->slices().at(index.row() - m_first);
197 } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesIndex || index.row() == m_labelsIndex)) {
198 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
199 return m_series->slices().at(index.column() - m_first);
194 if (!index.isValid())
195 return 0; // index is invalid
196
197 if (m_orientation == Qt::Vertical && (index.column() == m_valuesSection || index.column() == m_labelsSection)) {
198 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) {
199 if (m_model->index(index.row(), m_valuesSection).isValid() && m_model->index(index.row(), m_labelsSection).isValid())
200 return m_series->slices().at(index.row() - m_first);
201 else
202 return 0;
203 }
204 } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesSection || index.row() == m_labelsSection)) {
205 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count)) {
206 if (m_model->index(m_valuesSection, index.column()).isValid() && m_model->index(m_labelsSection, index.column()).isValid())
207 return m_series->slices().at(index.column() - m_first);
208 else
209 return 0;
210 }
200 211 }
201 212 return 0; // This part of model has not been mapped to any slice
202 213 }
@@ -207,9 +218,9 QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos)
207 218 return QModelIndex(); // invalid
208 219
209 220 if (m_orientation == Qt::Vertical)
210 return m_model->index(slicePos + m_first, m_valuesIndex);
221 return m_model->index(slicePos + m_first, m_valuesSection);
211 222 else
212 return m_model->index(m_valuesIndex, slicePos + m_first);
223 return m_model->index(m_valuesSection, slicePos + m_first);
213 224 }
214 225
215 226 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
@@ -218,16 +229,16 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
218 229 return QModelIndex(); // invalid
219 230
220 231 if (m_orientation == Qt::Vertical)
221 return m_model->index(slicePos + m_first, m_labelsIndex);
232 return m_model->index(slicePos + m_first, m_labelsSection);
222 233 else
223 return m_model->index(m_labelsIndex, slicePos + m_first);
234 return m_model->index(m_labelsSection, slicePos + m_first);
224 235 }
225 236
226 237 bool QPieModelMapperPrivate::isLabelIndex(QModelIndex index) const
227 238 {
228 if (m_orientation == Qt::Vertical && index.column() == m_labelsIndex)
239 if (m_orientation == Qt::Vertical && index.column() == m_labelsSection)
229 240 return true;
230 else if (m_orientation == Qt::Horizontal && index.row() == m_labelsIndex)
241 else if (m_orientation == Qt::Horizontal && index.row() == m_labelsSection)
231 242 return true;
232 243
233 244 return false;
@@ -235,9 +246,9 bool QPieModelMapperPrivate::isLabelIndex(QModelIndex index) const
235 246
236 247 bool QPieModelMapperPrivate::isValueIndex(QModelIndex index) const
237 248 {
238 if (m_orientation == Qt::Vertical && index.column() == m_valuesIndex)
249 if (m_orientation == Qt::Vertical && index.column() == m_valuesSection)
239 250 return true;
240 else if (m_orientation == Qt::Horizontal && index.row() == m_valuesIndex)
251 else if (m_orientation == Qt::Horizontal && index.row() == m_valuesSection)
241 252 return true;
242 253
243 254 return false;
@@ -361,7 +372,7 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int e
361 372 blockSeriesSignals();
362 373 if (m_orientation == Qt::Vertical)
363 374 insertData(start, end);
364 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
375 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
365 376 initializePieFromModel();
366 377 blockSeriesSignals(false);
367 378 }
@@ -375,7 +386,7 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int
375 386 blockSeriesSignals();
376 387 if (m_orientation == Qt::Vertical)
377 388 removeData(start, end);
378 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
389 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
379 390 initializePieFromModel();
380 391 blockSeriesSignals(false);
381 392 }
@@ -389,7 +400,7 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, in
389 400 blockSeriesSignals();
390 401 if (m_orientation == Qt::Horizontal)
391 402 insertData(start, end);
392 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
403 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
393 404 initializePieFromModel();
394 405 blockSeriesSignals(false);
395 406 }
@@ -403,7 +414,7 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start,
403 414 blockSeriesSignals();
404 415 if (m_orientation == Qt::Horizontal)
405 416 removeData(start, end);
406 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
417 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
407 418 initializePieFromModel();
408 419 blockSeriesSignals(false);
409 420 }
@@ -422,14 +433,18 void QPieModelMapperPrivate::insertData(int start, int end)
422 433 int first = qMax(start, m_first);
423 434 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
424 435 for (int i = first; i <= last; i++) {
425 QPieSlice *slice = new QPieSlice;
426 slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble());
427 slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString());
428 slice->setLabelVisible();
429 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
430 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
431 m_series->insert(i - m_first, slice);
432 m_slices.insert(i - m_first, slice);
436 QModelIndex valueIndex = valueModelIndex(i - m_first);
437 QModelIndex labelIndex = labelModelIndex(i - m_first);
438 if (valueIndex.isValid() && labelIndex.isValid()) {
439 QPieSlice *slice = new QPieSlice;
440 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
441 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
442 slice->setLabelVisible();
443 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
444 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
445 m_series->insert(i - m_first, slice);
446 m_slices.insert(i - m_first, slice);
447 }
433 448 }
434 449
435 450 // remove excess of slices (abouve m_count)
@@ -468,17 +483,16 void QPieModelMapperPrivate::removeData(int start, int end)
468 483 int currentSize = m_series->slices().size();
469 484 if (toBeAdded > 0)
470 485 for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) {
471 QPieSlice *slice = new QPieSlice;
472 if (m_orientation == Qt::Vertical) {
473 slice->setValue(m_model->data(m_model->index(i + m_first, m_valuesIndex), Qt::DisplayRole).toDouble());
474 slice->setLabel(m_model->data(m_model->index(i + m_first, m_labelsIndex), Qt::DisplayRole).toString());
475 } else {
476 slice->setValue(m_model->data(m_model->index(m_valuesIndex, i + m_first), Qt::DisplayRole).toDouble());
477 slice->setLabel(m_model->data(m_model->index(m_labelsIndex, i + m_first), Qt::DisplayRole).toString());
486 QModelIndex valueIndex = valueModelIndex(i - m_first);
487 QModelIndex labelIndex = labelModelIndex(i - m_first);
488 if (valueIndex.isValid() && labelIndex.isValid()) {
489 QPieSlice *slice = new QPieSlice;
490 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
491 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
492 slice->setLabelVisible();
493 m_series->insert(i, slice);
494 m_slices.insert(i, slice);
478 495 }
479 slice->setLabelVisible();
480 m_series->insert(i, slice);
481 m_slices.insert(i, slice);
482 496 }
483 497 }
484 498 }
@@ -60,11 +60,11 public:
60 60 void reset();
61 61
62 62 protected:
63 int valuesIndex() const;
64 void setValuesIndex(int valuesIndex);
63 int valuesSection() const;
64 void setValuesSection(int valuesSection);
65 65
66 int labelsIndex() const;
67 void setLabelsIndex(int labelsIndex);
66 int labelsSection() const;
67 void setLabelsSection(int labelsSection);
68 68
69 69 Qt::Orientation orientation() const;
70 70 void setOrientation(Qt::Orientation orientation);
@@ -84,8 +84,8 private:
84 84 int m_first;
85 85 int m_count;
86 86 Qt::Orientation m_orientation;
87 int m_valuesIndex;
88 int m_labelsIndex;
87 int m_valuesSection;
88 int m_labelsSection;
89 89 bool m_seriesSignalsBlock;
90 90 bool m_modelSignalsBlock;
91 91
@@ -31,22 +31,22 QVPieModelMapper::QVPieModelMapper(QObject *parent) :
31 31
32 32 int QVPieModelMapper::valuesColumn() const
33 33 {
34 return QPieModelMapper::valuesIndex();
34 return QPieModelMapper::valuesSection();
35 35 }
36 36
37 37 void QVPieModelMapper::setValuesColumn(int valuesColumn)
38 38 {
39 QPieModelMapper::setValuesIndex(valuesColumn);
39 QPieModelMapper::setValuesSection(valuesColumn);
40 40 }
41 41
42 42 int QVPieModelMapper::labelsColumn() const
43 43 {
44 return QPieModelMapper::labelsIndex();
44 return QPieModelMapper::labelsSection();
45 45 }
46 46
47 47 void QVPieModelMapper::setLabelsColumn(int labelsColumn)
48 48 {
49 QPieModelMapper::setLabelsIndex(labelsColumn);
49 QPieModelMapper::setLabelsSection(labelsColumn);
50 50 }
51 51
52 52 #include "moc_qvpiemodelmapper.cpp"
General Comments 0
You need to be logged in to leave comments. Login now