##// END OF EJS Templates
QPieModelMapper: added support for labelChanged and valueChanged signals from the slice
Marek Rosa -
r1236:a37d91e87542
parent child
Show More
@@ -1,428 +1,475
1 #include "qpiemodelmapper_p.h"
1 #include "qpiemodelmapper_p.h"
2 #include "qpiemodelmapper.h"
2 #include "qpiemodelmapper.h"
3 #include "qpieseries.h"
3 #include "qpieseries.h"
4 #include "qpieslice.h"
4 #include "qpieslice.h"
5 #include <QAbstractItemModel>
5 #include <QAbstractItemModel>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 QPieModelMapper::QPieModelMapper(QObject *parent) :
9 QPieModelMapper::QPieModelMapper(QObject *parent) :
10 QObject(parent),
10 QObject(parent),
11 d_ptr(new QPieModelMapperPrivate(this))
11 d_ptr(new QPieModelMapperPrivate(this))
12 {
12 {
13 }
13 }
14
14
15 QAbstractItemModel* QPieModelMapper::model() const
15 QAbstractItemModel* QPieModelMapper::model() const
16 {
16 {
17 Q_D(const QPieModelMapper);
17 Q_D(const QPieModelMapper);
18 return d->m_model;
18 return d->m_model;
19 }
19 }
20
20
21 void QPieModelMapper::setModel(QAbstractItemModel *model)
21 void QPieModelMapper::setModel(QAbstractItemModel *model)
22 {
22 {
23 if (model == 0)
23 if (model == 0)
24 return;
24 return;
25
25
26 Q_D(QPieModelMapper);
26 Q_D(QPieModelMapper);
27 if (d->m_model) {
27 if (d->m_model) {
28 disconnect(d->m_model, 0, d, 0);
28 disconnect(d->m_model, 0, d, 0);
29 }
29 }
30
30
31 d->m_model = model;
31 d->m_model = model;
32 d->initializePieFromModel();
32 d->initializePieFromModel();
33 // connect signals from the model
33 // connect signals from the model
34 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
34 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
35 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
35 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
36 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
36 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
37 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
37 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
38 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
38 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
39 }
39 }
40
40
41 QPieSeries* QPieModelMapper::series() const
41 QPieSeries* QPieModelMapper::series() const
42 {
42 {
43 Q_D(const QPieModelMapper);
43 Q_D(const QPieModelMapper);
44 return d->m_series;
44 return d->m_series;
45 }
45 }
46
46
47 void QPieModelMapper::setSeries(QPieSeries *series)
47 void QPieModelMapper::setSeries(QPieSeries *series)
48 {
48 {
49 if (series == 0)
49 if (series == 0)
50 return;
50 return;
51
51
52 Q_D(QPieModelMapper);
52 Q_D(QPieModelMapper);
53 if (d->m_series) {
53 if (d->m_series) {
54 disconnect(d->m_series, 0, d, 0);
54 disconnect(d->m_series, 0, d, 0);
55 }
55 }
56
56
57 d->m_series = series;
57 d->m_series = series;
58 d->initializePieFromModel();
58 d->initializePieFromModel();
59 // connect the signals from the series
59 // connect the signals from the series
60 connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>)));
60 connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>)));
61 connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>)));
61 connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>)));
62 // connect(d->m_model, SIGNAL(), d, SLOT());
62 // connect(d->m_model, SIGNAL(), d, SLOT());
63 }
63 }
64
64
65 int QPieModelMapper::first() const
65 int QPieModelMapper::first() const
66 {
66 {
67 Q_D(const QPieModelMapper);
67 Q_D(const QPieModelMapper);
68 return d->m_first;
68 return d->m_first;
69 }
69 }
70
70
71 void QPieModelMapper::setFirst(int first)
71 void QPieModelMapper::setFirst(int first)
72 {
72 {
73 Q_D(QPieModelMapper);
73 Q_D(QPieModelMapper);
74 d->m_first = qMax(first, 0);
74 d->m_first = qMax(first, 0);
75 d->initializePieFromModel();
75 d->initializePieFromModel();
76 // emit updated();
76 // emit updated();
77 }
77 }
78
78
79 int QPieModelMapper::count() const
79 int QPieModelMapper::count() const
80 {
80 {
81 Q_D(const QPieModelMapper);
81 Q_D(const QPieModelMapper);
82 return d->m_count;
82 return d->m_count;
83 }
83 }
84
84
85 void QPieModelMapper::setCount(int count)
85 void QPieModelMapper::setCount(int count)
86 {
86 {
87 Q_D(QPieModelMapper);
87 Q_D(QPieModelMapper);
88 d->m_count = qMax(count, -1);
88 d->m_count = qMax(count, -1);
89 d->initializePieFromModel();
89 d->initializePieFromModel();
90 // emit updated();
90 // emit updated();
91 }
91 }
92
92
93 Qt::Orientation QPieModelMapper::orientation() const
93 Qt::Orientation QPieModelMapper::orientation() const
94 {
94 {
95 Q_D(const QPieModelMapper);
95 Q_D(const QPieModelMapper);
96 return d->m_orientation;
96 return d->m_orientation;
97 }
97 }
98
98
99 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
99 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
100 {
100 {
101 Q_D(QPieModelMapper);
101 Q_D(QPieModelMapper);
102 d->m_orientation = orientation;
102 d->m_orientation = orientation;
103 d->initializePieFromModel();
103 d->initializePieFromModel();
104 // emit updated();
104 // emit updated();
105 }
105 }
106
106
107 int QPieModelMapper::valuesIndex() const
107 int QPieModelMapper::valuesIndex() const
108 {
108 {
109 Q_D(const QPieModelMapper);
109 Q_D(const QPieModelMapper);
110 return d->m_valuesIndex;
110 return d->m_valuesIndex;
111 }
111 }
112
112
113 void QPieModelMapper::setValuesIndex(int mapValues)
113 void QPieModelMapper::setValuesIndex(int mapValues)
114 {
114 {
115 Q_D(QPieModelMapper);
115 Q_D(QPieModelMapper);
116 d->m_valuesIndex = mapValues;
116 d->m_valuesIndex = mapValues;
117 d->initializePieFromModel();
117 d->initializePieFromModel();
118 // emit updated();
118 // emit updated();
119 }
119 }
120
120
121 int QPieModelMapper::labelsIndex() const
121 int QPieModelMapper::labelsIndex() const
122 {
122 {
123 Q_D(const QPieModelMapper);
123 Q_D(const QPieModelMapper);
124 return d->m_labelsIndex;
124 return d->m_labelsIndex;
125 }
125 }
126
126
127 void QPieModelMapper::setLabelsIndex(int mapLabels)
127 void QPieModelMapper::setLabelsIndex(int mapLabels)
128 {
128 {
129 Q_D(QPieModelMapper);
129 Q_D(QPieModelMapper);
130 d->m_labelsIndex = mapLabels;
130 d->m_labelsIndex = mapLabels;
131 d->initializePieFromModel();
131 d->initializePieFromModel();
132 // emit updated();
132 // emit updated();
133 }
133 }
134
134
135 void QPieModelMapper::reset()
135 void QPieModelMapper::reset()
136 {
136 {
137 Q_D(QPieModelMapper);
137 Q_D(QPieModelMapper);
138 d->m_first = 0;
138 d->m_first = 0;
139 d->m_count = -1;
139 d->m_count = -1;
140 d->m_orientation = Qt::Vertical;
140 d->m_orientation = Qt::Vertical;
141 d->m_valuesIndex = -1;
141 d->m_valuesIndex = -1;
142 d->m_labelsIndex = -1;
142 d->m_labelsIndex = -1;
143 // emit updated();
143 // emit updated();
144 }
144 }
145
145
146 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
146 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
147
147
148 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
148 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
149 q_ptr(q)
149 q_ptr(q)
150 {
150 {
151 m_series = 0;
151 m_series = 0;
152 m_model = 0;
152 m_model = 0;
153 m_first = 0;
153 m_first = 0;
154 m_count = -1;
154 m_count = -1;
155 m_orientation = Qt::Vertical;
155 m_orientation = Qt::Vertical;
156 m_valuesIndex = -1;
156 m_valuesIndex = -1;
157 m_labelsIndex = -1;
157 m_labelsIndex = -1;
158 m_seriesSignalsBlock = false;
158 m_seriesSignalsBlock = false;
159 m_modelSignalsBlock = false;
159 m_modelSignalsBlock = false;
160 }
160 }
161
161
162 void QPieModelMapperPrivate::blockModelSignals(bool block)
162 void QPieModelMapperPrivate::blockModelSignals(bool block)
163 {
163 {
164 m_modelSignalsBlock = block;
164 m_modelSignalsBlock = block;
165 }
165 }
166
166
167 void QPieModelMapperPrivate::blockSeriesSignals(bool block)
167 void QPieModelMapperPrivate::blockSeriesSignals(bool block)
168 {
168 {
169 m_seriesSignalsBlock = block;
169 m_seriesSignalsBlock = block;
170 }
170 }
171
171
172
172
173 QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const
173 QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const
174 {
174 {
175 if (m_orientation == Qt::Vertical && (index.column() == m_valuesIndex || index.column() == m_labelsIndex)) {
175 if (m_orientation == Qt::Vertical && (index.column() == m_valuesIndex || index.column() == m_labelsIndex)) {
176 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count))
176 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count))
177 return m_series->slices().at(index.row() - m_first);
177 return m_series->slices().at(index.row() - m_first);
178 } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesIndex || index.row() == m_labelsIndex)) {
178 } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesIndex || index.row() == m_labelsIndex)) {
179 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
179 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
180 return m_series->slices().at(index.column() - m_first);
180 return m_series->slices().at(index.column() - m_first);
181 }
181 }
182 return 0; // This part of model has not been mapped to any slice
182 return 0; // This part of model has not been mapped to any slice
183 }
183 }
184
184
185 QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos)
185 QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos)
186 {
186 {
187 if (m_count != -1 && slicePos >= m_count)
187 if (m_count != -1 && slicePos >= m_count)
188 return QModelIndex(); // invalid
188 return QModelIndex(); // invalid
189
189
190 if (m_orientation == Qt::Vertical)
190 if (m_orientation == Qt::Vertical)
191 return m_model->index(slicePos + m_first, m_valuesIndex);
191 return m_model->index(slicePos + m_first, m_valuesIndex);
192 else
192 else
193 return m_model->index(m_valuesIndex, slicePos + m_first);
193 return m_model->index(m_valuesIndex, slicePos + m_first);
194 }
194 }
195
195
196 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
196 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
197 {
197 {
198 if (m_count != -1 && slicePos >= m_count)
198 if (m_count != -1 && slicePos >= m_count)
199 return QModelIndex(); // invalid
199 return QModelIndex(); // invalid
200
200
201 if (m_orientation == Qt::Vertical)
201 if (m_orientation == Qt::Vertical)
202 return m_model->index(slicePos + m_first, m_labelsIndex);
202 return m_model->index(slicePos + m_first, m_labelsIndex);
203 else
203 else
204 return m_model->index(m_labelsIndex, slicePos + m_first);
204 return m_model->index(m_labelsIndex, slicePos + m_first);
205 }
205 }
206
206
207 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
207 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
208 {
208 {
209 if (m_seriesSignalsBlock)
209 if (m_seriesSignalsBlock)
210 return;
210 return;
211
211
212 if (slices.count() == 0)
212 if (slices.count() == 0)
213 return;
213 return;
214
214
215 int firstIndex = m_series->slices().indexOf(slices.at(0));
215 int firstIndex = m_series->slices().indexOf(slices.at(0));
216 if (firstIndex == -1)
216 if (firstIndex == -1)
217 return;
217 return;
218
218
219 if (m_count != -1)
220 m_count += slices.count();
221
222 for (int i = firstIndex; i < firstIndex + slices.count(); i++) {
223 m_slices.insert(i, slices.at(i - firstIndex));
224 connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
225 connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
226 }
227
219 blockModelSignals();
228 blockModelSignals();
220 if (m_orientation == Qt::Vertical)
229 if (m_orientation == Qt::Vertical)
221 m_model->insertRows(firstIndex + m_first, slices.count());
230 m_model->insertRows(firstIndex + m_first, slices.count());
222 else
231 else
223 m_model->insertColumns(firstIndex + m_first, slices.count());
232 m_model->insertColumns(firstIndex + m_first, slices.count());
224
233
225 for(int i = firstIndex; i < firstIndex + slices.count(); i++) {
234 for(int i = firstIndex; i < firstIndex + slices.count(); i++) {
226 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
235 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
227 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
236 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
228 }
237 }
229 blockModelSignals(false);
238 blockModelSignals(false);
230 }
239 }
231
240
232 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
241 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
233 {
242 {
234 if (m_seriesSignalsBlock)
243 if (m_seriesSignalsBlock)
235 return;
244 return;
236
245
237 if (slices.count() == 0)
246 if (slices.count() == 0)
238 return;
247 return;
239
248
240 int firstIndex = m_series->slices().indexOf(slices.at(0));
249 int firstIndex = m_slices.indexOf(slices.at(0));
241 if (firstIndex == -1)
250 if (firstIndex == -1)
242 return;
251 return;
243
252
253 if (m_count != -1)
254 m_count -= slices.count();
255
256 for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--)
257 m_slices.removeAt(i);
258
244 blockModelSignals();
259 blockModelSignals();
245 if (m_orientation == Qt::Vertical)
260 if (m_orientation == Qt::Vertical)
246 m_model->removeRows(firstIndex + m_first, slices.count());
261 m_model->removeRows(firstIndex + m_first, slices.count());
247 else
262 else
248 m_model->removeColumns(firstIndex + m_first, slices.count());
263 m_model->removeColumns(firstIndex + m_first, slices.count());
249 blockModelSignals(false);
264 blockModelSignals(false);
250 }
265 }
251
266
252 void QPieModelMapperPrivate::sliceChanged()
267 void QPieModelMapperPrivate::sliceLabelChanged()
253 {
268 {
269 if (m_seriesSignalsBlock)
270 return;
271
254 blockModelSignals();
272 blockModelSignals();
273 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
274 m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label());
275 blockModelSignals(false);
276 }
277
278 void QPieModelMapperPrivate::sliceValueChanged()
279 {
280 if (m_seriesSignalsBlock)
281 return;
282
283 blockModelSignals();
284 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
285 m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value());
255 blockModelSignals(false);
286 blockModelSignals(false);
256 }
287 }
257
288
258 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
289 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
259 {
290 {
260 if (m_modelSignalsBlock)
291 if (m_modelSignalsBlock)
261 return;
292 return;
262
293
263 blockSeriesSignals();
294 blockSeriesSignals();
264 QModelIndex index;
295 QModelIndex index;
265 QPieSlice *slice;
296 QPieSlice *slice;
266 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
297 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
267 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
298 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
268 index = topLeft.sibling(row, column);
299 index = topLeft.sibling(row, column);
269 slice = pieSlice(index);
300 slice = pieSlice(index);
270 if (slice) {
301 if (slice) {
271 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
302 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
272 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
303 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
273 }
304 }
274 }
305 }
275 }
306 }
276 blockSeriesSignals(false);
307 blockSeriesSignals(false);
277 }
308 }
278
309
279
310
280 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
311 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
281 {
312 {
282 Q_UNUSED(parent);
313 Q_UNUSED(parent);
283 if (m_modelSignalsBlock)
314 if (m_modelSignalsBlock)
284 return;
315 return;
285
316
286 blockSeriesSignals();
317 blockSeriesSignals();
287 if (m_orientation == Qt::Vertical)
318 if (m_orientation == Qt::Vertical)
288 insertData(start, end);
319 insertData(start, end);
289 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
320 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
290 initializePieFromModel();
321 initializePieFromModel();
291 blockSeriesSignals(false);
322 blockSeriesSignals(false);
292 }
323 }
293
324
294 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
325 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
295 {
326 {
296 Q_UNUSED(parent);
327 Q_UNUSED(parent);
297 if (m_modelSignalsBlock)
328 if (m_modelSignalsBlock)
298 return;
329 return;
299
330
300 blockSeriesSignals();
331 blockSeriesSignals();
301 if (m_orientation == Qt::Vertical)
332 if (m_orientation == Qt::Vertical)
302 removeData(start, end);
333 removeData(start, end);
303 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
334 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
304 initializePieFromModel();
335 initializePieFromModel();
305 blockSeriesSignals(false);
336 blockSeriesSignals(false);
306 }
337 }
307
338
308 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
339 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
309 {
340 {
310 Q_UNUSED(parent);
341 Q_UNUSED(parent);
311 if (m_modelSignalsBlock)
342 if (m_modelSignalsBlock)
312 return;
343 return;
313
344
314 blockSeriesSignals();
345 blockSeriesSignals();
315 if (m_orientation == Qt::Horizontal)
346 if (m_orientation == Qt::Horizontal)
316 insertData(start, end);
347 insertData(start, end);
317 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
348 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
318 initializePieFromModel();
349 initializePieFromModel();
319 blockSeriesSignals(false);
350 blockSeriesSignals(false);
320 }
351 }
321
352
322 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
353 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
323 {
354 {
324 Q_UNUSED(parent);
355 Q_UNUSED(parent);
325 if (m_modelSignalsBlock)
356 if (m_modelSignalsBlock)
326 return;
357 return;
327
358
328 blockSeriesSignals();
359 blockSeriesSignals();
329 if (m_orientation == Qt::Horizontal)
360 if (m_orientation == Qt::Horizontal)
330 removeData(start, end);
361 removeData(start, end);
331 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
362 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
332 initializePieFromModel();
363 initializePieFromModel();
333 blockSeriesSignals(false);
364 blockSeriesSignals(false);
334 }
365 }
335
366
336 void QPieModelMapperPrivate::insertData(int start, int end)
367 void QPieModelMapperPrivate::insertData(int start, int end)
337 {
368 {
338 if (m_count != -1 && start >= m_first + m_count) {
369 if (m_count != -1 && start >= m_first + m_count) {
339 return;
370 return;
340 } else {
371 } else {
341 int addedCount = end - start + 1;
372 int addedCount = end - start + 1;
342 if (m_count != -1 && addedCount > m_count)
373 if (m_count != -1 && addedCount > m_count)
343 addedCount = m_count;
374 addedCount = m_count;
344 int first = qMax(start, m_first);
375 int first = qMax(start, m_first);
345 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
376 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
346 for (int i = first; i <= last; i++) {
377 for (int i = first; i <= last; i++) {
347 QPieSlice *slice = new QPieSlice;
378 QPieSlice *slice = new QPieSlice;
348 slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble());
379 slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble());
349 slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString());
380 slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString());
350 slice->setLabelVisible();
381 slice->setLabelVisible();
382 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
383 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
351 m_series->insert(i - m_first, slice);
384 m_series->insert(i - m_first, slice);
385 m_slices.insert(i - m_first, slice);
352 }
386 }
353
387
354 // remove excess of slices (abouve m_count)
388 // remove excess of slices (abouve m_count)
355 if (m_count != -1 && m_series->slices().size() > m_count)
389 if (m_count != -1 && m_series->slices().size() > m_count)
356 for (int i = m_series->slices().size() - 1; i >= m_count; i--)
390 for (int i = m_series->slices().size() - 1; i >= m_count; i--) {
357 m_series->remove(m_series->slices().at(i));
391 m_series->remove(m_series->slices().at(i));
392 m_slices.removeAt(i);
393 }
358 }
394 }
359 }
395 }
360
396
361 void QPieModelMapperPrivate::removeData(int start, int end)
397 void QPieModelMapperPrivate::removeData(int start, int end)
362 {
398 {
363 int removedCount = end - start + 1;
399 int removedCount = end - start + 1;
364 if (m_count != -1 && start >= m_first + m_count) {
400 if (m_count != -1 && start >= m_first + m_count) {
365 return;
401 return;
366 } else {
402 } else {
367 int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed
403 int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed
368 int first = qMax(start, m_first); // get the index of the first item that will be removed.
404 int first = qMax(start, m_first); // get the index of the first item that will be removed.
369 int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed.
405 int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed.
370 for (int i = last; i >= first; i--)
406 for (int i = last; i >= first; i--) {
371 m_series->remove(m_series->slices().at(i - m_first));
407 m_series->remove(m_series->slices().at(i - m_first));
408 m_slices.removeAt(i - m_first);
409 }
372
410
373 if (m_count != -1) {
411 if (m_count != -1) {
374 int itemsAvailable; // check how many are available to be added
412 int itemsAvailable; // check how many are available to be added
375 if (m_orientation == Qt::Vertical)
413 if (m_orientation == Qt::Vertical)
376 itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size();
414 itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size();
377 else
415 else
378 itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size();
416 itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size();
379 int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled.
417 int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled.
380 int currentSize = m_series->slices().size();
418 int currentSize = m_series->slices().size();
381 if (toBeAdded > 0)
419 if (toBeAdded > 0)
382 for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) {
420 for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) {
383 QPieSlice *slice = new QPieSlice;
421 QPieSlice *slice = new QPieSlice;
384 if (m_orientation == Qt::Vertical) {
422 if (m_orientation == Qt::Vertical) {
385 slice->setValue(m_model->data(m_model->index(i + m_first, m_valuesIndex), Qt::DisplayRole).toDouble());
423 slice->setValue(m_model->data(m_model->index(i + m_first, m_valuesIndex), Qt::DisplayRole).toDouble());
386 slice->setLabel(m_model->data(m_model->index(i + m_first, m_labelsIndex), Qt::DisplayRole).toString());
424 slice->setLabel(m_model->data(m_model->index(i + m_first, m_labelsIndex), Qt::DisplayRole).toString());
387 } else {
425 } else {
388 slice->setValue(m_model->data(m_model->index(m_valuesIndex, i + m_first), Qt::DisplayRole).toDouble());
426 slice->setValue(m_model->data(m_model->index(m_valuesIndex, i + m_first), Qt::DisplayRole).toDouble());
389 slice->setLabel(m_model->data(m_model->index(m_labelsIndex, i + m_first), Qt::DisplayRole).toString());
427 slice->setLabel(m_model->data(m_model->index(m_labelsIndex, i + m_first), Qt::DisplayRole).toString());
390 }
428 }
391 slice->setLabelVisible();
429 slice->setLabelVisible();
392 m_series->insert(i, slice);
430 m_series->insert(i, slice);
431 m_slices.insert(i, slice);
393 }
432 }
394 }
433 }
395 }
434 }
396 }
435 }
397
436
398 void QPieModelMapperPrivate::initializePieFromModel()
437 void QPieModelMapperPrivate::initializePieFromModel()
399 {
438 {
400 if (m_model == 0 || m_series == 0)
439 if (m_model == 0 || m_series == 0)
401 return;
440 return;
402
441
403 // clear current content
404 m_series->clear();
405
406 // check if mappings are set
442 // check if mappings are set
407 if (m_valuesIndex == -1 || m_labelsIndex == -1)
443 if (m_valuesIndex == -1 || m_labelsIndex == -1)
408 return;
444 return;
409
445
410 blockSeriesSignals();
446 blockSeriesSignals();
447 // clear current content
448 m_series->clear();
449 m_slices.clear();
450
411 // create the initial slices set
451 // create the initial slices set
412 int slicePos = 0;
452 int slicePos = 0;
413 QModelIndex valueIndex = valueModelIndex(slicePos);
453 QModelIndex valueIndex = valueModelIndex(slicePos);
414 QModelIndex labelIndex = labelModelIndex(slicePos);
454 QModelIndex labelIndex = labelModelIndex(slicePos);
415 while (valueIndex.isValid() && labelIndex.isValid()) {
455 while (valueIndex.isValid() && labelIndex.isValid()) {
416 m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble());
456 QPieSlice *slice = new QPieSlice;
457 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
458 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
459 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
460 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
461 m_series->append(slice);
462 m_slices.append(slice);
463 // m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble());
417 slicePos++;
464 slicePos++;
418 valueIndex = valueModelIndex(slicePos);
465 valueIndex = valueModelIndex(slicePos);
419 labelIndex = labelModelIndex(slicePos);
466 labelIndex = labelModelIndex(slicePos);
420 }
467 }
421 m_series->setLabelsVisible(true);
468 m_series->setLabelsVisible(true);
422 blockSeriesSignals(false);
469 blockSeriesSignals(false);
423 }
470 }
424
471
425 #include "moc_qpiemodelmapper_p.cpp"
472 #include "moc_qpiemodelmapper_p.cpp"
426 #include "moc_qpiemodelmapper.cpp"
473 #include "moc_qpiemodelmapper.cpp"
427
474
428 QTCOMMERCIALCHART_END_NAMESPACE
475 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,57 +1,57
1 #ifndef QPIEMODELMAPPER_H
1 #ifndef QPIEMODELMAPPER_H
2 #define QPIEMODELMAPPER_H
2 #define QPIEMODELMAPPER_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QObject>
5 #include <QObject>
6
6
7 class QAbstractItemModel;
7 class QAbstractItemModel;
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 class QPieModelMapperPrivate;
11 class QPieModelMapperPrivate;
12 class QPieSeries;
12 class QPieSeries;
13
13
14 class QTCOMMERCIALCHART_EXPORT QPieModelMapper : public QObject
14 class QTCOMMERCIALCHART_EXPORT QPieModelMapper : public QObject
15 {
15 {
16 Q_OBJECT
16 Q_OBJECT
17 Q_PROPERTY(int valuesIndex READ valuesIndex WRITE setValuesIndex)
17 Q_PROPERTY(int valuesIndex READ valuesIndex WRITE setValuesIndex)
18 Q_PROPERTY(int labelsIndex READ labelsIndex WRITE setLabelsIndex)
18 Q_PROPERTY(int labelsIndex READ labelsIndex WRITE setLabelsIndex)
19 Q_PROPERTY(int first READ first WRITE setFirst)
19 Q_PROPERTY(int first READ first WRITE setFirst)
20 Q_PROPERTY(int count READ count WRITE setCount)
20 Q_PROPERTY(int count READ count WRITE setCount)
21 Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation)
21 Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation)
22 Q_ENUMS(Qt::Orientation)
22 Q_ENUMS(Qt::Orientation)
23
23
24 public:
24 public:
25 QPieModelMapper(QObject *parent = 0);
25 QPieModelMapper(QObject *parent = 0);
26
26
27 QAbstractItemModel* model() const;
27 QAbstractItemModel* model() const;
28 void setModel(QAbstractItemModel *model);
28 void setModel(QAbstractItemModel *model);
29
29
30 QPieSeries* series() const;
30 QPieSeries* series() const;
31 void setSeries(QPieSeries *series);
31 void setSeries(QPieSeries *series);
32
32
33 int first() const;
33 int first() const;
34 void setFirst(int first);
34 void setFirst(int first);
35
35
36 int count() const;
36 int count() const;
37 void setCount(int count);
37 void setCount(int count);
38
38
39 Qt::Orientation orientation() const;
39 Qt::Orientation orientation() const;
40 void setOrientation(Qt::Orientation orientation);
40 void setOrientation(Qt::Orientation orientation);
41
41
42 int valuesIndex() const;
42 int valuesIndex() const;
43 void setValuesIndex(int valuesIndex);
43 void setValuesIndex(int valuesIndex);
44
44
45 int labelsIndex() const;
45 int labelsIndex() const;
46 void setLabelsIndex(int labelsIndex);
46 void setLabelsIndex(int labelsIndex);
47
47
48 void reset();
48 void reset();
49
49
50 private:
50 protected:
51 QPieModelMapperPrivate * const d_ptr;
51 QPieModelMapperPrivate * const d_ptr;
52 Q_DECLARE_PRIVATE(QPieModelMapper)
52 Q_DECLARE_PRIVATE(QPieModelMapper)
53 };
53 };
54
54
55 QTCOMMERCIALCHART_END_NAMESPACE
55 QTCOMMERCIALCHART_END_NAMESPACE
56
56
57 #endif // QPIEMODELMAPPER_H
57 #endif // QPIEMODELMAPPER_H
@@ -1,68 +1,70
1 #ifndef QPIEMODELMAPPER_P_H
1 #ifndef QPIEMODELMAPPER_P_H
2 #define QPIEMODELMAPPER_P_H
2 #define QPIEMODELMAPPER_P_H
3
3
4 #include "qpiemodelmapper.h"
4 #include "qpiemodelmapper.h"
5 #include <QObject>
5 #include <QObject>
6
6
7 class QModelIndex;
7 class QModelIndex;
8 class QAbstractItemModel;
8 class QAbstractItemModel;
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class QPieModelMapper;
12 class QPieModelMapper;
13 class QPieSeries;
13 class QPieSeries;
14 class QPieSlice;
14 class QPieSlice;
15
15
16 class QPieModelMapperPrivate : public QObject
16 class QPieModelMapperPrivate : public QObject
17 {
17 {
18 Q_OBJECT
18 Q_OBJECT
19
19
20 public:
20 public:
21 QPieModelMapperPrivate(QPieModelMapper *q);
21 QPieModelMapperPrivate(QPieModelMapper *q);
22
22
23 public Q_SLOTS:
23 public Q_SLOTS:
24 // for the model
24 // for the model
25 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
25 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
26 void modelRowsAdded(QModelIndex parent, int start, int end);
26 void modelRowsAdded(QModelIndex parent, int start, int end);
27 void modelRowsRemoved(QModelIndex parent, int start, int end);
27 void modelRowsRemoved(QModelIndex parent, int start, int end);
28 void modelColumnsAdded(QModelIndex parent, int start, int end);
28 void modelColumnsAdded(QModelIndex parent, int start, int end);
29 void modelColumnsRemoved(QModelIndex parent, int start, int end);
29 void modelColumnsRemoved(QModelIndex parent, int start, int end);
30
30
31 // for the series
31 // for the series
32 void slicesAdded(QList<QPieSlice*> slices);
32 void slicesAdded(QList<QPieSlice*> slices);
33 void slicesRemoved(QList<QPieSlice*> slices);
33 void slicesRemoved(QList<QPieSlice*> slices);
34 void sliceChanged();
34 void sliceLabelChanged();
35 void sliceValueChanged();
35
36
36 void initializePieFromModel();
37 void initializePieFromModel();
37
38
38 private:
39 private:
39 QPieSlice* pieSlice(QModelIndex index) const;
40 QPieSlice* pieSlice(QModelIndex index) const;
40 QModelIndex valueModelIndex(int slicePos);
41 QModelIndex valueModelIndex(int slicePos);
41 QModelIndex labelModelIndex(int slicePos);
42 QModelIndex labelModelIndex(int slicePos);
42 void insertData(int start, int end);
43 void insertData(int start, int end);
43 void removeData(int start, int end);
44 void removeData(int start, int end);
44
45
45 void blockModelSignals(bool block = true);
46 void blockModelSignals(bool block = true);
46 void blockSeriesSignals(bool block = true);
47 void blockSeriesSignals(bool block = true);
47
48
48 private:
49 private:
49 bool m_seriesSignalsBlock;
50 bool m_seriesSignalsBlock;
50 bool m_modelSignalsBlock;
51 bool m_modelSignalsBlock;
51 QPieSeries *m_series;
52 QPieSeries *m_series;
53 QList<QPieSlice*> m_slices;
52 QAbstractItemModel *m_model;
54 QAbstractItemModel *m_model;
53 int m_first;
55 int m_first;
54 int m_count;
56 int m_count;
55 Qt::Orientation m_orientation;
57 Qt::Orientation m_orientation;
56 int m_valuesIndex;
58 int m_valuesIndex;
57 int m_labelsIndex;
59 int m_labelsIndex;
58
60
59 private:
61 private:
60
62
61 QPieModelMapper *q_ptr;
63 QPieModelMapper *q_ptr;
62 Q_DECLARE_PUBLIC(QPieModelMapper)
64 Q_DECLARE_PUBLIC(QPieModelMapper)
63 friend class QPieSeriesPrivate;
65 friend class QPieSeriesPrivate;
64 };
66 };
65
67
66 QTCOMMERCIALCHART_END_NAMESPACE
68 QTCOMMERCIALCHART_END_NAMESPACE
67
69
68 #endif // QPIEMODELMAPPER_P_H
70 #endif // QPIEMODELMAPPER_P_H
@@ -1,499 +1,519
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "tablewidget.h"
21 #include "tablewidget.h"
22 #include <QGridLayout>
22 #include <QGridLayout>
23 #include <QTableView>
23 #include <QTableView>
24 #include <QChart>
24 #include <QChart>
25 #include <QStyledItemDelegate>
25 #include <QStyledItemDelegate>
26 #include <QLineSeries>
26 #include <QLineSeries>
27 #include <QSplineSeries>
27 #include <QSplineSeries>
28 #include <QScatterSeries>
28 #include <QScatterSeries>
29 #include <QXYModelMapper>
29 #include <QXYModelMapper>
30 #include "customtablemodel.h"
30 #include "customtablemodel.h"
31 #include <QPieSeries>
31 #include <QPieSeries>
32 #include <QPieModelMapper>
32 #include <QPieModelMapper>
33 #include <QPieSlice>
33 #include <QPieSlice>
34 #include <QAreaSeries>
34 #include <QAreaSeries>
35 #include <QBarSeries>
35 #include <QBarSeries>
36 #include <QGroupedBarSeries>
36 #include <QGroupedBarSeries>
37 #include <QBarSet>
37 #include <QBarSet>
38 #include <QBarModelMapper>
38 #include <QBarModelMapper>
39 #include <QPushButton>
39 #include <QPushButton>
40 #include <QRadioButton>
40 #include <QRadioButton>
41 #include <QLabel>
41 #include <QLabel>
42 #include <QSpinBox>
42 #include <QSpinBox>
43 #include <QTime>
43 #include <QTime>
44 #include <QHeaderView>
44 #include <QHeaderView>
45
45
46 TableWidget::TableWidget(QWidget *parent)
46 TableWidget::TableWidget(QWidget *parent)
47 : QWidget(parent)
47 : QWidget(parent)
48 // specialPie(0)
48 // specialPie(0)
49 {
49 {
50 setGeometry(1900, 100, 1000, 600);
50 setGeometry(1900, 100, 1000, 600);
51 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
51 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
52 // create simple model for storing data
52 // create simple model for storing data
53 // user's table data model
53 // user's table data model
54 m_model = new CustomTableModel;
54 m_model = new CustomTableModel;
55 m_tableView = new QTableView;
55 m_tableView = new QTableView;
56 m_tableView->setModel(m_model);
56 m_tableView->setModel(m_model);
57 // m_tableView->setMinimumHeight(300);
57 // m_tableView->setMinimumHeight(300);
58 m_tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
58 m_tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
59 m_tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
59 m_tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
60
60
61 m_chart = new QChart;
61 m_chart = new QChart;
62 m_chart->legend()->setVisible(true);
62 m_chart->legend()->setVisible(true);
63 m_chart->setAnimationOptions(QChart::SeriesAnimations);
63 m_chart->setAnimationOptions(QChart::SeriesAnimations);
64 m_chartView = new QChartView(m_chart);
64 m_chartView = new QChartView(m_chart);
65 m_chartView->setRenderHint(QPainter::Antialiasing);
65 m_chartView->setRenderHint(QPainter::Antialiasing);
66 m_chartView->setMinimumSize(640, 480);
66 m_chartView->setMinimumSize(640, 480);
67
67
68 // add, remove data buttons
68 // add, remove data buttons
69 QPushButton* addRowAboveButton = new QPushButton("Add row above");
69 QPushButton* addRowAboveButton = new QPushButton("Add row above");
70 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
70 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
71
71
72 QPushButton* addRowBelowButton = new QPushButton("Add row below");
72 QPushButton* addRowBelowButton = new QPushButton("Add row below");
73 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
73 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
74
74
75 QPushButton* removeRowButton = new QPushButton("Remove row");
75 QPushButton* removeRowButton = new QPushButton("Remove row");
76 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
76 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
77
77
78 QPushButton* addColumnRightButton = new QPushButton("Add column to the right");
78 QPushButton* addColumnRightButton = new QPushButton("Add column to the right");
79 connect(addColumnRightButton, SIGNAL(clicked()), this, SLOT(addColumnRight()));
79 connect(addColumnRightButton, SIGNAL(clicked()), this, SLOT(addColumnRight()));
80
80
81 QPushButton* removeColumnButton = new QPushButton("Remove column");
81 QPushButton* removeColumnButton = new QPushButton("Remove column");
82 connect(removeColumnButton, SIGNAL(clicked()), this, SLOT(removeColumn()));
82 connect(removeColumnButton, SIGNAL(clicked()), this, SLOT(removeColumn()));
83
83
84 QPushButton* specialPieButton = new QPushButton("Add slices from series");
84 QPushButton* specialPieButton = new QPushButton("Add slices from series");
85 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
85 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
86
86
87 QPushButton* specialPieButton2 = new QPushButton("Remove slices from series");
87 QPushButton* specialPieButton2 = new QPushButton("Remove slices from series");
88 connect(specialPieButton2, SIGNAL(clicked()), this, SLOT(testPie2()));
88 connect(specialPieButton2, SIGNAL(clicked()), this, SLOT(testPie2()));
89
89
90 QPushButton* specialPieButton3 = new QPushButton("Remove slices from series");
91 connect(specialPieButton3, SIGNAL(clicked()), this, SLOT(testPie3()));
92
90
93
91 // QLabel *spinBoxLabel = new QLabel("Rows affected:");
94 // QLabel *spinBoxLabel = new QLabel("Rows affected:");
92
95
93 // spin box for setting number of affected items (add, remove)
96 // spin box for setting number of affected items (add, remove)
94 m_linesCountSpinBox = new QSpinBox;
97 m_linesCountSpinBox = new QSpinBox;
95 m_linesCountSpinBox->setRange(1, 10);
98 m_linesCountSpinBox->setRange(1, 10);
96 m_linesCountSpinBox->setValue(1);
99 m_linesCountSpinBox->setValue(1);
97
100
98 // buttons layout
101 // buttons layout
99 QVBoxLayout* buttonsLayout = new QVBoxLayout;
102 QVBoxLayout* buttonsLayout = new QVBoxLayout;
100 // buttonsLayout->addWidget(spinBoxLabel);
103 // buttonsLayout->addWidget(spinBoxLabel);
101 // buttonsLayout->addWidget(m_linesCountSpinBox);
104 // buttonsLayout->addWidget(m_linesCountSpinBox);
102 // buttonsLayout->addWidget(addRowAboveButton);
105 // buttonsLayout->addWidget(addRowAboveButton);
103 buttonsLayout->addWidget(addRowBelowButton);
106 buttonsLayout->addWidget(addRowBelowButton);
104 buttonsLayout->addWidget(removeRowButton);
107 buttonsLayout->addWidget(removeRowButton);
105 // buttonsLayout->addWidget(addColumnRightButton);
108 // buttonsLayout->addWidget(addColumnRightButton);
106 // buttonsLayout->addWidget(removeColumnButton);
109 // buttonsLayout->addWidget(removeColumnButton);
107 buttonsLayout->addWidget(specialPieButton);
110 buttonsLayout->addWidget(specialPieButton);
108 buttonsLayout->addWidget(specialPieButton2);
111 buttonsLayout->addWidget(specialPieButton2);
112 buttonsLayout->addWidget(specialPieButton3);
109 buttonsLayout->addStretch();
113 buttonsLayout->addStretch();
110
114
111 // chart type radio buttons
115 // chart type radio buttons
112 m_lineRadioButton = new QRadioButton("Line");
116 m_lineRadioButton = new QRadioButton("Line");
113 m_splineRadioButton = new QRadioButton("Spline");
117 m_splineRadioButton = new QRadioButton("Spline");
114 m_scatterRadioButton = new QRadioButton("Scatter");
118 m_scatterRadioButton = new QRadioButton("Scatter");
115 m_pieRadioButton = new QRadioButton("Pie");
119 m_pieRadioButton = new QRadioButton("Pie");
116 m_areaRadioButton = new QRadioButton("Area");
120 m_areaRadioButton = new QRadioButton("Area");
117 m_barRadioButton = new QRadioButton("Bar");
121 m_barRadioButton = new QRadioButton("Bar");
118
122
119 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
123 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
120 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
124 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
121 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
125 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
122 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
126 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
123 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
127 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
124 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
128 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
125 m_lineRadioButton->setChecked(true);
129 m_lineRadioButton->setChecked(true);
126
130
127 // radio buttons layout
131 // radio buttons layout
128 QVBoxLayout* radioLayout = new QVBoxLayout;
132 QVBoxLayout* radioLayout = new QVBoxLayout;
129 radioLayout->addWidget(m_lineRadioButton);
133 radioLayout->addWidget(m_lineRadioButton);
130 radioLayout->addWidget(m_splineRadioButton);
134 radioLayout->addWidget(m_splineRadioButton);
131 // radioLayout->addWidget(m_scatterRadioButton);
135 // radioLayout->addWidget(m_scatterRadioButton);
132 radioLayout->addWidget(m_pieRadioButton);
136 radioLayout->addWidget(m_pieRadioButton);
133 // radioLayout->addWidget(m_areaRadioButton);
137 // radioLayout->addWidget(m_areaRadioButton);
134 radioLayout->addWidget(m_barRadioButton);
138 radioLayout->addWidget(m_barRadioButton);
135 radioLayout->addStretch();
139 radioLayout->addStretch();
136
140
137 // create main layout
141 // create main layout
138 QGridLayout* mainLayout = new QGridLayout;
142 QGridLayout* mainLayout = new QGridLayout;
139 mainLayout->addLayout(buttonsLayout, 2, 0);
143 mainLayout->addLayout(buttonsLayout, 2, 0);
140 mainLayout->addLayout(radioLayout, 3, 0);
144 mainLayout->addLayout(radioLayout, 3, 0);
141 mainLayout->addWidget(m_tableView, 1, 0);
145 mainLayout->addWidget(m_tableView, 1, 0);
142 mainLayout->addWidget(m_chartView, 1, 1, 2, 1);
146 mainLayout->addWidget(m_chartView, 1, 1, 2, 1);
143 setLayout(mainLayout);
147 setLayout(mainLayout);
144 m_lineRadioButton->setFocus();
148 m_lineRadioButton->setFocus();
145 }
149 }
146
150
147 void TableWidget::addRowAbove()
151 void TableWidget::addRowAbove()
148 {
152 {
149 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
153 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
150
154
151 }
155 }
152
156
153 void TableWidget::addRowBelow()
157 void TableWidget::addRowBelow()
154 {
158 {
155 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
159 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
156
160
157 }
161 }
158
162
159 void TableWidget::removeRow()
163 void TableWidget::removeRow()
160 {
164 {
161 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
165 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
162 }
166 }
163
167
164 void TableWidget::addColumnRight()
168 void TableWidget::addColumnRight()
165 {
169 {
166 m_model->insertColumns(m_tableView->currentIndex().column() + 1, m_linesCountSpinBox->value());
170 m_model->insertColumns(m_tableView->currentIndex().column() + 1, m_linesCountSpinBox->value());
167 }
171 }
168
172
169 void TableWidget::removeColumn()
173 void TableWidget::removeColumn()
170 {
174 {
171 m_model->removeColumns(m_tableView->currentIndex().column(), qMin(m_model->columnCount() - m_tableView->currentIndex().column(), m_linesCountSpinBox->value()));
175 m_model->removeColumns(m_tableView->currentIndex().column(), qMin(m_model->columnCount() - m_tableView->currentIndex().column(), m_linesCountSpinBox->value()));
172 }
176 }
173
177
174 void TableWidget::updateChartType(bool toggle)
178 void TableWidget::updateChartType(bool toggle)
175 {
179 {
176 // this if is needed, so that the function is only called once.
180 // this if is needed, so that the function is only called once.
177 // For the radioButton that was enabled.
181 // For the radioButton that was enabled.
178 if (toggle) {
182 if (toggle) {
179 // specialPie = 0;
183 // specialPie = 0;
180 m_chart->removeAllSeries();
184 m_chart->removeAllSeries();
181 // m_chart->axisX()->setNiceNumbersEnabled(false);
185 // m_chart->axisX()->setNiceNumbersEnabled(false);
182 // m_chart->axisY()->setNiceNumbersEnabled(false);
186 // m_chart->axisY()->setNiceNumbersEnabled(false);
183
187
184 // // renable axes of the chart (pie hides them)
188 // // renable axes of the chart (pie hides them)
185 // // x axis
189 // // x axis
186 // QAxis *axis = m_chart->axisX();
190 // QAxis *axis = m_chart->axisX();
187 // axis->setAxisVisible(true);
191 // axis->setAxisVisible(true);
188 // axis->setGridLineVisible(true);
192 // axis->setGridLineVisible(true);
189 // axis->setLabelsVisible(true);
193 // axis->setLabelsVisible(true);
190
194
191 // // y axis
195 // // y axis
192 // axis = m_chart->axisY();
196 // axis = m_chart->axisY();
193 // axis->setAxisVisible(true);
197 // axis->setAxisVisible(true);
194 // axis->setGridLineVisible(true);
198 // axis->setGridLineVisible(true);
195 // axis->setLabelsVisible(true);
199 // axis->setLabelsVisible(true);
196
200
197 // m_model->clearMapping();
201 // m_model->clearMapping();
198
202
199 QString seriesColorHex = "#000000";
203 QString seriesColorHex = "#000000";
200 // QPen pen;
204 // QPen pen;
201 // pen.setWidth(2);
205 // pen.setWidth(2);
202
206
203 if (m_lineRadioButton->isChecked())
207 if (m_lineRadioButton->isChecked())
204 {
208 {
205 // m_chart->setAnimationOptions(QChart::NoAnimation);
209 // m_chart->setAnimationOptions(QChart::NoAnimation);
206
210
207 // // series 1
211 // // series 1
208 // m_series = new QLineSeries;
212 // m_series = new QLineSeries;
209 // m_series->setModel(m_model);
213 // m_series->setModel(m_model);
210
214
211 // QXYModelMapper *mapper = new QXYModelMapper;
215 // QXYModelMapper *mapper = new QXYModelMapper;
212 // mapper->setMapX(0);
216 // mapper->setMapX(0);
213 // mapper->setMapY(1);
217 // mapper->setMapY(1);
214 // mapper->setFirst(3);
218 // mapper->setFirst(3);
215 // mapper->setCount(4);
219 // mapper->setCount(4);
216 // m_series->setModelMapper(mapper);
220 // m_series->setModelMapper(mapper);
217 // // m_series->setModelMapping(0,1, Qt::Vertical);
221 // // m_series->setModelMapping(0,1, Qt::Vertical);
218 // // m_series->setModelMappingRange(3, 4);
222 // // m_series->setModelMappingRange(3, 4);
219 // m_chart->addSeries(m_series);
223 // m_chart->addSeries(m_series);
220 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
224 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
221 // m_model->addMapping(seriesColorHex, QRect(0, 3, 2, 4));
225 // m_model->addMapping(seriesColorHex, QRect(0, 3, 2, 4));
222
226
223 // // series 2
227 // // series 2
224 // m_series = new QLineSeries;
228 // m_series = new QLineSeries;
225 // m_series->setModel(m_model);
229 // m_series->setModel(m_model);
226
230
227 // mapper = new QXYModelMapper;
231 // mapper = new QXYModelMapper;
228 // mapper->setMapX(3);
232 // mapper->setMapX(3);
229 // mapper->setMapY(4);
233 // mapper->setMapY(4);
230 // // mapper->setFirst(3);
234 // // mapper->setFirst(3);
231 // // mapper->setCount(4);
235 // // mapper->setCount(4);
232 // m_series->setModelMapper(mapper);
236 // m_series->setModelMapper(mapper);
233 // // m_series->setModelMapping(2,3, Qt::Vertical);
237 // // m_series->setModelMapping(2,3, Qt::Vertical);
234 // m_chart->addSeries(m_series);
238 // m_chart->addSeries(m_series);
235 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
239 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
236 // m_model->addMapping(seriesColorHex, QRect(3, 0, 2, 1000));
240 // m_model->addMapping(seriesColorHex, QRect(3, 0, 2, 1000));
237
241
238 // // series 3
242 // // series 3
239 // m_series = new QLineSeries;
243 // m_series = new QLineSeries;
240 // m_series->setModel(m_model);
244 // m_series->setModel(m_model);
241
245
242 // mapper = new QXYModelMapper;
246 // mapper = new QXYModelMapper;
243 // mapper->setMapX(5);
247 // mapper->setMapX(5);
244 // mapper->setMapY(6);
248 // mapper->setMapY(6);
245 // mapper->setFirst(2);
249 // mapper->setFirst(2);
246 // mapper->setCount(-1);
250 // mapper->setCount(-1);
247 // m_series->setModelMapper(mapper);
251 // m_series->setModelMapper(mapper);
248 // // m_series->setModelMapping(4,5, Qt::Vertical);
252 // // m_series->setModelMapping(4,5, Qt::Vertical);
249 // // m_series->setModelMappingRange(2, -1);
253 // // m_series->setModelMappingRange(2, -1);
250 // m_chart->addSeries(m_series);
254 // m_chart->addSeries(m_series);
251 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
255 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
252 // m_model->addMapping(seriesColorHex, QRect(5, 2, 2, 1000));
256 // m_model->addMapping(seriesColorHex, QRect(5, 2, 2, 1000));
253 }
257 }
254 else if (m_splineRadioButton->isChecked())
258 else if (m_splineRadioButton->isChecked())
255 {
259 {
256 // m_chart->setAnimationOptions(QChart::NoAnimation);
260 // m_chart->setAnimationOptions(QChart::NoAnimation);
257
261
258 // // series 1
262 // // series 1
259 // m_series = new QSplineSeries;
263 // m_series = new QSplineSeries;
260 // m_series->setModel(m_model);
264 // m_series->setModel(m_model);
261
265
262 // QXYModelMapper *mapper = new QXYModelMapper;
266 // QXYModelMapper *mapper = new QXYModelMapper;
263 // mapper->setMapX(0);
267 // mapper->setMapX(0);
264 // mapper->setMapY(1);
268 // mapper->setMapY(1);
265 // mapper->setFirst(0);
269 // mapper->setFirst(0);
266 // mapper->setCount(-1);
270 // mapper->setCount(-1);
267
271
268 // m_series->setModelMapper(mapper);
272 // m_series->setModelMapper(mapper);
269
273
270 // m_chart->addSeries(m_series);
274 // m_chart->addSeries(m_series);
271 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
275 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
272 // m_model->addMapping(seriesColorHex, QRect(0, 0, 2, 1000));
276 // m_model->addMapping(seriesColorHex, QRect(0, 0, 2, 1000));
273
277
274 // // series 2
278 // // series 2
275 // m_series = new QSplineSeries;
279 // m_series = new QSplineSeries;
276 // m_series->setModel(m_model);
280 // m_series->setModel(m_model);
277
281
278 // mapper = new QXYModelMapper;
282 // mapper = new QXYModelMapper;
279 // mapper->setMapX(2);
283 // mapper->setMapX(2);
280 // mapper->setMapY(3);
284 // mapper->setMapY(3);
281 // mapper->setFirst(2);
285 // mapper->setFirst(2);
282 // mapper->setCount(4);
286 // mapper->setCount(4);
283
287
284 // m_series->setModelMapper(mapper);
288 // m_series->setModelMapper(mapper);
285
289
286 // m_chart->addSeries(m_series);
290 // m_chart->addSeries(m_series);
287 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
291 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
288 // m_model->addMapping(seriesColorHex, QRect(2, 2, 2, 4));
292 // m_model->addMapping(seriesColorHex, QRect(2, 2, 2, 4));
289
293
290 // // series 3
294 // // series 3
291 // m_series = new QSplineSeries;
295 // m_series = new QSplineSeries;
292 // m_series->setModel(m_model);
296 // m_series->setModel(m_model);
293
297
294 // mapper = new QXYModelMapper;
298 // mapper = new QXYModelMapper;
295 // mapper->setMapX(4);
299 // mapper->setMapX(4);
296 // mapper->setMapY(5);
300 // mapper->setMapY(5);
297 // mapper->setFirst(2);
301 // mapper->setFirst(2);
298 // mapper->setCount(-1);
302 // mapper->setCount(-1);
299
303
300 // m_series->setModelMapper(mapper);
304 // m_series->setModelMapper(mapper);
301
305
302 // m_chart->addSeries(m_series);
306 // m_chart->addSeries(m_series);
303 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
307 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
304 // m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
308 // m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
305 }
309 }
306 // else if (m_scatterRadioButton->isChecked())
310 // else if (m_scatterRadioButton->isChecked())
307 // {
311 // {
308 // m_chart->setAnimationOptions(QChart::NoAnimation);
312 // m_chart->setAnimationOptions(QChart::NoAnimation);
309
313
310 // // series 1
314 // // series 1
311 // m_series = new QScatterSeries;
315 // m_series = new QScatterSeries;
312 // m_series->setModel(m_model);
316 // m_series->setModel(m_model);
313 // m_series->setModelMapping(0,1, Qt::Vertical);
317 // m_series->setModelMapping(0,1, Qt::Vertical);
314 // // m_series->setModelMappingRange(2, 0);
318 // // m_series->setModelMappingRange(2, 0);
315 // // series->setModelMapping(0,1, Qt::Horizontal);
319 // // series->setModelMapping(0,1, Qt::Horizontal);
316 // m_chart->addSeries(m_series);
320 // m_chart->addSeries(m_series);
317
321
318 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
322 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
319 // m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
323 // m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
320
324
321 // // series 2
325 // // series 2
322 // m_series = new QScatterSeries;
326 // m_series = new QScatterSeries;
323 // m_series->setModel(m_model);
327 // m_series->setModel(m_model);
324 // m_series->setModelMapping(2,3, Qt::Vertical);
328 // m_series->setModelMapping(2,3, Qt::Vertical);
325 // // m_series->setModelMappingRange(1, 6);
329 // // m_series->setModelMappingRange(1, 6);
326 // // series->setModelMapping(2,3, Qt::Horizontal);
330 // // series->setModelMapping(2,3, Qt::Horizontal);
327 // m_chart->addSeries(m_series);
331 // m_chart->addSeries(m_series);
328
332
329 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
333 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
330 // m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
334 // m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
331
335
332 // // series 3
336 // // series 3
333 // m_series = new QScatterSeries;
337 // m_series = new QScatterSeries;
334 // m_series->setModel(m_model);
338 // m_series->setModel(m_model);
335 // m_series->setModelMapping(4,5, Qt::Vertical);
339 // m_series->setModelMapping(4,5, Qt::Vertical);
336 // // series->setModelMapping(4,5, Qt::Horizontal);
340 // // series->setModelMapping(4,5, Qt::Horizontal);
337 // m_chart->addSeries(m_series);
341 // m_chart->addSeries(m_series);
338 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
342 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
339 // m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
343 // m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
340 // }
344 // }
341 else if (m_pieRadioButton->isChecked())
345 else if (m_pieRadioButton->isChecked())
342 {
346 {
343 m_chart->setAnimationOptions(QChart::SeriesAnimations);
347 m_chart->setAnimationOptions(QChart::SeriesAnimations);
344
348
345 // pie 1
349 // pie 1
346 m_pieSeries = new QPieSeries;
350 m_pieSeries = new QPieSeries;
347
351
348 m_pieMapper = new QPieModelMapper;
352 m_pieMapper = new QPieModelMapper;
349 m_pieMapper->setValuesIndex(1);
353 m_pieMapper->setValuesIndex(1);
350 m_pieMapper->setLabelsIndex(1);
354 m_pieMapper->setLabelsIndex(7);
351 m_pieMapper->setSeries(m_pieSeries);
355 m_pieMapper->setSeries(m_pieSeries);
352 m_pieMapper->setModel(m_model);
356 m_pieMapper->setModel(m_model);
353 m_pieMapper->setFirst(2);
357 m_pieMapper->setFirst(2);
354 // m_pieMapper->setCount(5);
358 m_pieMapper->setCount(5);
355 // pieSeries->setModelMapper(mapper);
359 // pieSeries->setModelMapper(mapper);
356
360
357 m_pieSeries->setLabelsVisible(true);
361 m_pieSeries->setLabelsVisible(true);
358 m_pieSeries->setPieSize(0.75);
362 m_pieSeries->setPieSize(0.75);
359 // pieSeries->setHorizontalPosition(0.2);
363 // pieSeries->setHorizontalPosition(0.2);
360 // pieSeries->setVerticalPosition(0.3);
364 // pieSeries->setVerticalPosition(0.3);
361
365
362 m_chart->addSeries(m_pieSeries);
366 m_chart->addSeries(m_pieSeries);
363 seriesColorHex = "#" + QString::number(m_pieSeries->slices().at(m_pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
367 seriesColorHex = "#" + QString::number(m_pieSeries->slices().at(m_pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
364 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 50));
368 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 5));
365
369
366
370
367 // pieSeries->slices().at(0)->setValue(400);
371 // pieSeries->slices().at(0)->setValue(400);
368 // pieSeries->slices().at(0)->setLabel(QString("36"));
372 // pieSeries->slices().at(0)->setLabel(QString("36"));
369
373
370 // // pie 2
374 // // pie 2
371 // pieSeries = new QPieSeries;
375 // pieSeries = new QPieSeries;
372 // pieSeries->setModel(m_model);
376 // pieSeries->setModel(m_model);
373
377
374 // pieSeries->setModelMapping(1,1, Qt::Vertical);
378 // pieSeries->setModelMapping(1,1, Qt::Vertical);
375 // pieSeries->setModelMappingRange(2, -1);
379 // pieSeries->setModelMappingRange(2, -1);
376 // pieSeries->setLabelsVisible(true);
380 // pieSeries->setLabelsVisible(true);
377 // pieSeries->setPieSize(0.35);
381 // pieSeries->setPieSize(0.35);
378 // pieSeries->setHorizontalPosition(0.8);
382 // pieSeries->setHorizontalPosition(0.8);
379 // pieSeries->setVerticalPosition(0.3);
383 // pieSeries->setVerticalPosition(0.3);
380 // m_chart->addSeries(pieSeries);
384 // m_chart->addSeries(pieSeries);
381 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
385 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
382 // m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 1000));
386 // m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 1000));
383
387
384 // // pie 3
388 // // pie 3
385 // pieSeries = new QPieSeries;
389 // pieSeries = new QPieSeries;
386 // pieSeries->setModel(m_model);
390 // pieSeries->setModel(m_model);
387 // pieSeries->setModelMapping(2,2, Qt::Vertical);
391 // pieSeries->setModelMapping(2,2, Qt::Vertical);
388 // pieSeries->setLabelsVisible(true);
392 // pieSeries->setLabelsVisible(true);
389 // pieSeries->setPieSize(0.35);
393 // pieSeries->setPieSize(0.35);
390 // pieSeries->setHorizontalPosition(0.5);
394 // pieSeries->setHorizontalPosition(0.5);
391 // pieSeries->setVerticalPosition(0.75);
395 // pieSeries->setVerticalPosition(0.75);
392 // m_chart->addSeries(pieSeries);
396 // m_chart->addSeries(pieSeries);
393 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
397 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
394 // m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
398 // m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
395
399
396 // // special pie
400 // // special pie
397 // specialPie = new QPieSeries;
401 // specialPie = new QPieSeries;
398 // specialPie->append(17, "1");
402 // specialPie->append(17, "1");
399 // specialPie->append(45, "2");
403 // specialPie->append(45, "2");
400 // specialPie->append(77, "3");
404 // specialPie->append(77, "3");
401 // specialPie->append(37, "4");
405 // specialPie->append(37, "4");
402 // specialPie->append(27, "5");
406 // specialPie->append(27, "5");
403 // specialPie->append(47, "6");
407 // specialPie->append(47, "6");
404 // specialPie->setPieSize(0.35);
408 // specialPie->setPieSize(0.35);
405 // specialPie->setHorizontalPosition(0.8);
409 // specialPie->setHorizontalPosition(0.8);
406 // specialPie->setVerticalPosition(0.75);
410 // specialPie->setVerticalPosition(0.75);
407 // specialPie->setLabelsVisible(true);
411 // specialPie->setLabelsVisible(true);
408 // m_chart->addSeries(specialPie);
412 // m_chart->addSeries(specialPie);
409 }
413 }
410 // else if (m_areaRadioButton->isChecked())
414 // else if (m_areaRadioButton->isChecked())
411 // {
415 // {
412 // m_chart->setAnimationOptions(QChart::NoAnimation);
416 // m_chart->setAnimationOptions(QChart::NoAnimation);
413
417
414 // QLineSeries* upperLineSeries = new QLineSeries;
418 // QLineSeries* upperLineSeries = new QLineSeries;
415 // upperLineSeries->setModel(m_model);
419 // upperLineSeries->setModel(m_model);
416 // upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
420 // upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
417 // // upperLineSeries->setModelMappingRange(1, 5);
421 // // upperLineSeries->setModelMappingRange(1, 5);
418 // QLineSeries* lowerLineSeries = new QLineSeries;
422 // QLineSeries* lowerLineSeries = new QLineSeries;
419 // lowerLineSeries->setModel(m_model);
423 // lowerLineSeries->setModel(m_model);
420 // lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
424 // lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
421 // QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
425 // QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
422 // m_chart->addSeries(areaSeries);
426 // m_chart->addSeries(areaSeries);
423 // seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
427 // seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
424 // m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
428 // m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
425 // m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
429 // m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
426 // }
430 // }
427 else if (m_barRadioButton->isChecked())
431 else if (m_barRadioButton->isChecked())
428 {
432 {
429 // m_chart->setAnimationOptions(QChart::SeriesAnimations);
433 // m_chart->setAnimationOptions(QChart::SeriesAnimations);
430
434
431 // QGroupedBarSeries* barSeries = new QGroupedBarSeries();
435 // QGroupedBarSeries* barSeries = new QGroupedBarSeries();
432 // barSeries->setCategories(QStringList());
436 // barSeries->setCategories(QStringList());
433 // barSeries->setModel(m_model);
437 // barSeries->setModel(m_model);
434 // // barSeries->setModelMappingRange(2, 5);
438 // // barSeries->setModelMappingRange(2, 5);
435 //// barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
439 //// barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
436
440
437 // QBarModelMapper *mapper = new QBarModelMapper;
441 // QBarModelMapper *mapper = new QBarModelMapper;
438 // mapper->setMapCategories(5);
442 // mapper->setMapCategories(5);
439 // mapper->setMapBarBottom(2);
443 // mapper->setMapBarBottom(2);
440 // mapper->setMapBarTop(4);
444 // mapper->setMapBarTop(4);
441 // barSeries->setModelMapper(mapper);
445 // barSeries->setModelMapper(mapper);
442 // m_chart->addSeries(barSeries);
446 // m_chart->addSeries(barSeries);
443 // QList<QBarSet*> barsets = barSeries->barSets();
447 // QList<QBarSet*> barsets = barSeries->barSets();
444 // for (int i = 0; i < barsets.count(); i++) {
448 // for (int i = 0; i < barsets.count(); i++) {
445 // seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
449 // seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
446 // m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
450 // m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
447 // }
451 // }
448 }
452 }
449
453
450
454
451 if (!m_barRadioButton->isChecked()) {
455 if (!m_barRadioButton->isChecked()) {
452 m_chart->axisX()->setRange(0, 500);
456 m_chart->axisX()->setRange(0, 500);
453 m_chart->axisY()->setRange(0, 220);
457 m_chart->axisY()->setRange(0, 220);
454 }
458 }
455 m_chart->legend()->setVisible(true);
459 m_chart->legend()->setVisible(true);
456
460
457 // repaint table view colors
461 // repaint table view colors
458 m_tableView->repaint();
462 m_tableView->repaint();
459 m_tableView->setFocus();
463 m_tableView->setFocus();
460 }
464 }
461 }
465 }
462
466
463 void TableWidget::testPie()
467 void TableWidget::testPie()
464 {
468 {
465 // m_pieMapper->setCount(-1);
469 // m_pieMapper->setCount(-1);
466 QPieSlice *slice = new QPieSlice("Hehe", 145);
470 QPieSlice *slice = new QPieSlice("Hehe", 145);
467 slice->setLabelVisible();
471 slice->setLabelVisible();
468 m_pieSeries->append(slice);
472 m_pieSeries->append(slice);
469
473
470 slice = new QPieSlice("Hoho", 34);
474 slice = new QPieSlice("Hoho", 34);
471 slice->setLabelVisible();
475 slice->setLabelVisible();
472 m_pieSeries->append(slice);
476 m_pieSeries->append(slice);
473 // m_series->modelMapper()->setMapX(4);
477 // m_series->modelMapper()->setMapX(4);
474 // m_tableView->setColumnWidth(10, 250);
478 // m_tableView->setColumnWidth(10, 250);
475 // if (specialPie) {
479 // if (specialPie) {
476 // specialPie->remove(specialPie->slices().at(2));
480 // specialPie->remove(specialPie->slices().at(2));
477 // // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2));
481 // // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2));
478 // specialPie->append(4, "heloo");
482 // specialPie->append(4, "heloo");
479 // }
483 // }
480 }
484 }
481
485
482 void TableWidget::testPie2()
486 void TableWidget::testPie2()
483 {
487 {
484 QPieSlice *slice;
488 QPieSlice *slice;
485 if (m_pieSeries->count() > 0) {
489 if (m_pieSeries->count() > 0) {
486 slice = m_pieSeries->slices().last();
490 slice = m_pieSeries->slices().last();
487 m_pieSeries->remove(slice);
491 m_pieSeries->remove(slice);
488 }
492 }
489
493
490 if (m_pieSeries->count() > 0) {
494 if (m_pieSeries->count() > 0) {
491 slice = m_pieSeries->slices().first();
495 slice = m_pieSeries->slices().first();
492 m_pieSeries->remove(slice);
496 m_pieSeries->remove(slice);
493 }
497 }
494 }
498 }
495
499
500 void TableWidget::testPie3()
501 {
502 QPieSlice *slice;
503 if (m_pieSeries->count() > 0) {
504 slice = m_pieSeries->slices().last();
505 slice->setLabel("Dalej");
506 slice->setValue(222);
507 }
508
509 if (m_pieSeries->count() > 0) {
510 slice = m_pieSeries->slices().first();
511 slice->setLabel("Prawie");
512 slice->setValue(111);
513 }
514 }
515
496 TableWidget::~TableWidget()
516 TableWidget::~TableWidget()
497 {
517 {
498
518
499 }
519 }
@@ -1,75 +1,76
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef TABLEWIDGET_H
21 #ifndef TABLEWIDGET_H
22 #define TABLEWIDGET_H
22 #define TABLEWIDGET_H
23
23
24 #include <QtGui/QWidget>
24 #include <QtGui/QWidget>
25 //#include <QChartGlobal>
25 //#include <QChartGlobal>
26 #include "qchartview.h"
26 #include "qchartview.h"
27 //#include "qxyseries.h"
27 //#include "qxyseries.h"
28 #include <QPieSeries>
28 #include <QPieSeries>
29 #include <QPieModelMapper>
29 #include <QPieModelMapper>
30
30
31 class CustomTableModel;
31 class CustomTableModel;
32 class QTableView;
32 class QTableView;
33 class QRadioButton;
33 class QRadioButton;
34 class QSpinBox;
34 class QSpinBox;
35
35
36 QTCOMMERCIALCHART_USE_NAMESPACE
36 QTCOMMERCIALCHART_USE_NAMESPACE
37
37
38 class TableWidget : public QWidget
38 class TableWidget : public QWidget
39 {
39 {
40 Q_OBJECT
40 Q_OBJECT
41
41
42 public:
42 public:
43 TableWidget(QWidget *parent = 0);
43 TableWidget(QWidget *parent = 0);
44 ~TableWidget();
44 ~TableWidget();
45
45
46
46
47 public slots:
47 public slots:
48 void addRowAbove();
48 void addRowAbove();
49 void addRowBelow();
49 void addRowBelow();
50 void removeRow();
50 void removeRow();
51 void addColumnRight();
51 void addColumnRight();
52 void removeColumn();
52 void removeColumn();
53 void updateChartType(bool toggle);
53 void updateChartType(bool toggle);
54 void testPie();
54 void testPie();
55 void testPie2();
55 void testPie2();
56 void testPie3();
56
57
57 private:
58 private:
58 QChartView* m_chartView;
59 QChartView* m_chartView;
59 QChart* m_chart;
60 QChart* m_chart;
60 QXYSeries* m_series;
61 QXYSeries* m_series;
61 CustomTableModel* m_model;
62 CustomTableModel* m_model;
62 QTableView* m_tableView;
63 QTableView* m_tableView;
63 QRadioButton* m_lineRadioButton;
64 QRadioButton* m_lineRadioButton;
64 QRadioButton* m_splineRadioButton;
65 QRadioButton* m_splineRadioButton;
65 QRadioButton* m_scatterRadioButton;
66 QRadioButton* m_scatterRadioButton;
66 QRadioButton* m_pieRadioButton;
67 QRadioButton* m_pieRadioButton;
67 QRadioButton* m_areaRadioButton;
68 QRadioButton* m_areaRadioButton;
68 QRadioButton* m_barRadioButton;
69 QRadioButton* m_barRadioButton;
69 QSpinBox* m_linesCountSpinBox;
70 QSpinBox* m_linesCountSpinBox;
70 QPieModelMapper *m_pieMapper;
71 QPieModelMapper *m_pieMapper;
71 QPieSeries* m_pieSeries;
72 QPieSeries* m_pieSeries;
72 // QPieSeries* specialPie;
73 // QPieSeries* specialPie;
73 };
74 };
74
75
75 #endif // TABLEWIDGET_H
76 #endif // TABLEWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now