##// END OF EJS Templates
BarSeries API can now be used when the series is used with BarModelMapper
Marek Rosa -
r1435:5560f4cf8e35
parent child
Show More
@@ -1,451 +1,597
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 "qbarmodelmapper.h"
21 #include "qbarmodelmapper.h"
22 #include "qbarmodelmapper_p.h"
22 #include "qbarmodelmapper_p.h"
23 #include "qbarseries.h"
23 #include "qbarseries.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25 #include "qchart.h"
25 #include "qchart.h"
26 #include <QAbstractItemModel>
26 #include <QAbstractItemModel>
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 /*!
30 /*!
31 \class QBarModelMapper
31 \class QBarModelMapper
32 \brief part of QtCommercial chart API.
32 \brief part of QtCommercial chart API.
33 \mainclass
33 \mainclass
34
34
35 Model mappers allow you to use QAbstractItemModel derived models as a data source for a chart series.
35 Model mappers allow you to use QAbstractItemModel derived models as a data source for a chart series.
36 The instance of this class cannot be created directly. QHBarModelMapper of QVBarModelMapper should be used instead. This class is used to create a connection between QBarSeries and QAbstractItemModel derived model object.
36 The instance of this class cannot be created directly. QHBarModelMapper of QVBarModelMapper should be used instead. This class is used to create a connection between QBarSeries and QAbstractItemModel derived model object.
37 Curently it is NOT possible to use both QAbstractItemModel and QBarSeries model API.
37 Curently it is NOT possible to use both QAbstractItemModel and QBarSeries model API.
38 When the series is set to the mapper the QBarSeries and QBarSet API that affect the data (append, setValue, remove) should not be used.
38 When the series is set to the mapper the QBarSeries and QBarSet API that affect the data (append, setValue, remove) should not be used.
39 The model and the QBarSeries won't be kept in sync. Model API should be used to insert,remove,modify BarSets.
39 The model and the QBarSeries won't be kept in sync. Model API should be used to insert,remove,modify BarSets.
40 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
40 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
41 */
41 */
42
42
43 /*!
43 /*!
44 \property QBarModelMapper::series
44 \property QBarModelMapper::series
45 \brief Defines the QPieSeries object that is used by the mapper.
45 \brief Defines the QPieSeries object that is used by the mapper.
46
46
47 All the data in the series is discarded when it is set to the mapper.
47 All the data in the series is discarded when it is set to the mapper.
48 When new series is specified the old series is disconnected (it preserves its data)
48 When new series is specified the old series is disconnected (it preserves its data)
49 */
49 */
50
50
51 /*!
51 /*!
52 \property QBarModelMapper::model
52 \property QBarModelMapper::model
53 \brief Defines the model that is used by the mapper.
53 \brief Defines the model that is used by the mapper.
54 */
54 */
55
55
56 /*!
56 /*!
57 \property QBarModelMapper::first
57 \property QBarModelMapper::first
58 \brief Defines which item of the model's row/column should be mapped as the value of the first QBarSet in the series.
58 \brief Defines which item of the model's row/column should be mapped as the value of the first QBarSet in the series.
59
59
60 Minimal and default value is: 0
60 Minimal and default value is: 0
61 */
61 */
62
62
63 /*!
63 /*!
64 \property QBarModelMapper::count
64 \property QBarModelMapper::count
65 \brief Defines the number of rows/columns of the model that are mapped as the data for QBarSeries
65 \brief Defines the number of rows/columns of the model that are mapped as the data for QBarSeries
66
66
67 Minimal and default value is: -1 (count limited by the number of rows/columns in the model)
67 Minimal and default value is: -1 (count limited by the number of rows/columns in the model)
68 */
68 */
69
69
70 /*!
70 /*!
71 Constructs a mapper object which is a child of \a parent.
71 Constructs a mapper object which is a child of \a parent.
72 */
72 */
73 QBarModelMapper::QBarModelMapper(QObject *parent) :
73 QBarModelMapper::QBarModelMapper(QObject *parent) :
74 QObject(parent),
74 QObject(parent),
75 d_ptr(new QBarModelMapperPrivate(this))
75 d_ptr(new QBarModelMapperPrivate(this))
76 {
76 {
77 }
77 }
78
78
79 QAbstractItemModel* QBarModelMapper::model() const
79 QAbstractItemModel* QBarModelMapper::model() const
80 {
80 {
81 Q_D(const QBarModelMapper);
81 Q_D(const QBarModelMapper);
82 return d->m_model;
82 return d->m_model;
83 }
83 }
84
84
85 void QBarModelMapper::setModel(QAbstractItemModel *model)
85 void QBarModelMapper::setModel(QAbstractItemModel *model)
86 {
86 {
87 if (model == 0)
87 if (model == 0)
88 return;
88 return;
89
89
90 Q_D(QBarModelMapper);
90 Q_D(QBarModelMapper);
91 if (d->m_model) {
91 if (d->m_model) {
92 disconnect(d->m_model, 0, d, 0);
92 disconnect(d->m_model, 0, d, 0);
93 }
93 }
94
94
95 d->m_model = model;
95 d->m_model = model;
96 d->initializeBarFromModel();
96 d->initializeBarFromModel();
97 // connect signals from the model
97 // connect signals from the model
98 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
98 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
99 connect(d->m_model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)), d, SLOT(modelHeaderDataUpdated(Qt::Orientation,int,int)));
99 connect(d->m_model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)), d, SLOT(modelHeaderDataUpdated(Qt::Orientation,int,int)));
100 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
100 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
101 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
101 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
102 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
102 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
103 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
103 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
104 }
104 }
105
105
106 QBarSeries* QBarModelMapper::series() const
106 QBarSeries* QBarModelMapper::series() const
107 {
107 {
108 Q_D(const QBarModelMapper);
108 Q_D(const QBarModelMapper);
109 return d->m_series;
109 return d->m_series;
110 }
110 }
111
111
112 void QBarModelMapper::setSeries(QBarSeries *series)
112 void QBarModelMapper::setSeries(QBarSeries *series)
113 {
113 {
114 Q_D(QBarModelMapper);
114 Q_D(QBarModelMapper);
115 if (d->m_series) {
115 if (d->m_series) {
116 disconnect(d->m_series, 0, d, 0);
116 disconnect(d->m_series, 0, d, 0);
117 }
117 }
118
118
119 if (series == 0)
119 if (series == 0)
120 return;
120 return;
121
121
122 d->m_series = series;
122 d->m_series = series;
123 d->initializeBarFromModel();
123 d->initializeBarFromModel();
124 // connect the signals from the series
124 // connect the signals from the series
125 // TODO: TO be implemented
125 connect(d->m_series, SIGNAL(barsetsAdded(QList<QBarSet*>)), d, SLOT(barSetsAdded(QList<QBarSet*>)));
126 connect(d->m_series, SIGNAL(barsetsRemoved(QList<QBarSet*>)), d, SLOT(barSetsRemoved(QList<QBarSet*>)));
126 }
127 }
127
128
128 int QBarModelMapper::first() const
129 int QBarModelMapper::first() const
129 {
130 {
130 Q_D(const QBarModelMapper);
131 Q_D(const QBarModelMapper);
131 return d->m_first;
132 return d->m_first;
132 }
133 }
133
134
134 void QBarModelMapper::setFirst(int first)
135 void QBarModelMapper::setFirst(int first)
135 {
136 {
136 Q_D(QBarModelMapper);
137 Q_D(QBarModelMapper);
137 d->m_first = qMax(first, 0);
138 d->m_first = qMax(first, 0);
138 d->initializeBarFromModel();
139 d->initializeBarFromModel();
139 }
140 }
140
141
141 int QBarModelMapper::count() const
142 int QBarModelMapper::count() const
142 {
143 {
143 Q_D(const QBarModelMapper);
144 Q_D(const QBarModelMapper);
144 return d->m_count;
145 return d->m_count;
145 }
146 }
146
147
147 void QBarModelMapper::setCount(int count)
148 void QBarModelMapper::setCount(int count)
148 {
149 {
149 Q_D(QBarModelMapper);
150 Q_D(QBarModelMapper);
150 d->m_count = qMax(count, -1);
151 d->m_count = qMax(count, -1);
151 d->initializeBarFromModel();
152 d->initializeBarFromModel();
152 }
153 }
153
154
154 /*!
155 /*!
155 Returns the orientation that is used when QBarModelMapper accesses the model.
156 Returns the orientation that is used when QBarModelMapper accesses the model.
156 This mean whether the consecutive values of the bar set are read from row (Qt::Horizontal)
157 This mean whether the consecutive values of the bar set are read from row (Qt::Horizontal)
157 or from columns (Qt::Vertical)
158 or from columns (Qt::Vertical)
158 */
159 */
159 Qt::Orientation QBarModelMapper::orientation() const
160 Qt::Orientation QBarModelMapper::orientation() const
160 {
161 {
161 Q_D(const QBarModelMapper);
162 Q_D(const QBarModelMapper);
162 return d->m_orientation;
163 return d->m_orientation;
163 }
164 }
164
165
165 /*!
166 /*!
166 Returns the \a orientation that is used when QBarModelMapper accesses the model.
167 Returns the \a orientation that is used when QBarModelMapper accesses the model.
167 This mean whether the consecutive values of the pie are read from row (Qt::Horizontal)
168 This mean whether the consecutive values of the pie are read from row (Qt::Horizontal)
168 or from columns (Qt::Vertical)
169 or from columns (Qt::Vertical)
169 */
170 */
170 void QBarModelMapper::setOrientation(Qt::Orientation orientation)
171 void QBarModelMapper::setOrientation(Qt::Orientation orientation)
171 {
172 {
172 Q_D(QBarModelMapper);
173 Q_D(QBarModelMapper);
173 d->m_orientation = orientation;
174 d->m_orientation = orientation;
174 d->initializeBarFromModel();
175 d->initializeBarFromModel();
175 }
176 }
176
177
177 /*!
178 /*!
178 Returns which section of the model is used as the data source for the first bar set
179 Returns which section of the model is used as the data source for the first bar set
179 */
180 */
180 int QBarModelMapper::firstBarSetSection() const
181 int QBarModelMapper::firstBarSetSection() const
181 {
182 {
182 Q_D(const QBarModelMapper);
183 Q_D(const QBarModelMapper);
183 return d->m_firstBarSetSection;
184 return d->m_firstBarSetSection;
184 }
185 }
185
186
186 /*!
187 /*!
187 Sets the model section that is used as the data source for the first bar set
188 Sets the model section that is used as the data source for the first bar set
188 Parameter \a firstBarSetSection specifies the section of the model.
189 Parameter \a firstBarSetSection specifies the section of the model.
189 */
190 */
190 void QBarModelMapper::setFirstBarSetSection(int firstBarSetSection)
191 void QBarModelMapper::setFirstBarSetSection(int firstBarSetSection)
191 {
192 {
192 Q_D(QBarModelMapper);
193 Q_D(QBarModelMapper);
193 d->m_firstBarSetSection = qMax(-1, firstBarSetSection);
194 d->m_firstBarSetSection = qMax(-1, firstBarSetSection);
194 d->initializeBarFromModel();
195 d->initializeBarFromModel();
195 }
196 }
196
197
197 /*!
198 /*!
198 Returns which section of the model is used as the data source for the last bar set
199 Returns which section of the model is used as the data source for the last bar set
199 */
200 */
200 int QBarModelMapper::lastBarSetSection() const
201 int QBarModelMapper::lastBarSetSection() const
201 {
202 {
202 Q_D(const QBarModelMapper);
203 Q_D(const QBarModelMapper);
203 return d->m_lastBarSetSection;
204 return d->m_lastBarSetSection;
204 }
205 }
205
206
206 /*!
207 /*!
207 Sets the model section that is used as the data source for the last bar set
208 Sets the model section that is used as the data source for the last bar set
208 Parameter \a lastBarSetSection specifies the section of the model.
209 Parameter \a lastBarSetSection specifies the section of the model.
209 */
210 */
210 void QBarModelMapper::setLastBarSetSection(int lastBarSetSection)
211 void QBarModelMapper::setLastBarSetSection(int lastBarSetSection)
211 {
212 {
212 Q_D(QBarModelMapper);
213 Q_D(QBarModelMapper);
213 d->m_lastBarSetSection = qMax(-1, lastBarSetSection);
214 d->m_lastBarSetSection = qMax(-1, lastBarSetSection);
214 d->initializeBarFromModel();
215 d->initializeBarFromModel();
215 }
216 }
216
217
217 /*!
218 /*!
218 Resets the QBarModelMapper to the default state.
219 Resets the QBarModelMapper to the default state.
219 first: 0; count: -1; firstBarSetSection: -1; lastBarSetSection: -1; categoriesSection: -1
220 first: 0; count: -1; firstBarSetSection: -1; lastBarSetSection: -1; categoriesSection: -1
220 */
221 */
221 void QBarModelMapper::reset()
222 void QBarModelMapper::reset()
222 {
223 {
223 Q_D(QBarModelMapper);
224 Q_D(QBarModelMapper);
224 d->m_first = 0;
225 d->m_first = 0;
225 d->m_count = -1;
226 d->m_count = -1;
226 d->m_firstBarSetSection = -1;
227 d->m_firstBarSetSection = -1;
227 d->m_lastBarSetSection = -1;
228 d->m_lastBarSetSection = -1;
228 d->initializeBarFromModel();
229 d->initializeBarFromModel();
229 }
230 }
230
231
231 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
232 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
232
233
233 QBarModelMapperPrivate::QBarModelMapperPrivate(QBarModelMapper *q) :
234 QBarModelMapperPrivate::QBarModelMapperPrivate(QBarModelMapper *q) :
234 m_series(0),
235 m_series(0),
235 m_model(0),
236 m_model(0),
236 m_first(0),
237 m_first(0),
237 m_count(-1),
238 m_count(-1),
238 m_orientation(Qt::Vertical),
239 m_orientation(Qt::Vertical),
239 m_firstBarSetSection(-1),
240 m_firstBarSetSection(-1),
240 m_lastBarSetSection(-1),
241 m_lastBarSetSection(-1),
241 m_seriesSignalsBlock(false),
242 m_seriesSignalsBlock(false),
242 m_modelSignalsBlock(false),
243 m_modelSignalsBlock(false),
243 q_ptr(q)
244 q_ptr(q)
244 {
245 {
245 }
246 }
246
247
247 void QBarModelMapperPrivate::blockModelSignals(bool block)
248 void QBarModelMapperPrivate::blockModelSignals(bool block)
248 {
249 {
249 m_modelSignalsBlock = block;
250 m_modelSignalsBlock = block;
250 }
251 }
251
252
252 void QBarModelMapperPrivate::blockSeriesSignals(bool block)
253 void QBarModelMapperPrivate::blockSeriesSignals(bool block)
253 {
254 {
254 m_seriesSignalsBlock = block;
255 m_seriesSignalsBlock = block;
255 }
256 }
256
257
257 QBarSet* QBarModelMapperPrivate::barSet(QModelIndex index)
258 QBarSet* QBarModelMapperPrivate::barSet(QModelIndex index)
258 {
259 {
259 if (!index.isValid())
260 if (!index.isValid())
260 return 0;
261 return 0;
261
262
262 if (m_orientation == Qt::Vertical && index.column() >= m_firstBarSetSection && index.column() <= m_lastBarSetSection) {
263 if (m_orientation == Qt::Vertical && index.column() >= m_firstBarSetSection && index.column() <= m_lastBarSetSection) {
263 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) {
264 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) {
264 // if (m_model->index(index.row(), m_valuesSection).isValid() && m_model->index(index.row(), m_labelsSection).isValid())
265 // if (m_model->index(index.row(), m_valuesSection).isValid() && m_model->index(index.row(), m_labelsSection).isValid())
265 return m_series->barSets().at(index.column() - m_firstBarSetSection);
266 return m_series->barSets().at(index.column() - m_firstBarSetSection);
266 // else
267 // else
267 // return 0;
268 // return 0;
268 }
269 }
269 } else if (m_orientation == Qt::Horizontal && index.row() >= m_firstBarSetSection && index.row() <= m_lastBarSetSection) {
270 } else if (m_orientation == Qt::Horizontal && index.row() >= m_firstBarSetSection && index.row() <= m_lastBarSetSection) {
270 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
271 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
271 return m_series->barSets().at(index.row() - m_firstBarSetSection);
272 return m_series->barSets().at(index.row() - m_firstBarSetSection);
272 }
273 }
273 return 0; // This part of model has not been mapped to any slice
274 return 0; // This part of model has not been mapped to any slice
274 }
275 }
275
276
276 QModelIndex QBarModelMapperPrivate::barModelIndex(int barSection, int posInBar)
277 QModelIndex QBarModelMapperPrivate::barModelIndex(int barSection, int posInBar)
277 {
278 {
278 if (m_count != -1 && posInBar >= m_count)
279 if (m_count != -1 && posInBar >= m_count)
279 return QModelIndex(); // invalid
280 return QModelIndex(); // invalid
280
281
281 if (barSection < m_firstBarSetSection || barSection > m_lastBarSetSection)
282 if (barSection < m_firstBarSetSection || barSection > m_lastBarSetSection)
282 return QModelIndex(); // invalid
283 return QModelIndex(); // invalid
283
284
284 if (m_orientation == Qt::Vertical)
285 if (m_orientation == Qt::Vertical)
285 return m_model->index(posInBar + m_first, barSection);
286 return m_model->index(posInBar + m_first, barSection);
286 else
287 else
287 return m_model->index(barSection, posInBar + m_first);
288 return m_model->index(barSection, posInBar + m_first);
288 }
289 }
289
290
290 void QBarModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
291 void QBarModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
291 {
292 {
292 Q_UNUSED(topLeft)
293 Q_UNUSED(topLeft)
293 Q_UNUSED(bottomRight)
294 Q_UNUSED(bottomRight)
294
295
295 if (m_model == 0 || m_series == 0)
296 if (m_model == 0 || m_series == 0)
296 return;
297 return;
297
298
298 if (m_modelSignalsBlock)
299 if (m_modelSignalsBlock)
299 return;
300 return;
300
301
301 blockSeriesSignals();
302 blockSeriesSignals();
302 QModelIndex index;
303 QModelIndex index;
303 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
304 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
304 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
305 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
305 index = topLeft.sibling(row, column);
306 index = topLeft.sibling(row, column);
306 QBarSet* bar = barSet(index);
307 QBarSet* bar = barSet(index);
307 if (bar) {
308 if (bar) {
308 if (m_orientation == Qt::Vertical)
309 if (m_orientation == Qt::Vertical)
309 bar->replace(row - m_first, m_model->data(index).toReal());
310 bar->replace(row - m_first, m_model->data(index).toReal());
310 else
311 else
311 bar->replace(column - m_first, m_model->data(index).toReal());
312 bar->replace(column - m_first, m_model->data(index).toReal());
312 }
313 }
313 }
314 }
314 }
315 }
315 blockSeriesSignals(false);
316 blockSeriesSignals(false);
316 }
317 }
317
318
318 void QBarModelMapperPrivate::modelHeaderDataUpdated(Qt::Orientation orientation, int first, int last)
319 void QBarModelMapperPrivate::modelHeaderDataUpdated(Qt::Orientation orientation, int first, int last)
319 {
320 {
320 if (m_model == 0 || m_series == 0)
321 if (m_model == 0 || m_series == 0)
321 return;
322 return;
322
323
323 if (m_modelSignalsBlock)
324 if (m_modelSignalsBlock)
324 return;
325 return;
325
326
326 blockSeriesSignals();
327 blockSeriesSignals();
327 if (orientation != m_orientation) {
328 if (orientation != m_orientation) {
328 for (int section = first; section <= last; section++) {
329 for (int section = first; section <= last; section++) {
329 if (section >= m_firstBarSetSection && section <= m_lastBarSetSection) {
330 if (section >= m_firstBarSetSection && section <= m_lastBarSetSection) {
330 QBarSet* bar = m_series->barSets().at(section - m_firstBarSetSection);
331 QBarSet* bar = m_series->barSets().at(section - m_firstBarSetSection);
331 if (bar)
332 if (bar)
332 bar->setLabel(m_model->headerData(section, orientation).toString());
333 bar->setLabel(m_model->headerData(section, orientation).toString());
333 }
334 }
334 }
335 }
335 }
336 }
336 blockSeriesSignals(false);
337 blockSeriesSignals(false);
337 }
338 }
338
339
339 void QBarModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
340 void QBarModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
340 {
341 {
341 Q_UNUSED(parent);
342 Q_UNUSED(parent);
342 Q_UNUSED(end)
343 Q_UNUSED(end)
343 if (m_modelSignalsBlock)
344 if (m_modelSignalsBlock)
344 return;
345 return;
345
346
346 blockSeriesSignals();
347 blockSeriesSignals();
347 if (m_orientation == Qt::Vertical)
348 if (m_orientation == Qt::Vertical)
348 // insertData(start, end);
349 // insertData(start, end);
349 initializeBarFromModel();
350 initializeBarFromModel();
350 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
351 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
351 initializeBarFromModel();
352 initializeBarFromModel();
352 blockSeriesSignals(false);
353 blockSeriesSignals(false);
353 }
354 }
354
355
355 void QBarModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
356 void QBarModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
356 {
357 {
357 Q_UNUSED(parent);
358 Q_UNUSED(parent);
358 Q_UNUSED(end)
359 Q_UNUSED(end)
359 if (m_modelSignalsBlock)
360 if (m_modelSignalsBlock)
360 return;
361 return;
361
362
362 blockSeriesSignals();
363 blockSeriesSignals();
363 if (m_orientation == Qt::Vertical)
364 if (m_orientation == Qt::Vertical)
364 // removeData(start, end);
365 // removeData(start, end);
365 initializeBarFromModel();
366 initializeBarFromModel();
366 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
367 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
367 initializeBarFromModel();
368 initializeBarFromModel();
368 blockSeriesSignals(false);
369 blockSeriesSignals(false);
369 }
370 }
370
371
371 void QBarModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
372 void QBarModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
372 {
373 {
373 Q_UNUSED(parent);
374 Q_UNUSED(parent);
374 Q_UNUSED(end)
375 Q_UNUSED(end)
375 if (m_modelSignalsBlock)
376 if (m_modelSignalsBlock)
376 return;
377 return;
377
378
378 blockSeriesSignals();
379 blockSeriesSignals();
379 if (m_orientation == Qt::Horizontal)
380 if (m_orientation == Qt::Horizontal)
380 // insertData(start, end);
381 // insertData(start, end);
381 initializeBarFromModel();
382 initializeBarFromModel();
382 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
383 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
383 initializeBarFromModel();
384 initializeBarFromModel();
384 blockSeriesSignals(false);
385 blockSeriesSignals(false);
385 }
386 }
386
387
387 void QBarModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
388 void QBarModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
388 {
389 {
389 Q_UNUSED(parent);
390 Q_UNUSED(parent);
390 Q_UNUSED(end)
391 Q_UNUSED(end)
391 if (m_modelSignalsBlock)
392 if (m_modelSignalsBlock)
392 return;
393 return;
393
394
394 blockSeriesSignals();
395 blockSeriesSignals();
395 if (m_orientation == Qt::Horizontal)
396 if (m_orientation == Qt::Horizontal)
396 // removeData(start, end);
397 // removeData(start, end);
397 initializeBarFromModel();
398 initializeBarFromModel();
398 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
399 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
399 initializeBarFromModel();
400 initializeBarFromModel();
400 blockSeriesSignals(false);
401 blockSeriesSignals(false);
401 }
402 }
402
403
403 void QBarModelMapperPrivate::insertData(int start, int end)
404 void QBarModelMapperPrivate::insertData(int start, int end)
404 {
405 {
405 Q_UNUSED(end)
406 Q_UNUSED(end)
406 Q_UNUSED(start)
407 Q_UNUSED(start)
407 Q_UNUSED(end)
408 Q_UNUSED(end)
408 // To be implemented
409 // To be implemented
409 }
410 }
410
411
411 void QBarModelMapperPrivate::removeData(int start, int end)
412 void QBarModelMapperPrivate::removeData(int start, int end)
412 {
413 {
413 Q_UNUSED(end)
414 Q_UNUSED(end)
414 Q_UNUSED(start)
415 Q_UNUSED(start)
415 Q_UNUSED(end)
416 Q_UNUSED(end)
416 // To be implemented
417 // To be implemented
417 }
418 }
418
419
420 void QBarModelMapperPrivate::barSetsAdded(QList<QBarSet*> sets)
421 {
422 if (m_seriesSignalsBlock)
423 return;
424
425 if (sets.count() == 0)
426 return;
427
428 int firstIndex = m_series->barSets().indexOf(sets.at(0));
429 if (firstIndex == -1)
430 return;
431
432 int maxCount = 0;
433 for(int i = 0; i < sets.count(); i++)
434 if (sets.at(i)->count() > m_count)
435 maxCount = sets.at(i)->count();
436
437 if (m_count != -1 && m_count < maxCount)
438 m_count = maxCount;
439
440 m_lastBarSetSection += sets.count();
441
442 blockModelSignals();
443 int modelCapacity = m_orientation == Qt::Vertical ? m_model->rowCount() - m_first : m_model->columnCount() - m_first;
444 if (maxCount > modelCapacity) {
445 if (m_orientation == Qt::Vertical)
446 m_model->insertRows(m_model->rowCount(), maxCount - modelCapacity);
447 else
448 m_model->insertColumns(m_model->columnCount(), maxCount - modelCapacity);
449 }
450
451 if (m_orientation == Qt::Vertical)
452 m_model->insertColumns(firstIndex + m_firstBarSetSection, sets.count());
453 else
454 m_model->insertRows(firstIndex + m_firstBarSetSection, sets.count());
455
456
457 for(int i = firstIndex + m_firstBarSetSection; i < firstIndex + m_firstBarSetSection + sets.count(); i++) {
458 m_model->setHeaderData(i, m_orientation == Qt::Vertical ? Qt::Horizontal : Qt::Vertical, sets.at(i - firstIndex - m_firstBarSetSection)->label());
459 for (int j = 0; j < sets.at(i - firstIndex - m_firstBarSetSection)->count(); j++)
460 m_model->setData(barModelIndex(i, j), sets.at(i - firstIndex - m_firstBarSetSection)->at(j).y());
461 }
462 blockModelSignals(false);
463 initializeBarFromModel();
464 }
465
466 void QBarModelMapperPrivate::barSetsRemoved(QList<QBarSet*> sets)
467 {
468 if (m_seriesSignalsBlock)
469 return;
470
471 if (sets.count() == 0)
472 return;
473
474 int firstIndex = m_barSets.indexOf(sets.at(0));
475 if (firstIndex == -1)
476 return;
477
478 m_lastBarSetSection -= sets.count();
479
480 for (int i = firstIndex + sets.count() - 1; i >= firstIndex; i--)
481 m_barSets.removeAt(i);
482
483 blockModelSignals();
484 if (m_orientation == Qt::Vertical)
485 m_model->removeColumns(firstIndex + m_firstBarSetSection, sets.count());
486 else
487 m_model->removeRows(firstIndex + m_firstBarSetSection, sets.count());
488 blockModelSignals(false);
489 initializeBarFromModel();
490 }
491
492 void QBarModelMapperPrivate::valuesAdded(int index, int count)
493 {
494 if (m_seriesSignalsBlock)
495 return;
496
497 if (m_count != -1)
498 m_count += count;
499
500 int barSetIndex = m_barSets.indexOf(qobject_cast<QBarSet *>(QObject::sender()));
501
502 blockModelSignals();
503 if (m_orientation == Qt::Vertical)
504 m_model->insertRows(index + m_first, count);
505 else
506 m_model->insertColumns(index + m_first, count);
507
508 for (int j = index; j < index + count; j++)
509 m_model->setData(barModelIndex(barSetIndex + m_firstBarSetSection, j), m_barSets.at(barSetIndex)->at(j).y());
510
511 blockModelSignals(false);
512 initializeBarFromModel();
513 }
514
515 void QBarModelMapperPrivate::valuesRemoved(int index, int count)
516 {
517 if (m_seriesSignalsBlock)
518 return;
519
520 if (m_count != -1)
521 m_count -= count;
522
523 blockModelSignals();
524 if (m_orientation == Qt::Vertical)
525 m_model->removeRows(index + m_first, count);
526 else
527 m_model->removeColumns(index + m_first, count);
528
529 blockModelSignals(false);
530 initializeBarFromModel();
531 }
532
533 void QBarModelMapperPrivate::barLabelChanged()
534 {
535 if (m_seriesSignalsBlock)
536 return;
537
538 int barSetIndex = m_barSets.indexOf(qobject_cast<QBarSet *>(QObject::sender()));
539
540 blockModelSignals();
541 m_model->setHeaderData(barSetIndex + m_firstBarSetSection, m_orientation == Qt::Vertical ? Qt::Horizontal : Qt::Vertical, m_barSets.at(barSetIndex)->label());
542 blockModelSignals(false);
543 initializeBarFromModel();
544 }
545
546 void QBarModelMapperPrivate::barValueChanged(int index)
547 {
548 if (m_seriesSignalsBlock)
549 return;
550
551 int barSetIndex = m_barSets.indexOf(qobject_cast<QBarSet *>(QObject::sender()));
552
553 blockModelSignals();
554 m_model->setData(barModelIndex(barSetIndex + m_firstBarSetSection, index), m_barSets.at(barSetIndex)->at(index).y());
555 blockModelSignals(false);
556 initializeBarFromModel();
557 }
558
419 void QBarModelMapperPrivate::initializeBarFromModel()
559 void QBarModelMapperPrivate::initializeBarFromModel()
420 {
560 {
421 if (m_model == 0 || m_series == 0)
561 if (m_model == 0 || m_series == 0)
422 return;
562 return;
423
563
424 blockSeriesSignals();
564 blockSeriesSignals();
425 // clear current content
565 // clear current content
426 m_series->clear();
566 m_series->clear();
567 m_barSets.clear();
427
568
428 // create the initial bar sets
569 // create the initial bar sets
429 for (int i = m_firstBarSetSection; i <= m_lastBarSetSection; i++) {
570 for (int i = m_firstBarSetSection; i <= m_lastBarSetSection; i++) {
430 int posInBar = 0;
571 int posInBar = 0;
431 QModelIndex barIndex = barModelIndex(i, posInBar);
572 QModelIndex barIndex = barModelIndex(i, posInBar);
432 // check if there is such model index
573 // check if there is such model index
433 if (barIndex.isValid()) {
574 if (barIndex.isValid()) {
434 QBarSet *barSet = new QBarSet(m_model->headerData(i, m_orientation == Qt::Vertical ? Qt::Horizontal : Qt::Vertical).toString());
575 QBarSet *barSet = new QBarSet(m_model->headerData(i, m_orientation == Qt::Vertical ? Qt::Horizontal : Qt::Vertical).toString());
435 while (barIndex.isValid()) {
576 while (barIndex.isValid()) {
436 barSet->append(m_model->data(barIndex, Qt::DisplayRole).toDouble());
577 barSet->append(m_model->data(barIndex, Qt::DisplayRole).toDouble());
437 posInBar++;
578 posInBar++;
438 barIndex = barModelIndex(i, posInBar);
579 barIndex = barModelIndex(i, posInBar);
439 }
580 }
581 connect(barSet, SIGNAL(valuesAdded(int, int)), this, SLOT(valuesAdded(int, int)));
582 connect(barSet, SIGNAL(valuesRemoved(int, int)), this, SLOT(valuesRemoved(int, int)));
583 connect(barSet, SIGNAL(valueChanged(int)), this, SLOT(barValueChanged(int)));
584 connect(barSet, SIGNAL(labelChanged()), this, SLOT(barLabelChanged()));
440 m_series->append(barSet);
585 m_series->append(barSet);
586 m_barSets.append(barSet);
441 } else {
587 } else {
442 break;
588 break;
443 }
589 }
444 }
590 }
445 blockSeriesSignals(false);
591 blockSeriesSignals(false);
446 }
592 }
447
593
448 #include "moc_qbarmodelmapper.cpp"
594 #include "moc_qbarmodelmapper.cpp"
449 #include "moc_qbarmodelmapper_p.cpp"
595 #include "moc_qbarmodelmapper_p.cpp"
450
596
451 QTCOMMERCIALCHART_END_NAMESPACE
597 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,86 +1,95
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 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QBARMODELMAPPER_P_H
30 #ifndef QBARMODELMAPPER_P_H
31 #define QBARMODELMAPPER_P_H
31 #define QBARMODELMAPPER_P_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include <QObject>
34 #include <QObject>
35 #include "qbarmodelmapper.h"
35 #include "qbarmodelmapper.h"
36
36
37 class QModelIndex;
37 class QModelIndex;
38
38
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40
40
41 class QBarSet;
41 class QBarSet;
42
42
43 class QBarModelMapperPrivate : public QObject
43 class QBarModelMapperPrivate : public QObject
44 {
44 {
45 Q_OBJECT
45 Q_OBJECT
46 public:
46 public:
47 explicit QBarModelMapperPrivate(QBarModelMapper *q);
47 explicit QBarModelMapperPrivate(QBarModelMapper *q);
48
48
49 public Q_SLOTS:
49 public Q_SLOTS:
50 // for the model
50 // for the model
51 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
51 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
52 void modelHeaderDataUpdated(Qt::Orientation orientation, int first, int last);
52 void modelHeaderDataUpdated(Qt::Orientation orientation, int first, int last);
53 void modelRowsAdded(QModelIndex parent, int start, int end);
53 void modelRowsAdded(QModelIndex parent, int start, int end);
54 void modelRowsRemoved(QModelIndex parent, int start, int end);
54 void modelRowsRemoved(QModelIndex parent, int start, int end);
55 void modelColumnsAdded(QModelIndex parent, int start, int end);
55 void modelColumnsAdded(QModelIndex parent, int start, int end);
56 void modelColumnsRemoved(QModelIndex parent, int start, int end);
56 void modelColumnsRemoved(QModelIndex parent, int start, int end);
57
57
58 // for the series
59 void barSetsAdded(QList<QBarSet*> sets);
60 void barSetsRemoved(QList<QBarSet*> sets);
61 void valuesAdded(int index, int count);
62 void valuesRemoved(int index, int count);
63 void barLabelChanged();
64 void barValueChanged(int index);
65
58 void initializeBarFromModel();
66 void initializeBarFromModel();
59
67
60 private:
68 private:
61 QBarSet* barSet(QModelIndex index);
69 QBarSet* barSet(QModelIndex index);
62 QModelIndex barModelIndex(int barSection, int posInBar);
70 QModelIndex barModelIndex(int barSection, int posInBar);
63 void insertData(int start, int end);
71 void insertData(int start, int end);
64 void removeData(int start, int end);
72 void removeData(int start, int end);
65 void blockModelSignals(bool block = true);
73 void blockModelSignals(bool block = true);
66 void blockSeriesSignals(bool block = true);
74 void blockSeriesSignals(bool block = true);
67
75
68 private:
76 private:
69 QBarSeries *m_series;
77 QBarSeries *m_series;
78 QList<QBarSet*> m_barSets;
70 QAbstractItemModel *m_model;
79 QAbstractItemModel *m_model;
71 int m_first;
80 int m_first;
72 int m_count;
81 int m_count;
73 Qt::Orientation m_orientation;
82 Qt::Orientation m_orientation;
74 int m_firstBarSetSection;
83 int m_firstBarSetSection;
75 int m_lastBarSetSection;
84 int m_lastBarSetSection;
76 bool m_seriesSignalsBlock;
85 bool m_seriesSignalsBlock;
77 bool m_modelSignalsBlock;
86 bool m_modelSignalsBlock;
78
87
79 private:
88 private:
80 QBarModelMapper *q_ptr;
89 QBarModelMapper *q_ptr;
81 Q_DECLARE_PUBLIC(QBarModelMapper)
90 Q_DECLARE_PUBLIC(QBarModelMapper)
82 };
91 };
83
92
84 QTCOMMERCIALCHART_END_NAMESPACE
93 QTCOMMERCIALCHART_END_NAMESPACE
85
94
86 #endif // QBARMODELMAPPER_P_H
95 #endif // QBARMODELMAPPER_P_H
General Comments 0
You need to be logged in to leave comments. Login now