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