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