##// END OF EJS Templates
Minor fix to pie model mapper
Marek Rosa -
r1330:80cc71f77e52
parent child
Show More
@@ -1,47 +1,47
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QHPIEMODELMAPPER_H
21 #ifndef QHPIEMODELMAPPER_H
22 #define QHPIEMODELMAPPER_H
22 #define QHPIEMODELMAPPER_H
23
23
24 #include "qpiemodelmapper.h"
24 #include "qpiemodelmapper.h"
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
27
28 class QTCOMMERCIALCHART_EXPORT QHPieModelMapper : public QPieModelMapper
28 class QTCOMMERCIALCHART_EXPORT QHPieModelMapper : public QPieModelMapper
29 {
29 {
30 Q_OBJECT
30 Q_OBJECT
31 Q_PROPERTY(int valuesRow READ valuesRow WRITE setValuesRow)
31 Q_PROPERTY(int valuesRow READ valuesRow WRITE setValuesRow)
32 Q_PROPERTY(int labelsRow READ labelsRow WRITE setLabelsRow)
32 Q_PROPERTY(int labelsRow READ labelsRow WRITE setLabelsRow)
33
33
34 public:
34 public:
35 QHPieModelMapper(QObject *parent = 0);
35 explicit QHPieModelMapper(QObject *parent = 0);
36
36
37 int valuesRow() const;
37 int valuesRow() const;
38 void setValuesRow(int valuesRow);
38 void setValuesRow(int valuesRow);
39
39
40 int labelsRow() const;
40 int labelsRow() const;
41 void setLabelsRow(int labelsRow);
41 void setLabelsRow(int labelsRow);
42
42
43 };
43 };
44
44
45 QTCOMMERCIALCHART_END_NAMESPACE
45 QTCOMMERCIALCHART_END_NAMESPACE
46
46
47 #endif // QHPIEMODELMAPPER_H
47 #endif // QHPIEMODELMAPPER_H
@@ -1,535 +1,534
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qpiemodelmapper_p.h"
21 #include "qpiemodelmapper_p.h"
22 #include "qpiemodelmapper.h"
22 #include "qpiemodelmapper.h"
23 #include "qpieseries.h"
23 #include "qpieseries.h"
24 #include "qpieslice.h"
24 #include "qpieslice.h"
25 #include <QAbstractItemModel>
25 #include <QAbstractItemModel>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 QPieModelMapper::QPieModelMapper(QObject *parent) :
29 QPieModelMapper::QPieModelMapper(QObject *parent) :
30 QObject(parent),
30 QObject(parent),
31 d_ptr(new QPieModelMapperPrivate(this))
31 d_ptr(new QPieModelMapperPrivate(this))
32 {
32 {
33 }
33 }
34
34
35 QPieModelMapper::~QPieModelMapper()
35 QPieModelMapper::~QPieModelMapper()
36 {
36 {
37 // Q_D(QPieModelMapper);
37 // Q_D(QPieModelMapper);
38 // disconnect(d->m_model, 0, d, 0);
38 // disconnect(d->m_model, 0, d, 0);
39 }
39 }
40
40
41 QAbstractItemModel* QPieModelMapper::model() const
41 QAbstractItemModel* QPieModelMapper::model() const
42 {
42 {
43 Q_D(const QPieModelMapper);
43 Q_D(const QPieModelMapper);
44 return d->m_model;
44 return d->m_model;
45 }
45 }
46
46
47 void QPieModelMapper::setModel(QAbstractItemModel *model)
47 void QPieModelMapper::setModel(QAbstractItemModel *model)
48 {
48 {
49 if (model == 0)
49 if (model == 0)
50 return;
50 return;
51
51
52 Q_D(QPieModelMapper);
52 Q_D(QPieModelMapper);
53 if (d->m_model) {
53 if (d->m_model) {
54 disconnect(d->m_model, 0, d, 0);
54 disconnect(d->m_model, 0, d, 0);
55 }
55 }
56
56
57 d->m_model = model;
57 d->m_model = model;
58 d->initializePieFromModel();
58 d->initializePieFromModel();
59 // connect signals from the model
59 // connect signals from the model
60 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
60 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
61 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
61 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
62 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
62 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
63 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
63 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
64 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
64 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
65 }
65 }
66
66
67 QPieSeries* QPieModelMapper::series() const
67 QPieSeries* QPieModelMapper::series() const
68 {
68 {
69 Q_D(const QPieModelMapper);
69 Q_D(const QPieModelMapper);
70 return d->m_series;
70 return d->m_series;
71 }
71 }
72
72
73 void QPieModelMapper::setSeries(QPieSeries *series)
73 void QPieModelMapper::setSeries(QPieSeries *series)
74 {
74 {
75 Q_D(QPieModelMapper);
75 Q_D(QPieModelMapper);
76 if (d->m_series) {
76 if (d->m_series) {
77 disconnect(d->m_series, 0, d, 0);
77 disconnect(d->m_series, 0, d, 0);
78 }
78 }
79
79
80 if (series == 0)
80 if (series == 0)
81 return;
81 return;
82
82
83 d->m_series = series;
83 d->m_series = series;
84 d->initializePieFromModel();
84 d->initializePieFromModel();
85 // connect the signals from the series
85 // connect the signals from the series
86 connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>)));
86 connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>)));
87 connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>)));
87 connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>)));
88 }
88 }
89
89
90 int QPieModelMapper::first() const
90 int QPieModelMapper::first() const
91 {
91 {
92 Q_D(const QPieModelMapper);
92 Q_D(const QPieModelMapper);
93 return d->m_first;
93 return d->m_first;
94 }
94 }
95
95
96 void QPieModelMapper::setFirst(int first)
96 void QPieModelMapper::setFirst(int first)
97 {
97 {
98 Q_D(QPieModelMapper);
98 Q_D(QPieModelMapper);
99 d->m_first = qMax(first, 0);
99 d->m_first = qMax(first, 0);
100 d->initializePieFromModel();
100 d->initializePieFromModel();
101 }
101 }
102
102
103 int QPieModelMapper::count() const
103 int QPieModelMapper::count() const
104 {
104 {
105 Q_D(const QPieModelMapper);
105 Q_D(const QPieModelMapper);
106 return d->m_count;
106 return d->m_count;
107 }
107 }
108
108
109 void QPieModelMapper::setCount(int count)
109 void QPieModelMapper::setCount(int count)
110 {
110 {
111 Q_D(QPieModelMapper);
111 Q_D(QPieModelMapper);
112 d->m_count = qMax(count, -1);
112 d->m_count = qMax(count, -1);
113 d->initializePieFromModel();
113 d->initializePieFromModel();
114 }
114 }
115
115
116 Qt::Orientation QPieModelMapper::orientation() const
116 Qt::Orientation QPieModelMapper::orientation() const
117 {
117 {
118 Q_D(const QPieModelMapper);
118 Q_D(const QPieModelMapper);
119 return d->m_orientation;
119 return d->m_orientation;
120 }
120 }
121
121
122 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
122 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
123 {
123 {
124 Q_D(QPieModelMapper);
124 Q_D(QPieModelMapper);
125 d->m_orientation = orientation;
125 d->m_orientation = orientation;
126 d->initializePieFromModel();
126 d->initializePieFromModel();
127 }
127 }
128
128
129 int QPieModelMapper::valuesSection() const
129 int QPieModelMapper::valuesSection() const
130 {
130 {
131 Q_D(const QPieModelMapper);
131 Q_D(const QPieModelMapper);
132 return d->m_valuesSection;
132 return d->m_valuesSection;
133 }
133 }
134
134
135 void QPieModelMapper::setValuesSection(int valuesSection)
135 void QPieModelMapper::setValuesSection(int valuesSection)
136 {
136 {
137 Q_D(QPieModelMapper);
137 Q_D(QPieModelMapper);
138 d->m_valuesSection = qMax(-1, valuesSection);
138 d->m_valuesSection = qMax(-1, valuesSection);
139 d->initializePieFromModel();
139 d->initializePieFromModel();
140 }
140 }
141
141
142 int QPieModelMapper::labelsSection() const
142 int QPieModelMapper::labelsSection() const
143 {
143 {
144 Q_D(const QPieModelMapper);
144 Q_D(const QPieModelMapper);
145 return d->m_labelsSection;
145 return d->m_labelsSection;
146 }
146 }
147
147
148 void QPieModelMapper::setLabelsSection(int labelsSection)
148 void QPieModelMapper::setLabelsSection(int labelsSection)
149 {
149 {
150 Q_D(QPieModelMapper);
150 Q_D(QPieModelMapper);
151 d->m_labelsSection = qMax(-1, labelsSection);
151 d->m_labelsSection = qMax(-1, labelsSection);
152 d->initializePieFromModel();
152 d->initializePieFromModel();
153 }
153 }
154
154
155 void QPieModelMapper::reset()
155 void QPieModelMapper::reset()
156 {
156 {
157 Q_D(QPieModelMapper);
157 Q_D(QPieModelMapper);
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;
161 d->m_valuesSection = -1;
160 d->m_valuesSection = -1;
162 d->m_labelsSection = -1;
161 d->m_labelsSection = -1;
163 }
162 }
164
163
165 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
164 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
166
165
167 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
166 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
168 m_series(0),
167 m_series(0),
169 m_model(0),
168 m_model(0),
170 m_first(0),
169 m_first(0),
171 m_count(-1),
170 m_count(-1),
172 m_orientation(Qt::Vertical),
171 m_orientation(Qt::Vertical),
173 m_valuesSection(-1),
172 m_valuesSection(-1),
174 m_labelsSection(-1),
173 m_labelsSection(-1),
175 m_seriesSignalsBlock(false),
174 m_seriesSignalsBlock(false),
176 m_modelSignalsBlock(false),
175 m_modelSignalsBlock(false),
177 q_ptr(q)
176 q_ptr(q)
178 {
177 {
179 }
178 }
180
179
181 void QPieModelMapperPrivate::blockModelSignals(bool block)
180 void QPieModelMapperPrivate::blockModelSignals(bool block)
182 {
181 {
183 m_modelSignalsBlock = block;
182 m_modelSignalsBlock = block;
184 }
183 }
185
184
186 void QPieModelMapperPrivate::blockSeriesSignals(bool block)
185 void QPieModelMapperPrivate::blockSeriesSignals(bool block)
187 {
186 {
188 m_seriesSignalsBlock = block;
187 m_seriesSignalsBlock = block;
189 }
188 }
190
189
191
190
192 QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const
191 QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const
193 {
192 {
194 if (!index.isValid())
193 if (!index.isValid())
195 return 0; // index is invalid
194 return 0; // index is invalid
196
195
197 if (m_orientation == Qt::Vertical && (index.column() == m_valuesSection || index.column() == m_labelsSection)) {
196 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)) {
197 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())
198 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);
199 return m_series->slices().at(index.row() - m_first);
201 else
200 else
202 return 0;
201 return 0;
203 }
202 }
204 } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesSection || index.row() == m_labelsSection)) {
203 } 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)) {
204 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())
205 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);
206 return m_series->slices().at(index.column() - m_first);
208 else
207 else
209 return 0;
208 return 0;
210 }
209 }
211 }
210 }
212 return 0; // This part of model has not been mapped to any slice
211 return 0; // This part of model has not been mapped to any slice
213 }
212 }
214
213
215 QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos)
214 QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos)
216 {
215 {
217 if (m_count != -1 && slicePos >= m_count)
216 if (m_count != -1 && slicePos >= m_count)
218 return QModelIndex(); // invalid
217 return QModelIndex(); // invalid
219
218
220 if (m_orientation == Qt::Vertical)
219 if (m_orientation == Qt::Vertical)
221 return m_model->index(slicePos + m_first, m_valuesSection);
220 return m_model->index(slicePos + m_first, m_valuesSection);
222 else
221 else
223 return m_model->index(m_valuesSection, slicePos + m_first);
222 return m_model->index(m_valuesSection, slicePos + m_first);
224 }
223 }
225
224
226 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
225 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
227 {
226 {
228 if (m_count != -1 && slicePos >= m_count)
227 if (m_count != -1 && slicePos >= m_count)
229 return QModelIndex(); // invalid
228 return QModelIndex(); // invalid
230
229
231 if (m_orientation == Qt::Vertical)
230 if (m_orientation == Qt::Vertical)
232 return m_model->index(slicePos + m_first, m_labelsSection);
231 return m_model->index(slicePos + m_first, m_labelsSection);
233 else
232 else
234 return m_model->index(m_labelsSection, slicePos + m_first);
233 return m_model->index(m_labelsSection, slicePos + m_first);
235 }
234 }
236
235
237 bool QPieModelMapperPrivate::isLabelIndex(QModelIndex index) const
236 bool QPieModelMapperPrivate::isLabelIndex(QModelIndex index) const
238 {
237 {
239 if (m_orientation == Qt::Vertical && index.column() == m_labelsSection)
238 if (m_orientation == Qt::Vertical && index.column() == m_labelsSection)
240 return true;
239 return true;
241 else if (m_orientation == Qt::Horizontal && index.row() == m_labelsSection)
240 else if (m_orientation == Qt::Horizontal && index.row() == m_labelsSection)
242 return true;
241 return true;
243
242
244 return false;
243 return false;
245 }
244 }
246
245
247 bool QPieModelMapperPrivate::isValueIndex(QModelIndex index) const
246 bool QPieModelMapperPrivate::isValueIndex(QModelIndex index) const
248 {
247 {
249 if (m_orientation == Qt::Vertical && index.column() == m_valuesSection)
248 if (m_orientation == Qt::Vertical && index.column() == m_valuesSection)
250 return true;
249 return true;
251 else if (m_orientation == Qt::Horizontal && index.row() == m_valuesSection)
250 else if (m_orientation == Qt::Horizontal && index.row() == m_valuesSection)
252 return true;
251 return true;
253
252
254 return false;
253 return false;
255 }
254 }
256
255
257 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
256 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
258 {
257 {
259 if (m_seriesSignalsBlock)
258 if (m_seriesSignalsBlock)
260 return;
259 return;
261
260
262 if (slices.count() == 0)
261 if (slices.count() == 0)
263 return;
262 return;
264
263
265 int firstIndex = m_series->slices().indexOf(slices.at(0));
264 int firstIndex = m_series->slices().indexOf(slices.at(0));
266 if (firstIndex == -1)
265 if (firstIndex == -1)
267 return;
266 return;
268
267
269 if (m_count != -1)
268 if (m_count != -1)
270 m_count += slices.count();
269 m_count += slices.count();
271
270
272 for (int i = firstIndex; i < firstIndex + slices.count(); i++) {
271 for (int i = firstIndex; i < firstIndex + slices.count(); i++) {
273 m_slices.insert(i, slices.at(i - firstIndex));
272 m_slices.insert(i, slices.at(i - firstIndex));
274 connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
273 connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
275 connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
274 connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
276 }
275 }
277
276
278 blockModelSignals();
277 blockModelSignals();
279 if (m_orientation == Qt::Vertical)
278 if (m_orientation == Qt::Vertical)
280 m_model->insertRows(firstIndex + m_first, slices.count());
279 m_model->insertRows(firstIndex + m_first, slices.count());
281 else
280 else
282 m_model->insertColumns(firstIndex + m_first, slices.count());
281 m_model->insertColumns(firstIndex + m_first, slices.count());
283
282
284 for(int i = firstIndex; i < firstIndex + slices.count(); i++) {
283 for(int i = firstIndex; i < firstIndex + slices.count(); i++) {
285 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
284 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
286 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
285 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
287 }
286 }
288 blockModelSignals(false);
287 blockModelSignals(false);
289 }
288 }
290
289
291 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
290 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
292 {
291 {
293 if (m_seriesSignalsBlock)
292 if (m_seriesSignalsBlock)
294 return;
293 return;
295
294
296 if (slices.count() == 0)
295 if (slices.count() == 0)
297 return;
296 return;
298
297
299 int firstIndex = m_slices.indexOf(slices.at(0));
298 int firstIndex = m_slices.indexOf(slices.at(0));
300 if (firstIndex == -1)
299 if (firstIndex == -1)
301 return;
300 return;
302
301
303 if (m_count != -1)
302 if (m_count != -1)
304 m_count -= slices.count();
303 m_count -= slices.count();
305
304
306 for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--)
305 for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--)
307 m_slices.removeAt(i);
306 m_slices.removeAt(i);
308
307
309 blockModelSignals();
308 blockModelSignals();
310 if (m_orientation == Qt::Vertical)
309 if (m_orientation == Qt::Vertical)
311 m_model->removeRows(firstIndex + m_first, slices.count());
310 m_model->removeRows(firstIndex + m_first, slices.count());
312 else
311 else
313 m_model->removeColumns(firstIndex + m_first, slices.count());
312 m_model->removeColumns(firstIndex + m_first, slices.count());
314 blockModelSignals(false);
313 blockModelSignals(false);
315 }
314 }
316
315
317 void QPieModelMapperPrivate::sliceLabelChanged()
316 void QPieModelMapperPrivate::sliceLabelChanged()
318 {
317 {
319 if (m_seriesSignalsBlock)
318 if (m_seriesSignalsBlock)
320 return;
319 return;
321
320
322 blockModelSignals();
321 blockModelSignals();
323 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
322 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
324 m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label());
323 m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label());
325 blockModelSignals(false);
324 blockModelSignals(false);
326 }
325 }
327
326
328 void QPieModelMapperPrivate::sliceValueChanged()
327 void QPieModelMapperPrivate::sliceValueChanged()
329 {
328 {
330 if (m_seriesSignalsBlock)
329 if (m_seriesSignalsBlock)
331 return;
330 return;
332
331
333 blockModelSignals();
332 blockModelSignals();
334 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
333 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
335 m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value());
334 m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value());
336 blockModelSignals(false);
335 blockModelSignals(false);
337 }
336 }
338
337
339 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
338 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
340 {
339 {
341 if (m_model == 0 || m_series == 0)
340 if (m_model == 0 || m_series == 0)
342 return;
341 return;
343
342
344 if (m_modelSignalsBlock)
343 if (m_modelSignalsBlock)
345 return;
344 return;
346
345
347 blockSeriesSignals();
346 blockSeriesSignals();
348 QModelIndex index;
347 QModelIndex index;
349 QPieSlice *slice;
348 QPieSlice *slice;
350 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
349 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
351 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
350 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
352 index = topLeft.sibling(row, column);
351 index = topLeft.sibling(row, column);
353 slice = pieSlice(index);
352 slice = pieSlice(index);
354 if (slice) {
353 if (slice) {
355 if (isValueIndex(index))
354 if (isValueIndex(index))
356 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
355 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
357 if (isLabelIndex(index))
356 if (isLabelIndex(index))
358 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
357 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
359 }
358 }
360 }
359 }
361 }
360 }
362 blockSeriesSignals(false);
361 blockSeriesSignals(false);
363 }
362 }
364
363
365
364
366 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
365 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
367 {
366 {
368 Q_UNUSED(parent);
367 Q_UNUSED(parent);
369 if (m_modelSignalsBlock)
368 if (m_modelSignalsBlock)
370 return;
369 return;
371
370
372 blockSeriesSignals();
371 blockSeriesSignals();
373 if (m_orientation == Qt::Vertical)
372 if (m_orientation == Qt::Vertical)
374 insertData(start, end);
373 insertData(start, end);
375 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
374 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
376 initializePieFromModel();
375 initializePieFromModel();
377 blockSeriesSignals(false);
376 blockSeriesSignals(false);
378 }
377 }
379
378
380 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
379 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
381 {
380 {
382 Q_UNUSED(parent);
381 Q_UNUSED(parent);
383 if (m_modelSignalsBlock)
382 if (m_modelSignalsBlock)
384 return;
383 return;
385
384
386 blockSeriesSignals();
385 blockSeriesSignals();
387 if (m_orientation == Qt::Vertical)
386 if (m_orientation == Qt::Vertical)
388 removeData(start, end);
387 removeData(start, end);
389 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
388 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
390 initializePieFromModel();
389 initializePieFromModel();
391 blockSeriesSignals(false);
390 blockSeriesSignals(false);
392 }
391 }
393
392
394 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
393 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
395 {
394 {
396 Q_UNUSED(parent);
395 Q_UNUSED(parent);
397 if (m_modelSignalsBlock)
396 if (m_modelSignalsBlock)
398 return;
397 return;
399
398
400 blockSeriesSignals();
399 blockSeriesSignals();
401 if (m_orientation == Qt::Horizontal)
400 if (m_orientation == Qt::Horizontal)
402 insertData(start, end);
401 insertData(start, end);
403 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
402 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
404 initializePieFromModel();
403 initializePieFromModel();
405 blockSeriesSignals(false);
404 blockSeriesSignals(false);
406 }
405 }
407
406
408 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
407 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
409 {
408 {
410 Q_UNUSED(parent);
409 Q_UNUSED(parent);
411 if (m_modelSignalsBlock)
410 if (m_modelSignalsBlock)
412 return;
411 return;
413
412
414 blockSeriesSignals();
413 blockSeriesSignals();
415 if (m_orientation == Qt::Horizontal)
414 if (m_orientation == Qt::Horizontal)
416 removeData(start, end);
415 removeData(start, end);
417 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
416 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
418 initializePieFromModel();
417 initializePieFromModel();
419 blockSeriesSignals(false);
418 blockSeriesSignals(false);
420 }
419 }
421
420
422 void QPieModelMapperPrivate::insertData(int start, int end)
421 void QPieModelMapperPrivate::insertData(int start, int end)
423 {
422 {
424 if (m_model == 0 || m_series == 0)
423 if (m_model == 0 || m_series == 0)
425 return;
424 return;
426
425
427 if (m_count != -1 && start >= m_first + m_count) {
426 if (m_count != -1 && start >= m_first + m_count) {
428 return;
427 return;
429 } else {
428 } else {
430 int addedCount = end - start + 1;
429 int addedCount = end - start + 1;
431 if (m_count != -1 && addedCount > m_count)
430 if (m_count != -1 && addedCount > m_count)
432 addedCount = m_count;
431 addedCount = m_count;
433 int first = qMax(start, m_first);
432 int first = qMax(start, m_first);
434 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
433 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
435 for (int i = first; i <= last; i++) {
434 for (int i = first; i <= last; i++) {
436 QModelIndex valueIndex = valueModelIndex(i - m_first);
435 QModelIndex valueIndex = valueModelIndex(i - m_first);
437 QModelIndex labelIndex = labelModelIndex(i - m_first);
436 QModelIndex labelIndex = labelModelIndex(i - m_first);
438 if (valueIndex.isValid() && labelIndex.isValid()) {
437 if (valueIndex.isValid() && labelIndex.isValid()) {
439 QPieSlice *slice = new QPieSlice;
438 QPieSlice *slice = new QPieSlice;
440 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
439 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
441 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
440 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
442 slice->setLabelVisible();
441 slice->setLabelVisible();
443 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
442 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
444 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
443 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
445 m_series->insert(i - m_first, slice);
444 m_series->insert(i - m_first, slice);
446 m_slices.insert(i - m_first, slice);
445 m_slices.insert(i - m_first, slice);
447 }
446 }
448 }
447 }
449
448
450 // remove excess of slices (abouve m_count)
449 // remove excess of slices (abouve m_count)
451 if (m_count != -1 && m_series->slices().size() > m_count)
450 if (m_count != -1 && m_series->slices().size() > m_count)
452 for (int i = m_series->slices().size() - 1; i >= m_count; i--) {
451 for (int i = m_series->slices().size() - 1; i >= m_count; i--) {
453 m_series->remove(m_series->slices().at(i));
452 m_series->remove(m_series->slices().at(i));
454 m_slices.removeAt(i);
453 m_slices.removeAt(i);
455 }
454 }
456 }
455 }
457 }
456 }
458
457
459 void QPieModelMapperPrivate::removeData(int start, int end)
458 void QPieModelMapperPrivate::removeData(int start, int end)
460 {
459 {
461 if (m_model == 0 || m_series == 0)
460 if (m_model == 0 || m_series == 0)
462 return;
461 return;
463
462
464 int removedCount = end - start + 1;
463 int removedCount = end - start + 1;
465 if (m_count != -1 && start >= m_first + m_count) {
464 if (m_count != -1 && start >= m_first + m_count) {
466 return;
465 return;
467 } else {
466 } else {
468 int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed
467 int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed
469 int first = qMax(start, m_first); // get the index of the first item that will be removed.
468 int first = qMax(start, m_first); // get the index of the first item that will be removed.
470 int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed.
469 int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed.
471 for (int i = last; i >= first; i--) {
470 for (int i = last; i >= first; i--) {
472 m_series->remove(m_series->slices().at(i - m_first));
471 m_series->remove(m_series->slices().at(i - m_first));
473 m_slices.removeAt(i - m_first);
472 m_slices.removeAt(i - m_first);
474 }
473 }
475
474
476 if (m_count != -1) {
475 if (m_count != -1) {
477 int itemsAvailable; // check how many are available to be added
476 int itemsAvailable; // check how many are available to be added
478 if (m_orientation == Qt::Vertical)
477 if (m_orientation == Qt::Vertical)
479 itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size();
478 itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size();
480 else
479 else
481 itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size();
480 itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size();
482 int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled.
481 int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled.
483 int currentSize = m_series->slices().size();
482 int currentSize = m_series->slices().size();
484 if (toBeAdded > 0)
483 if (toBeAdded > 0)
485 for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) {
484 for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) {
486 QModelIndex valueIndex = valueModelIndex(i - m_first);
485 QModelIndex valueIndex = valueModelIndex(i - m_first);
487 QModelIndex labelIndex = labelModelIndex(i - m_first);
486 QModelIndex labelIndex = labelModelIndex(i - m_first);
488 if (valueIndex.isValid() && labelIndex.isValid()) {
487 if (valueIndex.isValid() && labelIndex.isValid()) {
489 QPieSlice *slice = new QPieSlice;
488 QPieSlice *slice = new QPieSlice;
490 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
489 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
491 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
490 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
492 slice->setLabelVisible();
491 slice->setLabelVisible();
493 m_series->insert(i, slice);
492 m_series->insert(i, slice);
494 m_slices.insert(i, slice);
493 m_slices.insert(i, slice);
495 }
494 }
496 }
495 }
497 }
496 }
498 }
497 }
499 }
498 }
500
499
501 void QPieModelMapperPrivate::initializePieFromModel()
500 void QPieModelMapperPrivate::initializePieFromModel()
502 {
501 {
503 if (m_model == 0 || m_series == 0)
502 if (m_model == 0 || m_series == 0)
504 return;
503 return;
505
504
506 blockSeriesSignals();
505 blockSeriesSignals();
507 // clear current content
506 // clear current content
508 m_series->clear();
507 m_series->clear();
509 m_slices.clear();
508 m_slices.clear();
510
509
511 // create the initial slices set
510 // create the initial slices set
512 int slicePos = 0;
511 int slicePos = 0;
513 QModelIndex valueIndex = valueModelIndex(slicePos);
512 QModelIndex valueIndex = valueModelIndex(slicePos);
514 QModelIndex labelIndex = labelModelIndex(slicePos);
513 QModelIndex labelIndex = labelModelIndex(slicePos);
515 while (valueIndex.isValid() && labelIndex.isValid()) {
514 while (valueIndex.isValid() && labelIndex.isValid()) {
516 QPieSlice *slice = new QPieSlice;
515 QPieSlice *slice = new QPieSlice;
517 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
516 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
518 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
517 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
519 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
518 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
520 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
519 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
521 m_series->append(slice);
520 m_series->append(slice);
522 m_slices.append(slice);
521 m_slices.append(slice);
523 // m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble());
522 // m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble());
524 slicePos++;
523 slicePos++;
525 valueIndex = valueModelIndex(slicePos);
524 valueIndex = valueModelIndex(slicePos);
526 labelIndex = labelModelIndex(slicePos);
525 labelIndex = labelModelIndex(slicePos);
527 }
526 }
528 m_series->setLabelsVisible(true);
527 m_series->setLabelsVisible(true);
529 blockSeriesSignals(false);
528 blockSeriesSignals(false);
530 }
529 }
531
530
532 #include "moc_qpiemodelmapper_p.cpp"
531 #include "moc_qpiemodelmapper_p.cpp"
533 #include "moc_qpiemodelmapper.cpp"
532 #include "moc_qpiemodelmapper.cpp"
534
533
535 QTCOMMERCIALCHART_END_NAMESPACE
534 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,276 +1,276
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "customtablemodel.h"
21 #include "customtablemodel.h"
22 #include <QVector>
22 #include <QVector>
23 #include <QTime>
23 #include <QTime>
24 #include <QRect>
24 #include <QRect>
25 #include <QColor>
25 #include <QColor>
26
26
27 CustomTableModel::CustomTableModel(QObject *parent) :
27 CustomTableModel::CustomTableModel(QObject *parent) :
28 QAbstractTableModel(parent)
28 QAbstractTableModel(parent)
29 {
29 {
30 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
30 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
31
31
32 m_columnCount = 7;
32 m_columnCount = 7;
33 m_rowCount = 1029;
33 m_rowCount = 29;
34
34
35 m_labels.append("Apples");
35 m_labels.append("Apples");
36 m_labels.append("Oranges");
36 m_labels.append("Oranges");
37 m_labels.append("Pears");
37 m_labels.append("Pears");
38 m_labels.append("Peaches");
38 m_labels.append("Peaches");
39 m_labels.append("Coconuts");
39 m_labels.append("Coconuts");
40 m_labels.append("Bananas");
40 m_labels.append("Bananas");
41 m_labels.append("Kiwis");
41 m_labels.append("Kiwis");
42 m_labels.append("Grapes");
42 m_labels.append("Grapes");
43 m_labels.append("Plums");
43 m_labels.append("Plums");
44
44
45 // m_data
45 // m_data
46 for (int i = 0; i < m_rowCount; i++)
46 for (int i = 0; i < m_rowCount; i++)
47 {
47 {
48 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
48 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
49 for (int k = 0; k < dataVec->size(); k++)
49 for (int k = 0; k < dataVec->size(); k++)
50 {
50 {
51 if (k%2 == 0)
51 if (k%2 == 0)
52 dataVec->replace(k, i * 50 + qrand()%20);
52 dataVec->replace(k, i * 50 + qrand()%20);
53 else
53 else
54 dataVec->replace(k, qrand()%100);
54 dataVec->replace(k, qrand()%100);
55 }
55 }
56 m_data.append(dataVec);
56 m_data.append(dataVec);
57 // m_labels.append(QString("Row: %1").arg((i + 1)));
57 // m_labels.append(QString("Row: %1").arg((i + 1)));
58 }
58 }
59 }
59 }
60
60
61 int CustomTableModel::rowCount(const QModelIndex & parent) const
61 int CustomTableModel::rowCount(const QModelIndex & parent) const
62 {
62 {
63 Q_UNUSED(parent)
63 Q_UNUSED(parent)
64 return m_data.count();
64 return m_data.count();
65 }
65 }
66
66
67 int CustomTableModel::columnCount(const QModelIndex & parent) const
67 int CustomTableModel::columnCount(const QModelIndex & parent) const
68 {
68 {
69 Q_UNUSED(parent)
69 Q_UNUSED(parent)
70 return m_columnCount;// + 1;
70 return m_columnCount;// + 1;
71 }
71 }
72
72
73 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
73 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
74 {
74 {
75 if (role != Qt::DisplayRole)
75 if (role != Qt::DisplayRole)
76 return QVariant();
76 return QVariant();
77
77
78 if (orientation == Qt::Horizontal)
78 if (orientation == Qt::Horizontal)
79 {
79 {
80 switch(section)
80 switch(section)
81 {
81 {
82 case 7:
82 case 7:
83 return "Fruit";
83 return "Fruit";
84 case 1:
84 case 1:
85 return "Count";
85 return "Count";
86 case 2:
86 case 2:
87 return "Ordered";
87 return "Ordered";
88 default:
88 default:
89 if (section%2 == 0)
89 if (section%2 == 0)
90 return "x";
90 return "x";
91 else
91 else
92 return "y";
92 return "y";
93 }
93 }
94 }
94 }
95 else
95 else
96 return QString("%1").arg(section /*+ 1*/);
96 return QString("%1").arg(section /*+ 1*/);
97 }
97 }
98
98
99 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
99 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
100 {
100 {
101 if (role == Qt::DisplayRole)
101 if (role == Qt::DisplayRole)
102 {
102 {
103 switch(index.column())
103 switch(index.column())
104 {
104 {
105 // case 7:
105 // case 7:
106 // return m_labels[index.row()];
106 // return m_labels[index.row()];
107 default:
107 default:
108 return m_data[index.row()]->at(index.column());
108 return m_data[index.row()]->at(index.column());
109 break;
109 break;
110 }
110 }
111 }
111 }
112 else if (role == Qt::EditRole)
112 else if (role == Qt::EditRole)
113 {
113 {
114 switch(index.column())
114 switch(index.column())
115 {
115 {
116 // case 7:
116 // case 7:
117 // return m_labels[index.row()];
117 // return m_labels[index.row()];
118 default:
118 default:
119 return m_data[index.row()]->at(index.column());
119 return m_data[index.row()]->at(index.column());
120 break;
120 break;
121 }
121 }
122 }
122 }
123 else if (role == Qt::BackgroundRole)
123 else if (role == Qt::BackgroundRole)
124 {
124 {
125 QRect rect;
125 QRect rect;
126 foreach(rect, m_mapping)
126 foreach(rect, m_mapping)
127 if(rect.contains(index.column(), index.row()))
127 if(rect.contains(index.column(), index.row()))
128 return QColor(m_mapping.key(rect));
128 return QColor(m_mapping.key(rect));
129
129
130 // cell not mapped return white color
130 // cell not mapped return white color
131 return QColor(Qt::white);
131 return QColor(Qt::white);
132 }
132 }
133 return QVariant();
133 return QVariant();
134 }
134 }
135
135
136 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
136 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
137 {
137 {
138 if (index.isValid() && role == Qt::EditRole)
138 if (index.isValid() && role == Qt::EditRole)
139 {
139 {
140 switch(index.column())
140 switch(index.column())
141 {
141 {
142 case 7:
142 case 7:
143 m_labels.replace(index.row(), value.toString());
143 m_labels.replace(index.row(), value.toString());
144 break;
144 break;
145 default:
145 default:
146 m_data[index.row()]->replace(index.column(), value.toDouble());
146 m_data[index.row()]->replace(index.column(), value.toDouble());
147 break;
147 break;
148 }
148 }
149 emit dataChanged(index, index);
149 emit dataChanged(index, index);
150 return true;
150 return true;
151 }
151 }
152 return false;
152 return false;
153 }
153 }
154
154
155 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
155 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
156 {
156 {
157 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
157 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
158 }
158 }
159
159
160 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
160 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
161 {
161 {
162 Q_UNUSED(parent)
162 Q_UNUSED(parent)
163
163
164 if (row < 0)
164 if (row < 0)
165 row = 0;
165 row = 0;
166 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
166 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
167 for (int i = row; i < row + count; i++)
167 for (int i = row; i < row + count; i++)
168 {
168 {
169 // m_points.insert(row, QPointF(10,20));
169 // m_points.insert(row, QPointF(10,20));
170 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
170 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
171 QVector<QColor>* colorVec = new QVector<QColor>(m_columnCount);
171 QVector<QColor>* colorVec = new QVector<QColor>(m_columnCount);
172 for (int k = 0; k < dataVec->size(); k++)
172 for (int k = 0; k < dataVec->size(); k++)
173 {
173 {
174 if (k%2 == 0)
174 if (k%2 == 0)
175 // dataVec->replace(k, i * 50 + qrand()%20);
175 // dataVec->replace(k, i * 50 + qrand()%20);
176 {
176 {
177 int difference = 0;
177 int difference = 0;
178 if (i < m_data.size())
178 if (i < m_data.size())
179 {
179 {
180 if (i - 1 >= 0)
180 if (i - 1 >= 0)
181 {
181 {
182 if (row > 0)
182 if (row > 0)
183 difference = (int)((qAbs(m_data[i]->at(k) - m_data[row - 1]->at(k)))/count);
183 difference = (int)((qAbs(m_data[i]->at(k) - m_data[row - 1]->at(k)))/count);
184 else
184 else
185 difference = (int)((qAbs(m_data[i]->at(k)/count)));
185 difference = (int)((qAbs(m_data[i]->at(k)/count)));
186 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%qMax(1, difference));
186 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%qMax(1, difference));
187 }
187 }
188 else
188 else
189 dataVec->replace(k, qrand()%40 + 10);
189 dataVec->replace(k, qrand()%40 + 10);
190 }
190 }
191 else
191 else
192 {
192 {
193 if (i - 1 >= 0)
193 if (i - 1 >= 0)
194 {
194 {
195 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%40 + 10);
195 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%40 + 10);
196 }
196 }
197 else
197 else
198 {
198 {
199 dataVec->replace(k, qrand()%40 + 10);
199 dataVec->replace(k, qrand()%40 + 10);
200 }
200 }
201 }
201 }
202 }
202 }
203 else
203 else
204 dataVec->replace(k, qrand()%100);
204 dataVec->replace(k, qrand()%100);
205 colorVec->replace(k, QColor(Qt::white));
205 colorVec->replace(k, QColor(Qt::white));
206 }
206 }
207 m_data.insert(i, dataVec);
207 m_data.insert(i, dataVec);
208 m_labels.insert(i,(QString("Row: %1").arg(i + 1)));
208 m_labels.insert(i,(QString("Row: %1").arg(i + 1)));
209 }
209 }
210 endInsertRows();
210 endInsertRows();
211 return true;
211 return true;
212 }
212 }
213
213
214 bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent)
214 bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent)
215 {
215 {
216 if (row > this->rowCount() - 1)
216 if (row > this->rowCount() - 1)
217 return false;
217 return false;
218 if (row < 0)
218 if (row < 0)
219 row = 0;
219 row = 0;
220 if (row + count > rowCount())
220 if (row + count > rowCount())
221 return false;
221 return false;
222 beginRemoveRows(parent, row, row + count - 1);
222 beginRemoveRows(parent, row, row + count - 1);
223 for (int i = row; i < row + count; i++)
223 for (int i = row; i < row + count; i++)
224 {
224 {
225 QVector<qreal>* item = m_data.at(row);
225 QVector<qreal>* item = m_data.at(row);
226 m_data.removeAt(row);
226 m_data.removeAt(row);
227 delete item;
227 delete item;
228 m_labels.removeAt(row);
228 m_labels.removeAt(row);
229 }
229 }
230 endRemoveRows();
230 endRemoveRows();
231 return true;
231 return true;
232 }
232 }
233
233
234 bool CustomTableModel::insertColumns ( int column, int count, const QModelIndex & parent)
234 bool CustomTableModel::insertColumns ( int column, int count, const QModelIndex & parent)
235 {
235 {
236 if (column < 0)
236 if (column < 0)
237 column = 0;
237 column = 0;
238 beginInsertColumns(parent, column, column + count - 1);
238 beginInsertColumns(parent, column, column + count - 1);
239 m_columnCount += count;
239 m_columnCount += count;
240 for (int i = column; i < column + count; i++)
240 for (int i = column; i < column + count; i++)
241 for (int k = 0; k < rowCount(); k++)
241 for (int k = 0; k < rowCount(); k++)
242 if (k - 1 >= 0) {
242 if (k - 1 >= 0) {
243 m_data[k]->insert(i, m_data[k - 1]->at(i) + qrand()%40 + 10);
243 m_data[k]->insert(i, m_data[k - 1]->at(i) + qrand()%40 + 10);
244 } else {
244 } else {
245 m_data[k]->insert(i, qrand()%40);
245 m_data[k]->insert(i, qrand()%40);
246 }
246 }
247 endInsertColumns();
247 endInsertColumns();
248 return true;
248 return true;
249 }
249 }
250
250
251 bool CustomTableModel::removeColumns ( int column, int count, const QModelIndex & parent)
251 bool CustomTableModel::removeColumns ( int column, int count, const QModelIndex & parent)
252 {
252 {
253 if (column > columnCount() - 1)
253 if (column > columnCount() - 1)
254 return false;
254 return false;
255 if (column < 0)
255 if (column < 0)
256 column = 0;
256 column = 0;
257 if (column + count > columnCount())
257 if (column + count > columnCount())
258 return false;
258 return false;
259 beginRemoveColumns(parent, column, column + count -1);
259 beginRemoveColumns(parent, column, column + count -1);
260 m_columnCount -= count;
260 m_columnCount -= count;
261 for (int i = column; i < column + count; i++)
261 for (int i = column; i < column + count; i++)
262 for (int k = 0; k < rowCount(); k++)
262 for (int k = 0; k < rowCount(); k++)
263 m_data[k]->remove(column);
263 m_data[k]->remove(column);
264 endRemoveColumns();
264 endRemoveColumns();
265 return true;
265 return true;
266 }
266 }
267
267
268 void CustomTableModel::addMapping(QString color, QRect area)
268 void CustomTableModel::addMapping(QString color, QRect area)
269 {
269 {
270 m_mapping.insertMulti(color, area);
270 m_mapping.insertMulti(color, area);
271 }
271 }
272
272
273 void CustomTableModel::addMapping(QString color, int left, int top, int right, int bottom)
273 void CustomTableModel::addMapping(QString color, int left, int top, int right, int bottom)
274 {
274 {
275 addMapping(color, QRect(QPoint(left, top), QPoint(right, bottom)));
275 addMapping(color, QRect(QPoint(left, top), QPoint(right, bottom)));
276 }
276 }
General Comments 0
You need to be logged in to leave comments. Login now