@@ -0,0 +1,64 | |||||
|
1 | #ifndef QXYMODELMAPPER_P_H | |||
|
2 | #define QXYMODELMAPPER_P_H | |||
|
3 | ||||
|
4 | #include "qxymodelmapper.h" | |||
|
5 | #include <QObject> | |||
|
6 | ||||
|
7 | class QModelIndex; | |||
|
8 | class QAbstractItemModel; | |||
|
9 | class QPointF; | |||
|
10 | ||||
|
11 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
12 | ||||
|
13 | class QXYModelMapper; | |||
|
14 | class QXYSeries; | |||
|
15 | ||||
|
16 | class QXYModelMapperPrivate : public QObject | |||
|
17 | { | |||
|
18 | Q_OBJECT | |||
|
19 | ||||
|
20 | public: | |||
|
21 | QXYModelMapperPrivate(QXYModelMapper *q); | |||
|
22 | ||||
|
23 | public Q_SLOTS: | |||
|
24 | // for the model | |||
|
25 | void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); | |||
|
26 | void modelRowsAdded(QModelIndex parent, int start, int end); | |||
|
27 | void modelRowsRemoved(QModelIndex parent, int start, int end); | |||
|
28 | void modelColumnsAdded(QModelIndex parent, int start, int end); | |||
|
29 | void modelColumnsRemoved(QModelIndex parent, int start, int end); | |||
|
30 | ||||
|
31 | // for the series | |||
|
32 | void handlePointAdded(int pointPos); | |||
|
33 | void handlePointRemoved(int pointPos); | |||
|
34 | void handlePointReplaced(int pointPos); | |||
|
35 | ||||
|
36 | void initializeXYFromModel(); | |||
|
37 | ||||
|
38 | private: | |||
|
39 | QModelIndex xModelIndex(int xPos); | |||
|
40 | QModelIndex yModelIndex(int yPos); | |||
|
41 | void insertData(int start, int end); | |||
|
42 | void removeData(int start, int end); | |||
|
43 | void blockModelSignals(bool block = true); | |||
|
44 | void blockSeriesSignals(bool block = true); | |||
|
45 | ||||
|
46 | private: | |||
|
47 | QXYSeries *m_series; | |||
|
48 | QAbstractItemModel *m_model; | |||
|
49 | int m_first; | |||
|
50 | int m_count; | |||
|
51 | Qt::Orientation m_orientation; | |||
|
52 | int m_xSection; | |||
|
53 | int m_ySection; | |||
|
54 | bool m_seriesSignalsBlock; | |||
|
55 | bool m_modelSignalsBlock; | |||
|
56 | ||||
|
57 | private: | |||
|
58 | QXYModelMapper *q_ptr; | |||
|
59 | Q_DECLARE_PUBLIC(QXYModelMapper) | |||
|
60 | }; | |||
|
61 | ||||
|
62 | QTCOMMERCIALCHART_END_NAMESPACE | |||
|
63 | ||||
|
64 | #endif // QXYMODELMAPPER_P_H |
@@ -1,81 +1,383 | |||||
1 | #include "qxymodelmapper.h" |
|
1 | #include "qxymodelmapper.h" | |
|
2 | #include "qxymodelmapper_p.h" | |||
|
3 | #include "qxyseries.h" | |||
|
4 | #include <QAbstractItemModel> | |||
2 |
|
5 | |||
3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
4 |
|
7 | |||
5 | QXYModelMapper::QXYModelMapper(QObject *parent): |
|
8 | QXYModelMapper::QXYModelMapper(QObject *parent): | |
6 | QObject(parent), |
|
9 | QObject(parent), | |
7 | m_first(0), |
|
10 | d_ptr(new QXYModelMapperPrivate(this)) | |
8 | m_count(-1), |
|
11 | { | |
9 | m_orientation(Qt::Vertical), |
|
12 | } | |
10 | m_xSection(-1), |
|
13 | ||
11 | m_ySection(-1) |
|
14 | QAbstractItemModel* QXYModelMapper::model() const | |
|
15 | { | |||
|
16 | Q_D(const QXYModelMapper); | |||
|
17 | return d->m_model; | |||
|
18 | } | |||
|
19 | ||||
|
20 | void QXYModelMapper::setModel(QAbstractItemModel *model) | |||
|
21 | { | |||
|
22 | if (model == 0) | |||
|
23 | return; | |||
|
24 | ||||
|
25 | Q_D(QXYModelMapper); | |||
|
26 | if (d->m_model) { | |||
|
27 | disconnect(d->m_model, 0, d, 0); | |||
|
28 | } | |||
|
29 | ||||
|
30 | d->m_model = model; | |||
|
31 | d->initializeXYFromModel(); | |||
|
32 | // connect signals from the model | |||
|
33 | connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex))); | |||
|
34 | connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int))); | |||
|
35 | connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int))); | |||
|
36 | connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int))); | |||
|
37 | connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int))); | |||
|
38 | } | |||
|
39 | ||||
|
40 | QXYSeries* QXYModelMapper::series() const | |||
12 | { |
|
41 | { | |
|
42 | Q_D(const QXYModelMapper); | |||
|
43 | return d->m_series; | |||
|
44 | } | |||
|
45 | ||||
|
46 | void QXYModelMapper::setSeries(QXYSeries *series) | |||
|
47 | { | |||
|
48 | Q_D(QXYModelMapper); | |||
|
49 | if (d->m_series) { | |||
|
50 | disconnect(d->m_series, 0, d, 0); | |||
|
51 | } | |||
|
52 | ||||
|
53 | if (series == 0) | |||
|
54 | return; | |||
|
55 | ||||
|
56 | d->m_series = series; | |||
|
57 | d->initializeXYFromModel(); | |||
|
58 | // connect the signals from the series | |||
|
59 | connect(d->m_series, SIGNAL(pointAdded(int)), d, SLOT(handlePointAdded(int))); | |||
|
60 | connect(d->m_series, SIGNAL(pointRemoved(int)), d, SLOT(handlePointRemoved(int))); | |||
|
61 | connect(d->m_series, SIGNAL(pointReplaced(int)), d, SLOT(pointReplaced(int))); | |||
13 | } |
|
62 | } | |
14 |
|
63 | |||
15 | int QXYModelMapper::first() const |
|
64 | int QXYModelMapper::first() const | |
16 | { |
|
65 | { | |
17 | return m_first; |
|
66 | Q_D(const QXYModelMapper); | |
|
67 | return d->m_first; | |||
18 | } |
|
68 | } | |
19 |
|
69 | |||
20 | void QXYModelMapper::setFirst(int first) |
|
70 | void QXYModelMapper::setFirst(int first) | |
21 | { |
|
71 | { | |
22 | m_first = qMax(first, 0); |
|
72 | Q_D(QXYModelMapper); | |
23 | // emit updated(); |
|
73 | d->m_first = qMax(first, 0); | |
|
74 | d->initializeXYFromModel(); | |||
24 | } |
|
75 | } | |
25 |
|
76 | |||
26 | int QXYModelMapper::count() const |
|
77 | int QXYModelMapper::count() const | |
27 | { |
|
78 | { | |
28 | return m_count; |
|
79 | Q_D(const QXYModelMapper); | |
|
80 | return d->m_count; | |||
29 | } |
|
81 | } | |
30 |
|
82 | |||
31 | void QXYModelMapper::setCount(int count) |
|
83 | void QXYModelMapper::setCount(int count) | |
32 | { |
|
84 | { | |
33 | m_count = qMax(count, -1); |
|
85 | Q_D(QXYModelMapper); | |
34 | // emit updated(); |
|
86 | d->m_count = qMax(count, -1); | |
|
87 | d->initializeXYFromModel(); | |||
35 | } |
|
88 | } | |
36 |
|
89 | |||
37 | Qt::Orientation QXYModelMapper::orientation() const |
|
90 | Qt::Orientation QXYModelMapper::orientation() const | |
38 | { |
|
91 | { | |
39 | return m_orientation; |
|
92 | Q_D(const QXYModelMapper); | |
|
93 | return d->m_orientation; | |||
40 | } |
|
94 | } | |
41 |
|
95 | |||
42 | void QXYModelMapper::setOrientation(Qt::Orientation orientation) |
|
96 | void QXYModelMapper::setOrientation(Qt::Orientation orientation) | |
43 | { |
|
97 | { | |
44 | m_orientation = orientation; |
|
98 | Q_D(QXYModelMapper); | |
45 | // emit updated(); |
|
99 | d->m_orientation = orientation; | |
|
100 | d->initializeXYFromModel(); | |||
46 | } |
|
101 | } | |
47 |
|
102 | |||
48 | int QXYModelMapper::xSection() const |
|
103 | int QXYModelMapper::xSection() const | |
49 | { |
|
104 | { | |
50 | return m_xSection; |
|
105 | Q_D(const QXYModelMapper); | |
|
106 | return d->m_xSection; | |||
51 | } |
|
107 | } | |
52 |
|
108 | |||
53 | void QXYModelMapper::setXSection(int xSection) |
|
109 | void QXYModelMapper::setXSection(int xSection) | |
54 | { |
|
110 | { | |
55 | m_xSection = xSection; |
|
111 | Q_D(QXYModelMapper); | |
56 | // emit updated(); |
|
112 | d->m_xSection = xSection; | |
|
113 | d->initializeXYFromModel(); | |||
57 | } |
|
114 | } | |
58 |
|
115 | |||
59 | int QXYModelMapper::ySection() const |
|
116 | int QXYModelMapper::ySection() const | |
60 | { |
|
117 | { | |
61 | return m_ySection; |
|
118 | Q_D(const QXYModelMapper); | |
|
119 | return d->m_ySection; | |||
62 | } |
|
120 | } | |
63 |
|
121 | |||
64 | void QXYModelMapper::setYSection(int ySection) |
|
122 | void QXYModelMapper::setYSection(int ySection) | |
65 | { |
|
123 | { | |
66 | m_ySection = ySection; |
|
124 | Q_D(QXYModelMapper); | |
67 | // emit updated(); |
|
125 | d->m_ySection = ySection; | |
|
126 | d->initializeXYFromModel(); | |||
68 | } |
|
127 | } | |
69 |
|
128 | |||
70 | void QXYModelMapper::reset() |
|
129 | void QXYModelMapper::reset() | |
71 | { |
|
130 | { | |
72 | m_first = 0; |
|
131 | Q_D(QXYModelMapper); | |
73 |
|
|
132 | d->m_first = 0; | |
74 | m_orientation = Qt::Vertical; |
|
133 | d->m_count = -1; | |
75 | m_xSection = -1; |
|
134 | d->m_orientation = Qt::Vertical; | |
76 |
|
|
135 | d->m_xSection = -1; | |
|
136 | d->m_ySection = -1; | |||
|
137 | d->initializeXYFromModel(); | |||
|
138 | } | |||
|
139 | ||||
|
140 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
|
141 | ||||
|
142 | QXYModelMapperPrivate::QXYModelMapperPrivate(QXYModelMapper *q) : | |||
|
143 | m_series(0), | |||
|
144 | m_model(0), | |||
|
145 | m_first(0), | |||
|
146 | m_count(-1), | |||
|
147 | m_orientation(Qt::Vertical), | |||
|
148 | m_xSection(-1), | |||
|
149 | m_ySection(-1), | |||
|
150 | m_seriesSignalsBlock(false), | |||
|
151 | m_modelSignalsBlock(false), | |||
|
152 | q_ptr(q) | |||
|
153 | { | |||
|
154 | } | |||
|
155 | ||||
|
156 | void QXYModelMapperPrivate::blockModelSignals(bool block) | |||
|
157 | { | |||
|
158 | m_modelSignalsBlock = block; | |||
|
159 | } | |||
|
160 | ||||
|
161 | void QXYModelMapperPrivate::blockSeriesSignals(bool block) | |||
|
162 | { | |||
|
163 | m_seriesSignalsBlock = block; | |||
|
164 | } | |||
|
165 | ||||
|
166 | QModelIndex QXYModelMapperPrivate::xModelIndex(int xPos) | |||
|
167 | { | |||
|
168 | if (m_count != -1 && xPos >= m_count) | |||
|
169 | return QModelIndex(); // invalid | |||
|
170 | ||||
|
171 | if (m_orientation == Qt::Vertical) | |||
|
172 | return m_model->index(xPos + m_first, m_xSection); | |||
|
173 | else | |||
|
174 | return m_model->index(m_xSection, xPos + m_first); | |||
|
175 | } | |||
|
176 | ||||
|
177 | QModelIndex QXYModelMapperPrivate::yModelIndex(int yPos) | |||
|
178 | { | |||
|
179 | if (m_count != -1 && yPos >= m_count) | |||
|
180 | return QModelIndex(); // invalid | |||
|
181 | ||||
|
182 | if (m_orientation == Qt::Vertical) | |||
|
183 | return m_model->index(yPos + m_first, m_ySection); | |||
|
184 | else | |||
|
185 | return m_model->index(m_ySection, yPos + m_first); | |||
|
186 | } | |||
|
187 | ||||
|
188 | void QXYModelMapperPrivate::handlePointAdded(int pointPos) | |||
|
189 | { | |||
|
190 | Q_UNUSED(pointPos) | |||
|
191 | } | |||
|
192 | ||||
|
193 | void QXYModelMapperPrivate::handlePointRemoved(int pointPos) | |||
|
194 | { | |||
|
195 | Q_UNUSED(pointPos) | |||
|
196 | } | |||
|
197 | ||||
|
198 | void QXYModelMapperPrivate::handlePointReplaced(int pointPos) | |||
|
199 | { | |||
|
200 | Q_UNUSED(pointPos) | |||
|
201 | } | |||
|
202 | ||||
|
203 | void QXYModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) | |||
|
204 | { | |||
|
205 | if (m_modelSignalsBlock) | |||
|
206 | return; | |||
|
207 | ||||
|
208 | blockSeriesSignals(); | |||
|
209 | QModelIndex index; | |||
|
210 | QPointF oldPoint; | |||
|
211 | QPointF newPoint; | |||
|
212 | for (int row = topLeft.row(); row <= bottomRight.row(); row++) { | |||
|
213 | for (int column = topLeft.column(); column <= bottomRight.column(); column++) { | |||
|
214 | index = topLeft.sibling(row, column); | |||
|
215 | if (m_orientation == Qt::Vertical && (index.column() == m_xSection|| index.column() == m_ySection)) { | |||
|
216 | if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) { | |||
|
217 | oldPoint = m_series->points().at(index.row() - m_first); | |||
|
218 | newPoint.setX(m_model->data(m_model->index(index.row(), m_xSection)).toReal()); | |||
|
219 | newPoint.setY(m_model->data(m_model->index(index.row(), m_ySection)).toReal()); | |||
|
220 | } | |||
|
221 | } else if (m_orientation == Qt::Horizontal && (index.row() == m_xSection || index.row() == m_ySection)) { | |||
|
222 | if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count)) { | |||
|
223 | oldPoint = m_series->points().at(index.column() - m_first); | |||
|
224 | newPoint.setX(m_model->data(m_model->index(m_xSection, index.column())).toReal()); | |||
|
225 | newPoint.setY(m_model->data(m_model->index(m_ySection, index.column())).toReal()); | |||
|
226 | } | |||
|
227 | } else { | |||
|
228 | continue; | |||
|
229 | } | |||
|
230 | m_series->replace(oldPoint, newPoint); | |||
|
231 | } | |||
|
232 | blockSeriesSignals(false); | |||
|
233 | } | |||
|
234 | } | |||
|
235 | ||||
|
236 | void QXYModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end) | |||
|
237 | { | |||
|
238 | Q_UNUSED(parent); | |||
|
239 | if (m_modelSignalsBlock) | |||
|
240 | return; | |||
|
241 | ||||
|
242 | blockSeriesSignals(); | |||
|
243 | if (m_orientation == Qt::Vertical) | |||
|
244 | insertData(start, end); | |||
|
245 | else if (start <= m_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy | |||
|
246 | initializeXYFromModel(); | |||
|
247 | blockSeriesSignals(false); | |||
|
248 | } | |||
|
249 | ||||
|
250 | void QXYModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end) | |||
|
251 | { | |||
|
252 | Q_UNUSED(parent); | |||
|
253 | if (m_modelSignalsBlock) | |||
|
254 | return; | |||
|
255 | ||||
|
256 | blockSeriesSignals(); | |||
|
257 | if (m_orientation == Qt::Vertical) | |||
|
258 | removeData(start, end); | |||
|
259 | else if (start <= m_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy | |||
|
260 | initializeXYFromModel(); | |||
|
261 | blockSeriesSignals(false); | |||
|
262 | } | |||
|
263 | ||||
|
264 | void QXYModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end) | |||
|
265 | { | |||
|
266 | Q_UNUSED(parent); | |||
|
267 | if (m_modelSignalsBlock) | |||
|
268 | return; | |||
|
269 | ||||
|
270 | blockSeriesSignals(); | |||
|
271 | if (m_orientation == Qt::Horizontal) | |||
|
272 | insertData(start, end); | |||
|
273 | else if (start <= m_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy | |||
|
274 | initializeXYFromModel(); | |||
|
275 | blockSeriesSignals(false); | |||
|
276 | } | |||
|
277 | ||||
|
278 | void QXYModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end) | |||
|
279 | { | |||
|
280 | Q_UNUSED(parent); | |||
|
281 | if (m_modelSignalsBlock) | |||
|
282 | return; | |||
|
283 | ||||
|
284 | blockSeriesSignals(); | |||
|
285 | if (m_orientation == Qt::Horizontal) | |||
|
286 | removeData(start, end); | |||
|
287 | else if (start <= m_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy | |||
|
288 | initializeXYFromModel(); | |||
|
289 | blockSeriesSignals(false); | |||
|
290 | } | |||
|
291 | ||||
|
292 | void QXYModelMapperPrivate::insertData(int start, int end) | |||
|
293 | { | |||
|
294 | if (m_model == 0 || m_series == 0) | |||
|
295 | return; | |||
|
296 | ||||
|
297 | if (m_count != -1 && start >= m_first + m_count) { | |||
|
298 | return; | |||
|
299 | } else { | |||
|
300 | int addedCount = end - start + 1; | |||
|
301 | if (m_count != -1 && addedCount > m_count) | |||
|
302 | addedCount = m_count; | |||
|
303 | int first = qMax(start, m_first); | |||
|
304 | int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1); | |||
|
305 | for (int i = first; i <= last; i++) { | |||
|
306 | QPointF point; | |||
|
307 | point.setX(m_model->data(xModelIndex(i - m_first), Qt::DisplayRole).toDouble()); | |||
|
308 | point.setY(m_model->data(yModelIndex(i - m_first), Qt::DisplayRole).toDouble()); | |||
|
309 | m_series->insert(i - m_first, point); | |||
|
310 | } | |||
|
311 | ||||
|
312 | // remove excess of slices (abouve m_count) | |||
|
313 | if (m_count != -1 && m_series->points().size() > m_count) | |||
|
314 | for (int i = m_series->points().size() - 1; i >= m_count; i--) { | |||
|
315 | m_series->remove(m_series->points().at(i)); | |||
|
316 | } | |||
|
317 | } | |||
|
318 | } | |||
|
319 | ||||
|
320 | void QXYModelMapperPrivate::removeData(int start, int end) | |||
|
321 | { | |||
|
322 | if (m_model == 0 || m_series == 0) | |||
|
323 | return; | |||
|
324 | ||||
|
325 | int removedCount = end - start + 1; | |||
|
326 | if (m_count != -1 && start >= m_first + m_count) { | |||
|
327 | return; | |||
|
328 | } else { | |||
|
329 | int toRemove = qMin(m_series->points().size(), removedCount); // first find how many items can actually be removed | |||
|
330 | int first = qMax(start, m_first); // get the index of the first item that will be removed. | |||
|
331 | int last = qMin(first + toRemove - 1, m_series->points().size() + m_first - 1); // get the index of the last item that will be removed. | |||
|
332 | for (int i = last; i >= first; i--) { | |||
|
333 | m_series->remove(m_series->points().at(i - m_first)); | |||
|
334 | } | |||
|
335 | ||||
|
336 | if (m_count != -1) { | |||
|
337 | int itemsAvailable; // check how many are available to be added | |||
|
338 | if (m_orientation == Qt::Vertical) | |||
|
339 | itemsAvailable = m_model->rowCount() - m_first - m_series->points().size(); | |||
|
340 | else | |||
|
341 | itemsAvailable = m_model->columnCount() - m_first - m_series->points().size(); | |||
|
342 | int toBeAdded = qMin(itemsAvailable, m_count - m_series->points().size()); // add not more items than there is space left to be filled. | |||
|
343 | int currentSize = m_series->points().size(); | |||
|
344 | if (toBeAdded > 0) | |||
|
345 | for (int i = m_series->points().size(); i < currentSize + toBeAdded; i++) { | |||
|
346 | QPointF point; | |||
|
347 | point.setX(m_model->data(xModelIndex(i), Qt::DisplayRole).toDouble()); | |||
|
348 | point.setY(m_model->data(yModelIndex(i), Qt::DisplayRole).toDouble()); | |||
|
349 | m_series->insert(i, point); | |||
|
350 | } | |||
|
351 | } | |||
|
352 | } | |||
|
353 | } | |||
|
354 | ||||
|
355 | void QXYModelMapperPrivate::initializeXYFromModel() | |||
|
356 | { | |||
|
357 | if (m_model == 0 || m_series == 0) | |||
|
358 | return; | |||
|
359 | ||||
|
360 | blockSeriesSignals(); | |||
|
361 | // clear current content | |||
|
362 | m_series->clear(); | |||
|
363 | ||||
|
364 | // create the initial slices set | |||
|
365 | int pointPos = 0; | |||
|
366 | QModelIndex xIndex = xModelIndex(pointPos); | |||
|
367 | QModelIndex yIndex = yModelIndex(pointPos); | |||
|
368 | while (xIndex.isValid() && yIndex.isValid()) { | |||
|
369 | QPointF point; | |||
|
370 | point.setX(m_model->data(xIndex, Qt::DisplayRole).toDouble()); | |||
|
371 | point.setY(m_model->data(yIndex, Qt::DisplayRole).toDouble()); | |||
|
372 | m_series->append(point); | |||
|
373 | pointPos++; | |||
|
374 | xIndex = xModelIndex(pointPos); | |||
|
375 | yIndex = yModelIndex(pointPos); | |||
|
376 | } | |||
|
377 | blockSeriesSignals(false); | |||
77 | } |
|
378 | } | |
78 |
|
379 | |||
79 | #include "moc_qxymodelmapper.cpp" |
|
380 | #include "moc_qxymodelmapper.cpp" | |
|
381 | #include "moc_qxymodelmapper_p.cpp" | |||
80 |
|
382 | |||
81 | QTCOMMERCIALCHART_END_NAMESPACE |
|
383 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -4,25 +4,37 | |||||
4 | #include "qchartglobal.h" |
|
4 | #include "qchartglobal.h" | |
5 | #include <QObject> |
|
5 | #include <QObject> | |
6 |
|
6 | |||
|
7 | class QAbstractItemModel; | |||
|
8 | ||||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
10 | |||
|
11 | class QXYModelMapperPrivate; | |||
|
12 | class QXYSeries; | |||
|
13 | ||||
9 | class QTCOMMERCIALCHART_EXPORT QXYModelMapper : public QObject |
|
14 | class QTCOMMERCIALCHART_EXPORT QXYModelMapper : public QObject | |
10 | { |
|
15 | { | |
11 | Q_OBJECT |
|
16 | Q_OBJECT | |
12 |
Q_PROPERTY( |
|
17 | Q_PROPERTY(QXYSeries *series READ series WRITE setSeries) | |
13 | Q_PROPERTY(int ySection READ ySection WRITE setYSection) |
|
18 | Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel) | |
14 | Q_PROPERTY(int first READ first WRITE setFirst) |
|
19 | Q_PROPERTY(int first READ first WRITE setFirst) | |
15 | Q_PROPERTY(int count READ count WRITE setCount) |
|
20 | Q_PROPERTY(int count READ count WRITE setCount) | |
16 | Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation) |
|
|||
17 | Q_ENUMS(Qt::Orientation) |
|
21 | Q_ENUMS(Qt::Orientation) | |
18 |
|
22 | |||
19 | public: |
|
23 | public: | |
|
24 | QAbstractItemModel* model() const; | |||
|
25 | void setModel(QAbstractItemModel *model); | |||
|
26 | ||||
|
27 | QXYSeries* series() const; | |||
|
28 | void setSeries(QXYSeries *series); | |||
|
29 | ||||
20 | int first() const; |
|
30 | int first() const; | |
21 | void setFirst(int first); |
|
31 | void setFirst(int first); | |
22 |
|
32 | |||
23 | int count() const; |
|
33 | int count() const; | |
24 | void setCount(int count); |
|
34 | void setCount(int count); | |
25 |
|
35 | |||
|
36 | void reset(); | |||
|
37 | ||||
26 | protected: |
|
38 | protected: | |
27 | explicit QXYModelMapper(QObject *parent = 0); |
|
39 | explicit QXYModelMapper(QObject *parent = 0); | |
28 |
|
40 | |||
@@ -35,17 +47,9 protected: | |||||
35 | int ySection() const; |
|
47 | int ySection() const; | |
36 | void setYSection(int ySection); |
|
48 | void setYSection(int ySection); | |
37 |
|
49 | |||
38 | void reset(); |
|
50 | protected: | |
39 |
|
51 | QXYModelMapperPrivate * const d_ptr; | ||
40 | Q_SIGNALS: |
|
52 | Q_DECLARE_PRIVATE(QXYModelMapper) | |
41 | void updated(); |
|
|||
42 |
|
||||
43 | private: |
|
|||
44 | int m_first; |
|
|||
45 | int m_count; |
|
|||
46 | Qt::Orientation m_orientation; |
|
|||
47 | int m_xSection; |
|
|||
48 | int m_ySection; |
|
|||
49 | }; |
|
53 | }; | |
50 |
|
54 | |||
51 | QTCOMMERCIALCHART_END_NAMESPACE |
|
55 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -162,6 +162,20 void QXYSeries::removeAll() | |||||
162 | } |
|
162 | } | |
163 | } |
|
163 | } | |
164 |
|
164 | |||
|
165 | void QXYSeries::insert(int index, const QPointF &point) | |||
|
166 | { | |||
|
167 | Q_D(QXYSeries); | |||
|
168 | d->m_points.insert(index, point); | |||
|
169 | emit d->pointAdded(index); | |||
|
170 | } | |||
|
171 | ||||
|
172 | void QXYSeries::clear() | |||
|
173 | { | |||
|
174 | Q_D(QXYSeries); | |||
|
175 | for (int i = d->m_points.size() - 1; i >= 0; i--) | |||
|
176 | remove(d->m_points.at(i)); | |||
|
177 | } | |||
|
178 | ||||
165 | /*! |
|
179 | /*! | |
166 | \internal \a pos |
|
180 | \internal \a pos | |
167 | */ |
|
181 | */ |
@@ -50,6 +50,8 public: | |||||
50 | void remove(qreal x, qreal y); |
|
50 | void remove(qreal x, qreal y); | |
51 | void remove(const QPointF &point); |
|
51 | void remove(const QPointF &point); | |
52 | void removeAll(); |
|
52 | void removeAll(); | |
|
53 | void insert(int index, const QPointF &point); | |||
|
54 | void clear(); | |||
53 |
|
55 | |||
54 | int count() const; |
|
56 | int count() const; | |
55 | QList<QPointF> points() const; |
|
57 | QList<QPointF> points() const; |
@@ -50,10 +50,7 Q_SIGNALS: | |||||
50 | void updated(); |
|
50 | void updated(); | |
51 | void pointReplaced(int index); |
|
51 | void pointReplaced(int index); | |
52 | void pointRemoved(int index); |
|
52 | void pointRemoved(int index); | |
53 | void pointsRemoved(int start, int end); |
|
|||
54 | void pointAdded(int index); |
|
53 | void pointAdded(int index); | |
55 | void pointsAdded(int start, int end); |
|
|||
56 | void reinitialized(); |
|
|||
57 |
|
54 | |||
58 | protected: |
|
55 | protected: | |
59 | QVector<QPointF> m_points; |
|
56 | QVector<QPointF> m_points; |
@@ -10,7 +10,8 SOURCES += \ | |||||
10 |
|
10 | |||
11 | PRIVATE_HEADERS += \ |
|
11 | PRIVATE_HEADERS += \ | |
12 | $$PWD/xychart_p.h \ |
|
12 | $$PWD/xychart_p.h \ | |
13 | $$PWD/qxyseries_p.h |
|
13 | $$PWD/qxyseries_p.h \ | |
|
14 | $$PWD/qxymodelmapper_p.h | |||
14 |
|
15 | |||
15 |
|
16 | |||
16 | PUBLIC_HEADERS += \ |
|
17 | PUBLIC_HEADERS += \ | |
@@ -18,4 +19,3 PUBLIC_HEADERS += \ | |||||
18 | $$PWD/qxymodelmapper.h \ |
|
19 | $$PWD/qxymodelmapper.h \ | |
19 | $$PWD/qvxymodelmapper.h \ |
|
20 | $$PWD/qvxymodelmapper.h \ | |
20 | $$PWD/qhxymodelmapper.h |
|
21 | $$PWD/qhxymodelmapper.h | |
21 |
|
@@ -26,7 +26,7 | |||||
26 | #include <QLineSeries> |
|
26 | #include <QLineSeries> | |
27 | #include <QSplineSeries> |
|
27 | #include <QSplineSeries> | |
28 | #include <QScatterSeries> |
|
28 | #include <QScatterSeries> | |
29 | #include <QXYModelMapper> |
|
29 | #include <QVXYModelMapper> | |
30 | #include "customtablemodel.h" |
|
30 | #include "customtablemodel.h" | |
31 | #include <QPieSeries> |
|
31 | #include <QPieSeries> | |
32 | #include <QVPieModelMapper> |
|
32 | #include <QVPieModelMapper> | |
@@ -91,7 +91,7 TableWidget::TableWidget(QWidget *parent) | |||||
91 | connect(specialPieButton3, SIGNAL(clicked()), this, SLOT(testPie3())); |
|
91 | connect(specialPieButton3, SIGNAL(clicked()), this, SLOT(testPie3())); | |
92 |
|
92 | |||
93 |
|
93 | |||
94 |
|
|
94 | QLabel *spinBoxLabel = new QLabel("Rows affected:"); | |
95 |
|
95 | |||
96 | // spin box for setting number of affected items (add, remove) |
|
96 | // spin box for setting number of affected items (add, remove) | |
97 | m_linesCountSpinBox = new QSpinBox; |
|
97 | m_linesCountSpinBox = new QSpinBox; | |
@@ -100,8 +100,8 TableWidget::TableWidget(QWidget *parent) | |||||
100 |
|
100 | |||
101 | // buttons layout |
|
101 | // buttons layout | |
102 | QVBoxLayout* buttonsLayout = new QVBoxLayout; |
|
102 | QVBoxLayout* buttonsLayout = new QVBoxLayout; | |
103 |
|
|
103 | buttonsLayout->addWidget(spinBoxLabel); | |
104 |
|
|
104 | buttonsLayout->addWidget(m_linesCountSpinBox); | |
105 | // buttonsLayout->addWidget(addRowAboveButton); |
|
105 | // buttonsLayout->addWidget(addRowAboveButton); | |
106 | buttonsLayout->addWidget(addRowBelowButton); |
|
106 | buttonsLayout->addWidget(addRowBelowButton); | |
107 | buttonsLayout->addWidget(removeRowButton); |
|
107 | buttonsLayout->addWidget(removeRowButton); | |
@@ -185,20 +185,20 void TableWidget::updateChartType(bool toggle) | |||||
185 | // m_chart->axisX()->setNiceNumbersEnabled(false); |
|
185 | // m_chart->axisX()->setNiceNumbersEnabled(false); | |
186 | // m_chart->axisY()->setNiceNumbersEnabled(false); |
|
186 | // m_chart->axisY()->setNiceNumbersEnabled(false); | |
187 |
|
187 | |||
188 |
|
|
188 | // renable axes of the chart (pie hides them) | |
189 |
|
|
189 | // x axis | |
190 |
|
|
190 | QAxis *axis = m_chart->axisX(); | |
191 |
|
|
191 | axis->setAxisVisible(true); | |
192 |
|
|
192 | axis->setGridLineVisible(true); | |
193 |
|
|
193 | axis->setLabelsVisible(true); | |
194 |
|
194 | |||
195 |
|
|
195 | // y axis | |
196 |
|
|
196 | axis = m_chart->axisY(); | |
197 |
|
|
197 | axis->setAxisVisible(true); | |
198 |
|
|
198 | axis->setGridLineVisible(true); | |
199 |
|
|
199 | axis->setLabelsVisible(true); | |
200 |
|
200 | |||
201 |
|
|
201 | m_model->clearMapping(); | |
202 |
|
202 | |||
203 | QString seriesColorHex = "#000000"; |
|
203 | QString seriesColorHex = "#000000"; | |
204 | // QPen pen; |
|
204 | // QPen pen; | |
@@ -208,21 +208,22 void TableWidget::updateChartType(bool toggle) | |||||
208 | { |
|
208 | { | |
209 | // m_chart->setAnimationOptions(QChart::NoAnimation); |
|
209 | // m_chart->setAnimationOptions(QChart::NoAnimation); | |
210 |
|
210 | |||
211 |
|
|
211 | // series 1 | |
212 |
|
|
212 | m_series = new QLineSeries(this); | |
213 | // m_series->setModel(m_model); |
|
|||
214 |
|
213 | |||
215 |
|
|
214 | QVXYModelMapper *mapper = new QVXYModelMapper; | |
216 |
|
|
215 | mapper->setModel(m_model); | |
217 |
|
|
216 | mapper->setSeries(m_series); | |
218 |
|
|
217 | mapper->setXColumn(0); | |
|
218 | mapper->setYColumn(1); | |||
|
219 | mapper->setFirst(3); | |||
219 | // mapper->setCount(4); |
|
220 | // mapper->setCount(4); | |
220 | // m_series->setModelMapper(mapper); |
|
221 | ||
221 |
|
|
222 | // m_series->setModelMapping(0,1, Qt::Vertical); | |
222 |
|
|
223 | // m_series->setModelMappingRange(3, 4); | |
223 |
|
|
224 | m_chart->addSeries(m_series); | |
224 |
|
|
225 | seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); | |
225 |
|
|
226 | m_model->addMapping(seriesColorHex, QRect(0, 3, 2, 4)); | |
226 |
|
227 | |||
227 | // // series 2 |
|
228 | // // series 2 | |
228 | // m_series = new QLineSeries; |
|
229 | // m_series = new QLineSeries; | |
@@ -354,7 +355,7 void TableWidget::updateChartType(bool toggle) | |||||
354 | m_pieMapper->setLabelsColumn(7); |
|
355 | m_pieMapper->setLabelsColumn(7); | |
355 | m_pieMapper->setSeries(m_pieSeries); |
|
356 | m_pieMapper->setSeries(m_pieSeries); | |
356 | m_pieMapper->setModel(m_model); |
|
357 | m_pieMapper->setModel(m_model); | |
357 | m_pieMapper->setFirst(2); |
|
358 | // m_pieMapper->setFirst(2); | |
358 | // m_pieMapper->setCount(5); |
|
359 | // m_pieMapper->setCount(5); | |
359 | // pieSeries->setModelMapper(mapper); |
|
360 | // pieSeries->setModelMapper(mapper); | |
360 |
|
361 | |||
@@ -377,9 +378,9 void TableWidget::updateChartType(bool toggle) | |||||
377 | m_pieMapper = new QVPieModelMapper; |
|
378 | m_pieMapper = new QVPieModelMapper; | |
378 | m_pieMapper->setValuesColumn(0); |
|
379 | m_pieMapper->setValuesColumn(0); | |
379 | m_pieMapper->setLabelsColumn(7); |
|
380 | m_pieMapper->setLabelsColumn(7); | |
380 | m_pieMapper->setSeries(m_pieSeries2); |
|
|||
381 | m_pieMapper->setModel(m_model); |
|
381 | m_pieMapper->setModel(m_model); | |
382 |
m_pieMapper->set |
|
382 | m_pieMapper->setSeries(m_pieSeries2); | |
|
383 | // m_pieMapper->setFirst(2); | |||
383 |
|
384 | |||
384 | m_pieSeries2->setLabelsVisible(true); |
|
385 | m_pieSeries2->setLabelsVisible(true); | |
385 | m_pieSeries2->setPieSize(0.35); |
|
386 | m_pieSeries2->setPieSize(0.35); |
General Comments 0
You need to be logged in to leave comments.
Login now