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