##// END OF EJS Templates
BarModel mapper refactored.
Marek Rosa -
r1293:221a9324f935
parent child
Show More
@@ -0,0 +1,60
1 #ifndef QBARMODELMAPPER_P_H
2 #define QBARMODELMAPPER_P_H
3
4 #include "qchartglobal.h"
5 #include <QObject>
6 #include "qbarmodelmapper.h"
7
8 class QModelIndex;
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
12 class QBarModelMapperPrivate : public QObject
13 {
14 Q_OBJECT
15 public:
16 explicit QBarModelMapperPrivate(QBarModelMapper *q);
17
18 public Q_SLOTS:
19 // for the model
20 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
21 void modelRowsAdded(QModelIndex parent, int start, int end);
22 void modelRowsRemoved(QModelIndex parent, int start, int end);
23 void modelColumnsAdded(QModelIndex parent, int start, int end);
24 void modelColumnsRemoved(QModelIndex parent, int start, int end);
25
26 // // for the series
27 // void handlePointAdded(int pointPos);
28 // void handlePointRemoved(int pointPos);
29 // void handlePointReplaced(int pointPos);
30
31 void initializeBarFromModel();
32
33 private:
34 QModelIndex barModelIndex(int barSection, int posInBar);
35 QModelIndex categoriesModelIndex(int posInCategories);
36 void insertData(int start, int end);
37 void removeData(int start, int end);
38 void blockModelSignals(bool block = true);
39 void blockSeriesSignals(bool block = true);
40
41 private:
42 QBarSeries *m_series;
43 QAbstractItemModel *m_model;
44 int m_first;
45 int m_count;
46 Qt::Orientation m_orientation;
47 int m_firstBarSection;
48 int m_lastBarSection;
49 int m_categoriesSection;
50 bool m_seriesSignalsBlock;
51 bool m_modelSignalsBlock;
52
53 private:
54 QBarModelMapper *q_ptr;
55 Q_DECLARE_PUBLIC(QBarModelMapper)
56 };
57
58 QTCOMMERCIALCHART_END_NAMESPACE
59
60 #endif // QBARMODELMAPPER_P_H
@@ -25,6 +25,7 PRIVATE_HEADERS += \
25 $$PWD/qstackedbarseries_p.h\
25 $$PWD/qstackedbarseries_p.h\
26 $$PWD/qpercentbarseries_p.h \
26 $$PWD/qpercentbarseries_p.h \
27 $$PWD/qgroupedbarseries_p.h \
27 $$PWD/qgroupedbarseries_p.h \
28 $$PWD/qbarmodelmapper_p.h
28
29
29 PUBLIC_HEADERS += \
30 PUBLIC_HEADERS += \
30 $$PWD/qbarseries.h \
31 $$PWD/qbarseries.h \
@@ -1,95 +1,403
1 #include "qbarmodelmapper.h"
1 #include "qbarmodelmapper.h"
2 #include "qbarmodelmapper_p.h"
3 #include "qbarseries.h"
4 #include "qbarset.h"
5 #include <QAbstractItemModel>
2
6
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
8
5 QBarModelMapper::QBarModelMapper(QObject *parent) :
9 QBarModelMapper::QBarModelMapper(QObject *parent) :
6 QObject(parent),
10 QObject(parent),
7 m_first(0),
11 d_ptr(new QBarModelMapperPrivate(this))
8 m_count(-1),
12 {
9 m_orientation(Qt::Vertical),
13 }
10 m_mapBarBottom(-1),
14
11 m_mapBarTop(-1),
15 QAbstractItemModel* QBarModelMapper::model() const
12 m_mapCategories(-1)
16 {
17 Q_D(const QBarModelMapper);
18 return d->m_model;
19 }
20
21 void QBarModelMapper::setModel(QAbstractItemModel *model)
22 {
23 if (model == 0)
24 return;
25
26 Q_D(QBarModelMapper);
27 if (d->m_model) {
28 disconnect(d->m_model, 0, d, 0);
29 }
30
31 d->m_model = model;
32 d->initializeBarFromModel();
33 // connect signals from the model
34 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
35 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
36 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
37 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
38 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
39 }
40
41 QBarSeries* QBarModelMapper::series() const
42 {
43 Q_D(const QBarModelMapper);
44 return d->m_series;
45 }
46
47 void QBarModelMapper::setSeries(QBarSeries *series)
13 {
48 {
49 Q_D(QBarModelMapper);
50 if (d->m_series) {
51 disconnect(d->m_series, 0, d, 0);
52 }
53
54 if (series == 0)
55 return;
56
57 d->m_series = series;
58 d->initializeBarFromModel();
59 // connect the signals from the series
60 // connect(d->m_series, SIGNAL(pointAdded(int)), d, SLOT(handlePointAdded(int)));
61 // connect(d->m_series, SIGNAL(pointRemoved(int)), d, SLOT(handlePointRemoved(int)));
62 // connect(d->m_series, SIGNAL(pointReplaced(int)), d, SLOT(handlePointReplaced(int)));
14 }
63 }
15
64
16 int QBarModelMapper::first() const
65 int QBarModelMapper::first() const
17 {
66 {
18 return m_first;
67 Q_D(const QBarModelMapper);
68 return d->m_first;
19 }
69 }
20
70
21 void QBarModelMapper::setFirst(int first)
71 void QBarModelMapper::setFirst(int first)
22 {
72 {
23 m_first = qMax(first, 0);
73 Q_D(QBarModelMapper);
24 emit updated();
74 d->m_first = qMax(first, 0);
75 d->initializeBarFromModel();
25 }
76 }
26
77
27 int QBarModelMapper::count() const
78 int QBarModelMapper::count() const
28 {
79 {
29 return m_count;
80 Q_D(const QBarModelMapper);
81 return d->m_count;
30 }
82 }
31
83
32 void QBarModelMapper::setCount(int count)
84 void QBarModelMapper::setCount(int count)
33 {
85 {
34 m_count = qMax(count, -1);
86 Q_D(QBarModelMapper);
35 emit updated();
87 d->m_count = qMax(count, -1);
88 d->initializeBarFromModel();
36 }
89 }
37
90
38 Qt::Orientation QBarModelMapper::orientation() const
91 Qt::Orientation QBarModelMapper::orientation() const
39 {
92 {
40 return m_orientation;
93 Q_D(const QBarModelMapper);
94 return d->m_orientation;
41 }
95 }
42
96
43 void QBarModelMapper::setOrientation(Qt::Orientation orientation)
97 void QBarModelMapper::setOrientation(Qt::Orientation orientation)
44 {
98 {
45 m_orientation = orientation;
99 Q_D(QBarModelMapper);
46 emit updated();
100 d->m_orientation = orientation;
101 d->initializeBarFromModel();
47 }
102 }
48
103
49 int QBarModelMapper::mapBarBottom() const
104 int QBarModelMapper::firstBarSection() const
50 {
105 {
51 return m_mapBarBottom;
106 Q_D(const QBarModelMapper);
107 return d->m_firstBarSection;
52 }
108 }
53
109
54 void QBarModelMapper::setMapBarBottom(int mapValues)
110 void QBarModelMapper::setFirstBarSection(int firstBarSection)
55 {
111 {
56 m_mapBarBottom = mapValues;
112 Q_D(QBarModelMapper);
57 emit updated();
113 d->m_firstBarSection = firstBarSection;
114 d->initializeBarFromModel();
58 }
115 }
59
116
60 int QBarModelMapper::mapBarTop() const
117 int QBarModelMapper::lastBarSection() const
61 {
118 {
62 return m_mapBarTop;
119 Q_D(const QBarModelMapper);
120 return d->m_lastBarSection;
63 }
121 }
64
122
65 void QBarModelMapper::setMapBarTop(int mapLabels)
123 void QBarModelMapper::setLastBarSection(int lastBarSection)
66 {
124 {
67 m_mapBarTop = mapLabels;
125 Q_D(QBarModelMapper);
68 emit updated();
126 d->m_lastBarSection = lastBarSection;
127 d->initializeBarFromModel();
69 }
128 }
70
129
71 int QBarModelMapper::mapCategories() const
130 int QBarModelMapper::categoriesSection() const
72 {
131 {
73 return m_mapCategories;
132 Q_D(const QBarModelMapper);
133 return d->m_categoriesSection;
74 }
134 }
75
135
76 void QBarModelMapper::setMapCategories(int mapCategories)
136 void QBarModelMapper::setCategoriesSection(int categoriesSection)
77 {
137 {
78 m_mapCategories = mapCategories;
138 Q_D(QBarModelMapper);
79 emit updated();
139 d->m_categoriesSection = categoriesSection;
140 d->initializeBarFromModel();
80 }
141 }
81
142
82 void QBarModelMapper::reset()
143 void QBarModelMapper::reset()
83 {
144 {
84 m_first = 0;
145 Q_D(QBarModelMapper);
85 m_count = -1;
146 d->m_first = 0;
86 m_orientation = Qt::Vertical;
147 d->m_count = -1;
87 m_mapBarBottom = -1;
148 d->m_orientation = Qt::Vertical;
88 m_mapBarTop = -1;
149 d->m_firstBarSection = -1;
89 m_mapCategories = -1;
150 d->m_lastBarSection = -1;
90 emit updated();
151 d->m_categoriesSection = -1;
152 d->initializeBarFromModel();
153 }
154
155 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
156
157 QBarModelMapperPrivate::QBarModelMapperPrivate(QBarModelMapper *q) :
158 m_series(0),
159 m_model(0),
160 m_first(0),
161 m_count(-1),
162 m_orientation(Qt::Vertical),
163 m_firstBarSection(-1),
164 m_lastBarSection(-1),
165 m_categoriesSection(-1),
166 m_seriesSignalsBlock(false),
167 m_modelSignalsBlock(false),
168 q_ptr(q)
169 {
170 }
171
172 void QBarModelMapperPrivate::blockModelSignals(bool block)
173 {
174 m_modelSignalsBlock = block;
175 }
176
177 void QBarModelMapperPrivate::blockSeriesSignals(bool block)
178 {
179 m_seriesSignalsBlock = block;
180 }
181
182 QModelIndex QBarModelMapperPrivate::barModelIndex(int barSection, int posInBar)
183 {
184 if (m_count != -1 && posInBar >= m_count)
185 return QModelIndex(); // invalid
186
187 if (barSection < m_firstBarSection || barSection > m_lastBarSection)
188 return QModelIndex(); // invalid
189
190 if (m_orientation == Qt::Vertical)
191 return m_model->index(posInBar + m_first, barSection);
192 else
193 return m_model->index(barSection, posInBar + m_first);
194 }
195
196 QModelIndex QBarModelMapperPrivate::categoriesModelIndex(int posInCategories)
197 {
198 if (m_count != -1 && posInCategories >= m_count)
199 return QModelIndex(); // invalid
200
201 if (m_orientation == Qt::Vertical)
202 return m_model->index(posInCategories + m_first, m_categoriesSection);
203 else
204 return m_model->index(m_categoriesSection, posInCategories + m_first);
205 }
206
207 void QBarModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
208 {
209 Q_UNUSED(topLeft)
210 Q_UNUSED(bottomRight)
211 if (m_modelSignalsBlock)
212 return;
213
214 // blockSeriesSignals();
215 // QModelIndex index;
216 // QPointF oldPoint;
217 // QPointF newPoint;
218 // for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
219 // for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
220 // index = topLeft.sibling(row, column);
221 // if (m_orientation == Qt::Vertical && (index.column() == m_xSection|| index.column() == m_ySection)) {
222 // if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) {
223 // oldPoint = m_series->points().at(index.row() - m_first);
224 // newPoint.setX(m_model->data(m_model->index(index.row(), m_xSection)).toReal());
225 // newPoint.setY(m_model->data(m_model->index(index.row(), m_ySection)).toReal());
226 // }
227 // } else if (m_orientation == Qt::Horizontal && (index.row() == m_xSection || index.row() == m_ySection)) {
228 // if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count)) {
229 // oldPoint = m_series->points().at(index.column() - m_first);
230 // newPoint.setX(m_model->data(m_model->index(m_xSection, index.column())).toReal());
231 // newPoint.setY(m_model->data(m_model->index(m_ySection, index.column())).toReal());
232 // }
233 // } else {
234 // continue;
235 // }
236 // m_series->replace(oldPoint, newPoint);
237 // }
238 // blockSeriesSignals(false);
239 // }
240 }
241
242 void QBarModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
243 {
244 Q_UNUSED(parent);
245 if (m_modelSignalsBlock)
246 return;
247
248 blockSeriesSignals();
249 if (m_orientation == Qt::Vertical)
250 insertData(start, end);
251 else if (start <= m_firstBarSection || start <= m_lastBarSection || start <= m_categoriesSection) // if the changes affect the map - reinitialize
252 initializeBarFromModel();
253 blockSeriesSignals(false);
254 }
255
256 void QBarModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
257 {
258 Q_UNUSED(parent);
259 if (m_modelSignalsBlock)
260 return;
261
262 blockSeriesSignals();
263 if (m_orientation == Qt::Vertical)
264 removeData(start, end);
265 else if (start <= m_firstBarSection || start <= m_lastBarSection || start <= m_categoriesSection) // if the changes affect the map - reinitialize
266 initializeBarFromModel();
267 blockSeriesSignals(false);
268 }
269
270 void QBarModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
271 {
272 Q_UNUSED(parent);
273 if (m_modelSignalsBlock)
274 return;
275
276 blockSeriesSignals();
277 if (m_orientation == Qt::Horizontal)
278 insertData(start, end);
279 else if (start <= m_firstBarSection || start <= m_lastBarSection || start <= m_categoriesSection) // if the changes affect the map - reinitialize
280 initializeBarFromModel();
281 blockSeriesSignals(false);
282 }
283
284 void QBarModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
285 {
286 Q_UNUSED(parent);
287 if (m_modelSignalsBlock)
288 return;
289
290 blockSeriesSignals();
291 if (m_orientation == Qt::Horizontal)
292 removeData(start, end);
293 else if (start <= m_firstBarSection || start <= m_lastBarSection || start <= m_categoriesSection) // if the changes affect the map - reinitialize
294 initializeBarFromModel();
295 blockSeriesSignals(false);
296 }
297
298 void QBarModelMapperPrivate::insertData(int start, int end)
299 {
300 Q_UNUSED(end)
301 if (m_model == 0 || m_series == 0)
302 return;
303
304 if (m_count != -1 && start >= m_first + m_count) {
305 return;
306 } /*else {
307 int addedCount = end - start + 1;
308 if (m_count != -1 && addedCount > m_count)
309 addedCount = m_count;
310 int first = qMax(start, m_first);
311 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
312 for (int i = first; i <= last; i++) {
313 QPointF point;
314 point.setX(m_model->data(xModelIndex(i - m_first), Qt::DisplayRole).toDouble());
315 point.setY(m_model->data(yModelIndex(i - m_first), Qt::DisplayRole).toDouble());
316 m_series->insert(i - m_first, point);
317 }
318
319 // remove excess of slices (abouve m_count)
320 if (m_count != -1 && m_series->points().size() > m_count)
321 for (int i = m_series->points().size() - 1; i >= m_count; i--) {
322 m_series->remove(m_series->points().at(i));
323 }
324 }*/
325 }
326
327 void QBarModelMapperPrivate::removeData(int start, int end)
328 {
329 Q_UNUSED(end)
330 if (m_model == 0 || m_series == 0)
331 return;
332
333 // int removedCount = end - start + 1;
334 if (m_count != -1 && start >= m_first + m_count) {
335 return;
336 } /*else {
337 int toRemove = qMin(m_series->count(), removedCount); // first find how many items can actually be removed
338 int first = qMax(start, m_first); // get the index of the first item that will be removed.
339 int last = qMin(first + toRemove - 1, m_series->count() + m_first - 1); // get the index of the last item that will be removed.
340 for (int i = last; i >= first; i--) {
341 m_series->remove(m_series->points().at(i - m_first));
342 }
343
344 if (m_count != -1) {
345 int itemsAvailable; // check how many are available to be added
346 if (m_orientation == Qt::Vertical)
347 itemsAvailable = m_model->rowCount() - m_first - m_series->count();
348 else
349 itemsAvailable = m_model->columnCount() - m_first - m_series->count();
350 int toBeAdded = qMin(itemsAvailable, m_count - m_series->count()); // add not more items than there is space left to be filled.
351 int currentSize = m_series->count();
352 if (toBeAdded > 0)
353 for (int i = m_series->count(); i < currentSize + toBeAdded; i++) {
354 QPointF point;
355 point.setX(m_model->data(xModelIndex(i), Qt::DisplayRole).toDouble());
356 point.setY(m_model->data(yModelIndex(i), Qt::DisplayRole).toDouble());
357 m_series->insert(i, point);
358 }
359 }
360 }*/
361 }
362
363 void QBarModelMapperPrivate::initializeBarFromModel()
364 {
365 if (m_model == 0 || m_series == 0)
366 return;
367
368 blockSeriesSignals();
369 // clear current content
370 // m_series->clear();
371
372 // create the initial bar sets
373 for (int i = m_firstBarSection; i <= m_lastBarSection; i++) {
374 int posInBar = 0;
375 QModelIndex barIndex = barModelIndex(i, posInBar);
376 QBarSet *barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal).toString());
377 // QModelIndex yIndex = yModelIndex(pointPos);
378 while (barIndex.isValid()) {
379 barSet->append(m_model->data(barIndex, Qt::DisplayRole).toDouble());
380 posInBar++;
381 barIndex = barModelIndex(i, posInBar);
382 }
383 m_series->append(barSet);
384 }
385
386 if (m_categoriesSection != -1) {
387 int posInCategories = 0;
388 QStringList categories;
389 QModelIndex categoriesIndex = categoriesModelIndex(posInCategories);
390 while (categoriesIndex.isValid()) {
391 categories.append(m_model->data(categoriesIndex, Qt::DisplayRole).toString());
392 posInCategories++;
393 categoriesIndex = categoriesModelIndex(posInCategories);
394 }
395 m_series->setCategories(categories);
396 }
397 blockSeriesSignals(false);
91 }
398 }
92
399
93 #include "moc_qbarmodelmapper.cpp"
400 #include "moc_qbarmodelmapper.cpp"
401 #include "moc_qbarmodelmapper_p.cpp"
94
402
95 QTCOMMERCIALCHART_END_NAMESPACE
403 QTCOMMERCIALCHART_END_NAMESPACE
@@ -4,45 +4,59
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QObject>
5 #include <QObject>
6
6
7 class QAbstractItemModel;
8
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
10
11 class QBarModelMapperPrivate;
12 class QBarSeries;
13
9 class QTCOMMERCIALCHART_EXPORT QBarModelMapper : public QObject
14 class QTCOMMERCIALCHART_EXPORT QBarModelMapper : public QObject
10 {
15 {
11 Q_OBJECT
16 Q_OBJECT
12 public:
17 Q_PROPERTY(QBarSeries *series READ series WRITE setSeries)
18 Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel)
19 Q_PROPERTY(int first READ first WRITE setFirst)
20 Q_PROPERTY(int count READ count WRITE setCount)
21 Q_ENUMS(Qt::Orientation)
22
23 protected:
13 explicit QBarModelMapper(QObject *parent = 0);
24 explicit QBarModelMapper(QObject *parent = 0);
14
25
26 public:
27 QAbstractItemModel* model() const;
28 void setModel(QAbstractItemModel *model);
29
30 QBarSeries* series() const;
31 void setSeries(QBarSeries *series);
32
15 int first() const;
33 int first() const;
16 void setFirst(int first);
34 void setFirst(int first);
17
35
18 int count() const;
36 int count() const;
19 void setCount(int count);
37 void setCount(int count);
20
38
21 Qt::Orientation orientation() const;
39 int firstBarSection() const;
22 void setOrientation(Qt::Orientation orientation);
40 void setFirstBarSection(int firstBarSection);
23
41
24 int mapBarBottom() const;
42 int lastBarSection() const;
25 void setMapBarBottom(int mapBarBottom);
43 void setLastBarSection(int lastBarSection);
26
44
27 int mapBarTop() const;
45 int categoriesSection() const;
28 void setMapBarTop(int mapBarTop);
46 void setCategoriesSection(int categoriesSection);
29
47
30 int mapCategories() const;
48 protected:
31 void setMapCategories(int mapCategories);
49 Qt::Orientation orientation() const;
50 void setOrientation(Qt::Orientation orientation);
32
51
33 void reset();
52 void reset();
34
53
35 Q_SIGNALS:
54 Q_SIGNALS:
36 void updated();
55 void updated();
37
56
38 private:
57 protected:
39 int m_first;
58 QBarModelMapperPrivate * const d_ptr;
40 int m_count;
59 Q_DECLARE_PRIVATE(QBarModelMapper)
41 Qt::Orientation m_orientation;
42 int m_mapBarBottom;
43 int m_mapBarTop;
44 int m_mapCategories;
45
46 };
60 };
47
61
48 QTCOMMERCIALCHART_END_NAMESPACE
62 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now