@@ -1,492 +1,495 | |||||
1 | #include "qpiemodelmapper_p.h" |
|
1 | #include "qpiemodelmapper_p.h" | |
2 | #include "qpiemodelmapper.h" |
|
2 | #include "qpiemodelmapper.h" | |
3 | #include "qpieseries.h" |
|
3 | #include "qpieseries.h" | |
4 | #include "qpieslice.h" |
|
4 | #include "qpieslice.h" | |
5 | #include <QAbstractItemModel> |
|
5 | #include <QAbstractItemModel> | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | QPieModelMapper::QPieModelMapper(QObject *parent) : |
|
9 | QPieModelMapper::QPieModelMapper(QObject *parent) : | |
10 | QObject(parent), |
|
10 | QObject(parent), | |
11 | d_ptr(new QPieModelMapperPrivate(this)) |
|
11 | d_ptr(new QPieModelMapperPrivate(this)) | |
12 | { |
|
12 | { | |
13 | } |
|
13 | } | |
14 |
|
14 | |||
15 | QAbstractItemModel* QPieModelMapper::model() const |
|
15 | QAbstractItemModel* QPieModelMapper::model() const | |
16 | { |
|
16 | { | |
17 | Q_D(const QPieModelMapper); |
|
17 | Q_D(const QPieModelMapper); | |
18 | return d->m_model; |
|
18 | return d->m_model; | |
19 | } |
|
19 | } | |
20 |
|
20 | |||
21 | void QPieModelMapper::setModel(QAbstractItemModel *model) |
|
21 | void QPieModelMapper::setModel(QAbstractItemModel *model) | |
22 | { |
|
22 | { | |
23 | if (model == 0) |
|
23 | if (model == 0) | |
24 | return; |
|
24 | return; | |
25 |
|
25 | |||
26 | Q_D(QPieModelMapper); |
|
26 | Q_D(QPieModelMapper); | |
27 | if (d->m_model) { |
|
27 | if (d->m_model) { | |
28 | disconnect(d->m_model, 0, d, 0); |
|
28 | disconnect(d->m_model, 0, d, 0); | |
29 | } |
|
29 | } | |
30 |
|
30 | |||
31 | d->m_model = model; |
|
31 | d->m_model = model; | |
32 | d->initializePieFromModel(); |
|
32 | d->initializePieFromModel(); | |
33 | // connect signals from the model |
|
33 | // connect signals from the model | |
34 | connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex))); |
|
34 | connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex))); | |
35 | connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int))); |
|
35 | connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int))); | |
36 | connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int))); |
|
36 | connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int))); | |
37 | connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int))); |
|
37 | connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int))); | |
38 | connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int))); |
|
38 | connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int))); | |
39 | } |
|
39 | } | |
40 |
|
40 | |||
41 | QPieSeries* QPieModelMapper::series() const |
|
41 | QPieSeries* QPieModelMapper::series() const | |
42 | { |
|
42 | { | |
43 | Q_D(const QPieModelMapper); |
|
43 | Q_D(const QPieModelMapper); | |
44 | return d->m_series; |
|
44 | return d->m_series; | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 | void QPieModelMapper::setSeries(QPieSeries *series) |
|
47 | void QPieModelMapper::setSeries(QPieSeries *series) | |
48 | { |
|
48 | { | |
49 | Q_D(QPieModelMapper); |
|
49 | Q_D(QPieModelMapper); | |
50 | if (d->m_series) { |
|
50 | if (d->m_series) { | |
51 | disconnect(d->m_series, 0, d, 0); |
|
51 | disconnect(d->m_series, 0, d, 0); | |
52 | } |
|
52 | } | |
53 |
|
53 | |||
54 | if (series == 0) |
|
54 | if (series == 0) | |
55 | return; |
|
55 | return; | |
56 |
|
56 | |||
57 | d->m_series = series; |
|
57 | d->m_series = series; | |
58 | d->initializePieFromModel(); |
|
58 | d->initializePieFromModel(); | |
59 | // connect the signals from the series |
|
59 | // connect the signals from the series | |
60 | connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>))); |
|
60 | connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>))); | |
61 | connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>))); |
|
61 | connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>))); | |
62 | } |
|
62 | } | |
63 |
|
63 | |||
64 | int QPieModelMapper::first() const |
|
64 | int QPieModelMapper::first() const | |
65 | { |
|
65 | { | |
66 | Q_D(const QPieModelMapper); |
|
66 | Q_D(const QPieModelMapper); | |
67 | return d->m_first; |
|
67 | return d->m_first; | |
68 | } |
|
68 | } | |
69 |
|
69 | |||
70 | void QPieModelMapper::setFirst(int first) |
|
70 | void QPieModelMapper::setFirst(int first) | |
71 | { |
|
71 | { | |
72 | Q_D(QPieModelMapper); |
|
72 | Q_D(QPieModelMapper); | |
73 | d->m_first = qMax(first, 0); |
|
73 | d->m_first = qMax(first, 0); | |
74 | d->initializePieFromModel(); |
|
74 | d->initializePieFromModel(); | |
75 | } |
|
75 | } | |
76 |
|
76 | |||
77 | int QPieModelMapper::count() const |
|
77 | int QPieModelMapper::count() const | |
78 | { |
|
78 | { | |
79 | Q_D(const QPieModelMapper); |
|
79 | Q_D(const QPieModelMapper); | |
80 | return d->m_count; |
|
80 | return d->m_count; | |
81 | } |
|
81 | } | |
82 |
|
82 | |||
83 | void QPieModelMapper::setCount(int count) |
|
83 | void QPieModelMapper::setCount(int count) | |
84 | { |
|
84 | { | |
85 | Q_D(QPieModelMapper); |
|
85 | Q_D(QPieModelMapper); | |
86 | d->m_count = qMax(count, -1); |
|
86 | d->m_count = qMax(count, -1); | |
87 | d->initializePieFromModel(); |
|
87 | d->initializePieFromModel(); | |
88 | } |
|
88 | } | |
89 |
|
89 | |||
90 | Qt::Orientation QPieModelMapper::orientation() const |
|
90 | Qt::Orientation QPieModelMapper::orientation() const | |
91 | { |
|
91 | { | |
92 | Q_D(const QPieModelMapper); |
|
92 | Q_D(const QPieModelMapper); | |
93 | return d->m_orientation; |
|
93 | return d->m_orientation; | |
94 | } |
|
94 | } | |
95 |
|
95 | |||
96 | void QPieModelMapper::setOrientation(Qt::Orientation orientation) |
|
96 | void QPieModelMapper::setOrientation(Qt::Orientation orientation) | |
97 | { |
|
97 | { | |
98 | Q_D(QPieModelMapper); |
|
98 | Q_D(QPieModelMapper); | |
99 | d->m_orientation = orientation; |
|
99 | d->m_orientation = orientation; | |
100 | d->initializePieFromModel(); |
|
100 | d->initializePieFromModel(); | |
101 | } |
|
101 | } | |
102 |
|
102 | |||
103 | int QPieModelMapper::valuesIndex() const |
|
103 | int QPieModelMapper::valuesIndex() const | |
104 | { |
|
104 | { | |
105 | Q_D(const QPieModelMapper); |
|
105 | Q_D(const QPieModelMapper); | |
106 | return d->m_valuesIndex; |
|
106 | return d->m_valuesIndex; | |
107 | } |
|
107 | } | |
108 |
|
108 | |||
109 | void QPieModelMapper::setValuesIndex(int valuesIndex) |
|
109 | void QPieModelMapper::setValuesIndex(int valuesIndex) | |
110 | { |
|
110 | { | |
111 | Q_D(QPieModelMapper); |
|
111 | Q_D(QPieModelMapper); | |
112 | d->m_valuesIndex = qMax(-1, valuesIndex); |
|
112 | d->m_valuesIndex = qMax(-1, valuesIndex); | |
113 | d->initializePieFromModel(); |
|
113 | d->initializePieFromModel(); | |
114 | } |
|
114 | } | |
115 |
|
115 | |||
116 | int QPieModelMapper::labelsIndex() const |
|
116 | int QPieModelMapper::labelsIndex() const | |
117 | { |
|
117 | { | |
118 | Q_D(const QPieModelMapper); |
|
118 | Q_D(const QPieModelMapper); | |
119 | return d->m_labelsIndex; |
|
119 | return d->m_labelsIndex; | |
120 | } |
|
120 | } | |
121 |
|
121 | |||
122 | void QPieModelMapper::setLabelsIndex(int labelsIndex) |
|
122 | void QPieModelMapper::setLabelsIndex(int labelsIndex) | |
123 | { |
|
123 | { | |
124 | Q_D(QPieModelMapper); |
|
124 | Q_D(QPieModelMapper); | |
125 | d->m_labelsIndex = qMax(-1, labelsIndex); |
|
125 | d->m_labelsIndex = qMax(-1, labelsIndex); | |
126 | d->initializePieFromModel(); |
|
126 | d->initializePieFromModel(); | |
127 | } |
|
127 | } | |
128 |
|
128 | |||
129 | void QPieModelMapper::reset() |
|
129 | void QPieModelMapper::reset() | |
130 | { |
|
130 | { | |
131 | Q_D(QPieModelMapper); |
|
131 | Q_D(QPieModelMapper); | |
132 | d->m_first = 0; |
|
132 | d->m_first = 0; | |
133 | d->m_count = -1; |
|
133 | d->m_count = -1; | |
134 | d->m_orientation = Qt::Vertical; |
|
134 | d->m_orientation = Qt::Vertical; | |
135 | d->m_valuesIndex = -1; |
|
135 | d->m_valuesIndex = -1; | |
136 | d->m_labelsIndex = -1; |
|
136 | d->m_labelsIndex = -1; | |
137 | } |
|
137 | } | |
138 |
|
138 | |||
139 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
139 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
140 |
|
140 | |||
141 | QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) : |
|
141 | QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) : | |
142 | m_series(0), |
|
142 | m_series(0), | |
143 | m_model(0), |
|
143 | m_model(0), | |
144 | m_first(0), |
|
144 | m_first(0), | |
145 | m_count(-1), |
|
145 | m_count(-1), | |
146 | m_orientation(Qt::Vertical), |
|
146 | m_orientation(Qt::Vertical), | |
147 | m_valuesIndex(-1), |
|
147 | m_valuesIndex(-1), | |
148 | m_labelsIndex(-1), |
|
148 | m_labelsIndex(-1), | |
149 | m_seriesSignalsBlock(false), |
|
149 | m_seriesSignalsBlock(false), | |
150 | m_modelSignalsBlock(false), |
|
150 | m_modelSignalsBlock(false), | |
151 | q_ptr(q) |
|
151 | q_ptr(q) | |
152 | { |
|
152 | { | |
153 | } |
|
153 | } | |
154 |
|
154 | |||
155 | void QPieModelMapperPrivate::blockModelSignals(bool block) |
|
155 | void QPieModelMapperPrivate::blockModelSignals(bool block) | |
156 | { |
|
156 | { | |
157 | m_modelSignalsBlock = block; |
|
157 | m_modelSignalsBlock = block; | |
158 | } |
|
158 | } | |
159 |
|
159 | |||
160 | void QPieModelMapperPrivate::blockSeriesSignals(bool block) |
|
160 | void QPieModelMapperPrivate::blockSeriesSignals(bool block) | |
161 | { |
|
161 | { | |
162 | m_seriesSignalsBlock = block; |
|
162 | m_seriesSignalsBlock = block; | |
163 | } |
|
163 | } | |
164 |
|
164 | |||
165 |
|
165 | |||
166 | QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const |
|
166 | QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const | |
167 | { |
|
167 | { | |
168 | if (m_orientation == Qt::Vertical && (index.column() == m_valuesIndex || index.column() == m_labelsIndex)) { |
|
168 | if (m_orientation == Qt::Vertical && (index.column() == m_valuesIndex || index.column() == m_labelsIndex)) { | |
169 | if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) |
|
169 | if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) | |
170 | return m_series->slices().at(index.row() - m_first); |
|
170 | return m_series->slices().at(index.row() - m_first); | |
171 | } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesIndex || index.row() == m_labelsIndex)) { |
|
171 | } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesIndex || index.row() == m_labelsIndex)) { | |
172 | if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count)) |
|
172 | if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count)) | |
173 | return m_series->slices().at(index.column() - m_first); |
|
173 | return m_series->slices().at(index.column() - m_first); | |
174 | } |
|
174 | } | |
175 | return 0; // This part of model has not been mapped to any slice |
|
175 | return 0; // This part of model has not been mapped to any slice | |
176 | } |
|
176 | } | |
177 |
|
177 | |||
178 | QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos) |
|
178 | QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos) | |
179 | { |
|
179 | { | |
180 | if (m_count != -1 && slicePos >= m_count) |
|
180 | if (m_count != -1 && slicePos >= m_count) | |
181 | return QModelIndex(); // invalid |
|
181 | return QModelIndex(); // invalid | |
182 |
|
182 | |||
183 | if (m_orientation == Qt::Vertical) |
|
183 | if (m_orientation == Qt::Vertical) | |
184 | return m_model->index(slicePos + m_first, m_valuesIndex); |
|
184 | return m_model->index(slicePos + m_first, m_valuesIndex); | |
185 | else |
|
185 | else | |
186 | return m_model->index(m_valuesIndex, slicePos + m_first); |
|
186 | return m_model->index(m_valuesIndex, slicePos + m_first); | |
187 | } |
|
187 | } | |
188 |
|
188 | |||
189 | QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos) |
|
189 | QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos) | |
190 | { |
|
190 | { | |
191 | if (m_count != -1 && slicePos >= m_count) |
|
191 | if (m_count != -1 && slicePos >= m_count) | |
192 | return QModelIndex(); // invalid |
|
192 | return QModelIndex(); // invalid | |
193 |
|
193 | |||
194 | if (m_orientation == Qt::Vertical) |
|
194 | if (m_orientation == Qt::Vertical) | |
195 | return m_model->index(slicePos + m_first, m_labelsIndex); |
|
195 | return m_model->index(slicePos + m_first, m_labelsIndex); | |
196 | else |
|
196 | else | |
197 | return m_model->index(m_labelsIndex, slicePos + m_first); |
|
197 | return m_model->index(m_labelsIndex, slicePos + m_first); | |
198 | } |
|
198 | } | |
199 |
|
199 | |||
200 | bool QPieModelMapperPrivate::isLabelIndex(QModelIndex index) const |
|
200 | bool QPieModelMapperPrivate::isLabelIndex(QModelIndex index) const | |
201 | { |
|
201 | { | |
202 | if (m_orientation == Qt::Vertical && index.column() == m_labelsIndex) |
|
202 | if (m_orientation == Qt::Vertical && index.column() == m_labelsIndex) | |
203 | return true; |
|
203 | return true; | |
204 | else if (m_orientation == Qt::Horizontal && index.row() == m_labelsIndex) |
|
204 | else if (m_orientation == Qt::Horizontal && index.row() == m_labelsIndex) | |
205 | return true; |
|
205 | return true; | |
206 |
|
206 | |||
207 | return false; |
|
207 | return false; | |
208 | } |
|
208 | } | |
209 |
|
209 | |||
210 | bool QPieModelMapperPrivate::isValueIndex(QModelIndex index) const |
|
210 | bool QPieModelMapperPrivate::isValueIndex(QModelIndex index) const | |
211 | { |
|
211 | { | |
212 | if (m_orientation == Qt::Vertical && index.column() == m_valuesIndex) |
|
212 | if (m_orientation == Qt::Vertical && index.column() == m_valuesIndex) | |
213 | return true; |
|
213 | return true; | |
214 | else if (m_orientation == Qt::Horizontal && index.row() == m_valuesIndex) |
|
214 | else if (m_orientation == Qt::Horizontal && index.row() == m_valuesIndex) | |
215 | return true; |
|
215 | return true; | |
216 |
|
216 | |||
217 | return false; |
|
217 | return false; | |
218 | } |
|
218 | } | |
219 |
|
219 | |||
220 | void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices) |
|
220 | void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices) | |
221 | { |
|
221 | { | |
222 | if (m_seriesSignalsBlock) |
|
222 | if (m_seriesSignalsBlock) | |
223 | return; |
|
223 | return; | |
224 |
|
224 | |||
225 | if (slices.count() == 0) |
|
225 | if (slices.count() == 0) | |
226 | return; |
|
226 | return; | |
227 |
|
227 | |||
228 | int firstIndex = m_series->slices().indexOf(slices.at(0)); |
|
228 | int firstIndex = m_series->slices().indexOf(slices.at(0)); | |
229 | if (firstIndex == -1) |
|
229 | if (firstIndex == -1) | |
230 | return; |
|
230 | return; | |
231 |
|
231 | |||
232 | if (m_count != -1) |
|
232 | if (m_count != -1) | |
233 | m_count += slices.count(); |
|
233 | m_count += slices.count(); | |
234 |
|
234 | |||
235 | for (int i = firstIndex; i < firstIndex + slices.count(); i++) { |
|
235 | for (int i = firstIndex; i < firstIndex + slices.count(); i++) { | |
236 | m_slices.insert(i, slices.at(i - firstIndex)); |
|
236 | m_slices.insert(i, slices.at(i - firstIndex)); | |
237 | connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged())); |
|
237 | connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged())); | |
238 | connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged())); |
|
238 | connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged())); | |
239 | } |
|
239 | } | |
240 |
|
240 | |||
241 | blockModelSignals(); |
|
241 | blockModelSignals(); | |
242 | if (m_orientation == Qt::Vertical) |
|
242 | if (m_orientation == Qt::Vertical) | |
243 | m_model->insertRows(firstIndex + m_first, slices.count()); |
|
243 | m_model->insertRows(firstIndex + m_first, slices.count()); | |
244 | else |
|
244 | else | |
245 | m_model->insertColumns(firstIndex + m_first, slices.count()); |
|
245 | m_model->insertColumns(firstIndex + m_first, slices.count()); | |
246 |
|
246 | |||
247 | for(int i = firstIndex; i < firstIndex + slices.count(); i++) { |
|
247 | for(int i = firstIndex; i < firstIndex + slices.count(); i++) { | |
248 | m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value()); |
|
248 | m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value()); | |
249 | m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label()); |
|
249 | m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label()); | |
250 | } |
|
250 | } | |
251 | blockModelSignals(false); |
|
251 | blockModelSignals(false); | |
252 | } |
|
252 | } | |
253 |
|
253 | |||
254 | void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices) |
|
254 | void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices) | |
255 | { |
|
255 | { | |
256 | if (m_seriesSignalsBlock) |
|
256 | if (m_seriesSignalsBlock) | |
257 | return; |
|
257 | return; | |
258 |
|
258 | |||
259 | if (slices.count() == 0) |
|
259 | if (slices.count() == 0) | |
260 | return; |
|
260 | return; | |
261 |
|
261 | |||
262 | int firstIndex = m_slices.indexOf(slices.at(0)); |
|
262 | int firstIndex = m_slices.indexOf(slices.at(0)); | |
263 | if (firstIndex == -1) |
|
263 | if (firstIndex == -1) | |
264 | return; |
|
264 | return; | |
265 |
|
265 | |||
266 | if (m_count != -1) |
|
266 | if (m_count != -1) | |
267 | m_count -= slices.count(); |
|
267 | m_count -= slices.count(); | |
268 |
|
268 | |||
269 | for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--) |
|
269 | for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--) | |
270 | m_slices.removeAt(i); |
|
270 | m_slices.removeAt(i); | |
271 |
|
271 | |||
272 | blockModelSignals(); |
|
272 | blockModelSignals(); | |
273 | if (m_orientation == Qt::Vertical) |
|
273 | if (m_orientation == Qt::Vertical) | |
274 | m_model->removeRows(firstIndex + m_first, slices.count()); |
|
274 | m_model->removeRows(firstIndex + m_first, slices.count()); | |
275 | else |
|
275 | else | |
276 | m_model->removeColumns(firstIndex + m_first, slices.count()); |
|
276 | m_model->removeColumns(firstIndex + m_first, slices.count()); | |
277 | blockModelSignals(false); |
|
277 | blockModelSignals(false); | |
278 | } |
|
278 | } | |
279 |
|
279 | |||
280 | void QPieModelMapperPrivate::sliceLabelChanged() |
|
280 | void QPieModelMapperPrivate::sliceLabelChanged() | |
281 | { |
|
281 | { | |
282 | if (m_seriesSignalsBlock) |
|
282 | if (m_seriesSignalsBlock) | |
283 | return; |
|
283 | return; | |
284 |
|
284 | |||
285 | blockModelSignals(); |
|
285 | blockModelSignals(); | |
286 | QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender()); |
|
286 | QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender()); | |
287 | m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label()); |
|
287 | m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label()); | |
288 | blockModelSignals(false); |
|
288 | blockModelSignals(false); | |
289 | } |
|
289 | } | |
290 |
|
290 | |||
291 | void QPieModelMapperPrivate::sliceValueChanged() |
|
291 | void QPieModelMapperPrivate::sliceValueChanged() | |
292 | { |
|
292 | { | |
293 | if (m_seriesSignalsBlock) |
|
293 | if (m_seriesSignalsBlock) | |
294 | return; |
|
294 | return; | |
295 |
|
295 | |||
296 | blockModelSignals(); |
|
296 | blockModelSignals(); | |
297 | QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender()); |
|
297 | QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender()); | |
298 | m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value()); |
|
298 | m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value()); | |
299 | blockModelSignals(false); |
|
299 | blockModelSignals(false); | |
300 | } |
|
300 | } | |
301 |
|
301 | |||
302 | void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) |
|
302 | void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) | |
303 | { |
|
303 | { | |
|
304 | if (m_model == 0 || m_series == 0) | |||
|
305 | return; | |||
|
306 | ||||
304 | if (m_modelSignalsBlock) |
|
307 | if (m_modelSignalsBlock) | |
305 | return; |
|
308 | return; | |
306 |
|
309 | |||
307 | blockSeriesSignals(); |
|
310 | blockSeriesSignals(); | |
308 | QModelIndex index; |
|
311 | QModelIndex index; | |
309 | QPieSlice *slice; |
|
312 | QPieSlice *slice; | |
310 | for (int row = topLeft.row(); row <= bottomRight.row(); row++) { |
|
313 | for (int row = topLeft.row(); row <= bottomRight.row(); row++) { | |
311 | for (int column = topLeft.column(); column <= bottomRight.column(); column++) { |
|
314 | for (int column = topLeft.column(); column <= bottomRight.column(); column++) { | |
312 | index = topLeft.sibling(row, column); |
|
315 | index = topLeft.sibling(row, column); | |
313 | slice = pieSlice(index); |
|
316 | slice = pieSlice(index); | |
314 | if (slice) { |
|
317 | if (slice) { | |
315 | if (isValueIndex(index)) |
|
318 | if (isValueIndex(index)) | |
316 | slice->setValue(m_model->data(index, Qt::DisplayRole).toReal()); |
|
319 | slice->setValue(m_model->data(index, Qt::DisplayRole).toReal()); | |
317 | if (isLabelIndex(index)) |
|
320 | if (isLabelIndex(index)) | |
318 | slice->setLabel(m_model->data(index, Qt::DisplayRole).toString()); |
|
321 | slice->setLabel(m_model->data(index, Qt::DisplayRole).toString()); | |
319 | } |
|
322 | } | |
320 | } |
|
323 | } | |
321 | } |
|
324 | } | |
322 | blockSeriesSignals(false); |
|
325 | blockSeriesSignals(false); | |
323 | } |
|
326 | } | |
324 |
|
327 | |||
325 |
|
328 | |||
326 | void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end) |
|
329 | void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end) | |
327 | { |
|
330 | { | |
328 | Q_UNUSED(parent); |
|
331 | Q_UNUSED(parent); | |
329 | if (m_modelSignalsBlock) |
|
332 | if (m_modelSignalsBlock) | |
330 | return; |
|
333 | return; | |
331 |
|
334 | |||
332 | blockSeriesSignals(); |
|
335 | blockSeriesSignals(); | |
333 | if (m_orientation == Qt::Vertical) |
|
336 | if (m_orientation == Qt::Vertical) | |
334 | insertData(start, end); |
|
337 | insertData(start, end); | |
335 | else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie |
|
338 | else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie | |
336 | initializePieFromModel(); |
|
339 | initializePieFromModel(); | |
337 | blockSeriesSignals(false); |
|
340 | blockSeriesSignals(false); | |
338 | } |
|
341 | } | |
339 |
|
342 | |||
340 | void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end) |
|
343 | void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end) | |
341 | { |
|
344 | { | |
342 | Q_UNUSED(parent); |
|
345 | Q_UNUSED(parent); | |
343 | if (m_modelSignalsBlock) |
|
346 | if (m_modelSignalsBlock) | |
344 | return; |
|
347 | return; | |
345 |
|
348 | |||
346 | blockSeriesSignals(); |
|
349 | blockSeriesSignals(); | |
347 | if (m_orientation == Qt::Vertical) |
|
350 | if (m_orientation == Qt::Vertical) | |
348 | removeData(start, end); |
|
351 | removeData(start, end); | |
349 | else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie |
|
352 | else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie | |
350 | initializePieFromModel(); |
|
353 | initializePieFromModel(); | |
351 | blockSeriesSignals(false); |
|
354 | blockSeriesSignals(false); | |
352 | } |
|
355 | } | |
353 |
|
356 | |||
354 | void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end) |
|
357 | void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end) | |
355 | { |
|
358 | { | |
356 | Q_UNUSED(parent); |
|
359 | Q_UNUSED(parent); | |
357 | if (m_modelSignalsBlock) |
|
360 | if (m_modelSignalsBlock) | |
358 | return; |
|
361 | return; | |
359 |
|
362 | |||
360 | blockSeriesSignals(); |
|
363 | blockSeriesSignals(); | |
361 | if (m_orientation == Qt::Horizontal) |
|
364 | if (m_orientation == Qt::Horizontal) | |
362 | insertData(start, end); |
|
365 | insertData(start, end); | |
363 | else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie |
|
366 | else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie | |
364 | initializePieFromModel(); |
|
367 | initializePieFromModel(); | |
365 | blockSeriesSignals(false); |
|
368 | blockSeriesSignals(false); | |
366 | } |
|
369 | } | |
367 |
|
370 | |||
368 | void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end) |
|
371 | void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end) | |
369 | { |
|
372 | { | |
370 | Q_UNUSED(parent); |
|
373 | Q_UNUSED(parent); | |
371 | if (m_modelSignalsBlock) |
|
374 | if (m_modelSignalsBlock) | |
372 | return; |
|
375 | return; | |
373 |
|
376 | |||
374 | blockSeriesSignals(); |
|
377 | blockSeriesSignals(); | |
375 | if (m_orientation == Qt::Horizontal) |
|
378 | if (m_orientation == Qt::Horizontal) | |
376 | removeData(start, end); |
|
379 | removeData(start, end); | |
377 | else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie |
|
380 | else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie | |
378 | initializePieFromModel(); |
|
381 | initializePieFromModel(); | |
379 | blockSeriesSignals(false); |
|
382 | blockSeriesSignals(false); | |
380 | } |
|
383 | } | |
381 |
|
384 | |||
382 | void QPieModelMapperPrivate::insertData(int start, int end) |
|
385 | void QPieModelMapperPrivate::insertData(int start, int end) | |
383 | { |
|
386 | { | |
384 | if (m_model == 0 || m_series == 0) |
|
387 | if (m_model == 0 || m_series == 0) | |
385 | return; |
|
388 | return; | |
386 |
|
389 | |||
387 | if (m_count != -1 && start >= m_first + m_count) { |
|
390 | if (m_count != -1 && start >= m_first + m_count) { | |
388 | return; |
|
391 | return; | |
389 | } else { |
|
392 | } else { | |
390 | int addedCount = end - start + 1; |
|
393 | int addedCount = end - start + 1; | |
391 | if (m_count != -1 && addedCount > m_count) |
|
394 | if (m_count != -1 && addedCount > m_count) | |
392 | addedCount = m_count; |
|
395 | addedCount = m_count; | |
393 | int first = qMax(start, m_first); |
|
396 | int first = qMax(start, m_first); | |
394 | int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1); |
|
397 | int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1); | |
395 | for (int i = first; i <= last; i++) { |
|
398 | for (int i = first; i <= last; i++) { | |
396 | QPieSlice *slice = new QPieSlice; |
|
399 | QPieSlice *slice = new QPieSlice; | |
397 | slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble()); |
|
400 | slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble()); | |
398 | slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString()); |
|
401 | slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString()); | |
399 | slice->setLabelVisible(); |
|
402 | slice->setLabelVisible(); | |
400 | connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged())); |
|
403 | connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged())); | |
401 | connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged())); |
|
404 | connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged())); | |
402 | m_series->insert(i - m_first, slice); |
|
405 | m_series->insert(i - m_first, slice); | |
403 | m_slices.insert(i - m_first, slice); |
|
406 | m_slices.insert(i - m_first, slice); | |
404 | } |
|
407 | } | |
405 |
|
408 | |||
406 | // remove excess of slices (abouve m_count) |
|
409 | // remove excess of slices (abouve m_count) | |
407 | if (m_count != -1 && m_series->slices().size() > m_count) |
|
410 | if (m_count != -1 && m_series->slices().size() > m_count) | |
408 | for (int i = m_series->slices().size() - 1; i >= m_count; i--) { |
|
411 | for (int i = m_series->slices().size() - 1; i >= m_count; i--) { | |
409 | m_series->remove(m_series->slices().at(i)); |
|
412 | m_series->remove(m_series->slices().at(i)); | |
410 | m_slices.removeAt(i); |
|
413 | m_slices.removeAt(i); | |
411 | } |
|
414 | } | |
412 | } |
|
415 | } | |
413 | } |
|
416 | } | |
414 |
|
417 | |||
415 | void QPieModelMapperPrivate::removeData(int start, int end) |
|
418 | void QPieModelMapperPrivate::removeData(int start, int end) | |
416 | { |
|
419 | { | |
417 | if (m_model == 0 || m_series == 0) |
|
420 | if (m_model == 0 || m_series == 0) | |
418 | return; |
|
421 | return; | |
419 |
|
422 | |||
420 | int removedCount = end - start + 1; |
|
423 | int removedCount = end - start + 1; | |
421 | if (m_count != -1 && start >= m_first + m_count) { |
|
424 | if (m_count != -1 && start >= m_first + m_count) { | |
422 | return; |
|
425 | return; | |
423 | } else { |
|
426 | } else { | |
424 | int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed |
|
427 | int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed | |
425 | int first = qMax(start, m_first); // get the index of the first item that will be removed. |
|
428 | int first = qMax(start, m_first); // get the index of the first item that will be removed. | |
426 | int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed. |
|
429 | int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed. | |
427 | for (int i = last; i >= first; i--) { |
|
430 | for (int i = last; i >= first; i--) { | |
428 | m_series->remove(m_series->slices().at(i - m_first)); |
|
431 | m_series->remove(m_series->slices().at(i - m_first)); | |
429 | m_slices.removeAt(i - m_first); |
|
432 | m_slices.removeAt(i - m_first); | |
430 | } |
|
433 | } | |
431 |
|
434 | |||
432 | if (m_count != -1) { |
|
435 | if (m_count != -1) { | |
433 | int itemsAvailable; // check how many are available to be added |
|
436 | int itemsAvailable; // check how many are available to be added | |
434 | if (m_orientation == Qt::Vertical) |
|
437 | if (m_orientation == Qt::Vertical) | |
435 | itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size(); |
|
438 | itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size(); | |
436 | else |
|
439 | else | |
437 | itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size(); |
|
440 | itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size(); | |
438 | int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled. |
|
441 | int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled. | |
439 | int currentSize = m_series->slices().size(); |
|
442 | int currentSize = m_series->slices().size(); | |
440 | if (toBeAdded > 0) |
|
443 | if (toBeAdded > 0) | |
441 | for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) { |
|
444 | for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) { | |
442 | QPieSlice *slice = new QPieSlice; |
|
445 | QPieSlice *slice = new QPieSlice; | |
443 | if (m_orientation == Qt::Vertical) { |
|
446 | if (m_orientation == Qt::Vertical) { | |
444 | slice->setValue(m_model->data(m_model->index(i + m_first, m_valuesIndex), Qt::DisplayRole).toDouble()); |
|
447 | slice->setValue(m_model->data(m_model->index(i + m_first, m_valuesIndex), Qt::DisplayRole).toDouble()); | |
445 | slice->setLabel(m_model->data(m_model->index(i + m_first, m_labelsIndex), Qt::DisplayRole).toString()); |
|
448 | slice->setLabel(m_model->data(m_model->index(i + m_first, m_labelsIndex), Qt::DisplayRole).toString()); | |
446 | } else { |
|
449 | } else { | |
447 | slice->setValue(m_model->data(m_model->index(m_valuesIndex, i + m_first), Qt::DisplayRole).toDouble()); |
|
450 | slice->setValue(m_model->data(m_model->index(m_valuesIndex, i + m_first), Qt::DisplayRole).toDouble()); | |
448 | slice->setLabel(m_model->data(m_model->index(m_labelsIndex, i + m_first), Qt::DisplayRole).toString()); |
|
451 | slice->setLabel(m_model->data(m_model->index(m_labelsIndex, i + m_first), Qt::DisplayRole).toString()); | |
449 | } |
|
452 | } | |
450 | slice->setLabelVisible(); |
|
453 | slice->setLabelVisible(); | |
451 | m_series->insert(i, slice); |
|
454 | m_series->insert(i, slice); | |
452 | m_slices.insert(i, slice); |
|
455 | m_slices.insert(i, slice); | |
453 | } |
|
456 | } | |
454 | } |
|
457 | } | |
455 | } |
|
458 | } | |
456 | } |
|
459 | } | |
457 |
|
460 | |||
458 | void QPieModelMapperPrivate::initializePieFromModel() |
|
461 | void QPieModelMapperPrivate::initializePieFromModel() | |
459 | { |
|
462 | { | |
460 | if (m_model == 0 || m_series == 0) |
|
463 | if (m_model == 0 || m_series == 0) | |
461 | return; |
|
464 | return; | |
462 |
|
465 | |||
463 | blockSeriesSignals(); |
|
466 | blockSeriesSignals(); | |
464 | // clear current content |
|
467 | // clear current content | |
465 | m_series->clear(); |
|
468 | m_series->clear(); | |
466 | m_slices.clear(); |
|
469 | m_slices.clear(); | |
467 |
|
470 | |||
468 | // create the initial slices set |
|
471 | // create the initial slices set | |
469 | int slicePos = 0; |
|
472 | int slicePos = 0; | |
470 | QModelIndex valueIndex = valueModelIndex(slicePos); |
|
473 | QModelIndex valueIndex = valueModelIndex(slicePos); | |
471 | QModelIndex labelIndex = labelModelIndex(slicePos); |
|
474 | QModelIndex labelIndex = labelModelIndex(slicePos); | |
472 | while (valueIndex.isValid() && labelIndex.isValid()) { |
|
475 | while (valueIndex.isValid() && labelIndex.isValid()) { | |
473 | QPieSlice *slice = new QPieSlice; |
|
476 | QPieSlice *slice = new QPieSlice; | |
474 | slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString()); |
|
477 | slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString()); | |
475 | slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble()); |
|
478 | slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble()); | |
476 | connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged())); |
|
479 | connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged())); | |
477 | connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged())); |
|
480 | connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged())); | |
478 | m_series->append(slice); |
|
481 | m_series->append(slice); | |
479 | m_slices.append(slice); |
|
482 | m_slices.append(slice); | |
480 | // m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble()); |
|
483 | // m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble()); | |
481 | slicePos++; |
|
484 | slicePos++; | |
482 | valueIndex = valueModelIndex(slicePos); |
|
485 | valueIndex = valueModelIndex(slicePos); | |
483 | labelIndex = labelModelIndex(slicePos); |
|
486 | labelIndex = labelModelIndex(slicePos); | |
484 | } |
|
487 | } | |
485 | m_series->setLabelsVisible(true); |
|
488 | m_series->setLabelsVisible(true); | |
486 | blockSeriesSignals(false); |
|
489 | blockSeriesSignals(false); | |
487 | } |
|
490 | } | |
488 |
|
491 | |||
489 | #include "moc_qpiemodelmapper_p.cpp" |
|
492 | #include "moc_qpiemodelmapper_p.cpp" | |
490 | #include "moc_qpiemodelmapper.cpp" |
|
493 | #include "moc_qpiemodelmapper.cpp" | |
491 |
|
494 | |||
492 | QTCOMMERCIALCHART_END_NAMESPACE |
|
495 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now