##// END OF EJS Templates
Fixed problem with qhxymodelmapper
Marek Rosa -
r1317:1951d151d7af
parent child
Show More
@@ -1,424 +1,440
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 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 if (m_orientation == Qt::Vertical && (index.column() == m_xSection|| index.column() == m_ySection)) {
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 oldPoint = m_series->points().at(index.row() - m_first);
259 newPoint.setX(m_model->data(m_model->index(index.row(), m_xSection)).toReal());
260 newPoint.setY(m_model->data(m_model->index(index.row(), m_ySection)).toReal());
258 QModelIndex xIndex = xModelIndex(index.row() - m_first);
259 QModelIndex yIndex = yModelIndex(index.row() - m_first);
260 if (xIndex.isValid() && yIndex.isValid()) {
261 oldPoint = m_series->points().at(index.row() - m_first);
262 newPoint.setX(m_model->data(xIndex).toReal());
263 newPoint.setY(m_model->data(yIndex).toReal());
264 }
261 265 }
262 266 } else if (m_orientation == Qt::Horizontal && (index.row() == m_xSection || index.row() == m_ySection)) {
263 267 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count)) {
264 oldPoint = m_series->points().at(index.column() - m_first);
265 newPoint.setX(m_model->data(m_model->index(m_xSection, index.column())).toReal());
266 newPoint.setY(m_model->data(m_model->index(m_ySection, index.column())).toReal());
268 QModelIndex xIndex = xModelIndex(index.column() - m_first);
269 QModelIndex yIndex = yModelIndex(index.column() - m_first);
270 if (xIndex.isValid() && yIndex.isValid()) {
271 oldPoint = m_series->points().at(index.column() - m_first);
272 newPoint.setX(m_model->data(xIndex).toReal());
273 newPoint.setY(m_model->data(yIndex).toReal());
274 }
267 275 }
268 276 } else {
269 277 continue;
270 278 }
271 279 m_series->replace(oldPoint, newPoint);
272 280 }
273 281 }
274 282 blockSeriesSignals(false);
275 283 }
276 284
277 285 void QXYModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
278 286 {
279 287 Q_UNUSED(parent);
280 288 if (m_modelSignalsBlock)
281 289 return;
282 290
283 291 blockSeriesSignals();
284 292 if (m_orientation == Qt::Vertical)
285 293 insertData(start, end);
286 294 else if (start <= m_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy
287 295 initializeXYFromModel();
288 296 blockSeriesSignals(false);
289 297 }
290 298
291 299 void QXYModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
292 300 {
293 301 Q_UNUSED(parent);
294 302 if (m_modelSignalsBlock)
295 303 return;
296 304
297 305 blockSeriesSignals();
298 306 if (m_orientation == Qt::Vertical)
299 307 removeData(start, end);
300 308 else if (start <= m_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy
301 309 initializeXYFromModel();
302 310 blockSeriesSignals(false);
303 311 }
304 312
305 313 void QXYModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
306 314 {
307 315 Q_UNUSED(parent);
308 316 if (m_modelSignalsBlock)
309 317 return;
310 318
311 319 blockSeriesSignals();
312 320 if (m_orientation == Qt::Horizontal)
313 321 insertData(start, end);
314 322 else if (start <= m_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy
315 323 initializeXYFromModel();
316 324 blockSeriesSignals(false);
317 325 }
318 326
319 327 void QXYModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
320 328 {
321 329 Q_UNUSED(parent);
322 330 if (m_modelSignalsBlock)
323 331 return;
324 332
325 333 blockSeriesSignals();
326 334 if (m_orientation == Qt::Horizontal)
327 335 removeData(start, end);
328 336 else if (start <= m_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy
329 337 initializeXYFromModel();
330 338 blockSeriesSignals(false);
331 339 }
332 340
333 341 void QXYModelMapperPrivate::insertData(int start, int end)
334 342 {
335 343 if (m_model == 0 || m_series == 0)
336 344 return;
337 345
338 346 if (m_count != -1 && start >= m_first + m_count) {
339 347 return;
340 348 } else {
341 349 int addedCount = end - start + 1;
342 350 if (m_count != -1 && addedCount > m_count)
343 351 addedCount = m_count;
344 352 int first = qMax(start, m_first);
345 353 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
346 354 for (int i = first; i <= last; i++) {
347 355 QPointF point;
348 point.setX(m_model->data(xModelIndex(i - m_first), Qt::DisplayRole).toDouble());
349 point.setY(m_model->data(yModelIndex(i - m_first), Qt::DisplayRole).toDouble());
350 m_series->insert(i - m_first, point);
356 QModelIndex xIndex = xModelIndex(i - m_first);
357 QModelIndex yIndex = yModelIndex(i - m_first);
358 if (xIndex.isValid() && yIndex.isValid()) {
359 point.setX(m_model->data(xIndex, Qt::DisplayRole).toDouble());
360 point.setY(m_model->data(yIndex, Qt::DisplayRole).toDouble());
361 m_series->insert(i - m_first, point);
362 }
351 363 }
352 364
353 365 // remove excess of slices (abouve m_count)
354 366 if (m_count != -1 && m_series->points().size() > m_count)
355 367 for (int i = m_series->points().size() - 1; i >= m_count; i--) {
356 368 m_series->remove(m_series->points().at(i));
357 369 }
358 370 }
359 371 }
360 372
361 373 void QXYModelMapperPrivate::removeData(int start, int end)
362 374 {
363 375 if (m_model == 0 || m_series == 0)
364 376 return;
365 377
366 378 int removedCount = end - start + 1;
367 379 if (m_count != -1 && start >= m_first + m_count) {
368 380 return;
369 381 } else {
370 382 int toRemove = qMin(m_series->count(), removedCount); // first find how many items can actually be removed
371 383 int first = qMax(start, m_first); // get the index of the first item that will be removed.
372 384 int last = qMin(first + toRemove - 1, m_series->count() + m_first - 1); // get the index of the last item that will be removed.
373 385 for (int i = last; i >= first; i--) {
374 386 m_series->remove(m_series->points().at(i - m_first));
375 387 }
376 388
377 389 if (m_count != -1) {
378 390 int itemsAvailable; // check how many are available to be added
379 391 if (m_orientation == Qt::Vertical)
380 392 itemsAvailable = m_model->rowCount() - m_first - m_series->count();
381 393 else
382 394 itemsAvailable = m_model->columnCount() - m_first - m_series->count();
383 395 int toBeAdded = qMin(itemsAvailable, m_count - m_series->count()); // add not more items than there is space left to be filled.
384 396 int currentSize = m_series->count();
385 397 if (toBeAdded > 0)
386 398 for (int i = m_series->count(); i < currentSize + toBeAdded; i++) {
387 399 QPointF point;
388 point.setX(m_model->data(xModelIndex(i), Qt::DisplayRole).toDouble());
389 point.setY(m_model->data(yModelIndex(i), Qt::DisplayRole).toDouble());
390 m_series->insert(i, point);
400 QModelIndex xIndex = xModelIndex(i);
401 QModelIndex yIndex = yModelIndex(i);
402 if (xIndex.isValid() && yIndex.isValid()) {
403 point.setX(m_model->data(xIndex, Qt::DisplayRole).toDouble());
404 point.setY(m_model->data(yIndex, Qt::DisplayRole).toDouble());
405 m_series->insert(i, point);
406 }
391 407 }
392 408 }
393 409 }
394 410 }
395 411
396 412 void QXYModelMapperPrivate::initializeXYFromModel()
397 413 {
398 414 if (m_model == 0 || m_series == 0)
399 415 return;
400 416
401 417 blockSeriesSignals();
402 418 // clear current content
403 419 m_series->clear();
404 420
405 421 // create the initial slices set
406 422 int pointPos = 0;
407 423 QModelIndex xIndex = xModelIndex(pointPos);
408 424 QModelIndex yIndex = yModelIndex(pointPos);
409 425 while (xIndex.isValid() && yIndex.isValid()) {
410 426 QPointF point;
411 427 point.setX(m_model->data(xIndex, Qt::DisplayRole).toDouble());
412 428 point.setY(m_model->data(yIndex, Qt::DisplayRole).toDouble());
413 429 m_series->append(point);
414 430 pointPos++;
415 431 xIndex = xModelIndex(pointPos);
416 432 yIndex = yModelIndex(pointPos);
417 433 }
418 434 blockSeriesSignals(false);
419 435 }
420 436
421 437 #include "moc_qxymodelmapper.cpp"
422 438 #include "moc_qxymodelmapper_p.cpp"
423 439
424 440 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,276 +1,276
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 "customtablemodel.h"
22 22 #include <QVector>
23 23 #include <QTime>
24 24 #include <QRect>
25 25 #include <QColor>
26 26
27 27 CustomTableModel::CustomTableModel(QObject *parent) :
28 28 QAbstractTableModel(parent)
29 29 {
30 30 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
31 31
32 32 m_columnCount = 7;
33 m_rowCount = 9;
33 m_rowCount = 1029;
34 34
35 35 m_labels.append("Apples");
36 36 m_labels.append("Oranges");
37 37 m_labels.append("Pears");
38 38 m_labels.append("Peaches");
39 39 m_labels.append("Coconuts");
40 40 m_labels.append("Bananas");
41 41 m_labels.append("Kiwis");
42 42 m_labels.append("Grapes");
43 43 m_labels.append("Plums");
44 44
45 45 // m_data
46 46 for (int i = 0; i < m_rowCount; i++)
47 47 {
48 48 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
49 49 for (int k = 0; k < dataVec->size(); k++)
50 50 {
51 51 if (k%2 == 0)
52 52 dataVec->replace(k, i * 50 + qrand()%20);
53 53 else
54 54 dataVec->replace(k, qrand()%100);
55 55 }
56 56 m_data.append(dataVec);
57 57 // m_labels.append(QString("Row: %1").arg((i + 1)));
58 58 }
59 59 }
60 60
61 61 int CustomTableModel::rowCount(const QModelIndex & parent) const
62 62 {
63 63 Q_UNUSED(parent)
64 64 return m_data.count();
65 65 }
66 66
67 67 int CustomTableModel::columnCount(const QModelIndex & parent) const
68 68 {
69 69 Q_UNUSED(parent)
70 return m_columnCount + 1;
70 return m_columnCount;// + 1;
71 71 }
72 72
73 73 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
74 74 {
75 75 if (role != Qt::DisplayRole)
76 76 return QVariant();
77 77
78 78 if (orientation == Qt::Horizontal)
79 79 {
80 80 switch(section)
81 81 {
82 82 case 7:
83 83 return "Fruit";
84 84 case 1:
85 85 return "Count";
86 86 case 2:
87 87 return "Ordered";
88 88 default:
89 89 if (section%2 == 0)
90 90 return "x";
91 91 else
92 92 return "y";
93 93 }
94 94 }
95 95 else
96 96 return QString("%1").arg(section /*+ 1*/);
97 97 }
98 98
99 99 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
100 100 {
101 101 if (role == Qt::DisplayRole)
102 102 {
103 103 switch(index.column())
104 104 {
105 case 7:
106 return m_labels[index.row()];
105 // case 7:
106 // return m_labels[index.row()];
107 107 default:
108 108 return m_data[index.row()]->at(index.column());
109 109 break;
110 110 }
111 111 }
112 112 else if (role == Qt::EditRole)
113 113 {
114 114 switch(index.column())
115 115 {
116 case 7:
117 return m_labels[index.row()];
116 // case 7:
117 // return m_labels[index.row()];
118 118 default:
119 119 return m_data[index.row()]->at(index.column());
120 120 break;
121 121 }
122 122 }
123 123 else if (role == Qt::BackgroundRole)
124 124 {
125 125 QRect rect;
126 126 foreach(rect, m_mapping)
127 127 if(rect.contains(index.column(), index.row()))
128 128 return QColor(m_mapping.key(rect));
129 129
130 130 // cell not mapped return white color
131 131 return QColor(Qt::white);
132 132 }
133 133 return QVariant();
134 134 }
135 135
136 136 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
137 137 {
138 138 if (index.isValid() && role == Qt::EditRole)
139 139 {
140 140 switch(index.column())
141 141 {
142 142 case 7:
143 143 m_labels.replace(index.row(), value.toString());
144 144 break;
145 145 default:
146 146 m_data[index.row()]->replace(index.column(), value.toDouble());
147 147 break;
148 148 }
149 149 emit dataChanged(index, index);
150 150 return true;
151 151 }
152 152 return false;
153 153 }
154 154
155 155 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
156 156 {
157 157 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
158 158 }
159 159
160 160 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
161 161 {
162 162 Q_UNUSED(parent)
163 163
164 164 if (row < 0)
165 165 row = 0;
166 166 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
167 167 for (int i = row; i < row + count; i++)
168 168 {
169 169 // m_points.insert(row, QPointF(10,20));
170 170 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
171 171 QVector<QColor>* colorVec = new QVector<QColor>(m_columnCount);
172 172 for (int k = 0; k < dataVec->size(); k++)
173 173 {
174 174 if (k%2 == 0)
175 175 // dataVec->replace(k, i * 50 + qrand()%20);
176 176 {
177 177 int difference = 0;
178 178 if (i < m_data.size())
179 179 {
180 180 if (i - 1 >= 0)
181 181 {
182 182 if (row > 0)
183 183 difference = (int)((qAbs(m_data[i]->at(k) - m_data[row - 1]->at(k)))/count);
184 184 else
185 185 difference = (int)((qAbs(m_data[i]->at(k)/count)));
186 186 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%qMax(1, difference));
187 187 }
188 188 else
189 189 dataVec->replace(k, qrand()%40 + 10);
190 190 }
191 191 else
192 192 {
193 193 if (i - 1 >= 0)
194 194 {
195 195 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%40 + 10);
196 196 }
197 197 else
198 198 {
199 199 dataVec->replace(k, qrand()%40 + 10);
200 200 }
201 201 }
202 202 }
203 203 else
204 204 dataVec->replace(k, qrand()%100);
205 205 colorVec->replace(k, QColor(Qt::white));
206 206 }
207 207 m_data.insert(i, dataVec);
208 208 m_labels.insert(i,(QString("Row: %1").arg(i + 1)));
209 209 }
210 210 endInsertRows();
211 211 return true;
212 212 }
213 213
214 214 bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent)
215 215 {
216 216 if (row > this->rowCount() - 1)
217 217 return false;
218 218 if (row < 0)
219 219 row = 0;
220 220 if (row + count > rowCount())
221 221 return false;
222 222 beginRemoveRows(parent, row, row + count - 1);
223 223 for (int i = row; i < row + count; i++)
224 224 {
225 225 QVector<qreal>* item = m_data.at(row);
226 226 m_data.removeAt(row);
227 227 delete item;
228 228 m_labels.removeAt(row);
229 229 }
230 230 endRemoveRows();
231 231 return true;
232 232 }
233 233
234 234 bool CustomTableModel::insertColumns ( int column, int count, const QModelIndex & parent)
235 235 {
236 236 if (column < 0)
237 237 column = 0;
238 238 beginInsertColumns(parent, column, column + count - 1);
239 239 m_columnCount += count;
240 240 for (int i = column; i < column + count; i++)
241 241 for (int k = 0; k < rowCount(); k++)
242 242 if (k - 1 >= 0) {
243 243 m_data[k]->insert(i, m_data[k - 1]->at(i) + qrand()%40 + 10);
244 244 } else {
245 245 m_data[k]->insert(i, qrand()%40);
246 246 }
247 247 endInsertColumns();
248 248 return true;
249 249 }
250 250
251 251 bool CustomTableModel::removeColumns ( int column, int count, const QModelIndex & parent)
252 252 {
253 253 if (column > columnCount() - 1)
254 254 return false;
255 255 if (column < 0)
256 256 column = 0;
257 257 if (column + count > columnCount())
258 258 return false;
259 259 beginRemoveColumns(parent, column, column + count -1);
260 260 m_columnCount -= count;
261 261 for (int i = column; i < column + count; i++)
262 262 for (int k = 0; k < rowCount(); k++)
263 263 m_data[k]->remove(column);
264 264 endRemoveColumns();
265 265 return true;
266 266 }
267 267
268 268 void CustomTableModel::addMapping(QString color, QRect area)
269 269 {
270 270 m_mapping.insertMulti(color, area);
271 271 }
272 272
273 273 void CustomTableModel::addMapping(QString color, int left, int top, int right, int bottom)
274 274 {
275 275 addMapping(color, QRect(QPoint(left, top), QPoint(right, bottom)));
276 276 }
@@ -1,582 +1,582
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 <QHXYModelMapper>
31 31 #include "customtablemodel.h"
32 32 #include <QPieSeries>
33 33 #include <QVPieModelMapper>
34 34 #include <QPieSlice>
35 35 #include <QAreaSeries>
36 36 #include <QBarSeries>
37 37 #include <QGroupedBarSeries>
38 38 #include <QBarSet>
39 39 #include <QVBarModelMapper>
40 40 #include <QPushButton>
41 41 #include <QRadioButton>
42 42 #include <QLabel>
43 43 #include <QSpinBox>
44 44 #include <QTime>
45 45 #include <QHeaderView>
46 46
47 47 TableWidget::TableWidget(QWidget *parent)
48 48 : QWidget(parent),
49 49 m_series(0),
50 50 m_mapper(0),
51 51 m_model(0),
52 52 m_pieMapper(0),
53 53 m_pieMapper2(0)
54 54 // specialPie(0)
55 55 {
56 56 setGeometry(1900, 100, 1000, 600);
57 57 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
58 58 // create simple model for storing data
59 59 // user's table data model
60 60 m_model = new CustomTableModel;
61 61 m_tableView = new QTableView;
62 62 m_tableView->setModel(m_model);
63 63 // m_tableView->setMinimumHeight(300);
64 64 m_tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
65 65 m_tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
66 66
67 67 m_chart = new QChart;
68 68 m_chart->legend()->setVisible(true);
69 69 m_chart->setAnimationOptions(QChart::SeriesAnimations);
70 70 m_chartView = new QChartView(m_chart);
71 71 m_chartView->setRenderHint(QPainter::Antialiasing);
72 72 m_chartView->setMinimumSize(640, 480);
73 73
74 74 // add, remove data buttons
75 75 QPushButton* addRowAboveButton = new QPushButton("Add row above");
76 76 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
77 77
78 78 QPushButton* addRowBelowButton = new QPushButton("Add row below");
79 79 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
80 80
81 81 QPushButton* removeRowButton = new QPushButton("Remove row");
82 82 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
83 83
84 84 QPushButton* addColumnRightButton = new QPushButton("Add column to the right");
85 85 connect(addColumnRightButton, SIGNAL(clicked()), this, SLOT(addColumnRight()));
86 86
87 87 QPushButton* removeColumnButton = new QPushButton("Remove column");
88 88 connect(removeColumnButton, SIGNAL(clicked()), this, SLOT(removeColumn()));
89 89
90 90 QPushButton* specialPieButton = new QPushButton("Add slices using series API");
91 91 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
92 92
93 93 QPushButton* specialPieButton2 = new QPushButton("Remove slices using series API");
94 94 connect(specialPieButton2, SIGNAL(clicked()), this, SLOT(testPie2()));
95 95
96 96 QPushButton* specialPieButton3 = new QPushButton("Modify slices using series API");
97 97 connect(specialPieButton3, SIGNAL(clicked()), this, SLOT(testPie3()));
98 98
99 99 QPushButton* xyTestButton = new QPushButton("Append XY point");
100 100 connect(xyTestButton, SIGNAL(clicked()), this, SLOT(testXY()));
101 101
102 102
103 103 QLabel *spinBoxLabel = new QLabel("Rows affected:");
104 104
105 105 // spin box for setting number of affected items (add, remove)
106 106 m_linesCountSpinBox = new QSpinBox;
107 107 m_linesCountSpinBox->setRange(1, 10);
108 108 m_linesCountSpinBox->setValue(1);
109 109
110 110 // buttons layout
111 111 QVBoxLayout* buttonsLayout = new QVBoxLayout;
112 112 buttonsLayout->addWidget(spinBoxLabel);
113 113 buttonsLayout->addWidget(m_linesCountSpinBox);
114 114 // buttonsLayout->addWidget(addRowAboveButton);
115 115 buttonsLayout->addWidget(addRowBelowButton);
116 116 buttonsLayout->addWidget(removeRowButton);
117 117 // buttonsLayout->addWidget(addColumnRightButton);
118 118 // buttonsLayout->addWidget(removeColumnButton);
119 119 buttonsLayout->addWidget(specialPieButton);
120 120 buttonsLayout->addWidget(specialPieButton2);
121 121 buttonsLayout->addWidget(specialPieButton3);
122 122 buttonsLayout->addWidget(xyTestButton);
123 123 buttonsLayout->addStretch();
124 124
125 125 // chart type radio buttons
126 126 m_lineRadioButton = new QRadioButton("Line");
127 127 m_splineRadioButton = new QRadioButton("Spline");
128 128 m_scatterRadioButton = new QRadioButton("Scatter");
129 129 m_pieRadioButton = new QRadioButton("Pie");
130 130 m_areaRadioButton = new QRadioButton("Area");
131 131 m_barRadioButton = new QRadioButton("Bar");
132 132
133 133 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
134 134 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
135 135 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
136 136 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
137 137 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
138 138 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
139 139 m_lineRadioButton->setChecked(true);
140 140
141 141 // radio buttons layout
142 142 QVBoxLayout* radioLayout = new QVBoxLayout;
143 143 radioLayout->addWidget(m_lineRadioButton);
144 144 radioLayout->addWidget(m_splineRadioButton);
145 145 radioLayout->addWidget(m_scatterRadioButton);
146 146 radioLayout->addWidget(m_pieRadioButton);
147 147 // radioLayout->addWidget(m_areaRadioButton);
148 148 radioLayout->addWidget(m_barRadioButton);
149 149 radioLayout->addStretch();
150 150
151 151 // create main layout
152 152 QGridLayout* mainLayout = new QGridLayout;
153 153 mainLayout->addLayout(buttonsLayout, 2, 0);
154 154 mainLayout->addLayout(radioLayout, 3, 0);
155 155 mainLayout->addWidget(m_tableView, 1, 0);
156 156 mainLayout->addWidget(m_chartView, 1, 1, 2, 1);
157 157 setLayout(mainLayout);
158 158 m_lineRadioButton->setFocus();
159 159 }
160 160
161 161 void TableWidget::addRowAbove()
162 162 {
163 163 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
164 164
165 165 }
166 166
167 167 void TableWidget::addRowBelow()
168 168 {
169 169 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
170 170
171 171 }
172 172
173 173 void TableWidget::removeRow()
174 174 {
175 175 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
176 176 }
177 177
178 178 void TableWidget::addColumnRight()
179 179 {
180 180 m_model->insertColumns(m_tableView->currentIndex().column() + 1, m_linesCountSpinBox->value());
181 181 }
182 182
183 183 void TableWidget::removeColumn()
184 184 {
185 185 m_model->removeColumns(m_tableView->currentIndex().column(), qMin(m_model->columnCount() - m_tableView->currentIndex().column(), m_linesCountSpinBox->value()));
186 186 }
187 187
188 188 void TableWidget::updateChartType(bool toggle)
189 189 {
190 190 // this if is needed, so that the function is only called once.
191 191 // For the radioButton that was enabled.
192 192 if (toggle) {
193 193 // specialPie = 0;
194 194 m_chart->removeAllSeries();
195 195 m_series = 0;
196 196 // m_chart->axisX()->setNiceNumbersEnabled(false);
197 197 // m_chart->axisY()->setNiceNumbersEnabled(false);
198 198 if (m_mapper) {
199 199 m_mapper->deleteLater();
200 200 m_mapper = 0;
201 201 }
202 202
203 203 if (m_pieMapper) {
204 204 m_pieMapper->deleteLater();
205 205 m_pieMapper = 0;
206 206 }
207 207
208 208 if (m_pieMapper2) {
209 209 m_pieMapper2->deleteLater();
210 210 m_pieMapper2 = 0;
211 211 }
212 212
213 213 // if (m_series) {
214 214 // delete m_series;
215 215 // m_series = 0;
216 216 // }
217 217
218 218 // renable axes of the chart (pie hides them)
219 219 // x axis
220 220 QAxis *axis = m_chart->axisX();
221 221 axis->setAxisVisible(true);
222 222 axis->setGridLineVisible(true);
223 223 axis->setLabelsVisible(true);
224 224
225 225 // y axis
226 226 axis = m_chart->axisY();
227 227 axis->setAxisVisible(true);
228 228 axis->setGridLineVisible(true);
229 229 axis->setLabelsVisible(true);
230 230
231 231 m_model->clearMapping();
232 232
233 233 QString seriesColorHex = "#000000";
234 234 // QPen pen;
235 235 // pen.setWidth(2);
236 236
237 237 if (m_lineRadioButton->isChecked())
238 238 {
239 m_chart->setAnimationOptions(QChart::NoAnimation);
239 // m_chart->setAnimationOptions(QChart::NoAnimation);
240 240
241 241 // series 1
242 242 m_series = new QLineSeries;
243 243
244 m_mapper = new QHXYModelMapper;
245 m_mapper->setModel(m_model);
246 m_mapper->setSeries(m_series);
247 m_mapper->setXRow(0);
248 m_mapper->setYRow(1);
249 m_mapper->setFirst(3);
250 m_mapper->setCount(4);
251
252 // m_mapper = new QVXYModelMapper;
244 // m_mapper = new QHXYModelMapper;
253 245 // m_mapper->setModel(m_model);
254 246 // m_mapper->setSeries(m_series);
255 // m_mapper->setXColumn(0);
256 // m_mapper->setYColumn(1);
247 // m_mapper->setXRow(0);
248 // m_mapper->setYRow(1);
257 249 // m_mapper->setFirst(3);
258 250 // m_mapper->setCount(4);
259 251
252 m_mapper = new QVXYModelMapper;
253 m_mapper->setModel(m_model);
254 m_mapper->setSeries(m_series);
255 m_mapper->setXColumn(0);
256 m_mapper->setYColumn(1);
257 m_mapper->setFirst(3);
258 // m_mapper->setCount(4);
259
260 260 // m_series->setModelMapping(0,1, Qt::Vertical);
261 261 // m_series->setModelMappingRange(3, 4);
262 262 m_chart->addSeries(m_series);
263 263 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
264 264 m_model->addMapping(seriesColorHex, QRect(0, 3, 2, 4));
265 265
266 266 // // series 2
267 267 // m_series = new QLineSeries;
268 268 // m_series->setModel(m_model);
269 269
270 270 // mapper = new QXYModelMapper;
271 271 // mapper->setMapX(3);
272 272 // mapper->setMapY(4);
273 273 // // mapper->setFirst(3);
274 274 // // mapper->setCount(4);
275 275 // m_series->setModelMapper(mapper);
276 276 // // m_series->setModelMapping(2,3, Qt::Vertical);
277 277 // m_chart->addSeries(m_series);
278 278 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
279 279 // m_model->addMapping(seriesColorHex, QRect(3, 0, 2, 1000));
280 280
281 281 // // series 3
282 282 // m_series = new QLineSeries;
283 283 // m_series->setModel(m_model);
284 284
285 285 // mapper = new QXYModelMapper;
286 286 // mapper->setMapX(5);
287 287 // mapper->setMapY(6);
288 288 // mapper->setFirst(2);
289 289 // mapper->setCount(-1);
290 290 // m_series->setModelMapper(mapper);
291 291 // // m_series->setModelMapping(4,5, Qt::Vertical);
292 292 // // m_series->setModelMappingRange(2, -1);
293 293 // m_chart->addSeries(m_series);
294 294 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
295 295 // m_model->addMapping(seriesColorHex, QRect(5, 2, 2, 1000));
296 296 }
297 297 else if (m_splineRadioButton->isChecked())
298 298 {
299 299 m_chart->setAnimationOptions(QChart::NoAnimation);
300 300
301 301 // series 1
302 302 m_series = new QSplineSeries;
303 303 // m_series->setModel(m_model);
304 304
305 305 // m_mapper = new QVXYModelMapper;
306 306 // m_mapper->setSeries(m_series);
307 307 // m_mapper->setModel(m_model);
308 308 // m_mapper->setXColumn(0);
309 309 // m_mapper->setYColumn(1);
310 310 // m_mapper->setFirst(0);
311 311 // m_mapper->setCount(-1);
312 312
313 313 // m_series->setModelMapper(mapper);
314 314
315 315 m_chart->addSeries(m_series);
316 316 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
317 317 m_model->addMapping(seriesColorHex, QRect(0, 0, 2, 1000));
318 318
319 319 // // series 2
320 320 // m_series = new QSplineSeries;
321 321 // m_series->setModel(m_model);
322 322
323 323 // mapper = new QXYModelMapper;
324 324 // mapper->setMapX(2);
325 325 // mapper->setMapY(3);
326 326 // mapper->setFirst(2);
327 327 // mapper->setCount(4);
328 328
329 329 // m_series->setModelMapper(mapper);
330 330
331 331 // m_chart->addSeries(m_series);
332 332 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
333 333 // m_model->addMapping(seriesColorHex, QRect(2, 2, 2, 4));
334 334
335 335 // // series 3
336 336 // m_series = new QSplineSeries;
337 337 // m_series->setModel(m_model);
338 338
339 339 // mapper = new QXYModelMapper;
340 340 // mapper->setMapX(4);
341 341 // mapper->setMapY(5);
342 342 // mapper->setFirst(2);
343 343 // mapper->setCount(-1);
344 344
345 345 // m_series->setModelMapper(mapper);
346 346
347 347 // m_chart->addSeries(m_series);
348 348 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
349 349 // m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
350 350 } else if (m_scatterRadioButton->isChecked())
351 351 {
352 352 m_chart->setAnimationOptions(QChart::NoAnimation);
353 353
354 354 // series 1
355 355 m_series = new QScatterSeries;
356 356
357 357 // m_mapper = new QVXYModelMapper;
358 358 // m_mapper->setSeries(m_series);
359 359 // m_mapper->setModel(m_model);
360 360 // m_mapper->setXColumn(0);
361 361 // m_mapper->setYColumn(1);
362 362 // m_mapper->setFirst(0);
363 363 // m_mapper->setCount(-1);
364 364
365 365 m_chart->addSeries(m_series);
366 366 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
367 367 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
368 368
369 369 // // series 2
370 370 // m_series = new QScatterSeries;
371 371 // m_series->setModel(m_model);
372 372 // m_series->setModelMapping(2,3, Qt::Vertical);
373 373 // // m_series->setModelMappingRange(1, 6);
374 374 // // series->setModelMapping(2,3, Qt::Horizontal);
375 375 // m_chart->addSeries(m_series);
376 376
377 377 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
378 378 // m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
379 379
380 380 // // series 3
381 381 // m_series = new QScatterSeries;
382 382 // m_series->setModel(m_model);
383 383 // m_series->setModelMapping(4,5, Qt::Vertical);
384 384 // // series->setModelMapping(4,5, Qt::Horizontal);
385 385 // m_chart->addSeries(m_series);
386 386 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
387 387 // m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
388 388 } else if (m_pieRadioButton->isChecked()) {
389 389 m_chart->setAnimationOptions(QChart::SeriesAnimations);
390 390
391 391 // pie 1
392 392 m_pieSeries = new QPieSeries;
393 393
394 394 m_pieMapper = new QVPieModelMapper;
395 395 m_pieMapper->setValuesColumn(1);
396 396 m_pieMapper->setLabelsColumn(7);
397 397 m_pieMapper->setSeries(m_pieSeries);
398 398 m_pieMapper->setModel(m_model);
399 399 m_pieMapper->setFirst(2);
400 400 // m_pieMapper->setCount(5);
401 401 // pieSeries->setModelMapper(mapper);
402 402
403 403 m_pieSeries->setLabelsVisible(true);
404 404 m_pieSeries->setPieSize(0.35);
405 405 m_pieSeries->setHorizontalPosition(0.25);
406 406 m_pieSeries->setVerticalPosition(0.35);
407 407
408 408 m_chart->addSeries(m_pieSeries);
409 409 seriesColorHex = "#" + QString::number(m_pieSeries->slices().at(m_pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
410 410 m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 50));
411 411
412 412
413 413 // pieSeries->slices().at(0)->setValue(400);
414 414 // pieSeries->slices().at(0)->setLabel(QString("36"));
415 415
416 416 // pie 2
417 417 m_pieSeries2 = new QPieSeries;
418 418
419 419 m_pieMapper2 = new QVPieModelMapper;
420 420 m_pieMapper2->setValuesColumn(0);
421 421 m_pieMapper2->setLabelsColumn(7);
422 422 m_pieMapper2->setModel(m_model);
423 423 m_pieMapper2->setSeries(m_pieSeries2);
424 424 m_pieMapper2->setFirst(2);
425 425
426 426 m_pieSeries2->setLabelsVisible(true);
427 427 m_pieSeries2->setPieSize(0.35);
428 428 m_pieSeries2->setHorizontalPosition(0.75);
429 429 m_pieSeries2->setVerticalPosition(0.65);
430 430 m_chart->addSeries(m_pieSeries2);
431 431 seriesColorHex = "#" + QString::number(m_pieSeries2->slices().at(m_pieSeries2->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
432 432 m_model->addMapping(seriesColorHex, QRect(0, 2, 1, 1000));
433 433
434 434 // // pie 3
435 435 // pieSeries = new QPieSeries;
436 436 // pieSeries->setModel(m_model);
437 437 // pieSeries->setModelMapping(2,2, Qt::Vertical);
438 438 // pieSeries->setLabelsVisible(true);
439 439 // pieSeries->setPieSize(0.35);
440 440 // pieSeries->setHorizontalPosition(0.5);
441 441 // pieSeries->setVerticalPosition(0.75);
442 442 // m_chart->addSeries(pieSeries);
443 443 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
444 444 // m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
445 445
446 446 // // special pie
447 447 // specialPie = new QPieSeries;
448 448 // specialPie->append(17, "1");
449 449 // specialPie->append(45, "2");
450 450 // specialPie->append(77, "3");
451 451 // specialPie->append(37, "4");
452 452 // specialPie->append(27, "5");
453 453 // specialPie->append(47, "6");
454 454 // specialPie->setPieSize(0.35);
455 455 // specialPie->setHorizontalPosition(0.8);
456 456 // specialPie->setVerticalPosition(0.75);
457 457 // specialPie->setLabelsVisible(true);
458 458 // m_chart->addSeries(specialPie);
459 459 }
460 460 // else if (m_areaRadioButton->isChecked())
461 461 // {
462 462 // m_chart->setAnimationOptions(QChart::NoAnimation);
463 463
464 464 // QLineSeries* upperLineSeries = new QLineSeries;
465 465 // upperLineSeries->setModel(m_model);
466 466 // upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
467 467 // // upperLineSeries->setModelMappingRange(1, 5);
468 468 // QLineSeries* lowerLineSeries = new QLineSeries;
469 469 // lowerLineSeries->setModel(m_model);
470 470 // lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
471 471 // QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
472 472 // m_chart->addSeries(areaSeries);
473 473 // seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
474 474 // m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
475 475 // m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
476 476 // }
477 477 else if (m_barRadioButton->isChecked())
478 478 {
479 479 m_chart->setAnimationOptions(QChart::SeriesAnimations);
480 480
481 481 QGroupedBarSeries* barSeries = new QGroupedBarSeries();
482 482 // barSeries->setCategories(QStringList());
483 483 // barSeries->setModel(m_model);
484 484 // barSeries->setModelMappingRange(2, 5);
485 485 // barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
486 486
487 487 int first = 3;
488 488 // int count = 4;
489 489 QVBarModelMapper *mapper = new QVBarModelMapper;
490 490 mapper->setCategoriesSection(5);
491 491 mapper->setFirstBarSetSection(2);
492 492 mapper->setLastBarSetSection(4);
493 493 mapper->setFirst(first);
494 494 // mapper->setCount(count);
495 495 mapper->setSeries(barSeries);
496 496 mapper->setModel(m_model);
497 497 // barSeries->setModelMapper(mapper);
498 498 m_chart->addSeries(barSeries);
499 499 QList<QBarSet*> barsets = barSeries->barSets();
500 500 for (int i = 0; i < barsets.count(); i++) {
501 501 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
502 502 m_model->addMapping(seriesColorHex, QRect(2 + i, first, 1, barsets.at(i)->count()));
503 503 }
504 504 }
505 505
506 506
507 507 if (!m_barRadioButton->isChecked()) {
508 m_chart->axisX()->setRange(0, 500);
509 m_chart->axisY()->setRange(0, 220);
508 // m_chart->axisX()->setRange(0, 500);
509 // m_chart->axisY()->setRange(0, 220);
510 510 }
511 511 m_chart->legend()->setVisible(true);
512 512
513 513 // repaint table view colors
514 514 m_tableView->repaint();
515 515 m_tableView->setFocus();
516 516 }
517 517 }
518 518
519 519 void TableWidget::testPie()
520 520 {
521 521 // m_pieMapper->setCount(-1);
522 522 QPieSlice *slice = new QPieSlice("Hehe", 145);
523 523 slice->setLabelVisible();
524 524 m_pieSeries->append(slice);
525 525
526 526 slice = new QPieSlice("Hoho", 34);
527 527 slice->setLabelVisible();
528 528 m_pieSeries->append(slice);
529 529 // m_series->modelMapper()->setMapX(4);
530 530 // m_tableView->setColumnWidth(10, 250);
531 531 // if (specialPie) {
532 532 // specialPie->remove(specialPie->slices().at(2));
533 533 // // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2));
534 534 // specialPie->append(4, "heloo");
535 535 // }
536 536 }
537 537
538 538 void TableWidget::testPie2()
539 539 {
540 540 QPieSlice *slice;
541 541 if (m_pieSeries->count() > 0) {
542 542 slice = m_pieSeries->slices().last();
543 543 m_pieSeries->remove(slice);
544 544 }
545 545
546 546 if (m_pieSeries->count() > 0) {
547 547 slice = m_pieSeries->slices().first();
548 548 m_pieSeries->remove(slice);
549 549 }
550 550 }
551 551
552 552 void TableWidget::testPie3()
553 553 {
554 554 QPieSlice *slice;
555 555 if (m_pieSeries->count() > 0) {
556 556 slice = m_pieSeries->slices().last();
557 557 slice->setLabel("Dalej");
558 558 slice->setValue(222);
559 559 }
560 560
561 561 if (m_pieSeries->count() > 0) {
562 562 slice = m_pieSeries->slices().first();
563 563 slice->setLabel("Prawie");
564 564 slice->setValue(111);
565 565 }
566 566 }
567 567
568 568 void TableWidget::testXY()
569 569 {
570 570 // if (m_series->type() != QAbstractSeries::SeriesTypeLine) {
571 571 // m_series->append(QPointF(150, 75));
572 572 // }
573 573
574 574 if (m_series->count() > 0) {
575 575 m_series->remove(m_series->points().last());
576 576 }
577 577 }
578 578
579 579 TableWidget::~TableWidget()
580 580 {
581 581
582 582 }
@@ -1,83 +1,83
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 TABLEWIDGET_H
22 22 #define TABLEWIDGET_H
23 23
24 24 #include <QtGui/QWidget>
25 25 //#include <QChartGlobal>
26 26 #include "qchartview.h"
27 27 //#include "qxyseries.h"
28 28 #include <QPieSeries>
29 29 #include <QVPieModelMapper>
30 30 #include <QVXYModelMapper>
31 31 #include <QHXYModelMapper>
32 32
33 33 class CustomTableModel;
34 34 class QTableView;
35 35 class QRadioButton;
36 36 class QSpinBox;
37 37
38 38 QTCOMMERCIALCHART_USE_NAMESPACE
39 39
40 40 class TableWidget : public QWidget
41 41 {
42 42 Q_OBJECT
43 43
44 44 public:
45 45 TableWidget(QWidget *parent = 0);
46 46 ~TableWidget();
47 47
48 48
49 49 public slots:
50 50 void addRowAbove();
51 51 void addRowBelow();
52 52 void removeRow();
53 53 void addColumnRight();
54 54 void removeColumn();
55 55 void updateChartType(bool toggle);
56 56 void testPie();
57 57 void testPie2();
58 58 void testPie3();
59 59
60 60 void testXY();
61 61
62 62 private:
63 63 QChartView* m_chartView;
64 64 QChart* m_chart;
65 65 QXYSeries* m_series;
66 QHXYModelMapper *m_mapper;
66 QVXYModelMapper *m_mapper;
67 67 CustomTableModel* m_model;
68 68 QTableView* m_tableView;
69 69 QRadioButton* m_lineRadioButton;
70 70 QRadioButton* m_splineRadioButton;
71 71 QRadioButton* m_scatterRadioButton;
72 72 QRadioButton* m_pieRadioButton;
73 73 QRadioButton* m_areaRadioButton;
74 74 QRadioButton* m_barRadioButton;
75 75 QSpinBox* m_linesCountSpinBox;
76 76 QVPieModelMapper *m_pieMapper;
77 77 QPieSeries* m_pieSeries;
78 78 QVPieModelMapper *m_pieMapper2;
79 79 QPieSeries* m_pieSeries2;
80 80 // QPieSeries* specialPie;
81 81 };
82 82
83 83 #endif // TABLEWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now