##// END OF EJS Templates
Fix compilation issue of chartdesigner
Michal Klocek -
r1199:84c5353058e3
parent child
Show More
@@ -1,317 +1,347
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 "engine.h"
21 #include "engine.h"
22 #include <QItemSelectionModel>
22 #include <QItemSelectionModel>
23 #include <QStandardItemModel>
23 #include <QStandardItemModel>
24 #include <QXYModelMapper>
25 #include <QBarModelMapper>
26 #include <QPieModelMapper>
24 #include <QLineSeries>
27 #include <QLineSeries>
25 #include <QSplineSeries>
28 #include <QSplineSeries>
26 #include <QScatterSeries>
29 #include <QScatterSeries>
27 #include <QBarSeries>
30 #include <QBarSeries>
28 #include <QPercentBarSeries>
31 #include <QPercentBarSeries>
29 #include <QStackedBarSeries>
32 #include <QStackedBarSeries>
30 #include <QAreaSeries>
33 #include <QAreaSeries>
31 #include <QPieSeries>
34 #include <QPieSeries>
32 #include <QChart>
35 #include <QChart>
33 #include <QBarSet>
36 #include <QBarSet>
34
37
38
35 const qint32 MAGIC_NUMBER = 0x66666666;
39 const qint32 MAGIC_NUMBER = 0x66666666;
36
40
37 Engine::Engine(QObject* parent) :
41 Engine::Engine(QObject* parent) :
38 QObject(parent), m_count(10), m_chart(new QChart()), m_model(0), m_selection(0)
42 QObject(parent), m_count(10), m_chart(new QChart()), m_model(0), m_selection(0)
39 {
43 {
40 createModels();
44 createModels();
41 }
45 }
42
46
43 Engine::~Engine()
47 Engine::~Engine()
44 {
48 {
45 delete m_chart;
49 delete m_chart;
46 delete m_selection;
50 delete m_selection;
47 delete m_model;
51 delete m_model;
48
52
49 }
53 }
50
54
51 void Engine::createModels()
55 void Engine::createModels()
52 {
56 {
53 m_model = new QStandardItemModel(m_count, m_count);
57 m_model = new QStandardItemModel(m_count, m_count);
54 m_model->setHorizontalHeaderLabels(
58 m_model->setHorizontalHeaderLabels(
55 QStringList() << "A" << "B" << "C" << "D" << "E" << "F" << "G" << "H" << "I" << "J");
59 QStringList() << "A" << "B" << "C" << "D" << "E" << "F" << "G" << "H" << "I" << "J");
56 m_model->setVerticalHeaderLabels(
60 m_model->setVerticalHeaderLabels(
57 QStringList() << "1" << "2" << "3" << "4" << "5" << "6" << "7" << "8" << "9" << "10");
61 QStringList() << "1" << "2" << "3" << "4" << "5" << "6" << "7" << "8" << "9" << "10");
58 m_selection = new QItemSelectionModel(m_model);
62 m_selection = new QItemSelectionModel(m_model);
59
63
60 }
64 }
61
65
62 QList<QAbstractSeries*> Engine::addSeries(QAbstractSeries::SeriesType type)
66 QList<QAbstractSeries*> Engine::addSeries(QAbstractSeries::SeriesType type)
63 {
67 {
64 const QModelIndexList& list = m_selection->selectedIndexes();
68 const QModelIndexList& list = m_selection->selectedIndexes();
65
69
66 QMap<int, QModelIndex> columns;
70 QMap<int, QModelIndex> columns;
67
71
68 foreach(const QModelIndex& index, list) {
72 foreach(const QModelIndex& index, list) {
69 columns.insertMulti(index.column(), index);
73 columns.insertMulti(index.column(), index);
70 }
74 }
71
75
72 QList<int> keys = columns.uniqueKeys();
76 QList<int> keys = columns.uniqueKeys();
73
77
74 QModelIndexList rows = columns.values(keys.first());
78 QModelIndexList rows = columns.values(keys.first());
75
79
76 int minRow = m_count + 1;
80 int minRow = m_count + 1;
77 int maxRow = -1;
81 int maxRow = -1;
78
82
79 foreach(const QModelIndex& index, rows) {
83 foreach(const QModelIndex& index, rows) {
80 minRow = qMin(index.row(), minRow);
84 minRow = qMin(index.row(), minRow);
81 maxRow = qMax(index.row(), maxRow);
85 maxRow = qMax(index.row(), maxRow);
82 }
86 }
83
87
84 QList<QAbstractSeries*> result;
88 QList<QAbstractSeries*> result;
85 QColor color;
89 QColor color;
86
90
87 switch (type) {
91 switch (type) {
88
92
89 case QAbstractSeries::SeriesTypeLine:
93 case QAbstractSeries::SeriesTypeLine:
90 {
94 {
91 for (int i = 1; i < keys.count(); ++i) {
95 for (int i = 1; i < keys.count(); ++i) {
92 QLineSeries *line = new QLineSeries();
96 QLineSeries *line = new QLineSeries();
93 setupXYSeries(line, keys, i, minRow, maxRow);
97 setupXYSeries(line, keys, i, minRow, maxRow);
94 result << line;
98 result << line;
95 }
99 }
96 break;
100 break;
97 }
101 }
98 case QAbstractSeries::SeriesTypeSpline:
102 case QAbstractSeries::SeriesTypeSpline:
99 {
103 {
100 for (int i = 1; i < keys.count(); ++i) {
104 for (int i = 1; i < keys.count(); ++i) {
101 QSplineSeries *line = new QSplineSeries();
105 QSplineSeries *line = new QSplineSeries();
102 setupXYSeries(line, keys, i, minRow, maxRow);
106 setupXYSeries(line, keys, i, minRow, maxRow);
103 result << line;
107 result << line;
104 }
108 }
105 break;
109 break;
106 }
110 }
107 case QAbstractSeries::SeriesTypeScatter:
111 case QAbstractSeries::SeriesTypeScatter:
108 {
112 {
109 for (int i = 1; i < keys.count(); ++i) {
113 for (int i = 1; i < keys.count(); ++i) {
110 QScatterSeries *line = new QScatterSeries();
114 QScatterSeries *line = new QScatterSeries();
111 setupXYSeries(line, keys, i, minRow, maxRow);
115 setupXYSeries(line, keys, i, minRow, maxRow);
112 result << line;
116 result << line;
113 }
117 }
114 break;
118 break;
115 }
119 }
116 case QAbstractSeries::SeriesTypeBar:
120 case QAbstractSeries::SeriesTypeBar:
117 {
121 {
118 //TODO: fix me
122 //TODO: fix me
119 QBarSeries *bar = new QBarSeries(QBarCategories() << "temp");
123 QBarSeries *bar = new QBarSeries();
120 setupBarSeries(bar,keys,minRow,maxRow);
124 setupBarSeries(bar,keys,minRow,maxRow);
121 result << bar;
125 result << bar;
122 break;
126 break;
123 }
127 }
124 case QAbstractSeries::SeriesTypePercentBar:
128 case QAbstractSeries::SeriesTypePercentBar:
125 {
129 {
126 QPercentBarSeries *bar = new QPercentBarSeries(QBarCategories() << "temp");
130 QPercentBarSeries *bar = new QPercentBarSeries();
127 setupBarSeries(bar,keys,minRow,maxRow);
131 setupBarSeries(bar,keys,minRow,maxRow);
128 result << bar;
132 result << bar;
129 break;
133 break;
130 }
134 }
131 case QAbstractSeries::SeriesTypeStackedBar:
135 case QAbstractSeries::SeriesTypeStackedBar:
132 {
136 {
133 QStackedBarSeries *bar = new QStackedBarSeries(QBarCategories() << "temp");
137 QStackedBarSeries *bar = new QStackedBarSeries();
134 setupBarSeries(bar,keys,minRow,maxRow);
138 setupBarSeries(bar,keys,minRow,maxRow);
135 result << bar;
139 result << bar;
136 break;
140 break;
137 }
141 }
138 case QAbstractSeries::SeriesTypePie:
142 case QAbstractSeries::SeriesTypePie:
139 {
143 {
140
144
141 QPieSeries *pie = new QPieSeries();
145 QPieSeries *pie = new QPieSeries();
142 setupPieSeries(pie,keys,minRow,maxRow);
146 setupPieSeries(pie,keys,minRow,maxRow);
143 result << pie;
147 result << pie;
144 break;
148 break;
145 }
149 }
146 case QAbstractSeries::SeriesTypeArea:
150 case QAbstractSeries::SeriesTypeArea:
147 {
151 {
148 QAreaSeries *area = new QAreaSeries( new QLineSeries(), new QLineSeries());
152 QAreaSeries *area = new QAreaSeries( new QLineSeries(), new QLineSeries());
149 setupAreaSeries(area,keys,minRow,maxRow);
153 setupAreaSeries(area,keys,minRow,maxRow);
150 result << area;
154 result << area;
151 break;
155 break;
152 }
156 }
153 }
157 }
154 return result;
158 return result;
155 }
159 }
156
160
157 void Engine::removeSeries(QAbstractSeries* series)
161 void Engine::removeSeries(QAbstractSeries* series)
158 {
162 {
159 m_chart->removeSeries(series);
163 m_chart->removeSeries(series);
160
164
161 foreach(const QModelIndex& index, m_seriesModelIndex.value(series)) {
165 foreach(const QModelIndex& index, m_seriesModelIndex.value(series)) {
162 m_model->setData(index, Qt::white, Qt::BackgroundRole);
166 m_model->setData(index, Qt::white, Qt::BackgroundRole);
163 }
167 }
164 }
168 }
165
169
166 void Engine::clearModels()
170 void Engine::clearModels()
167 {
171 {
168 delete m_selection;
172 delete m_selection;
169 m_selection = 0;
173 m_selection = 0;
170 delete m_model;
174 delete m_model;
171 m_model = 0;
175 m_model = 0;
172 createModels();
176 createModels();
173 }
177 }
174
178
175 bool Engine::save(const QString &filename) const
179 bool Engine::save(const QString &filename) const
176 {
180 {
177 if (filename.isEmpty())
181 if (filename.isEmpty())
178 return false;
182 return false;
179
183
180 QFile file(filename);
184 QFile file(filename);
181
185
182 if (!file.open(QIODevice::WriteOnly)) {
186 if (!file.open(QIODevice::WriteOnly)) {
183 return false;
187 return false;
184 }
188 }
185
189
186 QDataStream out(&file);
190 QDataStream out(&file);
187 out << MAGIC_NUMBER;
191 out << MAGIC_NUMBER;
188 out.setVersion(QDataStream::Qt_4_8);
192 out.setVersion(QDataStream::Qt_4_8);
189 out << m_model->rowCount();
193 out << m_model->rowCount();
190 out << m_model->columnCount();
194 out << m_model->columnCount();
191
195
192 for (int row = 0; row < m_model->rowCount(); ++row) {
196 for (int row = 0; row < m_model->rowCount(); ++row) {
193 for (int column = 0; column < m_model->columnCount(); ++column) {
197 for (int column = 0; column < m_model->columnCount(); ++column) {
194 QStandardItem *item = m_model->item(row, column);
198 QStandardItem *item = m_model->item(row, column);
195 if (item) {
199 if (item) {
196 out << row;
200 out << row;
197 out << column;
201 out << column;
198 out << item->data(Qt::EditRole).toString();
202 out << item->data(Qt::EditRole).toString();
199 }
203 }
200 }
204 }
201 }
205 }
202 return true;
206 return true;
203 }
207 }
204
208
205 bool Engine::load(const QString &filename)
209 bool Engine::load(const QString &filename)
206 {
210 {
207 clearModels();
211 clearModels();
208
212
209 if (filename.isEmpty())
213 if (filename.isEmpty())
210 return false;
214 return false;
211
215
212 QFile file(filename);
216 QFile file(filename);
213
217
214 if (!file.open(QIODevice::ReadOnly)) {
218 if (!file.open(QIODevice::ReadOnly)) {
215 return false;
219 return false;
216 }
220 }
217
221
218 QDataStream in(&file);
222 QDataStream in(&file);
219
223
220 qint32 magicNumber;
224 qint32 magicNumber;
221 in >> magicNumber;
225 in >> magicNumber;
222
226
223 if (magicNumber != MAGIC_NUMBER)
227 if (magicNumber != MAGIC_NUMBER)
224 return false;
228 return false;
225
229
226 in.setVersion(QDataStream::Qt_4_8);
230 in.setVersion(QDataStream::Qt_4_8);
227
231
228 int rowCount;
232 int rowCount;
229 in >> rowCount;
233 in >> rowCount;
230
234
231 int columnCount;
235 int columnCount;
232 in >> columnCount;
236 in >> columnCount;
233
237
234 while (!in.atEnd()) {
238 while (!in.atEnd()) {
235 int row;
239 int row;
236 int column;
240 int column;
237 QString value;
241 QString value;
238 in >> row >> column >> value;
242 in >> row >> column >> value;
239 QStandardItem *item = new QStandardItem();
243 QStandardItem *item = new QStandardItem();
240 bool ok;
244 bool ok;
241 double result = value.toDouble(&ok);
245 double result = value.toDouble(&ok);
242 if(ok)
246 if(ok)
243 item->setData(result, Qt::EditRole);
247 item->setData(result, Qt::EditRole);
244 else
248 else
245 item->setData(value, Qt::EditRole);
249 item->setData(value, Qt::EditRole);
246 m_model->setItem(row, column, item);
250 m_model->setItem(row, column, item);
247 }
251 }
248
252
249 return true;
253 return true;
250 }
254 }
251
255
252 void Engine::setupXYSeries(QXYSeries *xyseries, const QList<int>& columns, int column, int minRow, int maxRow)
256 void Engine::setupXYSeries(QXYSeries *xyseries, const QList<int>& columns, int column, int minRow, int maxRow)
253 {
257 {
254 xyseries->setModel(m_model);
258 xyseries->setModel(m_model);
255 xyseries->setModelMapping(columns.first(), columns.at(column), Qt::Vertical);
259 QXYModelMapper* mapper = new QXYModelMapper(xyseries);
256 xyseries->setModelMappingRange(minRow, maxRow - minRow + 1);
260 xyseries->setModelMapper(mapper);
261 mapper->setMapX(columns.first());
262 mapper->setMapY(columns.at(column));
263 mapper->setOrientation(Qt::Vertical);
264 mapper->setFirst(minRow);
265 mapper->setCount(maxRow - minRow + 1);
257 m_chart->addSeries(xyseries);
266 m_chart->addSeries(xyseries);
258 xyseries->setName(QString("Series %1").arg(m_chart->series().count()));
267 xyseries->setName(QString("Series %1").arg(m_chart->series().count()));
259 QObject::connect(xyseries,SIGNAL(clicked(const QPointF&)),this,SIGNAL(selected()));
268 QObject::connect(xyseries,SIGNAL(clicked(const QPointF&)),this,SIGNAL(selected()));
260 const QModelIndexList& list = m_selection->selectedIndexes();
269 const QModelIndexList& list = m_selection->selectedIndexes();
261 QModelIndexList result;
270 QModelIndexList result;
262 foreach(const QModelIndex& index, list) {
271 foreach(const QModelIndex& index, list) {
263 if (index.column() ==columns.at(column)){
272 if (index.column() ==columns.at(column)){
264 m_model->setData(index, xyseries->pen().color(), Qt::BackgroundRole);
273 m_model->setData(index, xyseries->pen().color(), Qt::BackgroundRole);
265 result << index;
274 result << index;
266 }
275 }
267 }
276 }
268 m_seriesModelIndex.insert(xyseries,result);
277 m_seriesModelIndex.insert(xyseries,result);
269 }
278 }
270
279
271 void Engine::setupBarSeries(QBarSeries *bar, const QList<int>& columns, int minRow, int maxRow)
280 void Engine::setupBarSeries(QBarSeries *bar, const QList<int>& columns, int minRow, int maxRow)
272 {
281 {
273 bar->setModel(m_model);
282 bar->setModel(m_model);
274 bar->setModelMapping(columns.first(), columns.at(1), columns.last(), Qt::Vertical);
283 QBarModelMapper* mapper = new QBarModelMapper(bar);
275 bar->setModelMappingRange(minRow, maxRow - minRow + 1);
284 bar->setModelMapper(mapper);
285 mapper->setMapCategories(columns.first());
286 mapper->setMapBarTop(columns.last());
287 mapper->setMapBarBottom(columns.at(1));
288 mapper->setOrientation(Qt::Vertical);
289 mapper->setFirst(minRow);
290 mapper->setCount(maxRow - minRow + 1);
276 m_chart->addSeries(bar);
291 m_chart->addSeries(bar);
277 bar->setName(QString("Series %1").arg(m_chart->series().count()));
292 bar->setName(QString("Series %1").arg(m_chart->series().count()));
278
293
279 const QModelIndexList& list = m_selection->selectedIndexes();
294 const QModelIndexList& list = m_selection->selectedIndexes();
280 foreach(const QModelIndex& index, list) {
295 foreach(const QModelIndex& index, list) {
281 if (index.column() >= columns.at(1) && index.column()<= columns.last()) {
296 if (index.column() >= columns.at(1) && index.column()<= columns.last()) {
282 //m_model->setData(index, bar->barSets().at(index.column())->brush().color(), Qt::BackgroundRole);
297 //m_model->setData(index, bar->barSets().at(index.column())->brush().color(), Qt::BackgroundRole);
283 }
298 }
284 }
299 }
285 }
300 }
286
301
287 void Engine::setupPieSeries(QPieSeries *pie, const QList<int>& columns, int minRow, int maxRow)
302 void Engine::setupPieSeries(QPieSeries *pie, const QList<int>& columns, int minRow, int maxRow)
288 {
303 {
289 pie->setModel(m_model);
304 pie->setModel(m_model);
290 pie->setModelMapping(columns.at(1),columns.first() ,Qt::Vertical);
305 QPieModelMapper* mapper = new QPieModelMapper(pie);
291 pie->setModelMappingRange(minRow, maxRow - minRow + 1);
306 pie->setModelMapper(mapper);
307 mapper->setMapValues(columns.at(1));
308 mapper->setMapLabels(columns.first());
309 mapper->setOrientation(Qt::Vertical);
310 mapper->setFirst(minRow);
311 mapper->setCount(maxRow - minRow + 1);
292 m_chart->addSeries(pie);
312 m_chart->addSeries(pie);
293 pie->setName(QString("Series %1").arg(m_chart->series().count()));
313 pie->setName(QString("Series %1").arg(m_chart->series().count()));
294
314
295 const QModelIndexList& list = m_selection->selectedIndexes();
315 const QModelIndexList& list = m_selection->selectedIndexes();
296 foreach(const QModelIndex& index, list) {
316 foreach(const QModelIndex& index, list) {
297 // m_model->setData(index, bar->barSets()pen().color(), Qt::BackgroundRole);
317 // m_model->setData(index, bar->barSets()pen().color(), Qt::BackgroundRole);
298 }
318 }
299 }
319 }
300
320
301 void Engine::setupAreaSeries(QAreaSeries *series, const QList<int>& columns, int minRow, int maxRow)
321 void Engine::setupAreaSeries(QAreaSeries *series, const QList<int>& columns, int minRow, int maxRow)
302 {
322 {
303 series->lowerSeries()->setModel(m_model);
323 series->lowerSeries()->setModel(m_model);
304 series->upperSeries()->setModel(m_model);
324 series->upperSeries()->setModel(m_model);
305 series->upperSeries()->setModelMapping(columns.first(), columns.at(1), Qt::Vertical);
325 QXYModelMapper* umapper = new QXYModelMapper(series);
306 series->lowerSeries()->setModelMapping(columns.first(), columns.at(2), Qt::Vertical);
326 umapper->setMapX(columns.first());
307 series->upperSeries()->setModelMappingRange(minRow, maxRow - minRow + 1);
327 umapper->setMapY(columns.at(1));
308 series->lowerSeries()->setModelMappingRange(minRow, maxRow - minRow + 1);
328 umapper->setOrientation(Qt::Vertical);
329 umapper->setFirst(minRow);
330 umapper->setCount(maxRow - minRow + 1);
331 QXYModelMapper* lmapper = new QXYModelMapper(series);
332 lmapper->setMapX(columns.first());
333 lmapper->setMapY(columns.at(2));
334 lmapper->setOrientation(Qt::Vertical);
335 lmapper->setFirst(minRow);
336 lmapper->setCount(maxRow - minRow + 1);
337 series->upperSeries()->setModelMapper(umapper);
338 series->lowerSeries()->setModelMapper(lmapper);
309 m_chart->addSeries(series);
339 m_chart->addSeries(series);
310 series->setName(QString("Series %1").arg(m_chart->series().count()));
340 series->setName(QString("Series %1").arg(m_chart->series().count()));
311
341
312 const QModelIndexList& list = m_selection->selectedIndexes();
342 const QModelIndexList& list = m_selection->selectedIndexes();
313 foreach(const QModelIndex& index, list) {
343 foreach(const QModelIndex& index, list) {
314 //if (index.column() ==columns.at(column))
344 //if (index.column() ==columns.at(column))
315 // m_model->setData(index, xyseries->pen().color(), Qt::BackgroundRole);
345 // m_model->setData(index, xyseries->pen().color(), Qt::BackgroundRole);
316 }
346 }
317 }
347 }
General Comments 0
You need to be logged in to leave comments. Login now