##// END OF EJS Templates
Added documentation for BarModelMapper classes
Marek Rosa -
r1348:da176c513ee6
parent child
Show More
@@ -1,432 +1,506
1 #include "qbarmodelmapper.h"
1 #include "qbarmodelmapper.h"
2 #include "qbarmodelmapper_p.h"
2 #include "qbarmodelmapper_p.h"
3 #include "qbarseries.h"
3 #include "qbarseries.h"
4 #include "qbarset.h"
4 #include "qbarset.h"
5 #include "qchart.h"
5 #include "qchart.h"
6 #include "qaxis.h"
6 #include "qaxis.h"
7 #include <QAbstractItemModel>
7 #include <QAbstractItemModel>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 /*!
11 /*!
12 \property QBarModelMapper::series
13 \brief Defines the QPieSeries object that is used by the mapper.
14
15 All the data in the series in the series is discarded when it is set to the mapper.
16 When new series is specified the old series is disconnected (it preserves its data)
17 */
18
19 /*!
20 \property QBarModelMapper::model
21 \brief Defines the model that is used by the mapper.
22 */
23
24 /*!
25 \property QBarModelMapper::first
26 \brief Defines which item of the model's row/column should be mapped as the value of the first QBarSet in the series.
27
28 Minimal and default value is: 0
29 */
30
31 /*!
32 \property QBarModelMapper::count
33 \brief Defines the number of rows/columns of the model that are mapped as the data for QBarSeries
34
35 Minimal and default value is: -1 (count limited by the number of rows/columns in the model)
36 */
37
38 /*!
39 \class QBarModelMapper
40 \brief part of QtCommercial chart API.
41 \mainclass
42
43 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.
44 Curently it is NOT possible to use both QAbstractItemModel and QXYSeries model API.
45 When the series is set to the mapper the QBarSeries and QBarSet API that affect the data (append, setValue, remove) should not be used.
46 The model and the QBarSeries won't be kept in sync. Model API should be used to insert,remove,modify BarSets.
47 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
48 */
49
50 /*!
12 Constructs a mapper object which is a child of \a parent.
51 Constructs a mapper object which is a child of \a parent.
13 */
52 */
14 QBarModelMapper::QBarModelMapper(QObject *parent) :
53 QBarModelMapper::QBarModelMapper(QObject *parent) :
15 QObject(parent),
54 QObject(parent),
16 d_ptr(new QBarModelMapperPrivate(this))
55 d_ptr(new QBarModelMapperPrivate(this))
17 {
56 {
18 }
57 }
19
58
20 QAbstractItemModel* QBarModelMapper::model() const
59 QAbstractItemModel* QBarModelMapper::model() const
21 {
60 {
22 Q_D(const QBarModelMapper);
61 Q_D(const QBarModelMapper);
23 return d->m_model;
62 return d->m_model;
24 }
63 }
25
64
26 void QBarModelMapper::setModel(QAbstractItemModel *model)
65 void QBarModelMapper::setModel(QAbstractItemModel *model)
27 {
66 {
28 if (model == 0)
67 if (model == 0)
29 return;
68 return;
30
69
31 Q_D(QBarModelMapper);
70 Q_D(QBarModelMapper);
32 if (d->m_model) {
71 if (d->m_model) {
33 disconnect(d->m_model, 0, d, 0);
72 disconnect(d->m_model, 0, d, 0);
34 }
73 }
35
74
36 d->m_model = model;
75 d->m_model = model;
37 d->initializeBarFromModel();
76 d->initializeBarFromModel();
38 // connect signals from the model
77 // connect signals from the model
39 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
78 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
40 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
79 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
41 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
80 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
42 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
81 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
43 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
82 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
44 }
83 }
45
84
46 QBarSeries* QBarModelMapper::series() const
85 QBarSeries* QBarModelMapper::series() const
47 {
86 {
48 Q_D(const QBarModelMapper);
87 Q_D(const QBarModelMapper);
49 return d->m_series;
88 return d->m_series;
50 }
89 }
51
90
52 void QBarModelMapper::setSeries(QBarSeries *series)
91 void QBarModelMapper::setSeries(QBarSeries *series)
53 {
92 {
54 Q_D(QBarModelMapper);
93 Q_D(QBarModelMapper);
55 if (d->m_series) {
94 if (d->m_series) {
56 disconnect(d->m_series, 0, d, 0);
95 disconnect(d->m_series, 0, d, 0);
57 }
96 }
58
97
59 if (series == 0)
98 if (series == 0)
60 return;
99 return;
61
100
62 d->m_series = series;
101 d->m_series = series;
63 d->initializeBarFromModel();
102 d->initializeBarFromModel();
64 // connect the signals from the series
103 // connect the signals from the series
65 // connect(d->m_series, SIGNAL(pointAdded(int)), d, SLOT(handlePointAdded(int)));
104 // connect(d->m_series, SIGNAL(pointAdded(int)), d, SLOT(handlePointAdded(int)));
66 // connect(d->m_series, SIGNAL(pointRemoved(int)), d, SLOT(handlePointRemoved(int)));
105 // connect(d->m_series, SIGNAL(pointRemoved(int)), d, SLOT(handlePointRemoved(int)));
67 // connect(d->m_series, SIGNAL(pointReplaced(int)), d, SLOT(handlePointReplaced(int)));
106 // connect(d->m_series, SIGNAL(pointReplaced(int)), d, SLOT(handlePointReplaced(int)));
68 }
107 }
69
108
70 int QBarModelMapper::first() const
109 int QBarModelMapper::first() const
71 {
110 {
72 Q_D(const QBarModelMapper);
111 Q_D(const QBarModelMapper);
73 return d->m_first;
112 return d->m_first;
74 }
113 }
75
114
76 void QBarModelMapper::setFirst(int first)
115 void QBarModelMapper::setFirst(int first)
77 {
116 {
78 Q_D(QBarModelMapper);
117 Q_D(QBarModelMapper);
79 d->m_first = qMax(first, 0);
118 d->m_first = qMax(first, 0);
80 d->initializeBarFromModel();
119 d->initializeBarFromModel();
81 }
120 }
82
121
83 int QBarModelMapper::count() const
122 int QBarModelMapper::count() const
84 {
123 {
85 Q_D(const QBarModelMapper);
124 Q_D(const QBarModelMapper);
86 return d->m_count;
125 return d->m_count;
87 }
126 }
88
127
89 void QBarModelMapper::setCount(int count)
128 void QBarModelMapper::setCount(int count)
90 {
129 {
91 Q_D(QBarModelMapper);
130 Q_D(QBarModelMapper);
92 d->m_count = qMax(count, -1);
131 d->m_count = qMax(count, -1);
93 d->initializeBarFromModel();
132 d->initializeBarFromModel();
94 }
133 }
95
134
135 /*!
136 Returns the orientation that is used when QBarModelMapper accesses the model.
137 This mean whether the consecutive values of the bar set are read from row (Qt::Horizontal)
138 or from columns (Qt::Vertical)
139 */
96 Qt::Orientation QBarModelMapper::orientation() const
140 Qt::Orientation QBarModelMapper::orientation() const
97 {
141 {
98 Q_D(const QBarModelMapper);
142 Q_D(const QBarModelMapper);
99 return d->m_orientation;
143 return d->m_orientation;
100 }
144 }
101
145
146 /*!
147 Returns the \a orientation that is used when QBarModelMapper accesses the model.
148 This mean whether the consecutive values of the pie are read from row (Qt::Horizontal)
149 or from columns (Qt::Vertical)
150 */
102 void QBarModelMapper::setOrientation(Qt::Orientation orientation)
151 void QBarModelMapper::setOrientation(Qt::Orientation orientation)
103 {
152 {
104 Q_D(QBarModelMapper);
153 Q_D(QBarModelMapper);
105 d->m_orientation = orientation;
154 d->m_orientation = orientation;
106 d->initializeBarFromModel();
155 d->initializeBarFromModel();
107 }
156 }
108
157
158 /*!
159 Returns which section of the model is used as the data source for the first bar set
160 */
109 int QBarModelMapper::firstBarSetSection() const
161 int QBarModelMapper::firstBarSetSection() const
110 {
162 {
111 Q_D(const QBarModelMapper);
163 Q_D(const QBarModelMapper);
112 return d->m_firstBarSetSection;
164 return d->m_firstBarSetSection;
113 }
165 }
114
166
167 /*!
168 Sets the model section that is used as the data source for the first bar set
169 Parameter \a firstBarSetSection specifies the section of the model.
170 */
115 void QBarModelMapper::setFirstBarSetSection(int firstBarSetSection)
171 void QBarModelMapper::setFirstBarSetSection(int firstBarSetSection)
116 {
172 {
117 Q_D(QBarModelMapper);
173 Q_D(QBarModelMapper);
118 d->m_firstBarSetSection = firstBarSetSection;
174 d->m_firstBarSetSection = firstBarSetSection;
119 d->initializeBarFromModel();
175 d->initializeBarFromModel();
120 }
176 }
121
177
178 /*!
179 Returns which section of the model is used as the data source for the last bar set
180 */
122 int QBarModelMapper::lastBarSetSection() const
181 int QBarModelMapper::lastBarSetSection() const
123 {
182 {
124 Q_D(const QBarModelMapper);
183 Q_D(const QBarModelMapper);
125 return d->m_lastBarSetSection;
184 return d->m_lastBarSetSection;
126 }
185 }
127
186
187 /*!
188 Sets the model section that is used as the data source for the last bar set
189 Parameter \a lastBarSetSection specifies the section of the model.
190 */
128 void QBarModelMapper::setLastBarSetSection(int lastBarSetSection)
191 void QBarModelMapper::setLastBarSetSection(int lastBarSetSection)
129 {
192 {
130 Q_D(QBarModelMapper);
193 Q_D(QBarModelMapper);
131 d->m_lastBarSetSection = lastBarSetSection;
194 d->m_lastBarSetSection = lastBarSetSection;
132 d->initializeBarFromModel();
195 d->initializeBarFromModel();
133 }
196 }
134
197
198 /*!
199 Returns which section of the model is used as the data source for the x axis categories.
200 */
135 int QBarModelMapper::categoriesSection() const
201 int QBarModelMapper::categoriesSection() const
136 {
202 {
137 Q_D(const QBarModelMapper);
203 Q_D(const QBarModelMapper);
138 return d->m_categoriesSection;
204 return d->m_categoriesSection;
139 }
205 }
140
206
207 /*!
208 Sets the model section that is used as the data source for the x axis categories.
209 Parameter \a categoriesSection specifies the section of the model.
210 */
141 void QBarModelMapper::setCategoriesSection(int categoriesSection)
211 void QBarModelMapper::setCategoriesSection(int categoriesSection)
142 {
212 {
143 Q_D(QBarModelMapper);
213 Q_D(QBarModelMapper);
144 d->m_categoriesSection = categoriesSection;
214 d->m_categoriesSection = categoriesSection;
145 d->initializeBarFromModel();
215 d->initializeBarFromModel();
146 }
216 }
147
217
218 /*!
219 Resets the QBarModelMapper to the default state.
220 first: 0; count: -1; firstBarSetSection: -1; lastBarSetSection: -1; categoriesSection: -1
221 */
148 void QBarModelMapper::reset()
222 void QBarModelMapper::reset()
149 {
223 {
150 Q_D(QBarModelMapper);
224 Q_D(QBarModelMapper);
151 d->m_first = 0;
225 d->m_first = 0;
152 d->m_count = -1;
226 d->m_count = -1;
153 d->m_firstBarSetSection = -1;
227 d->m_firstBarSetSection = -1;
154 d->m_lastBarSetSection = -1;
228 d->m_lastBarSetSection = -1;
155 d->m_categoriesSection = -1;
229 d->m_categoriesSection = -1;
156 d->initializeBarFromModel();
230 d->initializeBarFromModel();
157 }
231 }
158
232
159 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
233 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
160
234
161 QBarModelMapperPrivate::QBarModelMapperPrivate(QBarModelMapper *q) :
235 QBarModelMapperPrivate::QBarModelMapperPrivate(QBarModelMapper *q) :
162 m_series(0),
236 m_series(0),
163 m_model(0),
237 m_model(0),
164 m_first(0),
238 m_first(0),
165 m_count(-1),
239 m_count(-1),
166 m_orientation(Qt::Vertical),
240 m_orientation(Qt::Vertical),
167 m_firstBarSetSection(-1),
241 m_firstBarSetSection(-1),
168 m_lastBarSetSection(-1),
242 m_lastBarSetSection(-1),
169 m_categoriesSection(-1),
243 m_categoriesSection(-1),
170 m_seriesSignalsBlock(false),
244 m_seriesSignalsBlock(false),
171 m_modelSignalsBlock(false),
245 m_modelSignalsBlock(false),
172 q_ptr(q)
246 q_ptr(q)
173 {
247 {
174 }
248 }
175
249
176 void QBarModelMapperPrivate::blockModelSignals(bool block)
250 void QBarModelMapperPrivate::blockModelSignals(bool block)
177 {
251 {
178 m_modelSignalsBlock = block;
252 m_modelSignalsBlock = block;
179 }
253 }
180
254
181 void QBarModelMapperPrivate::blockSeriesSignals(bool block)
255 void QBarModelMapperPrivate::blockSeriesSignals(bool block)
182 {
256 {
183 m_seriesSignalsBlock = block;
257 m_seriesSignalsBlock = block;
184 }
258 }
185
259
186 QBarSet* QBarModelMapperPrivate::barSet(QModelIndex index)
260 QBarSet* QBarModelMapperPrivate::barSet(QModelIndex index)
187 {
261 {
188 if (!index.isValid())
262 if (!index.isValid())
189 return 0;
263 return 0;
190
264
191 if (m_orientation == Qt::Vertical && index.column() >= m_firstBarSetSection && index.column() <= m_lastBarSetSection) {
265 if (m_orientation == Qt::Vertical && index.column() >= m_firstBarSetSection && index.column() <= m_lastBarSetSection) {
192 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) {
266 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) {
193 // if (m_model->index(index.row(), m_valuesSection).isValid() && m_model->index(index.row(), m_labelsSection).isValid())
267 // if (m_model->index(index.row(), m_valuesSection).isValid() && m_model->index(index.row(), m_labelsSection).isValid())
194 return m_series->barSets().at(index.column() - m_firstBarSetSection);
268 return m_series->barSets().at(index.column() - m_firstBarSetSection);
195 // else
269 // else
196 // return 0;
270 // return 0;
197 }
271 }
198 } else if (m_orientation == Qt::Horizontal && index.row() >= m_firstBarSetSection && index.row() <= m_lastBarSetSection) {
272 } else if (m_orientation == Qt::Horizontal && index.row() >= m_firstBarSetSection && index.row() <= m_lastBarSetSection) {
199 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
273 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
200 return m_series->barSets().at(index.row() - m_firstBarSetSection);
274 return m_series->barSets().at(index.row() - m_firstBarSetSection);
201 }
275 }
202 return 0; // This part of model has not been mapped to any slice
276 return 0; // This part of model has not been mapped to any slice
203 }
277 }
204
278
205 QModelIndex QBarModelMapperPrivate::barModelIndex(int barSection, int posInBar)
279 QModelIndex QBarModelMapperPrivate::barModelIndex(int barSection, int posInBar)
206 {
280 {
207 if (m_count != -1 && posInBar >= m_count)
281 if (m_count != -1 && posInBar >= m_count)
208 return QModelIndex(); // invalid
282 return QModelIndex(); // invalid
209
283
210 if (barSection < m_firstBarSetSection || barSection > m_lastBarSetSection)
284 if (barSection < m_firstBarSetSection || barSection > m_lastBarSetSection)
211 return QModelIndex(); // invalid
285 return QModelIndex(); // invalid
212
286
213 if (m_orientation == Qt::Vertical)
287 if (m_orientation == Qt::Vertical)
214 return m_model->index(posInBar + m_first, barSection);
288 return m_model->index(posInBar + m_first, barSection);
215 else
289 else
216 return m_model->index(barSection, posInBar + m_first);
290 return m_model->index(barSection, posInBar + m_first);
217 }
291 }
218
292
219 QModelIndex QBarModelMapperPrivate::categoriesModelIndex(int posInCategories)
293 QModelIndex QBarModelMapperPrivate::categoriesModelIndex(int posInCategories)
220 {
294 {
221 if (m_count != -1 && posInCategories >= m_count)
295 if (m_count != -1 && posInCategories >= m_count)
222 return QModelIndex(); // invalid
296 return QModelIndex(); // invalid
223
297
224 if (m_orientation == Qt::Vertical)
298 if (m_orientation == Qt::Vertical)
225 return m_model->index(posInCategories + m_first, m_categoriesSection);
299 return m_model->index(posInCategories + m_first, m_categoriesSection);
226 else
300 else
227 return m_model->index(m_categoriesSection, posInCategories + m_first);
301 return m_model->index(m_categoriesSection, posInCategories + m_first);
228 }
302 }
229
303
230 void QBarModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
304 void QBarModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
231 {
305 {
232 Q_UNUSED(topLeft)
306 Q_UNUSED(topLeft)
233 Q_UNUSED(bottomRight)
307 Q_UNUSED(bottomRight)
234
308
235 if (m_model == 0 || m_series == 0)
309 if (m_model == 0 || m_series == 0)
236 return;
310 return;
237
311
238 if (m_modelSignalsBlock)
312 if (m_modelSignalsBlock)
239 return;
313 return;
240
314
241 blockSeriesSignals();
315 blockSeriesSignals();
242 QModelIndex index;
316 QModelIndex index;
243 // QPointF oldPoint;
317 // QPointF oldPoint;
244 // QPointF newPoint;
318 // QPointF newPoint;
245 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
319 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
246 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
320 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
247 index = topLeft.sibling(row, column);
321 index = topLeft.sibling(row, column);
248 QBarSet* bar = barSet(index);
322 QBarSet* bar = barSet(index);
249 if (bar) {
323 if (bar) {
250 if (m_orientation == Qt::Vertical)
324 if (m_orientation == Qt::Vertical)
251 bar->replace(row - m_first, m_model->data(index).toReal());
325 bar->replace(row - m_first, m_model->data(index).toReal());
252 else
326 else
253 bar->replace(column - m_first, m_model->data(index).toReal());
327 bar->replace(column - m_first, m_model->data(index).toReal());
254 }
328 }
255 }
329 }
256 }
330 }
257 blockSeriesSignals(false);
331 blockSeriesSignals(false);
258 }
332 }
259
333
260 void QBarModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
334 void QBarModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
261 {
335 {
262 Q_UNUSED(parent);
336 Q_UNUSED(parent);
263 Q_UNUSED(end)
337 Q_UNUSED(end)
264 if (m_modelSignalsBlock)
338 if (m_modelSignalsBlock)
265 return;
339 return;
266
340
267 blockSeriesSignals();
341 blockSeriesSignals();
268 if (m_orientation == Qt::Vertical)
342 if (m_orientation == Qt::Vertical)
269 // insertData(start, end);
343 // insertData(start, end);
270 initializeBarFromModel();
344 initializeBarFromModel();
271 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection || start <= m_categoriesSection) // if the changes affect the map - reinitialize
345 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection || start <= m_categoriesSection) // if the changes affect the map - reinitialize
272 initializeBarFromModel();
346 initializeBarFromModel();
273 blockSeriesSignals(false);
347 blockSeriesSignals(false);
274 }
348 }
275
349
276 void QBarModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
350 void QBarModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
277 {
351 {
278 Q_UNUSED(parent);
352 Q_UNUSED(parent);
279 Q_UNUSED(end)
353 Q_UNUSED(end)
280 if (m_modelSignalsBlock)
354 if (m_modelSignalsBlock)
281 return;
355 return;
282
356
283 blockSeriesSignals();
357 blockSeriesSignals();
284 if (m_orientation == Qt::Vertical)
358 if (m_orientation == Qt::Vertical)
285 // removeData(start, end);
359 // removeData(start, end);
286 initializeBarFromModel();
360 initializeBarFromModel();
287 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection || start <= m_categoriesSection) // if the changes affect the map - reinitialize
361 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection || start <= m_categoriesSection) // if the changes affect the map - reinitialize
288 initializeBarFromModel();
362 initializeBarFromModel();
289 blockSeriesSignals(false);
363 blockSeriesSignals(false);
290 }
364 }
291
365
292 void QBarModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
366 void QBarModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
293 {
367 {
294 Q_UNUSED(parent);
368 Q_UNUSED(parent);
295 Q_UNUSED(end)
369 Q_UNUSED(end)
296 if (m_modelSignalsBlock)
370 if (m_modelSignalsBlock)
297 return;
371 return;
298
372
299 blockSeriesSignals();
373 blockSeriesSignals();
300 if (m_orientation == Qt::Horizontal)
374 if (m_orientation == Qt::Horizontal)
301 // insertData(start, end);
375 // insertData(start, end);
302 initializeBarFromModel();
376 initializeBarFromModel();
303 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection || start <= m_categoriesSection) // if the changes affect the map - reinitialize
377 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection || start <= m_categoriesSection) // if the changes affect the map - reinitialize
304 initializeBarFromModel();
378 initializeBarFromModel();
305 blockSeriesSignals(false);
379 blockSeriesSignals(false);
306 }
380 }
307
381
308 void QBarModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
382 void QBarModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
309 {
383 {
310 Q_UNUSED(parent);
384 Q_UNUSED(parent);
311 Q_UNUSED(end)
385 Q_UNUSED(end)
312 if (m_modelSignalsBlock)
386 if (m_modelSignalsBlock)
313 return;
387 return;
314
388
315 blockSeriesSignals();
389 blockSeriesSignals();
316 if (m_orientation == Qt::Horizontal)
390 if (m_orientation == Qt::Horizontal)
317 // removeData(start, end);
391 // removeData(start, end);
318 initializeBarFromModel();
392 initializeBarFromModel();
319 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection || start <= m_categoriesSection) // if the changes affect the map - reinitialize
393 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection || start <= m_categoriesSection) // if the changes affect the map - reinitialize
320 initializeBarFromModel();
394 initializeBarFromModel();
321 blockSeriesSignals(false);
395 blockSeriesSignals(false);
322 }
396 }
323
397
324 void QBarModelMapperPrivate::insertData(int start, int end)
398 void QBarModelMapperPrivate::insertData(int start, int end)
325 {
399 {
326 Q_UNUSED(end)
400 Q_UNUSED(end)
327 if (m_model == 0 || m_series == 0)
401 if (m_model == 0 || m_series == 0)
328 return;
402 return;
329
403
330 if (m_count != -1 && start >= m_first + m_count) {
404 if (m_count != -1 && start >= m_first + m_count) {
331 return;
405 return;
332 } /*else {
406 } /*else {
333 int addedCount = end - start + 1;
407 int addedCount = end - start + 1;
334 if (m_count != -1 && addedCount > m_count)
408 if (m_count != -1 && addedCount > m_count)
335 addedCount = m_count;
409 addedCount = m_count;
336 int first = qMax(start, m_first);
410 int first = qMax(start, m_first);
337 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
411 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
338 for (int k = 0; k < m_series->barSets().count(); k++) {
412 for (int k = 0; k < m_series->barSets().count(); k++) {
339 for (int i = first; i <= last; i++) {
413 for (int i = first; i <= last; i++) {
340 QBar point;
414 QBar point;
341 point.setX(m_model->data(xModelIndex(i - m_first), Qt::DisplayRole).toDouble());
415 point.setX(m_model->data(xModelIndex(i - m_first), Qt::DisplayRole).toDouble());
342 point.setY(m_model->data(yModelIndex(i - m_first), Qt::DisplayRole).toDouble());
416 point.setY(m_model->data(yModelIndex(i - m_first), Qt::DisplayRole).toDouble());
343 m_series->insert(i - m_first, point);
417 m_series->insert(i - m_first, point);
344 }
418 }
345 >>>>>>> Stashed changes
419 >>>>>>> Stashed changes
346 }
420 }
347
421
348 // remove excess of slices (abouve m_count)
422 // remove excess of slices (abouve m_count)
349 if (m_count != -1 && m_series->points().size() > m_count)
423 if (m_count != -1 && m_series->points().size() > m_count)
350 for (int i = m_series->points().size() - 1; i >= m_count; i--) {
424 for (int i = m_series->points().size() - 1; i >= m_count; i--) {
351 m_series->remove(m_series->points().at(i));
425 m_series->remove(m_series->points().at(i));
352 }
426 }
353 }*/
427 }*/
354 }
428 }
355
429
356 void QBarModelMapperPrivate::removeData(int start, int end)
430 void QBarModelMapperPrivate::removeData(int start, int end)
357 {
431 {
358 Q_UNUSED(end)
432 Q_UNUSED(end)
359 if (m_model == 0 || m_series == 0)
433 if (m_model == 0 || m_series == 0)
360 return;
434 return;
361
435
362 // int removedCount = end - start + 1;
436 // int removedCount = end - start + 1;
363 if (m_count != -1 && start >= m_first + m_count) {
437 if (m_count != -1 && start >= m_first + m_count) {
364 return;
438 return;
365 } /*else {
439 } /*else {
366 int toRemove = qMin(m_series->count(), removedCount); // first find how many items can actually be removed
440 int toRemove = qMin(m_series->count(), removedCount); // first find how many items can actually be removed
367 int first = qMax(start, m_first); // get the index of the first item that will be removed.
441 int first = qMax(start, m_first); // get the index of the first item that will be removed.
368 int last = qMin(first + toRemove - 1, m_series->count() + m_first - 1); // get the index of the last item that will be removed.
442 int last = qMin(first + toRemove - 1, m_series->count() + m_first - 1); // get the index of the last item that will be removed.
369 for (int i = last; i >= first; i--) {
443 for (int i = last; i >= first; i--) {
370 m_series->remove(m_series->points().at(i - m_first));
444 m_series->remove(m_series->points().at(i - m_first));
371 }
445 }
372
446
373 if (m_count != -1) {
447 if (m_count != -1) {
374 int itemsAvailable; // check how many are available to be added
448 int itemsAvailable; // check how many are available to be added
375 if (m_orientation == Qt::Vertical)
449 if (m_orientation == Qt::Vertical)
376 itemsAvailable = m_model->rowCount() - m_first - m_series->count();
450 itemsAvailable = m_model->rowCount() - m_first - m_series->count();
377 else
451 else
378 itemsAvailable = m_model->columnCount() - m_first - m_series->count();
452 itemsAvailable = m_model->columnCount() - m_first - m_series->count();
379 int toBeAdded = qMin(itemsAvailable, m_count - m_series->count()); // add not more items than there is space left to be filled.
453 int toBeAdded = qMin(itemsAvailable, m_count - m_series->count()); // add not more items than there is space left to be filled.
380 int currentSize = m_series->count();
454 int currentSize = m_series->count();
381 if (toBeAdded > 0)
455 if (toBeAdded > 0)
382 for (int i = m_series->count(); i < currentSize + toBeAdded; i++) {
456 for (int i = m_series->count(); i < currentSize + toBeAdded; i++) {
383 QPointF point;
457 QPointF point;
384 point.setX(m_model->data(xModelIndex(i), Qt::DisplayRole).toDouble());
458 point.setX(m_model->data(xModelIndex(i), Qt::DisplayRole).toDouble());
385 point.setY(m_model->data(yModelIndex(i), Qt::DisplayRole).toDouble());
459 point.setY(m_model->data(yModelIndex(i), Qt::DisplayRole).toDouble());
386 m_series->insert(i, point);
460 m_series->insert(i, point);
387 }
461 }
388 }
462 }
389 }*/
463 }*/
390 }
464 }
391
465
392 void QBarModelMapperPrivate::initializeBarFromModel()
466 void QBarModelMapperPrivate::initializeBarFromModel()
393 {
467 {
394 if (m_model == 0 || m_series == 0)
468 if (m_model == 0 || m_series == 0)
395 return;
469 return;
396
470
397 blockSeriesSignals();
471 blockSeriesSignals();
398 // clear current content
472 // clear current content
399 m_series->clear();
473 m_series->clear();
400
474
401 // create the initial bar sets
475 // create the initial bar sets
402 for (int i = m_firstBarSetSection; i <= m_lastBarSetSection; i++) {
476 for (int i = m_firstBarSetSection; i <= m_lastBarSetSection; i++) {
403 int posInBar = 0;
477 int posInBar = 0;
404 QModelIndex barIndex = barModelIndex(i, posInBar);
478 QModelIndex barIndex = barModelIndex(i, posInBar);
405 QBarSet *barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal).toString());
479 QBarSet *barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal).toString());
406 // QModelIndex yIndex = yModelIndex(pointPos);
480 // QModelIndex yIndex = yModelIndex(pointPos);
407 while (barIndex.isValid()) {
481 while (barIndex.isValid()) {
408 barSet->append(m_model->data(barIndex, Qt::DisplayRole).toDouble());
482 barSet->append(m_model->data(barIndex, Qt::DisplayRole).toDouble());
409 posInBar++;
483 posInBar++;
410 barIndex = barModelIndex(i, posInBar);
484 barIndex = barModelIndex(i, posInBar);
411 }
485 }
412 m_series->append(barSet);
486 m_series->append(barSet);
413 }
487 }
414
488
415 if (m_series->chart() && m_categoriesSection != -1) {
489 if (m_series->chart() && m_categoriesSection != -1) {
416 int posInCategories = 0;
490 int posInCategories = 0;
417 QStringList categories;
491 QStringList categories;
418 QModelIndex categoriesIndex = categoriesModelIndex(posInCategories);
492 QModelIndex categoriesIndex = categoriesModelIndex(posInCategories);
419 while (categoriesIndex.isValid()) {
493 while (categoriesIndex.isValid()) {
420 categories.append(m_model->data(categoriesIndex, Qt::DisplayRole).toString());
494 categories.append(m_model->data(categoriesIndex, Qt::DisplayRole).toString());
421 posInCategories++;
495 posInCategories++;
422 categoriesIndex = categoriesModelIndex(posInCategories);
496 categoriesIndex = categoriesModelIndex(posInCategories);
423 }
497 }
424 m_series->chart()->axisX()->categories()->insert(categories);
498 m_series->chart()->axisX()->categories()->insert(categories);
425 }
499 }
426 blockSeriesSignals(false);
500 blockSeriesSignals(false);
427 }
501 }
428
502
429 #include "moc_qbarmodelmapper.cpp"
503 #include "moc_qbarmodelmapper.cpp"
430 #include "moc_qbarmodelmapper_p.cpp"
504 #include "moc_qbarmodelmapper_p.cpp"
431
505
432 QTCOMMERCIALCHART_END_NAMESPACE
506 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,65 +1,62
1 #ifndef QBARMODELMAPPER_H
1 #ifndef QBARMODELMAPPER_H
2 #define QBARMODELMAPPER_H
2 #define QBARMODELMAPPER_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QObject>
5 #include <QObject>
6
6
7 class QAbstractItemModel;
7 class QAbstractItemModel;
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 class QBarModelMapperPrivate;
11 class QBarModelMapperPrivate;
12 class QBarSeries;
12 class QBarSeries;
13 class QChart;
13 class QChart;
14
14
15 class QTCOMMERCIALCHART_EXPORT QBarModelMapper : public QObject
15 class QTCOMMERCIALCHART_EXPORT QBarModelMapper : public QObject
16 {
16 {
17 Q_OBJECT
17 Q_OBJECT
18 Q_PROPERTY(QBarSeries *series READ series WRITE setSeries)
18 Q_PROPERTY(QBarSeries *series READ series WRITE setSeries)
19 Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel)
19 Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel)
20 Q_PROPERTY(int first READ first WRITE setFirst)
20 Q_PROPERTY(int first READ first WRITE setFirst)
21 Q_PROPERTY(int count READ count WRITE setCount)
21 Q_PROPERTY(int count READ count WRITE setCount)
22 Q_ENUMS(Qt::Orientation)
22 Q_ENUMS(Qt::Orientation)
23
23
24 protected:
24 protected:
25 explicit QBarModelMapper(QObject *parent = 0);
25 explicit QBarModelMapper(QObject *parent = 0);
26
26
27 public:
27 public:
28 QAbstractItemModel* model() const;
28 QAbstractItemModel* model() const;
29 void setModel(QAbstractItemModel *model);
29 void setModel(QAbstractItemModel *model);
30
30
31 QBarSeries* series() const;
31 QBarSeries* series() const;
32 void setSeries(QBarSeries *series);
32 void setSeries(QBarSeries *series);
33
33
34 int first() const;
34 int first() const;
35 void setFirst(int first);
35 void setFirst(int first);
36
36
37 int count() const;
37 int count() const;
38 void setCount(int count);
38 void setCount(int count);
39
39
40 void reset();
40 void reset();
41
41
42 protected:
42 protected:
43 int firstBarSetSection() const;
43 int firstBarSetSection() const;
44 void setFirstBarSetSection(int firstBarSetSection);
44 void setFirstBarSetSection(int firstBarSetSection);
45
45
46 int lastBarSetSection() const;
46 int lastBarSetSection() const;
47 void setLastBarSetSection(int lastBarSetSection);
47 void setLastBarSetSection(int lastBarSetSection);
48
48
49 int categoriesSection() const;
49 int categoriesSection() const;
50 void setCategoriesSection(int categoriesSection);
50 void setCategoriesSection(int categoriesSection);
51
51
52 Qt::Orientation orientation() const;
52 Qt::Orientation orientation() const;
53 void setOrientation(Qt::Orientation orientation);
53 void setOrientation(Qt::Orientation orientation);
54
54
55 Q_SIGNALS:
56 void updated();
57
58 protected:
55 protected:
59 QBarModelMapperPrivate * const d_ptr;
56 QBarModelMapperPrivate * const d_ptr;
60 Q_DECLARE_PRIVATE(QBarModelMapper)
57 Q_DECLARE_PRIVATE(QBarModelMapper)
61 };
58 };
62
59
63 QTCOMMERCIALCHART_END_NAMESPACE
60 QTCOMMERCIALCHART_END_NAMESPACE
64
61
65 #endif // QBARMODELMAPPER_H
62 #endif // QBARMODELMAPPER_H
@@ -1,54 +1,75
1 #include "qhbarmodelmapper.h"
1 #include "qhbarmodelmapper.h"
2
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 /*!
5 /*!
6 \class QHBarModelMapper
6 \class QHBarModelMapper
7 \brief part of QtCommercial chart API.
7 \brief part of QtCommercial chart API.
8 \mainclass
8 \mainclass
9
9
10 Nothing here yet
10 Nothing here yet
11 */
11 */
12
12
13 /*!
13 /*!
14 \property QHBarModelMapper::firstBarSetRow
15 \brief Defines which column of the model is used as the data source for the first bar set
16
17 Default value is: -1 (invalid mapping)
18 */
19
20 /*!
21 \property QHBarModelMapper::lastBarSetRow
22 \brief Defines which column of the model is used as the data source for the last bar set
23
24 Default value is: -1 (invalid mapping)
25 */
26
27 /*!
28 \property QHBarModelMapper::categoriesRow
29 \brief Defines which row of the model is used as the data source for the x axis categories
30
31 Default value is: -1 (invalid mapping)
32 */
33
34 /*!
14 Constructs a mapper object which is a child of \a parent.
35 Constructs a mapper object which is a child of \a parent.
15 */
36 */
16 QHBarModelMapper::QHBarModelMapper(QObject *parent) :
37 QHBarModelMapper::QHBarModelMapper(QObject *parent) :
17 QBarModelMapper(parent)
38 QBarModelMapper(parent)
18 {
39 {
19 QBarModelMapper::setOrientation(Qt::Horizontal);
40 QBarModelMapper::setOrientation(Qt::Horizontal);
20 }
41 }
21
42
22 int QHBarModelMapper::firstBarSetRow() const
43 int QHBarModelMapper::firstBarSetRow() const
23 {
44 {
24 return QBarModelMapper::firstBarSetSection();
45 return QBarModelMapper::firstBarSetSection();
25 }
46 }
26
47
27 void QHBarModelMapper::setFirstBarSetRow(int firstBarSetRow)
48 void QHBarModelMapper::setFirstBarSetRow(int firstBarSetRow)
28 {
49 {
29 return QBarModelMapper::setFirstBarSetSection(firstBarSetRow);
50 return QBarModelMapper::setFirstBarSetSection(firstBarSetRow);
30 }
51 }
31
52
32 int QHBarModelMapper::lastBarSetRow() const
53 int QHBarModelMapper::lastBarSetRow() const
33 {
54 {
34 return QBarModelMapper::lastBarSetSection();
55 return QBarModelMapper::lastBarSetSection();
35 }
56 }
36
57
37 void QHBarModelMapper::setLastBarSetRow(int lastBarSetRow)
58 void QHBarModelMapper::setLastBarSetRow(int lastBarSetRow)
38 {
59 {
39 return QBarModelMapper::setLastBarSetSection(lastBarSetRow);
60 return QBarModelMapper::setLastBarSetSection(lastBarSetRow);
40 }
61 }
41
62
42 int QHBarModelMapper::categoriesRow() const
63 int QHBarModelMapper::categoriesRow() const
43 {
64 {
44 return QBarModelMapper::categoriesSection();
65 return QBarModelMapper::categoriesSection();
45 }
66 }
46
67
47 void QHBarModelMapper::setCategoriesRow(int categoriesRow)
68 void QHBarModelMapper::setCategoriesRow(int categoriesRow)
48 {
69 {
49 return QBarModelMapper::setCategoriesSection(categoriesRow);
70 return QBarModelMapper::setCategoriesSection(categoriesRow);
50 }
71 }
51
72
52 #include "moc_qhbarmodelmapper.cpp"
73 #include "moc_qhbarmodelmapper.cpp"
53
74
54 QTCOMMERCIALCHART_END_NAMESPACE
75 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,54 +1,75
1 #include "qvbarmodelmapper.h"
1 #include "qvbarmodelmapper.h"
2
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 /*!
5 /*!
6 \class QVBarModelMapper
6 \class QVBarModelMapper
7 \brief part of QtCommercial chart API.
7 \brief part of QtCommercial chart API.
8 \mainclass
8 \mainclass
9
9
10 Nothing here yet
10 Nothing here yet
11 */
11 */
12
12
13 /*!
13 /*!
14 \property QVBarModelMapper::firstBarSetColumn
15 \brief Defines which column of the model is used as the data source for the first bar set
16
17 Default value is: -1 (invalid mapping)
18 */
19
20 /*!
21 \property QVBarModelMapper::lastBarSetColumn
22 \brief Defines which column of the model is used as the data source for the last bar set
23
24 Default value is: -1 (invalid mapping)
25 */
26
27 /*!
28 \property QVBarModelMapper::categoriesColumn
29 \brief Defines which column of the model is used as the data source for the x axis categories
30
31 Default value is: -1 (invalid mapping)
32 */
33
34 /*!
14 Constructs a mapper object which is a child of \a parent.
35 Constructs a mapper object which is a child of \a parent.
15 */
36 */
16 QVBarModelMapper::QVBarModelMapper(QObject *parent) :
37 QVBarModelMapper::QVBarModelMapper(QObject *parent) :
17 QBarModelMapper(parent)
38 QBarModelMapper(parent)
18 {
39 {
19 QBarModelMapper::setOrientation(Qt::Vertical);
40 QBarModelMapper::setOrientation(Qt::Vertical);
20 }
41 }
21
42
22 int QVBarModelMapper::firstBarSetColumn() const
43 int QVBarModelMapper::firstBarSetColumn() const
23 {
44 {
24 return QBarModelMapper::firstBarSetSection();
45 return QBarModelMapper::firstBarSetSection();
25 }
46 }
26
47
27 void QVBarModelMapper::setFirstBarSetColumn(int firstBarSetColumn)
48 void QVBarModelMapper::setFirstBarSetColumn(int firstBarSetColumn)
28 {
49 {
29 return QBarModelMapper::setFirstBarSetSection(firstBarSetColumn);
50 return QBarModelMapper::setFirstBarSetSection(firstBarSetColumn);
30 }
51 }
31
52
32 int QVBarModelMapper::lastBarSetColumn() const
53 int QVBarModelMapper::lastBarSetColumn() const
33 {
54 {
34 return QBarModelMapper::lastBarSetSection();
55 return QBarModelMapper::lastBarSetSection();
35 }
56 }
36
57
37 void QVBarModelMapper::setLastBarSetColumn(int lastBarSetColumn)
58 void QVBarModelMapper::setLastBarSetColumn(int lastBarSetColumn)
38 {
59 {
39 return QBarModelMapper::setLastBarSetSection(lastBarSetColumn);
60 return QBarModelMapper::setLastBarSetSection(lastBarSetColumn);
40 }
61 }
41
62
42 int QVBarModelMapper::categoriesColumn() const
63 int QVBarModelMapper::categoriesColumn() const
43 {
64 {
44 return QBarModelMapper::categoriesSection();
65 return QBarModelMapper::categoriesSection();
45 }
66 }
46
67
47 void QVBarModelMapper::setCategoriesColumn(int categoriesColumn)
68 void QVBarModelMapper::setCategoriesColumn(int categoriesColumn)
48 {
69 {
49 return QBarModelMapper::setCategoriesSection(categoriesColumn);
70 return QBarModelMapper::setCategoriesSection(categoriesColumn);
50 }
71 }
51
72
52 #include "moc_qvbarmodelmapper.cpp"
73 #include "moc_qvbarmodelmapper.cpp"
53
74
54 QTCOMMERCIALCHART_END_NAMESPACE
75 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now