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