@@ -1,407 +1,423 | |||
|
1 | 1 | #include "qbarmodelmapper.h" |
|
2 | 2 | #include "qbarmodelmapper_p.h" |
|
3 | 3 | #include "qbarseries.h" |
|
4 | 4 | #include "qbarset.h" |
|
5 | 5 | #include <QAbstractItemModel> |
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 8 | |
|
9 | 9 | QBarModelMapper::QBarModelMapper(QObject *parent) : |
|
10 | 10 | QObject(parent), |
|
11 | 11 | d_ptr(new QBarModelMapperPrivate(this)) |
|
12 | 12 | { |
|
13 | 13 | } |
|
14 | 14 | |
|
15 | 15 | QAbstractItemModel* QBarModelMapper::model() const |
|
16 | 16 | { |
|
17 | 17 | Q_D(const QBarModelMapper); |
|
18 | 18 | return d->m_model; |
|
19 | 19 | } |
|
20 | 20 | |
|
21 | 21 | void QBarModelMapper::setModel(QAbstractItemModel *model) |
|
22 | 22 | { |
|
23 | 23 | if (model == 0) |
|
24 | 24 | return; |
|
25 | 25 | |
|
26 | 26 | Q_D(QBarModelMapper); |
|
27 | 27 | if (d->m_model) { |
|
28 | 28 | disconnect(d->m_model, 0, d, 0); |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | d->m_model = model; |
|
32 | 32 | d->initializeBarFromModel(); |
|
33 | 33 | // connect signals from the model |
|
34 | 34 | connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex))); |
|
35 | 35 | connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int))); |
|
36 | 36 | connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int))); |
|
37 | 37 | connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int))); |
|
38 | 38 | connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int))); |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | QBarSeries* QBarModelMapper::series() const |
|
42 | 42 | { |
|
43 | 43 | Q_D(const QBarModelMapper); |
|
44 | 44 | return d->m_series; |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | void QBarModelMapper::setSeries(QBarSeries *series) |
|
48 | 48 | { |
|
49 | 49 | Q_D(QBarModelMapper); |
|
50 | 50 | if (d->m_series) { |
|
51 | 51 | disconnect(d->m_series, 0, d, 0); |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | if (series == 0) |
|
55 | 55 | return; |
|
56 | 56 | |
|
57 | 57 | d->m_series = series; |
|
58 | 58 | d->initializeBarFromModel(); |
|
59 | 59 | // connect the signals from the series |
|
60 | 60 | // connect(d->m_series, SIGNAL(pointAdded(int)), d, SLOT(handlePointAdded(int))); |
|
61 | 61 | // connect(d->m_series, SIGNAL(pointRemoved(int)), d, SLOT(handlePointRemoved(int))); |
|
62 | 62 | // connect(d->m_series, SIGNAL(pointReplaced(int)), d, SLOT(handlePointReplaced(int))); |
|
63 | 63 | } |
|
64 | 64 | |
|
65 | 65 | int QBarModelMapper::first() const |
|
66 | 66 | { |
|
67 | 67 | Q_D(const QBarModelMapper); |
|
68 | 68 | return d->m_first; |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | void QBarModelMapper::setFirst(int first) |
|
72 | 72 | { |
|
73 | 73 | Q_D(QBarModelMapper); |
|
74 | 74 | d->m_first = qMax(first, 0); |
|
75 | 75 | d->initializeBarFromModel(); |
|
76 | 76 | } |
|
77 | 77 | |
|
78 | 78 | int QBarModelMapper::count() const |
|
79 | 79 | { |
|
80 | 80 | Q_D(const QBarModelMapper); |
|
81 | 81 | return d->m_count; |
|
82 | 82 | } |
|
83 | 83 | |
|
84 | 84 | void QBarModelMapper::setCount(int count) |
|
85 | 85 | { |
|
86 | 86 | Q_D(QBarModelMapper); |
|
87 | 87 | d->m_count = qMax(count, -1); |
|
88 | 88 | d->initializeBarFromModel(); |
|
89 | 89 | } |
|
90 | 90 | |
|
91 | 91 | Qt::Orientation QBarModelMapper::orientation() const |
|
92 | 92 | { |
|
93 | 93 | Q_D(const QBarModelMapper); |
|
94 | 94 | return d->m_orientation; |
|
95 | 95 | } |
|
96 | 96 | |
|
97 | 97 | void QBarModelMapper::setOrientation(Qt::Orientation orientation) |
|
98 | 98 | { |
|
99 | 99 | Q_D(QBarModelMapper); |
|
100 | 100 | d->m_orientation = orientation; |
|
101 | 101 | d->initializeBarFromModel(); |
|
102 | 102 | } |
|
103 | 103 | |
|
104 | 104 | int QBarModelMapper::firstBarSection() const |
|
105 | 105 | { |
|
106 | 106 | Q_D(const QBarModelMapper); |
|
107 | 107 | return d->m_firstBarSection; |
|
108 | 108 | } |
|
109 | 109 | |
|
110 | 110 | void QBarModelMapper::setFirstBarSection(int firstBarSection) |
|
111 | 111 | { |
|
112 | 112 | Q_D(QBarModelMapper); |
|
113 | 113 | d->m_firstBarSection = firstBarSection; |
|
114 | 114 | d->initializeBarFromModel(); |
|
115 | 115 | } |
|
116 | 116 | |
|
117 | 117 | int QBarModelMapper::lastBarSection() const |
|
118 | 118 | { |
|
119 | 119 | Q_D(const QBarModelMapper); |
|
120 | 120 | return d->m_lastBarSection; |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | void QBarModelMapper::setLastBarSection(int lastBarSection) |
|
124 | 124 | { |
|
125 | 125 | Q_D(QBarModelMapper); |
|
126 | 126 | d->m_lastBarSection = lastBarSection; |
|
127 | 127 | d->initializeBarFromModel(); |
|
128 | 128 | } |
|
129 | 129 | |
|
130 | 130 | int QBarModelMapper::categoriesSection() const |
|
131 | 131 | { |
|
132 | 132 | Q_D(const QBarModelMapper); |
|
133 | 133 | return d->m_categoriesSection; |
|
134 | 134 | } |
|
135 | 135 | |
|
136 | 136 | void QBarModelMapper::setCategoriesSection(int categoriesSection) |
|
137 | 137 | { |
|
138 | 138 | Q_D(QBarModelMapper); |
|
139 | 139 | d->m_categoriesSection = categoriesSection; |
|
140 | 140 | d->initializeBarFromModel(); |
|
141 | 141 | } |
|
142 | 142 | |
|
143 | 143 | void QBarModelMapper::reset() |
|
144 | 144 | { |
|
145 | 145 | Q_D(QBarModelMapper); |
|
146 | 146 | d->m_first = 0; |
|
147 | 147 | d->m_count = -1; |
|
148 | 148 | d->m_orientation = Qt::Vertical; |
|
149 | 149 | d->m_firstBarSection = -1; |
|
150 | 150 | d->m_lastBarSection = -1; |
|
151 | 151 | d->m_categoriesSection = -1; |
|
152 | 152 | d->initializeBarFromModel(); |
|
153 | 153 | } |
|
154 | 154 | |
|
155 | 155 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
156 | 156 | |
|
157 | 157 | QBarModelMapperPrivate::QBarModelMapperPrivate(QBarModelMapper *q) : |
|
158 | 158 | m_series(0), |
|
159 | 159 | m_model(0), |
|
160 | 160 | m_first(0), |
|
161 | 161 | m_count(-1), |
|
162 | 162 | m_orientation(Qt::Vertical), |
|
163 | 163 | m_firstBarSection(-1), |
|
164 | 164 | m_lastBarSection(-1), |
|
165 | 165 | m_categoriesSection(-1), |
|
166 | 166 | m_seriesSignalsBlock(false), |
|
167 | 167 | m_modelSignalsBlock(false), |
|
168 | 168 | q_ptr(q) |
|
169 | 169 | { |
|
170 | 170 | } |
|
171 | 171 | |
|
172 | 172 | void QBarModelMapperPrivate::blockModelSignals(bool block) |
|
173 | 173 | { |
|
174 | 174 | m_modelSignalsBlock = block; |
|
175 | 175 | } |
|
176 | 176 | |
|
177 | 177 | void QBarModelMapperPrivate::blockSeriesSignals(bool block) |
|
178 | 178 | { |
|
179 | 179 | m_seriesSignalsBlock = block; |
|
180 | 180 | } |
|
181 | 181 | |
|
182 | QBarSet* QBarModelMapperPrivate::barSet(QModelIndex index) | |
|
183 | { | |
|
184 | if (!index.isValid()) | |
|
185 | return 0; | |
|
186 | ||
|
187 | if (m_orientation == Qt::Vertical && index.column() >= m_firstBarSection && index.column() <= m_lastBarSection) { | |
|
188 | if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) | |
|
189 | return m_series->barSets().at(index.column() - m_firstBarSection); | |
|
190 | } else if (m_orientation == Qt::Horizontal && index.row() >= m_firstBarSection && index.row() <= m_lastBarSection) { | |
|
191 | if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count)) | |
|
192 | return m_series->barSets().at(index.row() - m_firstBarSection); | |
|
193 | } | |
|
194 | return 0; // This part of model has not been mapped to any slice | |
|
195 | } | |
|
196 | ||
|
182 | 197 | QModelIndex QBarModelMapperPrivate::barModelIndex(int barSection, int posInBar) |
|
183 | 198 | { |
|
184 | 199 | if (m_count != -1 && posInBar >= m_count) |
|
185 | 200 | return QModelIndex(); // invalid |
|
186 | 201 | |
|
187 | 202 | if (barSection < m_firstBarSection || barSection > m_lastBarSection) |
|
188 | 203 | return QModelIndex(); // invalid |
|
189 | 204 | |
|
190 | 205 | if (m_orientation == Qt::Vertical) |
|
191 | 206 | return m_model->index(posInBar + m_first, barSection); |
|
192 | 207 | else |
|
193 | 208 | return m_model->index(barSection, posInBar + m_first); |
|
194 | 209 | } |
|
195 | 210 | |
|
196 | 211 | QModelIndex QBarModelMapperPrivate::categoriesModelIndex(int posInCategories) |
|
197 | 212 | { |
|
198 | 213 | if (m_count != -1 && posInCategories >= m_count) |
|
199 | 214 | return QModelIndex(); // invalid |
|
200 | 215 | |
|
201 | 216 | if (m_orientation == Qt::Vertical) |
|
202 | 217 | return m_model->index(posInCategories + m_first, m_categoriesSection); |
|
203 | 218 | else |
|
204 | 219 | return m_model->index(m_categoriesSection, posInCategories + m_first); |
|
205 | 220 | } |
|
206 | 221 | |
|
207 | 222 | void QBarModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) |
|
208 | 223 | { |
|
209 | 224 | Q_UNUSED(topLeft) |
|
210 | 225 | Q_UNUSED(bottomRight) |
|
211 | 226 | |
|
212 | 227 | if (m_model == 0 || m_series == 0) |
|
213 | 228 | return; |
|
214 | 229 | |
|
215 | 230 | if (m_modelSignalsBlock) |
|
216 | 231 | return; |
|
217 | 232 | |
|
218 |
|
|
|
219 |
|
|
|
233 | blockSeriesSignals(); | |
|
234 | QModelIndex index; | |
|
220 | 235 | // QPointF oldPoint; |
|
221 | 236 | // QPointF newPoint; |
|
222 |
|
|
|
223 |
|
|
|
224 |
|
|
|
225 | // if (m_orientation == Qt::Vertical && (index.column() == m_xSection|| index.column() == m_ySection)) { | |
|
226 | // if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) { | |
|
227 | // oldPoint = m_series->points().at(index.row() - m_first); | |
|
228 | // newPoint.setX(m_model->data(m_model->index(index.row(), m_xSection)).toReal()); | |
|
229 | // newPoint.setY(m_model->data(m_model->index(index.row(), m_ySection)).toReal()); | |
|
230 | // } | |
|
231 | // } else if (m_orientation == Qt::Horizontal && (index.row() == m_xSection || index.row() == m_ySection)) { | |
|
232 | // if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count)) { | |
|
233 | // oldPoint = m_series->points().at(index.column() - m_first); | |
|
234 | // newPoint.setX(m_model->data(m_model->index(m_xSection, index.column())).toReal()); | |
|
235 | // newPoint.setY(m_model->data(m_model->index(m_ySection, index.column())).toReal()); | |
|
236 | // } | |
|
237 | // } else { | |
|
238 | // continue; | |
|
239 | // } | |
|
240 | // m_series->replace(oldPoint, newPoint); | |
|
241 | // } | |
|
242 | // blockSeriesSignals(false); | |
|
243 | // } | |
|
237 | for (int row = topLeft.row(); row <= bottomRight.row(); row++) { | |
|
238 | for (int column = topLeft.column(); column <= bottomRight.column(); column++) { | |
|
239 | index = topLeft.sibling(row, column); | |
|
240 | QBarSet* bar = barSet(index); | |
|
241 | if (bar) { | |
|
242 | if (m_orientation == Qt::Vertical) | |
|
243 | bar->replace(row - m_first, m_model->data(index).toReal()); | |
|
244 | else | |
|
245 | bar->replace(column - m_first, m_model->data(index).toReal()); | |
|
246 | } | |
|
247 | } | |
|
248 | } | |
|
249 | blockSeriesSignals(false); | |
|
244 | 250 | } |
|
245 | 251 | |
|
246 | 252 | void QBarModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end) |
|
247 | 253 | { |
|
248 | 254 | Q_UNUSED(parent); |
|
255 | Q_UNUSED(end) | |
|
249 | 256 | if (m_modelSignalsBlock) |
|
250 | 257 | return; |
|
251 | 258 | |
|
252 | 259 | blockSeriesSignals(); |
|
253 | 260 | if (m_orientation == Qt::Vertical) |
|
254 | insertData(start, end); | |
|
261 | // insertData(start, end); | |
|
262 | initializeBarFromModel(); | |
|
255 | 263 | else if (start <= m_firstBarSection || start <= m_lastBarSection || start <= m_categoriesSection) // if the changes affect the map - reinitialize |
|
256 | 264 | initializeBarFromModel(); |
|
257 | 265 | blockSeriesSignals(false); |
|
258 | 266 | } |
|
259 | 267 | |
|
260 | 268 | void QBarModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end) |
|
261 | 269 | { |
|
262 | 270 | Q_UNUSED(parent); |
|
271 | Q_UNUSED(end) | |
|
263 | 272 | if (m_modelSignalsBlock) |
|
264 | 273 | return; |
|
265 | 274 | |
|
266 | 275 | blockSeriesSignals(); |
|
267 | 276 | if (m_orientation == Qt::Vertical) |
|
268 | removeData(start, end); | |
|
277 | // removeData(start, end); | |
|
278 | initializeBarFromModel(); | |
|
269 | 279 | else if (start <= m_firstBarSection || start <= m_lastBarSection || start <= m_categoriesSection) // if the changes affect the map - reinitialize |
|
270 | 280 | initializeBarFromModel(); |
|
271 | 281 | blockSeriesSignals(false); |
|
272 | 282 | } |
|
273 | 283 | |
|
274 | 284 | void QBarModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end) |
|
275 | 285 | { |
|
276 | 286 | Q_UNUSED(parent); |
|
287 | Q_UNUSED(end) | |
|
277 | 288 | if (m_modelSignalsBlock) |
|
278 | 289 | return; |
|
279 | 290 | |
|
280 | 291 | blockSeriesSignals(); |
|
281 | 292 | if (m_orientation == Qt::Horizontal) |
|
282 | insertData(start, end); | |
|
293 | // insertData(start, end); | |
|
294 | initializeBarFromModel(); | |
|
283 | 295 | else if (start <= m_firstBarSection || start <= m_lastBarSection || start <= m_categoriesSection) // if the changes affect the map - reinitialize |
|
284 | 296 | initializeBarFromModel(); |
|
285 | 297 | blockSeriesSignals(false); |
|
286 | 298 | } |
|
287 | 299 | |
|
288 | 300 | void QBarModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end) |
|
289 | 301 | { |
|
290 | 302 | Q_UNUSED(parent); |
|
303 | Q_UNUSED(end) | |
|
291 | 304 | if (m_modelSignalsBlock) |
|
292 | 305 | return; |
|
293 | 306 | |
|
294 | 307 | blockSeriesSignals(); |
|
295 | 308 | if (m_orientation == Qt::Horizontal) |
|
296 | removeData(start, end); | |
|
309 | // removeData(start, end); | |
|
310 | initializeBarFromModel(); | |
|
297 | 311 | else if (start <= m_firstBarSection || start <= m_lastBarSection || start <= m_categoriesSection) // if the changes affect the map - reinitialize |
|
298 | 312 | initializeBarFromModel(); |
|
299 | 313 | blockSeriesSignals(false); |
|
300 | 314 | } |
|
301 | 315 | |
|
302 | 316 | void QBarModelMapperPrivate::insertData(int start, int end) |
|
303 | 317 | { |
|
304 | 318 | Q_UNUSED(end) |
|
305 | 319 | if (m_model == 0 || m_series == 0) |
|
306 | 320 | return; |
|
307 | 321 | |
|
308 | 322 | if (m_count != -1 && start >= m_first + m_count) { |
|
309 | 323 | return; |
|
310 | 324 | } /*else { |
|
311 | 325 | int addedCount = end - start + 1; |
|
312 | 326 | if (m_count != -1 && addedCount > m_count) |
|
313 | 327 | addedCount = m_count; |
|
314 | 328 | int first = qMax(start, m_first); |
|
315 | 329 | int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1); |
|
316 |
for (int |
|
|
317 | QPointF point; | |
|
318 | point.setX(m_model->data(xModelIndex(i - m_first), Qt::DisplayRole).toDouble()); | |
|
319 |
point.set |
|
|
320 | m_series->insert(i - m_first, point); | |
|
330 | for (int k = 0; k < m_series->barSets().count(); k++) { | |
|
331 | for (int i = first; i <= last; i++) { | |
|
332 | QBar point; | |
|
333 | point.setX(m_model->data(xModelIndex(i - m_first), Qt::DisplayRole).toDouble()); | |
|
334 | point.setY(m_model->data(yModelIndex(i - m_first), Qt::DisplayRole).toDouble()); | |
|
335 | m_series->insert(i - m_first, point); | |
|
336 | } | |
|
321 | 337 | } |
|
322 | 338 | |
|
323 | 339 | // remove excess of slices (abouve m_count) |
|
324 | 340 | if (m_count != -1 && m_series->points().size() > m_count) |
|
325 | 341 | for (int i = m_series->points().size() - 1; i >= m_count; i--) { |
|
326 | 342 | m_series->remove(m_series->points().at(i)); |
|
327 | 343 | } |
|
328 | 344 | }*/ |
|
329 | 345 | } |
|
330 | 346 | |
|
331 | 347 | void QBarModelMapperPrivate::removeData(int start, int end) |
|
332 | 348 | { |
|
333 | 349 | Q_UNUSED(end) |
|
334 | 350 | if (m_model == 0 || m_series == 0) |
|
335 | 351 | return; |
|
336 | 352 | |
|
337 | 353 | // int removedCount = end - start + 1; |
|
338 | 354 | if (m_count != -1 && start >= m_first + m_count) { |
|
339 | 355 | return; |
|
340 | 356 | } /*else { |
|
341 | 357 | int toRemove = qMin(m_series->count(), removedCount); // first find how many items can actually be removed |
|
342 | 358 | int first = qMax(start, m_first); // get the index of the first item that will be removed. |
|
343 | 359 | int last = qMin(first + toRemove - 1, m_series->count() + m_first - 1); // get the index of the last item that will be removed. |
|
344 | 360 | for (int i = last; i >= first; i--) { |
|
345 | 361 | m_series->remove(m_series->points().at(i - m_first)); |
|
346 | 362 | } |
|
347 | 363 | |
|
348 | 364 | if (m_count != -1) { |
|
349 | 365 | int itemsAvailable; // check how many are available to be added |
|
350 | 366 | if (m_orientation == Qt::Vertical) |
|
351 | 367 | itemsAvailable = m_model->rowCount() - m_first - m_series->count(); |
|
352 | 368 | else |
|
353 | 369 | itemsAvailable = m_model->columnCount() - m_first - m_series->count(); |
|
354 | 370 | int toBeAdded = qMin(itemsAvailable, m_count - m_series->count()); // add not more items than there is space left to be filled. |
|
355 | 371 | int currentSize = m_series->count(); |
|
356 | 372 | if (toBeAdded > 0) |
|
357 | 373 | for (int i = m_series->count(); i < currentSize + toBeAdded; i++) { |
|
358 | 374 | QPointF point; |
|
359 | 375 | point.setX(m_model->data(xModelIndex(i), Qt::DisplayRole).toDouble()); |
|
360 | 376 | point.setY(m_model->data(yModelIndex(i), Qt::DisplayRole).toDouble()); |
|
361 | 377 | m_series->insert(i, point); |
|
362 | 378 | } |
|
363 | 379 | } |
|
364 | 380 | }*/ |
|
365 | 381 | } |
|
366 | 382 | |
|
367 | 383 | void QBarModelMapperPrivate::initializeBarFromModel() |
|
368 | 384 | { |
|
369 | 385 | if (m_model == 0 || m_series == 0) |
|
370 | 386 | return; |
|
371 | 387 | |
|
372 | 388 | blockSeriesSignals(); |
|
373 | 389 | // clear current content |
|
374 |
|
|
|
390 | m_series->clear(); | |
|
375 | 391 | |
|
376 | 392 | // create the initial bar sets |
|
377 | 393 | for (int i = m_firstBarSection; i <= m_lastBarSection; i++) { |
|
378 | 394 | int posInBar = 0; |
|
379 | 395 | QModelIndex barIndex = barModelIndex(i, posInBar); |
|
380 | 396 | QBarSet *barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal).toString()); |
|
381 | 397 | // QModelIndex yIndex = yModelIndex(pointPos); |
|
382 | 398 | while (barIndex.isValid()) { |
|
383 | 399 | barSet->append(m_model->data(barIndex, Qt::DisplayRole).toDouble()); |
|
384 | 400 | posInBar++; |
|
385 | 401 | barIndex = barModelIndex(i, posInBar); |
|
386 | 402 | } |
|
387 | 403 | m_series->append(barSet); |
|
388 | 404 | } |
|
389 | 405 | |
|
390 | 406 | if (m_categoriesSection != -1) { |
|
391 | 407 | int posInCategories = 0; |
|
392 | 408 | QStringList categories; |
|
393 | 409 | QModelIndex categoriesIndex = categoriesModelIndex(posInCategories); |
|
394 | 410 | while (categoriesIndex.isValid()) { |
|
395 | 411 | categories.append(m_model->data(categoriesIndex, Qt::DisplayRole).toString()); |
|
396 | 412 | posInCategories++; |
|
397 | 413 | categoriesIndex = categoriesModelIndex(posInCategories); |
|
398 | 414 | } |
|
399 | 415 | m_series->setCategories(categories); |
|
400 | 416 | } |
|
401 | 417 | blockSeriesSignals(false); |
|
402 | 418 | } |
|
403 | 419 | |
|
404 | 420 | #include "moc_qbarmodelmapper.cpp" |
|
405 | 421 | #include "moc_qbarmodelmapper_p.cpp" |
|
406 | 422 | |
|
407 | 423 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,60 +1,63 | |||
|
1 | 1 | #ifndef QBARMODELMAPPER_P_H |
|
2 | 2 | #define QBARMODELMAPPER_P_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include <QObject> |
|
6 | 6 | #include "qbarmodelmapper.h" |
|
7 | 7 | |
|
8 | 8 | class QModelIndex; |
|
9 | 9 | |
|
10 | 10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
11 | 11 | |
|
12 | class QBarSet; | |
|
13 | ||
|
12 | 14 | class QBarModelMapperPrivate : public QObject |
|
13 | 15 | { |
|
14 | 16 | Q_OBJECT |
|
15 | 17 | public: |
|
16 | 18 | explicit QBarModelMapperPrivate(QBarModelMapper *q); |
|
17 | 19 | |
|
18 | 20 | public Q_SLOTS: |
|
19 | 21 | // for the model |
|
20 | 22 | void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); |
|
21 | 23 | void modelRowsAdded(QModelIndex parent, int start, int end); |
|
22 | 24 | void modelRowsRemoved(QModelIndex parent, int start, int end); |
|
23 | 25 | void modelColumnsAdded(QModelIndex parent, int start, int end); |
|
24 | 26 | void modelColumnsRemoved(QModelIndex parent, int start, int end); |
|
25 | 27 | |
|
26 | 28 | // // for the series |
|
27 | 29 | // void handlePointAdded(int pointPos); |
|
28 | 30 | // void handlePointRemoved(int pointPos); |
|
29 | 31 | // void handlePointReplaced(int pointPos); |
|
30 | 32 | |
|
31 | 33 | void initializeBarFromModel(); |
|
32 | 34 | |
|
33 | 35 | private: |
|
36 | QBarSet* barSet(QModelIndex index); | |
|
34 | 37 | QModelIndex barModelIndex(int barSection, int posInBar); |
|
35 | 38 | QModelIndex categoriesModelIndex(int posInCategories); |
|
36 | 39 | void insertData(int start, int end); |
|
37 | 40 | void removeData(int start, int end); |
|
38 | 41 | void blockModelSignals(bool block = true); |
|
39 | 42 | void blockSeriesSignals(bool block = true); |
|
40 | 43 | |
|
41 | 44 | private: |
|
42 | 45 | QBarSeries *m_series; |
|
43 | 46 | QAbstractItemModel *m_model; |
|
44 | 47 | int m_first; |
|
45 | 48 | int m_count; |
|
46 | 49 | Qt::Orientation m_orientation; |
|
47 | 50 | int m_firstBarSection; |
|
48 | 51 | int m_lastBarSection; |
|
49 | 52 | int m_categoriesSection; |
|
50 | 53 | bool m_seriesSignalsBlock; |
|
51 | 54 | bool m_modelSignalsBlock; |
|
52 | 55 | |
|
53 | 56 | private: |
|
54 | 57 | QBarModelMapper *q_ptr; |
|
55 | 58 | Q_DECLARE_PUBLIC(QBarModelMapper) |
|
56 | 59 | }; |
|
57 | 60 | |
|
58 | 61 | QTCOMMERCIALCHART_END_NAMESPACE |
|
59 | 62 | |
|
60 | 63 | #endif // QBARMODELMAPPER_P_H |
@@ -1,568 +1,575 | |||
|
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 "qbarseries.h" |
|
22 | 22 | #include "qbarseries_p.h" |
|
23 | 23 | #include "qbarset.h" |
|
24 | 24 | #include "qbarset_p.h" |
|
25 | 25 | #include "domain_p.h" |
|
26 | 26 | #include "legendmarker_p.h" |
|
27 | 27 | #include "chartdataset_p.h" |
|
28 | 28 | #include "charttheme_p.h" |
|
29 | 29 | #include "chartanimator_p.h" |
|
30 | 30 | |
|
31 | 31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
32 | 32 | |
|
33 | 33 | /*! |
|
34 | 34 | \class QBarSeries |
|
35 | 35 | \brief part of QtCommercial chart API. |
|
36 | 36 | \mainclass |
|
37 | 37 | |
|
38 | 38 | QBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to |
|
39 | 39 | the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar |
|
40 | 40 | and y-value is the height of the bar. The category names are ignored with this series and x-axis |
|
41 | 41 | shows the x-values. |
|
42 | 42 | |
|
43 | 43 | See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart. |
|
44 | 44 | \image examples_barchart.png |
|
45 | 45 | |
|
46 | 46 | \sa QBarSet, QStackedBarSeries, QPercentBarSeries |
|
47 | 47 | */ |
|
48 | 48 | |
|
49 | 49 | /*! |
|
50 | 50 | \fn void QBarSeries::clicked(QBarSet *barset, QString category) |
|
51 | 51 | |
|
52 | 52 | The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset of category \a category |
|
53 | 53 | contained by the series. |
|
54 | 54 | */ |
|
55 | 55 | |
|
56 | 56 | /*! |
|
57 | 57 | \fn void QBarSeries::hovered(QBarSet* barset, bool status) |
|
58 | 58 | |
|
59 | 59 | The signal is emitted if mouse is hovered on top of series. |
|
60 | 60 | Parameter \a barset is the pointer of barset, where hover happened. |
|
61 | 61 | Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series. |
|
62 | 62 | */ |
|
63 | 63 | |
|
64 | 64 | /*! |
|
65 | 65 | Constructs empty QBarSeries. |
|
66 | 66 | QBarSeries is QObject which is a child of a \a parent. |
|
67 | 67 | */ |
|
68 | 68 | QBarSeries::QBarSeries(QObject *parent) : |
|
69 | 69 | QAbstractSeries(*new QBarSeriesPrivate(this),parent) |
|
70 | 70 | { |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | /*! |
|
74 | 74 | Destructs barseries and owned barsets. |
|
75 | 75 | */ |
|
76 | 76 | QBarSeries::~QBarSeries() |
|
77 | 77 | { |
|
78 | 78 | Q_D(QBarSeries); |
|
79 | 79 | if(d->m_dataset){ |
|
80 | 80 | d->m_dataset->removeSeries(this); |
|
81 | 81 | } |
|
82 | 82 | } |
|
83 | 83 | |
|
84 | 84 | /*! |
|
85 | 85 | \internal |
|
86 | 86 | */ |
|
87 | 87 | QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) : |
|
88 | 88 | QAbstractSeries(d,parent) |
|
89 | 89 | { |
|
90 | 90 | } |
|
91 | 91 | |
|
92 | 92 | /*! |
|
93 | 93 | Returns the type of series. Derived classes override this. |
|
94 | 94 | */ |
|
95 | 95 | QAbstractSeries::SeriesType QBarSeries::type() const |
|
96 | 96 | { |
|
97 | 97 | return QAbstractSeries::SeriesTypeBar; |
|
98 | 98 | } |
|
99 | 99 | |
|
100 | 100 | /*! |
|
101 | 101 | Sets the \a categories, which are used to to group the data. |
|
102 | 102 | */ |
|
103 | 103 | void QBarSeries::setCategories(QBarCategories categories) |
|
104 | 104 | { |
|
105 | 105 | Q_D(QBarSeries); |
|
106 | 106 | d->setCategories(categories); |
|
107 | 107 | emit d->categoriesUpdated(); |
|
108 | 108 | } |
|
109 | 109 | |
|
110 | 110 | void QBarSeries::setBarMargin(qreal margin) |
|
111 | 111 | { |
|
112 | 112 | Q_D(QBarSeries); |
|
113 | 113 | d->setBarMargin(margin); |
|
114 | 114 | } |
|
115 | 115 | |
|
116 | 116 | qreal QBarSeries::barMargin() const |
|
117 | 117 | { |
|
118 | 118 | Q_D(const QBarSeries); |
|
119 | 119 | return d->barMargin(); |
|
120 | 120 | } |
|
121 | 121 | |
|
122 | 122 | /*! |
|
123 | 123 | Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended. |
|
124 | 124 | Returns true, if appending succeeded. |
|
125 | 125 | |
|
126 | 126 | */ |
|
127 | 127 | bool QBarSeries::append(QBarSet *set) |
|
128 | 128 | { |
|
129 | 129 | Q_D(QBarSeries); |
|
130 | 130 | return d->append(set); |
|
131 | 131 | } |
|
132 | 132 | |
|
133 | 133 | /*! |
|
134 | 134 | Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set. |
|
135 | 135 | Returns true, if set was removed. |
|
136 | 136 | */ |
|
137 | 137 | bool QBarSeries::remove(QBarSet *set) |
|
138 | 138 | { |
|
139 | 139 | Q_D(QBarSeries); |
|
140 | 140 | return d->remove(set); |
|
141 | 141 | } |
|
142 | 142 | |
|
143 | 143 | /*! |
|
144 | 144 | Adds a list of barsets to series. Takes ownership of \a sets. |
|
145 | 145 | Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series, |
|
146 | 146 | nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended |
|
147 | 147 | and function returns false. |
|
148 | 148 | */ |
|
149 | 149 | bool QBarSeries::append(QList<QBarSet* > sets) |
|
150 | 150 | { |
|
151 | 151 | Q_D(QBarSeries); |
|
152 | 152 | return d->append(sets); |
|
153 | 153 | } |
|
154 | 154 | |
|
155 | 155 | /*! |
|
156 | 156 | Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets. |
|
157 | 157 | */ |
|
158 | 158 | bool QBarSeries::remove(QList<QBarSet* > sets) |
|
159 | 159 | { |
|
160 | 160 | Q_D(QBarSeries); |
|
161 | 161 | return d->remove(sets); |
|
162 | 162 | } |
|
163 | 163 | |
|
164 | void QBarSeries::clear() | |
|
165 | { | |
|
166 | Q_D(QBarSeries); | |
|
167 | d->m_barSets.clear(); | |
|
168 | d->m_categories.clear(); | |
|
169 | } | |
|
170 | ||
|
164 | 171 | /*! |
|
165 | 172 | Returns number of sets in series. |
|
166 | 173 | */ |
|
167 | 174 | int QBarSeries::barsetCount() const |
|
168 | 175 | { |
|
169 | 176 | Q_D(const QBarSeries); |
|
170 | 177 | return d->m_barSets.count(); |
|
171 | 178 | } |
|
172 | 179 | |
|
173 | 180 | /*! |
|
174 | 181 | Returns number of categories in series |
|
175 | 182 | */ |
|
176 | 183 | int QBarSeries::categoryCount() const |
|
177 | 184 | { |
|
178 | 185 | Q_D(const QBarSeries); |
|
179 | 186 | return d->categoryCount(); |
|
180 | 187 | } |
|
181 | 188 | |
|
182 | 189 | /*! |
|
183 | 190 | Returns a list of sets in series. Keeps ownership of sets. |
|
184 | 191 | */ |
|
185 | 192 | QList<QBarSet*> QBarSeries::barSets() const |
|
186 | 193 | { |
|
187 | 194 | Q_D(const QBarSeries); |
|
188 | 195 | return d->m_barSets; |
|
189 | 196 | } |
|
190 | 197 | |
|
191 | 198 | /*! |
|
192 | 199 | Returns the bar categories of the series. |
|
193 | 200 | */ |
|
194 | 201 | QBarCategories QBarSeries::categories() const |
|
195 | 202 | { |
|
196 | 203 | Q_D(const QBarSeries); |
|
197 | 204 | return d->categories(); |
|
198 | 205 | } |
|
199 | 206 | |
|
200 | 207 | void QBarSeries::setVisible(bool visible) |
|
201 | 208 | { |
|
202 | 209 | Q_D(QBarSeries); |
|
203 | 210 | d->setVisible(visible); |
|
204 | 211 | } |
|
205 | 212 | |
|
206 | 213 | bool QBarSeries::isVisible() const |
|
207 | 214 | { |
|
208 | 215 | Q_D(const QBarSeries); |
|
209 | 216 | return d->isVisible(); |
|
210 | 217 | } |
|
211 | 218 | |
|
212 | 219 | /*! |
|
213 | 220 | Sets the visibility of labels in series to \a visible |
|
214 | 221 | */ |
|
215 | 222 | void QBarSeries::setLabelsVisible(bool visible) |
|
216 | 223 | { |
|
217 | 224 | Q_D(QBarSeries); |
|
218 | 225 | if (d->m_labelsVisible != visible) { |
|
219 | 226 | d->m_labelsVisible = visible; |
|
220 | 227 | emit d->updatedBars(); |
|
221 | 228 | } |
|
222 | 229 | } |
|
223 | 230 | |
|
224 | 231 | bool QBarSeries::isLabelsVisible() const |
|
225 | 232 | { |
|
226 | 233 | Q_D(const QBarSeries); |
|
227 | 234 | return d->m_labelsVisible; |
|
228 | 235 | } |
|
229 | 236 | |
|
230 | 237 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
231 | 238 | |
|
232 | 239 | QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) : |
|
233 | 240 | QAbstractSeriesPrivate(q), |
|
234 | 241 | m_barMargin(0.5), // Default value is 50% of category width |
|
235 | 242 | m_labelsVisible(false), |
|
236 | 243 | m_visible(true) |
|
237 | 244 | { |
|
238 | 245 | } |
|
239 | 246 | |
|
240 | 247 | void QBarSeriesPrivate::setCategories(QBarCategories categories) |
|
241 | 248 | { |
|
242 | 249 | m_categories = categories; |
|
243 | 250 | } |
|
244 | 251 | |
|
245 | 252 | void QBarSeriesPrivate::insertCategory(int index, const QString category) |
|
246 | 253 | { |
|
247 | 254 | m_categories.insert(index, category); |
|
248 | 255 | emit categoriesUpdated(); |
|
249 | 256 | } |
|
250 | 257 | |
|
251 | 258 | void QBarSeriesPrivate::removeCategory(int index) |
|
252 | 259 | { |
|
253 | 260 | m_categories.removeAt(index); |
|
254 | 261 | emit categoriesUpdated(); |
|
255 | 262 | } |
|
256 | 263 | |
|
257 | 264 | int QBarSeriesPrivate::categoryCount() const |
|
258 | 265 | { |
|
259 | 266 | if (m_categories.count() > 0) { |
|
260 | 267 | return m_categories.count(); |
|
261 | 268 | } |
|
262 | 269 | |
|
263 | 270 | // No categories defined. return count of longest set. |
|
264 | 271 | int count = 0; |
|
265 | 272 | for (int i=0; i<m_barSets.count(); i++) { |
|
266 | 273 | if (m_barSets.at(i)->count() > count) { |
|
267 | 274 | count = m_barSets.at(i)->count(); |
|
268 | 275 | } |
|
269 | 276 | } |
|
270 | 277 | |
|
271 | 278 | return count; |
|
272 | 279 | } |
|
273 | 280 | |
|
274 | 281 | QBarCategories QBarSeriesPrivate::categories() const |
|
275 | 282 | { |
|
276 | 283 | if (m_categories.count() > 0) { |
|
277 | 284 | return m_categories; |
|
278 | 285 | } |
|
279 | 286 | |
|
280 | 287 | // No categories defined. retun list of indices. |
|
281 | 288 | QBarCategories categories; |
|
282 | 289 | |
|
283 | 290 | int count = categoryCount(); |
|
284 | 291 | for (int i = 0; i < count; i++) { |
|
285 | 292 | categories.append(QString::number(i)); |
|
286 | 293 | } |
|
287 | 294 | return categories; |
|
288 | 295 | } |
|
289 | 296 | |
|
290 | 297 | void QBarSeriesPrivate::setBarMargin(qreal margin) |
|
291 | 298 | { |
|
292 | 299 | if (margin > 1.0) { |
|
293 | 300 | margin = 1.0; |
|
294 | 301 | } else if (margin < 0.0) { |
|
295 | 302 | margin = 0.0; |
|
296 | 303 | } |
|
297 | 304 | |
|
298 | 305 | m_barMargin = margin; |
|
299 | 306 | emit updatedBars(); |
|
300 | 307 | } |
|
301 | 308 | |
|
302 | 309 | qreal QBarSeriesPrivate::barMargin() const |
|
303 | 310 | { |
|
304 | 311 | return m_barMargin; |
|
305 | 312 | } |
|
306 | 313 | |
|
307 | 314 | QBarSet* QBarSeriesPrivate::barsetAt(int index) |
|
308 | 315 | { |
|
309 | 316 | return m_barSets.at(index); |
|
310 | 317 | } |
|
311 | 318 | |
|
312 | 319 | void QBarSeriesPrivate::setVisible(bool visible) |
|
313 | 320 | { |
|
314 | 321 | if (m_visible != visible) { |
|
315 | 322 | m_visible = visible; |
|
316 | 323 | emit updatedBars(); |
|
317 | 324 | } |
|
318 | 325 | } |
|
319 | 326 | |
|
320 | 327 | bool QBarSeriesPrivate::isVisible() const |
|
321 | 328 | { |
|
322 | 329 | return m_visible; |
|
323 | 330 | } |
|
324 | 331 | |
|
325 | 332 | QString QBarSeriesPrivate::categoryName(int category) |
|
326 | 333 | { |
|
327 | 334 | if ((category >= 0) && (category < m_categories.count())) { |
|
328 | 335 | return m_categories.at(category); |
|
329 | 336 | } |
|
330 | 337 | |
|
331 | 338 | return QString::number(category); |
|
332 | 339 | } |
|
333 | 340 | |
|
334 | 341 | qreal QBarSeriesPrivate::min() |
|
335 | 342 | { |
|
336 | 343 | if (m_barSets.count() <= 0) { |
|
337 | 344 | return 0; |
|
338 | 345 | } |
|
339 | 346 | qreal min = INT_MAX; |
|
340 | 347 | |
|
341 | 348 | for (int i = 0; i < m_barSets.count(); i++) { |
|
342 | 349 | int categoryCount = m_barSets.at(i)->count(); |
|
343 | 350 | for (int j = 0; j < categoryCount; j++) { |
|
344 | 351 | qreal temp = m_barSets.at(i)->at(j).y(); |
|
345 | 352 | if (temp < min) |
|
346 | 353 | min = temp; |
|
347 | 354 | } |
|
348 | 355 | } |
|
349 | 356 | return min; |
|
350 | 357 | } |
|
351 | 358 | |
|
352 | 359 | qreal QBarSeriesPrivate::max() |
|
353 | 360 | { |
|
354 | 361 | if (m_barSets.count() <= 0) { |
|
355 | 362 | return 0; |
|
356 | 363 | } |
|
357 | 364 | qreal max = INT_MIN; |
|
358 | 365 | |
|
359 | 366 | for (int i = 0; i < m_barSets.count(); i++) { |
|
360 | 367 | int categoryCount = m_barSets.at(i)->count(); |
|
361 | 368 | for (int j = 0; j < categoryCount; j++) { |
|
362 | 369 | qreal temp = m_barSets.at(i)->at(j).y(); |
|
363 | 370 | if (temp > max) |
|
364 | 371 | max = temp; |
|
365 | 372 | } |
|
366 | 373 | } |
|
367 | 374 | |
|
368 | 375 | return max; |
|
369 | 376 | } |
|
370 | 377 | |
|
371 | 378 | qreal QBarSeriesPrivate::valueAt(int set, int category) |
|
372 | 379 | { |
|
373 | 380 | if ((set < 0) || (set >= m_barSets.count())) { |
|
374 | 381 | // No set, no value. |
|
375 | 382 | return 0; |
|
376 | 383 | } else if ((category < 0) || (category >= m_barSets.at(set)->count())) { |
|
377 | 384 | // No category, no value. |
|
378 | 385 | return 0; |
|
379 | 386 | } |
|
380 | 387 | |
|
381 | 388 | return m_barSets.at(set)->at(category).y(); |
|
382 | 389 | } |
|
383 | 390 | |
|
384 | 391 | qreal QBarSeriesPrivate::percentageAt(int set, int category) |
|
385 | 392 | { |
|
386 | 393 | if ((set < 0) || (set >= m_barSets.count())) { |
|
387 | 394 | // No set, no value. |
|
388 | 395 | return 0; |
|
389 | 396 | } else if ((category < 0) || (category >= m_barSets.at(set)->count())) { |
|
390 | 397 | // No category, no value. |
|
391 | 398 | return 0; |
|
392 | 399 | } |
|
393 | 400 | |
|
394 | 401 | qreal value = m_barSets.at(set)->at(category).y(); |
|
395 | 402 | qreal sum = categorySum(category); |
|
396 | 403 | if ( qFuzzyIsNull(sum) ) { |
|
397 | 404 | return 0; |
|
398 | 405 | } |
|
399 | 406 | |
|
400 | 407 | return value / sum; |
|
401 | 408 | } |
|
402 | 409 | |
|
403 | 410 | qreal QBarSeriesPrivate::categorySum(int category) |
|
404 | 411 | { |
|
405 | 412 | qreal sum(0); |
|
406 | 413 | int count = m_barSets.count(); // Count sets |
|
407 | 414 | for (int set = 0; set < count; set++) { |
|
408 | 415 | if (category < m_barSets.at(set)->count()) |
|
409 | 416 | sum += m_barSets.at(set)->at(category).y(); |
|
410 | 417 | } |
|
411 | 418 | return sum; |
|
412 | 419 | } |
|
413 | 420 | |
|
414 | 421 | qreal QBarSeriesPrivate::absoluteCategorySum(int category) |
|
415 | 422 | { |
|
416 | 423 | qreal sum(0); |
|
417 | 424 | int count = m_barSets.count(); // Count sets |
|
418 | 425 | for (int set = 0; set < count; set++) { |
|
419 | 426 | if (category < m_barSets.at(set)->count()) |
|
420 | 427 | sum += qAbs(m_barSets.at(set)->at(category).y()); |
|
421 | 428 | } |
|
422 | 429 | return sum; |
|
423 | 430 | } |
|
424 | 431 | |
|
425 | 432 | qreal QBarSeriesPrivate::maxCategorySum() |
|
426 | 433 | { |
|
427 | 434 | qreal max = INT_MIN; |
|
428 | 435 | int count = categoryCount(); |
|
429 | 436 | for (int i = 0; i < count; i++) { |
|
430 | 437 | qreal sum = categorySum(i); |
|
431 | 438 | if (sum > max) |
|
432 | 439 | max = sum; |
|
433 | 440 | } |
|
434 | 441 | return max; |
|
435 | 442 | } |
|
436 | 443 | |
|
437 | 444 | void QBarSeriesPrivate::barsetChanged() |
|
438 | 445 | { |
|
439 | 446 | emit updatedBars(); |
|
440 | 447 | } |
|
441 | 448 | |
|
442 | 449 | void QBarSeriesPrivate::scaleDomain(Domain& domain) |
|
443 | 450 | { |
|
444 | 451 | qreal minX(domain.minX()); |
|
445 | 452 | qreal minY(domain.minY()); |
|
446 | 453 | qreal maxX(domain.maxX()); |
|
447 | 454 | qreal maxY(domain.maxY()); |
|
448 | 455 | int tickXCount(domain.tickXCount()); |
|
449 | 456 | int tickYCount(domain.tickYCount()); |
|
450 | 457 | |
|
451 | 458 | qreal x = categoryCount(); |
|
452 | 459 | qreal y = max(); |
|
453 | 460 | minX = qMin(minX, x) - 0.5; |
|
454 | 461 | minY = qMin(minY, y); |
|
455 | 462 | maxX = qMax(maxX, x) - 0.5; |
|
456 | 463 | maxY = qMax(maxY, y); |
|
457 | 464 | tickXCount = x+1; |
|
458 | 465 | |
|
459 | 466 | domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount); |
|
460 | 467 | } |
|
461 | 468 | |
|
462 | 469 | Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter) |
|
463 | 470 | { |
|
464 | 471 | Q_Q(QBarSeries); |
|
465 | 472 | |
|
466 | 473 | BarChartItem* bar = new BarChartItem(q,presenter); |
|
467 | 474 | if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { |
|
468 | 475 | presenter->animator()->addAnimation(bar); |
|
469 | 476 | } |
|
470 | 477 | presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); |
|
471 | 478 | return bar; |
|
472 | 479 | |
|
473 | 480 | } |
|
474 | 481 | |
|
475 | 482 | QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend) |
|
476 | 483 | { |
|
477 | 484 | Q_Q(QBarSeries); |
|
478 | 485 | QList<LegendMarker*> markers; |
|
479 | 486 | foreach(QBarSet* set, q->barSets()) { |
|
480 | 487 | BarLegendMarker* marker = new BarLegendMarker(q,set,legend); |
|
481 | 488 | markers << marker; |
|
482 | 489 | } |
|
483 | 490 | |
|
484 | 491 | return markers; |
|
485 | 492 | } |
|
486 | 493 | |
|
487 | 494 | bool QBarSeriesPrivate::append(QBarSet *set) |
|
488 | 495 | { |
|
489 | 496 | Q_Q(QBarSeries); |
|
490 | 497 | if ((m_barSets.contains(set)) || (set == 0)) { |
|
491 | 498 | // Fail if set is already in list or set is null. |
|
492 | 499 | return false; |
|
493 | 500 | } |
|
494 | 501 | m_barSets.append(set); |
|
495 | 502 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged())); |
|
496 | 503 | if (m_dataset) { |
|
497 | 504 | m_dataset->updateSeries(q); // this notifies legend |
|
498 | 505 | } |
|
499 | 506 | emit restructuredBars(); // this notifies barchartitem |
|
500 | 507 | return true; |
|
501 | 508 | } |
|
502 | 509 | |
|
503 | 510 | bool QBarSeriesPrivate::remove(QBarSet *set) |
|
504 | 511 | { |
|
505 | 512 | Q_Q(QBarSeries); |
|
506 | 513 | if (!m_barSets.contains(set)) { |
|
507 | 514 | // Fail if set is not in list |
|
508 | 515 | return false; |
|
509 | 516 | } |
|
510 | 517 | m_barSets.removeOne(set); |
|
511 | 518 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged())); |
|
512 | 519 | if (m_dataset) { |
|
513 | 520 | m_dataset->updateSeries(q); // this notifies legend |
|
514 | 521 | } |
|
515 | 522 | emit restructuredBars(); // this notifies barchartitem |
|
516 | 523 | return true; |
|
517 | 524 | } |
|
518 | 525 | |
|
519 | 526 | bool QBarSeriesPrivate::append(QList<QBarSet* > sets) |
|
520 | 527 | { |
|
521 | 528 | Q_Q(QBarSeries); |
|
522 | 529 | foreach (QBarSet* set, sets) { |
|
523 | 530 | if ((set == 0) || (m_barSets.contains(set))) { |
|
524 | 531 | // Fail if any of the sets is null or is already appended. |
|
525 | 532 | return false; |
|
526 | 533 | } |
|
527 | 534 | if (sets.count(set) != 1) { |
|
528 | 535 | // Also fail if same set is more than once in given list. |
|
529 | 536 | return false; |
|
530 | 537 | } |
|
531 | 538 | } |
|
532 | 539 | |
|
533 | 540 | foreach (QBarSet* set, sets) { |
|
534 | 541 | m_barSets.append(set); |
|
535 | 542 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged())); |
|
536 | 543 | } |
|
537 | 544 | if (m_dataset) { |
|
538 | 545 | m_dataset->updateSeries(q); // this notifies legend |
|
539 | 546 | } |
|
540 | 547 | emit restructuredBars(); // this notifies barchartitem |
|
541 | 548 | return true; |
|
542 | 549 | } |
|
543 | 550 | |
|
544 | 551 | bool QBarSeriesPrivate::remove(QList<QBarSet* > sets) |
|
545 | 552 | { |
|
546 | 553 | Q_Q(QBarSeries); |
|
547 | 554 | bool setsRemoved = false; |
|
548 | 555 | foreach (QBarSet* set, sets) { |
|
549 | 556 | if (m_barSets.contains(set)) { |
|
550 | 557 | m_barSets.removeOne(set); |
|
551 | 558 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged())); |
|
552 | 559 | setsRemoved = true; |
|
553 | 560 | } |
|
554 | 561 | } |
|
555 | 562 | |
|
556 | 563 | if (setsRemoved) { |
|
557 | 564 | if (m_dataset) { |
|
558 | 565 | m_dataset->updateSeries(q); // this notifies legend |
|
559 | 566 | } |
|
560 | 567 | emit restructuredBars(); // this notifies barchartitem |
|
561 | 568 | } |
|
562 | 569 | return setsRemoved; |
|
563 | 570 | } |
|
564 | 571 | |
|
565 | 572 | #include "moc_qbarseries.cpp" |
|
566 | 573 | #include "moc_qbarseries_p.cpp" |
|
567 | 574 | |
|
568 | 575 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,80 +1,81 | |||
|
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 BARSERIES_H |
|
22 | 22 | #define BARSERIES_H |
|
23 | 23 | |
|
24 | 24 | #include <qabstractseries.h> |
|
25 | 25 | #include <QStringList> |
|
26 | 26 | |
|
27 | 27 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
28 | 28 | |
|
29 | 29 | typedef QStringList QBarCategories; |
|
30 | 30 | |
|
31 | 31 | class QBarSet; |
|
32 | 32 | class BarCategory; |
|
33 | 33 | class QBarSeriesPrivate; |
|
34 | 34 | |
|
35 | 35 | // Container for series |
|
36 | 36 | class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries |
|
37 | 37 | { |
|
38 | 38 | Q_OBJECT |
|
39 | 39 | public: |
|
40 | 40 | explicit QBarSeries(QObject *parent = 0); |
|
41 | 41 | virtual ~QBarSeries(); |
|
42 | 42 | |
|
43 | 43 | QAbstractSeries::SeriesType type() const; |
|
44 | 44 | void setCategories(QBarCategories categories); |
|
45 | 45 | |
|
46 | 46 | void setBarMargin(qreal margin); |
|
47 | 47 | qreal barMargin() const; |
|
48 | 48 | |
|
49 | 49 | bool append(QBarSet *set); |
|
50 | 50 | bool remove(QBarSet *set); |
|
51 | 51 | bool append(QList<QBarSet* > sets); |
|
52 | 52 | bool remove(QList<QBarSet* > sets); |
|
53 | 53 | int barsetCount() const; |
|
54 | 54 | int categoryCount() const; |
|
55 | 55 | QList<QBarSet*> barSets() const; |
|
56 | 56 | QBarCategories categories() const; |
|
57 | void clear(); | |
|
57 | 58 | |
|
58 | 59 | void setVisible(bool visible = true); |
|
59 | 60 | bool isVisible() const; |
|
60 | 61 | void setLabelsVisible(bool visible = true); |
|
61 | 62 | bool isLabelsVisible() const; |
|
62 | 63 | |
|
63 | 64 | protected: |
|
64 | 65 | explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0); |
|
65 | 66 | |
|
66 | 67 | Q_SIGNALS: |
|
67 | 68 | void clicked(QBarSet *barset, QString category); |
|
68 | 69 | void hovered(QBarSet* barset, bool status); |
|
69 | 70 | |
|
70 | 71 | protected: |
|
71 | 72 | Q_DECLARE_PRIVATE(QBarSeries) |
|
72 | 73 | friend class BarChartItem; |
|
73 | 74 | friend class PercentBarChartItem; |
|
74 | 75 | friend class StackedBarChartItem; |
|
75 | 76 | friend class GroupedBarChartItem; |
|
76 | 77 | }; |
|
77 | 78 | |
|
78 | 79 | QTCOMMERCIALCHART_END_NAMESPACE |
|
79 | 80 | |
|
80 | 81 | #endif // BARSERIES_H |
@@ -1,424 +1,424 | |||
|
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 | 8 | QXYModelMapper::QXYModelMapper(QObject *parent): |
|
9 | 9 | QObject(parent), |
|
10 | 10 | d_ptr(new QXYModelMapperPrivate(this)) |
|
11 | 11 | { |
|
12 | 12 | } |
|
13 | 13 | |
|
14 | 14 | QXYModelMapper::~QXYModelMapper() |
|
15 | 15 | { |
|
16 | 16 | Q_D(QXYModelMapper); |
|
17 | 17 | disconnect(d->m_model, 0, d, 0); |
|
18 | // disconnect(d->m_series, 0, d, 0); | |
|
18 | // disconnect(d->m_series, 0, d, 0); | |
|
19 | 19 | } |
|
20 | 20 | |
|
21 | 21 | QAbstractItemModel* QXYModelMapper::model() const |
|
22 | 22 | { |
|
23 | 23 | Q_D(const QXYModelMapper); |
|
24 | 24 | return d->m_model; |
|
25 | 25 | } |
|
26 | 26 | |
|
27 | 27 | void QXYModelMapper::setModel(QAbstractItemModel *model) |
|
28 | 28 | { |
|
29 | 29 | if (model == 0) |
|
30 | 30 | return; |
|
31 | 31 | |
|
32 | 32 | Q_D(QXYModelMapper); |
|
33 | 33 | if (d->m_model) { |
|
34 | 34 | disconnect(d->m_model, 0, d, 0); |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | d->m_model = model; |
|
38 | 38 | d->initializeXYFromModel(); |
|
39 | 39 | // connect signals from the model |
|
40 | 40 | connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex))); |
|
41 | 41 | connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int))); |
|
42 | 42 | connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int))); |
|
43 | 43 | connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int))); |
|
44 | 44 | connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int))); |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | QXYSeries* QXYModelMapper::series() const |
|
48 | 48 | { |
|
49 | 49 | Q_D(const QXYModelMapper); |
|
50 | 50 | return d->m_series; |
|
51 | 51 | } |
|
52 | 52 | |
|
53 | 53 | void QXYModelMapper::setSeries(QXYSeries *series) |
|
54 | 54 | { |
|
55 | 55 | Q_D(QXYModelMapper); |
|
56 | 56 | if (d->m_series) { |
|
57 | 57 | disconnect(d->m_series, 0, d, 0); |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | if (series == 0) |
|
61 | 61 | return; |
|
62 | 62 | |
|
63 | 63 | d->m_series = series; |
|
64 | 64 | d->initializeXYFromModel(); |
|
65 | 65 | // connect the signals from the series |
|
66 | 66 | connect(d->m_series, SIGNAL(pointAdded(int)), d, SLOT(handlePointAdded(int))); |
|
67 | 67 | connect(d->m_series, SIGNAL(pointRemoved(int)), d, SLOT(handlePointRemoved(int))); |
|
68 | 68 | connect(d->m_series, SIGNAL(pointReplaced(int)), d, SLOT(handlePointReplaced(int))); |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | int QXYModelMapper::first() const |
|
72 | 72 | { |
|
73 | 73 | Q_D(const QXYModelMapper); |
|
74 | 74 | return d->m_first; |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | 77 | void QXYModelMapper::setFirst(int first) |
|
78 | 78 | { |
|
79 | 79 | Q_D(QXYModelMapper); |
|
80 | 80 | d->m_first = qMax(first, 0); |
|
81 | 81 | d->initializeXYFromModel(); |
|
82 | 82 | } |
|
83 | 83 | |
|
84 | 84 | int QXYModelMapper::count() const |
|
85 | 85 | { |
|
86 | 86 | Q_D(const QXYModelMapper); |
|
87 | 87 | return d->m_count; |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | void QXYModelMapper::setCount(int count) |
|
91 | 91 | { |
|
92 | 92 | Q_D(QXYModelMapper); |
|
93 | 93 | d->m_count = qMax(count, -1); |
|
94 | 94 | d->initializeXYFromModel(); |
|
95 | 95 | } |
|
96 | 96 | |
|
97 | 97 | Qt::Orientation QXYModelMapper::orientation() const |
|
98 | 98 | { |
|
99 | 99 | Q_D(const QXYModelMapper); |
|
100 | 100 | return d->m_orientation; |
|
101 | 101 | } |
|
102 | 102 | |
|
103 | 103 | void QXYModelMapper::setOrientation(Qt::Orientation orientation) |
|
104 | 104 | { |
|
105 | 105 | Q_D(QXYModelMapper); |
|
106 | 106 | d->m_orientation = orientation; |
|
107 | 107 | d->initializeXYFromModel(); |
|
108 | 108 | } |
|
109 | 109 | |
|
110 | 110 | int QXYModelMapper::xSection() const |
|
111 | 111 | { |
|
112 | 112 | Q_D(const QXYModelMapper); |
|
113 | 113 | return d->m_xSection; |
|
114 | 114 | } |
|
115 | 115 | |
|
116 | 116 | void QXYModelMapper::setXSection(int xSection) |
|
117 | 117 | { |
|
118 | 118 | Q_D(QXYModelMapper); |
|
119 | 119 | d->m_xSection = xSection; |
|
120 | 120 | d->initializeXYFromModel(); |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | int QXYModelMapper::ySection() const |
|
124 | 124 | { |
|
125 | 125 | Q_D(const QXYModelMapper); |
|
126 | 126 | return d->m_ySection; |
|
127 | 127 | } |
|
128 | 128 | |
|
129 | 129 | void QXYModelMapper::setYSection(int ySection) |
|
130 | 130 | { |
|
131 | 131 | Q_D(QXYModelMapper); |
|
132 | 132 | d->m_ySection = ySection; |
|
133 | 133 | d->initializeXYFromModel(); |
|
134 | 134 | } |
|
135 | 135 | |
|
136 | 136 | void QXYModelMapper::reset() |
|
137 | 137 | { |
|
138 | 138 | Q_D(QXYModelMapper); |
|
139 | 139 | d->m_first = 0; |
|
140 | 140 | d->m_count = -1; |
|
141 | 141 | d->m_orientation = Qt::Vertical; |
|
142 | 142 | d->m_xSection = -1; |
|
143 | 143 | d->m_ySection = -1; |
|
144 | 144 | d->initializeXYFromModel(); |
|
145 | 145 | } |
|
146 | 146 | |
|
147 | 147 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
148 | 148 | |
|
149 | 149 | QXYModelMapperPrivate::QXYModelMapperPrivate(QXYModelMapper *q) : |
|
150 | 150 | m_series(0), |
|
151 | 151 | m_model(0), |
|
152 | 152 | m_first(0), |
|
153 | 153 | m_count(-1), |
|
154 | 154 | m_orientation(Qt::Vertical), |
|
155 | 155 | m_xSection(-1), |
|
156 | 156 | m_ySection(-1), |
|
157 | 157 | m_seriesSignalsBlock(false), |
|
158 | 158 | m_modelSignalsBlock(false), |
|
159 | 159 | q_ptr(q) |
|
160 | 160 | { |
|
161 | 161 | } |
|
162 | 162 | |
|
163 | 163 | void QXYModelMapperPrivate::blockModelSignals(bool block) |
|
164 | 164 | { |
|
165 | 165 | m_modelSignalsBlock = block; |
|
166 | 166 | } |
|
167 | 167 | |
|
168 | 168 | void QXYModelMapperPrivate::blockSeriesSignals(bool block) |
|
169 | 169 | { |
|
170 | 170 | m_seriesSignalsBlock = block; |
|
171 | 171 | } |
|
172 | 172 | |
|
173 | 173 | QModelIndex QXYModelMapperPrivate::xModelIndex(int xPos) |
|
174 | 174 | { |
|
175 | 175 | if (m_count != -1 && xPos >= m_count) |
|
176 | 176 | return QModelIndex(); // invalid |
|
177 | 177 | |
|
178 | 178 | if (m_orientation == Qt::Vertical) |
|
179 | 179 | return m_model->index(xPos + m_first, m_xSection); |
|
180 | 180 | else |
|
181 | 181 | return m_model->index(m_xSection, xPos + m_first); |
|
182 | 182 | } |
|
183 | 183 | |
|
184 | 184 | QModelIndex QXYModelMapperPrivate::yModelIndex(int yPos) |
|
185 | 185 | { |
|
186 | 186 | if (m_count != -1 && yPos >= m_count) |
|
187 | 187 | return QModelIndex(); // invalid |
|
188 | 188 | |
|
189 | 189 | if (m_orientation == Qt::Vertical) |
|
190 | 190 | return m_model->index(yPos + m_first, m_ySection); |
|
191 | 191 | else |
|
192 | 192 | return m_model->index(m_ySection, yPos + m_first); |
|
193 | 193 | } |
|
194 | 194 | |
|
195 | 195 | void QXYModelMapperPrivate::handlePointAdded(int pointPos) |
|
196 | 196 | { |
|
197 | 197 | if (m_seriesSignalsBlock) |
|
198 | 198 | return; |
|
199 | 199 | |
|
200 | 200 | if (m_count != -1) |
|
201 | 201 | m_count += 1; |
|
202 | 202 | |
|
203 | 203 | blockModelSignals(); |
|
204 | 204 | if (m_orientation == Qt::Vertical) |
|
205 | 205 | m_model->insertRows(pointPos + m_first, 1); |
|
206 | 206 | else |
|
207 | 207 | m_model->insertColumns(pointPos + m_first, 1); |
|
208 | 208 | |
|
209 | 209 | m_model->setData(xModelIndex(pointPos), m_series->points().at(pointPos).x()); |
|
210 | 210 | m_model->setData(yModelIndex(pointPos), m_series->points().at(pointPos).y()); |
|
211 | 211 | blockModelSignals(false); |
|
212 | 212 | } |
|
213 | 213 | |
|
214 | 214 | void QXYModelMapperPrivate::handlePointRemoved(int pointPos) |
|
215 | 215 | { |
|
216 | 216 | if (m_seriesSignalsBlock) |
|
217 | 217 | return; |
|
218 | 218 | |
|
219 | 219 | if (m_count != -1) |
|
220 | 220 | m_count -= 1; |
|
221 | 221 | |
|
222 | 222 | blockModelSignals(); |
|
223 | 223 | if (m_orientation == Qt::Vertical) |
|
224 | 224 | m_model->removeRow(pointPos + m_first); |
|
225 | 225 | else |
|
226 | 226 | m_model->removeColumn(pointPos + m_first); |
|
227 | 227 | blockModelSignals(false); |
|
228 | 228 | } |
|
229 | 229 | |
|
230 | 230 | void QXYModelMapperPrivate::handlePointReplaced(int pointPos) |
|
231 | 231 | { |
|
232 | 232 | if (m_seriesSignalsBlock) |
|
233 | 233 | return; |
|
234 | 234 | |
|
235 | 235 | blockModelSignals(); |
|
236 | 236 | m_model->setData(xModelIndex(pointPos), m_series->points().at(pointPos).x()); |
|
237 | 237 | m_model->setData(yModelIndex(pointPos), m_series->points().at(pointPos).y()); |
|
238 | 238 | blockModelSignals(false); |
|
239 | 239 | } |
|
240 | 240 | |
|
241 | 241 | void QXYModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) |
|
242 | 242 | { |
|
243 | 243 | if (m_model == 0 || m_series == 0) |
|
244 | 244 | return; |
|
245 | 245 | |
|
246 | 246 | if (m_modelSignalsBlock) |
|
247 | 247 | return; |
|
248 | 248 | |
|
249 | 249 | blockSeriesSignals(); |
|
250 | 250 | QModelIndex index; |
|
251 | 251 | QPointF oldPoint; |
|
252 | 252 | QPointF newPoint; |
|
253 | 253 | for (int row = topLeft.row(); row <= bottomRight.row(); row++) { |
|
254 | 254 | for (int column = topLeft.column(); column <= bottomRight.column(); column++) { |
|
255 | 255 | index = topLeft.sibling(row, column); |
|
256 | 256 | if (m_orientation == Qt::Vertical && (index.column() == m_xSection|| index.column() == m_ySection)) { |
|
257 | 257 | if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) { |
|
258 | 258 | oldPoint = m_series->points().at(index.row() - m_first); |
|
259 | 259 | newPoint.setX(m_model->data(m_model->index(index.row(), m_xSection)).toReal()); |
|
260 | 260 | newPoint.setY(m_model->data(m_model->index(index.row(), m_ySection)).toReal()); |
|
261 | 261 | } |
|
262 | 262 | } else if (m_orientation == Qt::Horizontal && (index.row() == m_xSection || index.row() == m_ySection)) { |
|
263 | 263 | if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count)) { |
|
264 | 264 | oldPoint = m_series->points().at(index.column() - m_first); |
|
265 | 265 | newPoint.setX(m_model->data(m_model->index(m_xSection, index.column())).toReal()); |
|
266 | 266 | newPoint.setY(m_model->data(m_model->index(m_ySection, index.column())).toReal()); |
|
267 | 267 | } |
|
268 | 268 | } else { |
|
269 | 269 | continue; |
|
270 | 270 | } |
|
271 | 271 | m_series->replace(oldPoint, newPoint); |
|
272 | 272 | } |
|
273 | blockSeriesSignals(false); | |
|
274 | 273 | } |
|
274 | blockSeriesSignals(false); | |
|
275 | 275 | } |
|
276 | 276 | |
|
277 | 277 | void QXYModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end) |
|
278 | 278 | { |
|
279 | 279 | Q_UNUSED(parent); |
|
280 | 280 | if (m_modelSignalsBlock) |
|
281 | 281 | return; |
|
282 | 282 | |
|
283 | 283 | blockSeriesSignals(); |
|
284 | 284 | if (m_orientation == Qt::Vertical) |
|
285 | 285 | insertData(start, end); |
|
286 | 286 | else if (start <= m_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy |
|
287 | 287 | initializeXYFromModel(); |
|
288 | 288 | blockSeriesSignals(false); |
|
289 | 289 | } |
|
290 | 290 | |
|
291 | 291 | void QXYModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end) |
|
292 | 292 | { |
|
293 | 293 | Q_UNUSED(parent); |
|
294 | 294 | if (m_modelSignalsBlock) |
|
295 | 295 | return; |
|
296 | 296 | |
|
297 | 297 | blockSeriesSignals(); |
|
298 | 298 | if (m_orientation == Qt::Vertical) |
|
299 | 299 | removeData(start, end); |
|
300 | 300 | else if (start <= m_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy |
|
301 | 301 | initializeXYFromModel(); |
|
302 | 302 | blockSeriesSignals(false); |
|
303 | 303 | } |
|
304 | 304 | |
|
305 | 305 | void QXYModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end) |
|
306 | 306 | { |
|
307 | 307 | Q_UNUSED(parent); |
|
308 | 308 | if (m_modelSignalsBlock) |
|
309 | 309 | return; |
|
310 | 310 | |
|
311 | 311 | blockSeriesSignals(); |
|
312 | 312 | if (m_orientation == Qt::Horizontal) |
|
313 | 313 | insertData(start, end); |
|
314 | 314 | else if (start <= m_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy |
|
315 | 315 | initializeXYFromModel(); |
|
316 | 316 | blockSeriesSignals(false); |
|
317 | 317 | } |
|
318 | 318 | |
|
319 | 319 | void QXYModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end) |
|
320 | 320 | { |
|
321 | 321 | Q_UNUSED(parent); |
|
322 | 322 | if (m_modelSignalsBlock) |
|
323 | 323 | return; |
|
324 | 324 | |
|
325 | 325 | blockSeriesSignals(); |
|
326 | 326 | if (m_orientation == Qt::Horizontal) |
|
327 | 327 | removeData(start, end); |
|
328 | 328 | else if (start <= m_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy |
|
329 | 329 | initializeXYFromModel(); |
|
330 | 330 | blockSeriesSignals(false); |
|
331 | 331 | } |
|
332 | 332 | |
|
333 | 333 | void QXYModelMapperPrivate::insertData(int start, int end) |
|
334 | 334 | { |
|
335 | 335 | if (m_model == 0 || m_series == 0) |
|
336 | 336 | return; |
|
337 | 337 | |
|
338 | 338 | if (m_count != -1 && start >= m_first + m_count) { |
|
339 | 339 | return; |
|
340 | 340 | } else { |
|
341 | 341 | int addedCount = end - start + 1; |
|
342 | 342 | if (m_count != -1 && addedCount > m_count) |
|
343 | 343 | addedCount = m_count; |
|
344 | 344 | int first = qMax(start, m_first); |
|
345 | 345 | int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1); |
|
346 | 346 | for (int i = first; i <= last; i++) { |
|
347 | 347 | QPointF point; |
|
348 | 348 | point.setX(m_model->data(xModelIndex(i - m_first), Qt::DisplayRole).toDouble()); |
|
349 | 349 | point.setY(m_model->data(yModelIndex(i - m_first), Qt::DisplayRole).toDouble()); |
|
350 | 350 | m_series->insert(i - m_first, point); |
|
351 | 351 | } |
|
352 | 352 | |
|
353 | 353 | // remove excess of slices (abouve m_count) |
|
354 | 354 | if (m_count != -1 && m_series->points().size() > m_count) |
|
355 | 355 | for (int i = m_series->points().size() - 1; i >= m_count; i--) { |
|
356 | 356 | m_series->remove(m_series->points().at(i)); |
|
357 | 357 | } |
|
358 | 358 | } |
|
359 | 359 | } |
|
360 | 360 | |
|
361 | 361 | void QXYModelMapperPrivate::removeData(int start, int end) |
|
362 | 362 | { |
|
363 | 363 | if (m_model == 0 || m_series == 0) |
|
364 | 364 | return; |
|
365 | 365 | |
|
366 | 366 | int removedCount = end - start + 1; |
|
367 | 367 | if (m_count != -1 && start >= m_first + m_count) { |
|
368 | 368 | return; |
|
369 | 369 | } else { |
|
370 | 370 | int toRemove = qMin(m_series->count(), removedCount); // first find how many items can actually be removed |
|
371 | 371 | int first = qMax(start, m_first); // get the index of the first item that will be removed. |
|
372 | 372 | int last = qMin(first + toRemove - 1, m_series->count() + m_first - 1); // get the index of the last item that will be removed. |
|
373 | 373 | for (int i = last; i >= first; i--) { |
|
374 | 374 | m_series->remove(m_series->points().at(i - m_first)); |
|
375 | 375 | } |
|
376 | 376 | |
|
377 | 377 | if (m_count != -1) { |
|
378 | 378 | int itemsAvailable; // check how many are available to be added |
|
379 | 379 | if (m_orientation == Qt::Vertical) |
|
380 | 380 | itemsAvailable = m_model->rowCount() - m_first - m_series->count(); |
|
381 | 381 | else |
|
382 | 382 | itemsAvailable = m_model->columnCount() - m_first - m_series->count(); |
|
383 | 383 | int toBeAdded = qMin(itemsAvailable, m_count - m_series->count()); // add not more items than there is space left to be filled. |
|
384 | 384 | int currentSize = m_series->count(); |
|
385 | 385 | if (toBeAdded > 0) |
|
386 | 386 | for (int i = m_series->count(); i < currentSize + toBeAdded; i++) { |
|
387 | 387 | QPointF point; |
|
388 | 388 | point.setX(m_model->data(xModelIndex(i), Qt::DisplayRole).toDouble()); |
|
389 | 389 | point.setY(m_model->data(yModelIndex(i), Qt::DisplayRole).toDouble()); |
|
390 | 390 | m_series->insert(i, point); |
|
391 | 391 | } |
|
392 | 392 | } |
|
393 | 393 | } |
|
394 | 394 | } |
|
395 | 395 | |
|
396 | 396 | void QXYModelMapperPrivate::initializeXYFromModel() |
|
397 | 397 | { |
|
398 | 398 | if (m_model == 0 || m_series == 0) |
|
399 | 399 | return; |
|
400 | 400 | |
|
401 | 401 | blockSeriesSignals(); |
|
402 | 402 | // clear current content |
|
403 | 403 | m_series->clear(); |
|
404 | 404 | |
|
405 | 405 | // create the initial slices set |
|
406 | 406 | int pointPos = 0; |
|
407 | 407 | QModelIndex xIndex = xModelIndex(pointPos); |
|
408 | 408 | QModelIndex yIndex = yModelIndex(pointPos); |
|
409 | 409 | while (xIndex.isValid() && yIndex.isValid()) { |
|
410 | 410 | QPointF point; |
|
411 | 411 | point.setX(m_model->data(xIndex, Qt::DisplayRole).toDouble()); |
|
412 | 412 | point.setY(m_model->data(yIndex, Qt::DisplayRole).toDouble()); |
|
413 | 413 | m_series->append(point); |
|
414 | 414 | pointPos++; |
|
415 | 415 | xIndex = xModelIndex(pointPos); |
|
416 | 416 | yIndex = yModelIndex(pointPos); |
|
417 | 417 | } |
|
418 | 418 | blockSeriesSignals(false); |
|
419 | 419 | } |
|
420 | 420 | |
|
421 | 421 | #include "moc_qxymodelmapper.cpp" |
|
422 | 422 | #include "moc_qxymodelmapper_p.cpp" |
|
423 | 423 | |
|
424 | 424 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,573 +1,573 | |||
|
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 "tablewidget.h" |
|
22 | 22 | #include <QGridLayout> |
|
23 | 23 | #include <QTableView> |
|
24 | 24 | #include <QChart> |
|
25 | 25 | #include <QStyledItemDelegate> |
|
26 | 26 | #include <QLineSeries> |
|
27 | 27 | #include <QSplineSeries> |
|
28 | 28 | #include <QScatterSeries> |
|
29 | 29 | #include <QVXYModelMapper> |
|
30 | 30 | #include "customtablemodel.h" |
|
31 | 31 | #include <QPieSeries> |
|
32 | 32 | #include <QVPieModelMapper> |
|
33 | 33 | #include <QPieSlice> |
|
34 | 34 | #include <QAreaSeries> |
|
35 | 35 | #include <QBarSeries> |
|
36 | 36 | #include <QGroupedBarSeries> |
|
37 | 37 | #include <QBarSet> |
|
38 | 38 | #include <QVBarModelMapper> |
|
39 | 39 | #include <QPushButton> |
|
40 | 40 | #include <QRadioButton> |
|
41 | 41 | #include <QLabel> |
|
42 | 42 | #include <QSpinBox> |
|
43 | 43 | #include <QTime> |
|
44 | 44 | #include <QHeaderView> |
|
45 | 45 | |
|
46 | 46 | TableWidget::TableWidget(QWidget *parent) |
|
47 | 47 | : QWidget(parent), |
|
48 | 48 | m_series(0), |
|
49 | 49 | m_mapper(0), |
|
50 | 50 | m_model(0), |
|
51 | 51 | m_pieMapper(0), |
|
52 | 52 | m_pieMapper2(0) |
|
53 | 53 | // specialPie(0) |
|
54 | 54 | { |
|
55 | 55 | setGeometry(1900, 100, 1000, 600); |
|
56 | 56 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
57 | 57 | // create simple model for storing data |
|
58 | 58 | // user's table data model |
|
59 | 59 | m_model = new CustomTableModel; |
|
60 | 60 | m_tableView = new QTableView; |
|
61 | 61 | m_tableView->setModel(m_model); |
|
62 | 62 | // m_tableView->setMinimumHeight(300); |
|
63 | 63 | m_tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch); |
|
64 | 64 | m_tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch); |
|
65 | 65 | |
|
66 | 66 | m_chart = new QChart; |
|
67 | 67 | m_chart->legend()->setVisible(true); |
|
68 | 68 | m_chart->setAnimationOptions(QChart::SeriesAnimations); |
|
69 | 69 | m_chartView = new QChartView(m_chart); |
|
70 | 70 | m_chartView->setRenderHint(QPainter::Antialiasing); |
|
71 | 71 | m_chartView->setMinimumSize(640, 480); |
|
72 | 72 | |
|
73 | 73 | // add, remove data buttons |
|
74 | 74 | QPushButton* addRowAboveButton = new QPushButton("Add row above"); |
|
75 | 75 | connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove())); |
|
76 | 76 | |
|
77 | 77 | QPushButton* addRowBelowButton = new QPushButton("Add row below"); |
|
78 | 78 | connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow())); |
|
79 | 79 | |
|
80 | 80 | QPushButton* removeRowButton = new QPushButton("Remove row"); |
|
81 | 81 | connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow())); |
|
82 | 82 | |
|
83 | 83 | QPushButton* addColumnRightButton = new QPushButton("Add column to the right"); |
|
84 | 84 | connect(addColumnRightButton, SIGNAL(clicked()), this, SLOT(addColumnRight())); |
|
85 | 85 | |
|
86 | 86 | QPushButton* removeColumnButton = new QPushButton("Remove column"); |
|
87 | 87 | connect(removeColumnButton, SIGNAL(clicked()), this, SLOT(removeColumn())); |
|
88 | 88 | |
|
89 | 89 | QPushButton* specialPieButton = new QPushButton("Add slices using series API"); |
|
90 | 90 | connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie())); |
|
91 | 91 | |
|
92 | 92 | QPushButton* specialPieButton2 = new QPushButton("Remove slices using series API"); |
|
93 | 93 | connect(specialPieButton2, SIGNAL(clicked()), this, SLOT(testPie2())); |
|
94 | 94 | |
|
95 | 95 | QPushButton* specialPieButton3 = new QPushButton("Modify slices using series API"); |
|
96 | 96 | connect(specialPieButton3, SIGNAL(clicked()), this, SLOT(testPie3())); |
|
97 | 97 | |
|
98 | 98 | QPushButton* xyTestButton = new QPushButton("Append XY point"); |
|
99 | 99 | connect(xyTestButton, SIGNAL(clicked()), this, SLOT(testXY())); |
|
100 | 100 | |
|
101 | 101 | |
|
102 | 102 | QLabel *spinBoxLabel = new QLabel("Rows affected:"); |
|
103 | 103 | |
|
104 | 104 | // spin box for setting number of affected items (add, remove) |
|
105 | 105 | m_linesCountSpinBox = new QSpinBox; |
|
106 | 106 | m_linesCountSpinBox->setRange(1, 10); |
|
107 | 107 | m_linesCountSpinBox->setValue(1); |
|
108 | 108 | |
|
109 | 109 | // buttons layout |
|
110 | 110 | QVBoxLayout* buttonsLayout = new QVBoxLayout; |
|
111 | 111 | buttonsLayout->addWidget(spinBoxLabel); |
|
112 | 112 | buttonsLayout->addWidget(m_linesCountSpinBox); |
|
113 | 113 | // buttonsLayout->addWidget(addRowAboveButton); |
|
114 | 114 | buttonsLayout->addWidget(addRowBelowButton); |
|
115 | 115 | buttonsLayout->addWidget(removeRowButton); |
|
116 | 116 | // buttonsLayout->addWidget(addColumnRightButton); |
|
117 | 117 | // buttonsLayout->addWidget(removeColumnButton); |
|
118 | 118 | buttonsLayout->addWidget(specialPieButton); |
|
119 | 119 | buttonsLayout->addWidget(specialPieButton2); |
|
120 | 120 | buttonsLayout->addWidget(specialPieButton3); |
|
121 | 121 | buttonsLayout->addWidget(xyTestButton); |
|
122 | 122 | buttonsLayout->addStretch(); |
|
123 | 123 | |
|
124 | 124 | // chart type radio buttons |
|
125 | 125 | m_lineRadioButton = new QRadioButton("Line"); |
|
126 | 126 | m_splineRadioButton = new QRadioButton("Spline"); |
|
127 | 127 | m_scatterRadioButton = new QRadioButton("Scatter"); |
|
128 | 128 | m_pieRadioButton = new QRadioButton("Pie"); |
|
129 | 129 | m_areaRadioButton = new QRadioButton("Area"); |
|
130 | 130 | m_barRadioButton = new QRadioButton("Bar"); |
|
131 | 131 | |
|
132 | 132 | connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool))); |
|
133 | 133 | connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool))); |
|
134 | 134 | connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool))); |
|
135 | 135 | connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool))); |
|
136 | 136 | connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool))); |
|
137 | 137 | connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool))); |
|
138 | 138 | m_barRadioButton->setChecked(true); |
|
139 | 139 | |
|
140 | 140 | // radio buttons layout |
|
141 | 141 | QVBoxLayout* radioLayout = new QVBoxLayout; |
|
142 | 142 | radioLayout->addWidget(m_lineRadioButton); |
|
143 | 143 | radioLayout->addWidget(m_splineRadioButton); |
|
144 | 144 | radioLayout->addWidget(m_scatterRadioButton); |
|
145 | 145 | radioLayout->addWidget(m_pieRadioButton); |
|
146 | 146 | // radioLayout->addWidget(m_areaRadioButton); |
|
147 | 147 | radioLayout->addWidget(m_barRadioButton); |
|
148 | 148 | radioLayout->addStretch(); |
|
149 | 149 | |
|
150 | 150 | // create main layout |
|
151 | 151 | QGridLayout* mainLayout = new QGridLayout; |
|
152 | 152 | mainLayout->addLayout(buttonsLayout, 2, 0); |
|
153 | 153 | mainLayout->addLayout(radioLayout, 3, 0); |
|
154 | 154 | mainLayout->addWidget(m_tableView, 1, 0); |
|
155 | 155 | mainLayout->addWidget(m_chartView, 1, 1, 2, 1); |
|
156 | 156 | setLayout(mainLayout); |
|
157 | 157 | m_lineRadioButton->setFocus(); |
|
158 | 158 | } |
|
159 | 159 | |
|
160 | 160 | void TableWidget::addRowAbove() |
|
161 | 161 | { |
|
162 | 162 | m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value()); |
|
163 | 163 | |
|
164 | 164 | } |
|
165 | 165 | |
|
166 | 166 | void TableWidget::addRowBelow() |
|
167 | 167 | { |
|
168 | 168 | m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value()); |
|
169 | 169 | |
|
170 | 170 | } |
|
171 | 171 | |
|
172 | 172 | void TableWidget::removeRow() |
|
173 | 173 | { |
|
174 | 174 | m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value())); |
|
175 | 175 | } |
|
176 | 176 | |
|
177 | 177 | void TableWidget::addColumnRight() |
|
178 | 178 | { |
|
179 | 179 | m_model->insertColumns(m_tableView->currentIndex().column() + 1, m_linesCountSpinBox->value()); |
|
180 | 180 | } |
|
181 | 181 | |
|
182 | 182 | void TableWidget::removeColumn() |
|
183 | 183 | { |
|
184 | 184 | m_model->removeColumns(m_tableView->currentIndex().column(), qMin(m_model->columnCount() - m_tableView->currentIndex().column(), m_linesCountSpinBox->value())); |
|
185 | 185 | } |
|
186 | 186 | |
|
187 | 187 | void TableWidget::updateChartType(bool toggle) |
|
188 | 188 | { |
|
189 | 189 | // this if is needed, so that the function is only called once. |
|
190 | 190 | // For the radioButton that was enabled. |
|
191 | 191 | if (toggle) { |
|
192 | 192 | // specialPie = 0; |
|
193 | 193 | m_chart->removeAllSeries(); |
|
194 | 194 | m_series = 0; |
|
195 | 195 | // m_chart->axisX()->setNiceNumbersEnabled(false); |
|
196 | 196 | // m_chart->axisY()->setNiceNumbersEnabled(false); |
|
197 | 197 | if (m_mapper) { |
|
198 | 198 | m_mapper->deleteLater(); |
|
199 | 199 | m_mapper = 0; |
|
200 | 200 | } |
|
201 | 201 | |
|
202 | 202 | if (m_pieMapper) { |
|
203 | 203 | m_pieMapper->deleteLater(); |
|
204 | 204 | m_pieMapper = 0; |
|
205 | 205 | } |
|
206 | 206 | |
|
207 | 207 | if (m_pieMapper2) { |
|
208 | 208 | m_pieMapper2->deleteLater(); |
|
209 | 209 | m_pieMapper2 = 0; |
|
210 | 210 | } |
|
211 | 211 | |
|
212 | 212 | // if (m_series) { |
|
213 | 213 | // delete m_series; |
|
214 | 214 | // m_series = 0; |
|
215 | 215 | // } |
|
216 | 216 | |
|
217 | 217 | // renable axes of the chart (pie hides them) |
|
218 | 218 | // x axis |
|
219 | 219 | QAxis *axis = m_chart->axisX(); |
|
220 | 220 | axis->setAxisVisible(true); |
|
221 | 221 | axis->setGridLineVisible(true); |
|
222 | 222 | axis->setLabelsVisible(true); |
|
223 | 223 | |
|
224 | 224 | // y axis |
|
225 | 225 | axis = m_chart->axisY(); |
|
226 | 226 | axis->setAxisVisible(true); |
|
227 | 227 | axis->setGridLineVisible(true); |
|
228 | 228 | axis->setLabelsVisible(true); |
|
229 | 229 | |
|
230 | 230 | m_model->clearMapping(); |
|
231 | 231 | |
|
232 | 232 | QString seriesColorHex = "#000000"; |
|
233 | 233 | // QPen pen; |
|
234 | 234 | // pen.setWidth(2); |
|
235 | 235 | |
|
236 | 236 | if (m_lineRadioButton->isChecked()) |
|
237 | 237 | { |
|
238 | 238 | m_chart->setAnimationOptions(QChart::NoAnimation); |
|
239 | 239 | |
|
240 | 240 | // series 1 |
|
241 | 241 | m_series = new QLineSeries; |
|
242 | 242 | |
|
243 | 243 | m_mapper = new QVXYModelMapper; |
|
244 | 244 | m_mapper->setModel(m_model); |
|
245 | 245 | m_mapper->setSeries(m_series); |
|
246 | 246 | m_mapper->setXColumn(0); |
|
247 | 247 | m_mapper->setYColumn(1); |
|
248 | 248 | m_mapper->setFirst(3); |
|
249 | 249 | m_mapper->setCount(4); |
|
250 | 250 | |
|
251 | 251 | // m_series->setModelMapping(0,1, Qt::Vertical); |
|
252 | 252 | // m_series->setModelMappingRange(3, 4); |
|
253 | 253 | m_chart->addSeries(m_series); |
|
254 | 254 | seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); |
|
255 | 255 | m_model->addMapping(seriesColorHex, QRect(0, 3, 2, 4)); |
|
256 | 256 | |
|
257 | 257 | // // series 2 |
|
258 | 258 | // m_series = new QLineSeries; |
|
259 | 259 | // m_series->setModel(m_model); |
|
260 | 260 | |
|
261 | 261 | // mapper = new QXYModelMapper; |
|
262 | 262 | // mapper->setMapX(3); |
|
263 | 263 | // mapper->setMapY(4); |
|
264 | 264 | // // mapper->setFirst(3); |
|
265 | 265 | // // mapper->setCount(4); |
|
266 | 266 | // m_series->setModelMapper(mapper); |
|
267 | 267 | // // m_series->setModelMapping(2,3, Qt::Vertical); |
|
268 | 268 | // m_chart->addSeries(m_series); |
|
269 | 269 | // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); |
|
270 | 270 | // m_model->addMapping(seriesColorHex, QRect(3, 0, 2, 1000)); |
|
271 | 271 | |
|
272 | 272 | // // series 3 |
|
273 | 273 | // m_series = new QLineSeries; |
|
274 | 274 | // m_series->setModel(m_model); |
|
275 | 275 | |
|
276 | 276 | // mapper = new QXYModelMapper; |
|
277 | 277 | // mapper->setMapX(5); |
|
278 | 278 | // mapper->setMapY(6); |
|
279 | 279 | // mapper->setFirst(2); |
|
280 | 280 | // mapper->setCount(-1); |
|
281 | 281 | // m_series->setModelMapper(mapper); |
|
282 | 282 | // // m_series->setModelMapping(4,5, Qt::Vertical); |
|
283 | 283 | // // m_series->setModelMappingRange(2, -1); |
|
284 | 284 | // m_chart->addSeries(m_series); |
|
285 | 285 | // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); |
|
286 | 286 | // m_model->addMapping(seriesColorHex, QRect(5, 2, 2, 1000)); |
|
287 | 287 | } |
|
288 | 288 | else if (m_splineRadioButton->isChecked()) |
|
289 | 289 | { |
|
290 | 290 | m_chart->setAnimationOptions(QChart::NoAnimation); |
|
291 | 291 | |
|
292 | 292 | // series 1 |
|
293 | 293 | m_series = new QSplineSeries; |
|
294 | 294 | // m_series->setModel(m_model); |
|
295 | 295 | |
|
296 | 296 | m_mapper = new QVXYModelMapper; |
|
297 | 297 | m_mapper->setSeries(m_series); |
|
298 | 298 | m_mapper->setModel(m_model); |
|
299 | 299 | m_mapper->setXColumn(0); |
|
300 | 300 | m_mapper->setYColumn(1); |
|
301 | 301 | m_mapper->setFirst(0); |
|
302 | 302 | m_mapper->setCount(-1); |
|
303 | 303 | |
|
304 | 304 | // m_series->setModelMapper(mapper); |
|
305 | 305 | |
|
306 | 306 | m_chart->addSeries(m_series); |
|
307 | 307 | seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); |
|
308 | 308 | m_model->addMapping(seriesColorHex, QRect(0, 0, 2, 1000)); |
|
309 | 309 | |
|
310 | 310 | // // series 2 |
|
311 | 311 | // m_series = new QSplineSeries; |
|
312 | 312 | // m_series->setModel(m_model); |
|
313 | 313 | |
|
314 | 314 | // mapper = new QXYModelMapper; |
|
315 | 315 | // mapper->setMapX(2); |
|
316 | 316 | // mapper->setMapY(3); |
|
317 | 317 | // mapper->setFirst(2); |
|
318 | 318 | // mapper->setCount(4); |
|
319 | 319 | |
|
320 | 320 | // m_series->setModelMapper(mapper); |
|
321 | 321 | |
|
322 | 322 | // m_chart->addSeries(m_series); |
|
323 | 323 | // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); |
|
324 | 324 | // m_model->addMapping(seriesColorHex, QRect(2, 2, 2, 4)); |
|
325 | 325 | |
|
326 | 326 | // // series 3 |
|
327 | 327 | // m_series = new QSplineSeries; |
|
328 | 328 | // m_series->setModel(m_model); |
|
329 | 329 | |
|
330 | 330 | // mapper = new QXYModelMapper; |
|
331 | 331 | // mapper->setMapX(4); |
|
332 | 332 | // mapper->setMapY(5); |
|
333 | 333 | // mapper->setFirst(2); |
|
334 | 334 | // mapper->setCount(-1); |
|
335 | 335 | |
|
336 | 336 | // m_series->setModelMapper(mapper); |
|
337 | 337 | |
|
338 | 338 | // m_chart->addSeries(m_series); |
|
339 | 339 | // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); |
|
340 | 340 | // m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000)); |
|
341 | 341 | } else if (m_scatterRadioButton->isChecked()) |
|
342 | 342 | { |
|
343 | 343 | m_chart->setAnimationOptions(QChart::NoAnimation); |
|
344 | 344 | |
|
345 | 345 | // series 1 |
|
346 | 346 | m_series = new QScatterSeries; |
|
347 | 347 | |
|
348 | 348 | m_mapper = new QVXYModelMapper; |
|
349 | 349 | m_mapper->setSeries(m_series); |
|
350 | 350 | m_mapper->setModel(m_model); |
|
351 | 351 | m_mapper->setXColumn(0); |
|
352 | 352 | m_mapper->setYColumn(1); |
|
353 | 353 | m_mapper->setFirst(0); |
|
354 | 354 | m_mapper->setCount(-1); |
|
355 | 355 | |
|
356 | 356 | m_chart->addSeries(m_series); |
|
357 | 357 | seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper(); |
|
358 | 358 | m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000)); |
|
359 | 359 | |
|
360 | 360 | // // series 2 |
|
361 | 361 | // m_series = new QScatterSeries; |
|
362 | 362 | // m_series->setModel(m_model); |
|
363 | 363 | // m_series->setModelMapping(2,3, Qt::Vertical); |
|
364 | 364 | // // m_series->setModelMappingRange(1, 6); |
|
365 | 365 | // // series->setModelMapping(2,3, Qt::Horizontal); |
|
366 | 366 | // m_chart->addSeries(m_series); |
|
367 | 367 | |
|
368 | 368 | // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper(); |
|
369 | 369 | // m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6)); |
|
370 | 370 | |
|
371 | 371 | // // series 3 |
|
372 | 372 | // m_series = new QScatterSeries; |
|
373 | 373 | // m_series->setModel(m_model); |
|
374 | 374 | // m_series->setModelMapping(4,5, Qt::Vertical); |
|
375 | 375 | // // series->setModelMapping(4,5, Qt::Horizontal); |
|
376 | 376 | // m_chart->addSeries(m_series); |
|
377 | 377 | // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper(); |
|
378 | 378 | // m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000)); |
|
379 | 379 | } else if (m_pieRadioButton->isChecked()) { |
|
380 | 380 | m_chart->setAnimationOptions(QChart::SeriesAnimations); |
|
381 | 381 | |
|
382 | 382 | // pie 1 |
|
383 | 383 | m_pieSeries = new QPieSeries; |
|
384 | 384 | |
|
385 | 385 | m_pieMapper = new QVPieModelMapper; |
|
386 | 386 | m_pieMapper->setValuesColumn(1); |
|
387 | 387 | m_pieMapper->setLabelsColumn(7); |
|
388 | 388 | m_pieMapper->setSeries(m_pieSeries); |
|
389 | 389 | m_pieMapper->setModel(m_model); |
|
390 | 390 | m_pieMapper->setFirst(2); |
|
391 | 391 | // m_pieMapper->setCount(5); |
|
392 | 392 | // pieSeries->setModelMapper(mapper); |
|
393 | 393 | |
|
394 | 394 | m_pieSeries->setLabelsVisible(true); |
|
395 | 395 | m_pieSeries->setPieSize(0.35); |
|
396 | 396 | m_pieSeries->setHorizontalPosition(0.25); |
|
397 | 397 | m_pieSeries->setVerticalPosition(0.35); |
|
398 | 398 | |
|
399 | 399 | m_chart->addSeries(m_pieSeries); |
|
400 | 400 | seriesColorHex = "#" + QString::number(m_pieSeries->slices().at(m_pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper(); |
|
401 | 401 | m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 50)); |
|
402 | 402 | |
|
403 | 403 | |
|
404 | 404 | // pieSeries->slices().at(0)->setValue(400); |
|
405 | 405 | // pieSeries->slices().at(0)->setLabel(QString("36")); |
|
406 | 406 | |
|
407 | 407 | // pie 2 |
|
408 | 408 | m_pieSeries2 = new QPieSeries; |
|
409 | 409 | |
|
410 | 410 | m_pieMapper2 = new QVPieModelMapper; |
|
411 | 411 | m_pieMapper2->setValuesColumn(0); |
|
412 | 412 | m_pieMapper2->setLabelsColumn(7); |
|
413 | 413 | m_pieMapper2->setModel(m_model); |
|
414 | 414 | m_pieMapper2->setSeries(m_pieSeries2); |
|
415 | 415 | m_pieMapper2->setFirst(2); |
|
416 | 416 | |
|
417 | 417 | m_pieSeries2->setLabelsVisible(true); |
|
418 | 418 | m_pieSeries2->setPieSize(0.35); |
|
419 | 419 | m_pieSeries2->setHorizontalPosition(0.75); |
|
420 | 420 | m_pieSeries2->setVerticalPosition(0.65); |
|
421 | 421 | m_chart->addSeries(m_pieSeries2); |
|
422 | 422 | seriesColorHex = "#" + QString::number(m_pieSeries2->slices().at(m_pieSeries2->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper(); |
|
423 | 423 | m_model->addMapping(seriesColorHex, QRect(0, 2, 1, 1000)); |
|
424 | 424 | |
|
425 | 425 | // // pie 3 |
|
426 | 426 | // pieSeries = new QPieSeries; |
|
427 | 427 | // pieSeries->setModel(m_model); |
|
428 | 428 | // pieSeries->setModelMapping(2,2, Qt::Vertical); |
|
429 | 429 | // pieSeries->setLabelsVisible(true); |
|
430 | 430 | // pieSeries->setPieSize(0.35); |
|
431 | 431 | // pieSeries->setHorizontalPosition(0.5); |
|
432 | 432 | // pieSeries->setVerticalPosition(0.75); |
|
433 | 433 | // m_chart->addSeries(pieSeries); |
|
434 | 434 | // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper(); |
|
435 | 435 | // m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000)); |
|
436 | 436 | |
|
437 | 437 | // // special pie |
|
438 | 438 | // specialPie = new QPieSeries; |
|
439 | 439 | // specialPie->append(17, "1"); |
|
440 | 440 | // specialPie->append(45, "2"); |
|
441 | 441 | // specialPie->append(77, "3"); |
|
442 | 442 | // specialPie->append(37, "4"); |
|
443 | 443 | // specialPie->append(27, "5"); |
|
444 | 444 | // specialPie->append(47, "6"); |
|
445 | 445 | // specialPie->setPieSize(0.35); |
|
446 | 446 | // specialPie->setHorizontalPosition(0.8); |
|
447 | 447 | // specialPie->setVerticalPosition(0.75); |
|
448 | 448 | // specialPie->setLabelsVisible(true); |
|
449 | 449 | // m_chart->addSeries(specialPie); |
|
450 | 450 | } |
|
451 | 451 | // else if (m_areaRadioButton->isChecked()) |
|
452 | 452 | // { |
|
453 | 453 | // m_chart->setAnimationOptions(QChart::NoAnimation); |
|
454 | 454 | |
|
455 | 455 | // QLineSeries* upperLineSeries = new QLineSeries; |
|
456 | 456 | // upperLineSeries->setModel(m_model); |
|
457 | 457 | // upperLineSeries->setModelMapping(0, 1, Qt::Vertical); |
|
458 | 458 | // // upperLineSeries->setModelMappingRange(1, 5); |
|
459 | 459 | // QLineSeries* lowerLineSeries = new QLineSeries; |
|
460 | 460 | // lowerLineSeries->setModel(m_model); |
|
461 | 461 | // lowerLineSeries->setModelMapping(2, 3, Qt::Vertical); |
|
462 | 462 | // QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries); |
|
463 | 463 | // m_chart->addSeries(areaSeries); |
|
464 | 464 | // seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper(); |
|
465 | 465 | // m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5)); |
|
466 | 466 | // m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000)); |
|
467 | 467 | // } |
|
468 | 468 | else if (m_barRadioButton->isChecked()) |
|
469 | 469 | { |
|
470 | 470 | m_chart->setAnimationOptions(QChart::SeriesAnimations); |
|
471 | 471 | |
|
472 | 472 | QGroupedBarSeries* barSeries = new QGroupedBarSeries(); |
|
473 | 473 | // barSeries->setCategories(QStringList()); |
|
474 | 474 | // barSeries->setModel(m_model); |
|
475 | 475 | // barSeries->setModelMappingRange(2, 5); |
|
476 | 476 | // barSeries->setModelMapping(5, 2, 4, Qt::Vertical); |
|
477 | 477 | |
|
478 | 478 | int first = 3; |
|
479 | int count = 4; | |
|
479 | // int count = 4; | |
|
480 | 480 | QVBarModelMapper *mapper = new QVBarModelMapper; |
|
481 | 481 | mapper->setCategoriesSection(5); |
|
482 | 482 | mapper->setFirstBarSection(2); |
|
483 | 483 | mapper->setLastBarSection(4); |
|
484 | 484 | mapper->setFirst(first); |
|
485 | mapper->setCount(count); | |
|
485 | // mapper->setCount(count); | |
|
486 | 486 | mapper->setSeries(barSeries); |
|
487 | 487 | mapper->setModel(m_model); |
|
488 | 488 | // barSeries->setModelMapper(mapper); |
|
489 | 489 | m_chart->addSeries(barSeries); |
|
490 | 490 | QList<QBarSet*> barsets = barSeries->barSets(); |
|
491 | 491 | for (int i = 0; i < barsets.count(); i++) { |
|
492 | 492 | seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper(); |
|
493 | m_model->addMapping(seriesColorHex, QRect(2 + i, first, 1, count)); | |
|
493 | m_model->addMapping(seriesColorHex, QRect(2 + i, first, 1, barsets.at(i)->count())); | |
|
494 | 494 | } |
|
495 | 495 | } |
|
496 | 496 | |
|
497 | 497 | |
|
498 | 498 | if (!m_barRadioButton->isChecked()) { |
|
499 | 499 | m_chart->axisX()->setRange(0, 500); |
|
500 | 500 | m_chart->axisY()->setRange(0, 220); |
|
501 | 501 | } |
|
502 | 502 | m_chart->legend()->setVisible(true); |
|
503 | 503 | |
|
504 | 504 | // repaint table view colors |
|
505 | 505 | m_tableView->repaint(); |
|
506 | 506 | m_tableView->setFocus(); |
|
507 | 507 | } |
|
508 | 508 | } |
|
509 | 509 | |
|
510 | 510 | void TableWidget::testPie() |
|
511 | 511 | { |
|
512 | 512 | // m_pieMapper->setCount(-1); |
|
513 | 513 | QPieSlice *slice = new QPieSlice("Hehe", 145); |
|
514 | 514 | slice->setLabelVisible(); |
|
515 | 515 | m_pieSeries->append(slice); |
|
516 | 516 | |
|
517 | 517 | slice = new QPieSlice("Hoho", 34); |
|
518 | 518 | slice->setLabelVisible(); |
|
519 | 519 | m_pieSeries->append(slice); |
|
520 | 520 | // m_series->modelMapper()->setMapX(4); |
|
521 | 521 | // m_tableView->setColumnWidth(10, 250); |
|
522 | 522 | // if (specialPie) { |
|
523 | 523 | // specialPie->remove(specialPie->slices().at(2)); |
|
524 | 524 | // // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2)); |
|
525 | 525 | // specialPie->append(4, "heloo"); |
|
526 | 526 | // } |
|
527 | 527 | } |
|
528 | 528 | |
|
529 | 529 | void TableWidget::testPie2() |
|
530 | 530 | { |
|
531 | 531 | QPieSlice *slice; |
|
532 | 532 | if (m_pieSeries->count() > 0) { |
|
533 | 533 | slice = m_pieSeries->slices().last(); |
|
534 | 534 | m_pieSeries->remove(slice); |
|
535 | 535 | } |
|
536 | 536 | |
|
537 | 537 | if (m_pieSeries->count() > 0) { |
|
538 | 538 | slice = m_pieSeries->slices().first(); |
|
539 | 539 | m_pieSeries->remove(slice); |
|
540 | 540 | } |
|
541 | 541 | } |
|
542 | 542 | |
|
543 | 543 | void TableWidget::testPie3() |
|
544 | 544 | { |
|
545 | 545 | QPieSlice *slice; |
|
546 | 546 | if (m_pieSeries->count() > 0) { |
|
547 | 547 | slice = m_pieSeries->slices().last(); |
|
548 | 548 | slice->setLabel("Dalej"); |
|
549 | 549 | slice->setValue(222); |
|
550 | 550 | } |
|
551 | 551 | |
|
552 | 552 | if (m_pieSeries->count() > 0) { |
|
553 | 553 | slice = m_pieSeries->slices().first(); |
|
554 | 554 | slice->setLabel("Prawie"); |
|
555 | 555 | slice->setValue(111); |
|
556 | 556 | } |
|
557 | 557 | } |
|
558 | 558 | |
|
559 | 559 | void TableWidget::testXY() |
|
560 | 560 | { |
|
561 | 561 | // if (m_series->type() != QAbstractSeries::SeriesTypeLine) { |
|
562 | 562 | // m_series->append(QPointF(150, 75)); |
|
563 | 563 | // } |
|
564 | 564 | |
|
565 | 565 | if (m_series->count() > 0) { |
|
566 | 566 | m_series->remove(m_series->points().last()); |
|
567 | 567 | } |
|
568 | 568 | } |
|
569 | 569 | |
|
570 | 570 | TableWidget::~TableWidget() |
|
571 | 571 | { |
|
572 | 572 | |
|
573 | 573 | } |
General Comments 0
You need to be logged in to leave comments.
Login now