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