@@ -1,439 +1,459 | |||
|
1 | /**************************************************************************** | |
|
2 | ** | |
|
3 | ** Copyright (C) 2012 Digia Plc | |
|
4 | ** All rights reserved. | |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
|
6 | ** | |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
|
8 | ** | |
|
9 | ** $QT_BEGIN_LICENSE$ | |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
|
13 | ** a written agreement between you and Digia. | |
|
14 | ** | |
|
15 | ** If you have questions regarding the use of this file, please use | |
|
16 | ** contact form at http://qt.digia.com | |
|
17 | ** $QT_END_LICENSE$ | |
|
18 | ** | |
|
19 | ****************************************************************************/ | |
|
20 | ||
|
1 | 21 | #include "qchart.h" |
|
2 | 22 | #include "qchart_p.h" |
|
3 | 23 | #include <QGraphicsScene> |
|
4 | 24 | #include <QGraphicsSceneResizeEvent> |
|
5 | 25 | |
|
6 | 26 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | 27 | |
|
8 | 28 | /*! |
|
9 | 29 | \enum QChart::ChartTheme |
|
10 | 30 | |
|
11 | 31 | This enum describes the theme used by the chart. |
|
12 | 32 | |
|
13 | 33 | \value ChartThemeDefault Follows the GUI style of the Operating System |
|
14 | 34 | \value ChartThemeLight |
|
15 | 35 | \value ChartThemeBlueCerulean |
|
16 | 36 | \value ChartThemeDark |
|
17 | 37 | \value ChartThemeBrownSand |
|
18 | 38 | \value ChartThemeBlueNcs |
|
19 | 39 | \value ChartThemeIcy |
|
20 | 40 | \value ChartThemeScientific |
|
21 | 41 | \value ChartThemeCount Not really a theme; the total count of themes. |
|
22 | 42 | */ |
|
23 | 43 | |
|
24 | 44 | /*! |
|
25 | 45 | \enum QChart::AnimationOption |
|
26 | 46 | |
|
27 | 47 | For enabling/disabling animations. Defaults to NoAnimation. |
|
28 | 48 | |
|
29 | 49 | \value NoAnimation |
|
30 | 50 | \value GridAxisAnimations |
|
31 | 51 | \value SeriesAnimations |
|
32 | 52 | \value AllAnimations |
|
33 | 53 | */ |
|
34 | 54 | |
|
35 | 55 | /*! |
|
36 | 56 | \class QChart |
|
37 | 57 | \brief QtCommercial chart API. |
|
38 | 58 | |
|
39 | 59 | QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical |
|
40 | 60 | representation of different types of QChartSeries and other chart related objects like |
|
41 | 61 | QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the |
|
42 | 62 | convenience class QChartView instead of QChart. |
|
43 | 63 | \sa QChartView |
|
44 | 64 | */ |
|
45 | 65 | |
|
46 | 66 | /*! |
|
47 | 67 | Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor. |
|
48 | 68 | */ |
|
49 | 69 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), |
|
50 | 70 | d_ptr(new QChartPrivate(this)) |
|
51 | 71 | { |
|
52 | 72 | d_ptr->m_legend = new QLegend(this); |
|
53 | 73 | d_ptr->m_dataset = new ChartDataSet(this); |
|
54 | 74 | d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset); |
|
55 | 75 | |
|
56 | 76 | connect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); |
|
57 | 77 | connect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*))); |
|
58 | 78 | } |
|
59 | 79 | |
|
60 | 80 | /*! |
|
61 | 81 | Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. |
|
62 | 82 | */ |
|
63 | 83 | QChart::~QChart() |
|
64 | 84 | { |
|
65 | 85 | //delete first presenter , since this is a root of all the graphical items |
|
66 | 86 | delete d_ptr->m_presenter; |
|
67 | 87 | d_ptr->m_presenter=0; |
|
68 | 88 | } |
|
69 | 89 | |
|
70 | 90 | /*! |
|
71 | 91 | Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects. |
|
72 | 92 | If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and |
|
73 | 93 | the y axis). |
|
74 | 94 | */ |
|
75 | 95 | void QChart::addSeries(QSeries* series, QChartAxis* axisY) |
|
76 | 96 | { |
|
77 | 97 | d_ptr->m_dataset->addSeries(series, axisY); |
|
78 | 98 | } |
|
79 | 99 | |
|
80 | 100 | /*! |
|
81 | 101 | Removes the \a series specified in a perameter from the QChartView. |
|
82 | 102 | It releses its ownership of the specified QChartSeries object. |
|
83 | 103 | It does not delete the pointed QChartSeries data object |
|
84 | 104 | \sa addSeries(), removeAllSeries() |
|
85 | 105 | */ |
|
86 | 106 | void QChart::removeSeries(QSeries* series) |
|
87 | 107 | { |
|
88 | 108 | d_ptr->m_dataset->removeSeries(series); |
|
89 | 109 | } |
|
90 | 110 | |
|
91 | 111 | /*! |
|
92 | 112 | Removes all the QChartSeries that have been added to the QChartView |
|
93 | 113 | It also deletes the pointed QChartSeries data objects |
|
94 | 114 | \sa addSeries(), removeSeries() |
|
95 | 115 | */ |
|
96 | 116 | void QChart::removeAllSeries() |
|
97 | 117 | { |
|
98 | 118 | d_ptr->m_dataset->removeAllSeries(); |
|
99 | 119 | } |
|
100 | 120 | |
|
101 | 121 | /*! |
|
102 | 122 | Sets the \a brush that is used for painting the background of the chart area. |
|
103 | 123 | */ |
|
104 | 124 | void QChart::setBackgroundBrush(const QBrush& brush) |
|
105 | 125 | { |
|
106 | 126 | d_ptr->createChartBackgroundItem(); |
|
107 | 127 | d_ptr->m_backgroundItem->setBrush(brush); |
|
108 | 128 | d_ptr->m_backgroundItem->update(); |
|
109 | 129 | } |
|
110 | 130 | |
|
111 | 131 | QBrush QChart::backgroundBrush() const |
|
112 | 132 | { |
|
113 | 133 | if (!d_ptr->m_backgroundItem) return QBrush(); |
|
114 | 134 | return (d_ptr->m_backgroundItem)->brush(); |
|
115 | 135 | } |
|
116 | 136 | |
|
117 | 137 | /*! |
|
118 | 138 | Sets the \a pen that is used for painting the background of the chart area. |
|
119 | 139 | */ |
|
120 | 140 | void QChart::setBackgroundPen(const QPen& pen) |
|
121 | 141 | { |
|
122 | 142 | d_ptr->createChartBackgroundItem(); |
|
123 | 143 | d_ptr->m_backgroundItem->setPen(pen); |
|
124 | 144 | d_ptr->m_backgroundItem->update(); |
|
125 | 145 | } |
|
126 | 146 | |
|
127 | 147 | QPen QChart::backgroundPen() const |
|
128 | 148 | { |
|
129 | 149 | if (!d_ptr->m_backgroundItem) return QPen(); |
|
130 | 150 | return d_ptr->m_backgroundItem->pen(); |
|
131 | 151 | } |
|
132 | 152 | |
|
133 | 153 | /*! |
|
134 | 154 | Sets the chart \a title. The description text that is drawn above the chart. |
|
135 | 155 | */ |
|
136 | 156 | void QChart::setTitle(const QString& title) |
|
137 | 157 | { |
|
138 | 158 | d_ptr->createChartTitleItem(); |
|
139 | 159 | d_ptr->m_titleItem->setText(title); |
|
140 | 160 | d_ptr->updateLayout(); |
|
141 | 161 | } |
|
142 | 162 | |
|
143 | 163 | /*! |
|
144 | 164 | Returns the chart title. The description text that is drawn above the chart. |
|
145 | 165 | */ |
|
146 | 166 | QString QChart::title() const |
|
147 | 167 | { |
|
148 | 168 | if (d_ptr->m_titleItem) |
|
149 | 169 | return d_ptr->m_titleItem->text(); |
|
150 | 170 | else |
|
151 | 171 | return QString(); |
|
152 | 172 | } |
|
153 | 173 | |
|
154 | 174 | /*! |
|
155 | 175 | Sets the \a font that is used for rendering the description text that is rendered above the chart. |
|
156 | 176 | */ |
|
157 | 177 | void QChart::setTitleFont(const QFont& font) |
|
158 | 178 | { |
|
159 | 179 | d_ptr->createChartTitleItem(); |
|
160 | 180 | d_ptr->m_titleItem->setFont(font); |
|
161 | 181 | d_ptr->updateLayout(); |
|
162 | 182 | } |
|
163 | 183 | |
|
164 | 184 | /*! |
|
165 | 185 | Sets the \a brush used for rendering the title text. |
|
166 | 186 | */ |
|
167 | 187 | void QChart::setTitleBrush(const QBrush &brush) |
|
168 | 188 | { |
|
169 | 189 | d_ptr->createChartTitleItem(); |
|
170 | 190 | d_ptr->m_titleItem->setBrush(brush); |
|
171 | 191 | d_ptr->updateLayout(); |
|
172 | 192 | } |
|
173 | 193 | |
|
174 | 194 | /*! |
|
175 | 195 | Returns the brush used for rendering the title text. |
|
176 | 196 | */ |
|
177 | 197 | QBrush QChart::titleBrush() const |
|
178 | 198 | { |
|
179 | 199 | if (!d_ptr->m_titleItem) return QBrush(); |
|
180 | 200 | return d_ptr->m_titleItem->brush(); |
|
181 | 201 | } |
|
182 | 202 | |
|
183 | 203 | /*! |
|
184 | 204 | Sets the \a theme used by the chart for rendering the graphical representation of the data |
|
185 | 205 | \sa ChartTheme, chartTheme() |
|
186 | 206 | */ |
|
187 | 207 | void QChart::setTheme(QChart::ChartTheme theme) |
|
188 | 208 | { |
|
189 | 209 | d_ptr->m_presenter->setTheme(theme); |
|
190 | 210 | } |
|
191 | 211 | |
|
192 | 212 | /*! |
|
193 | 213 | Returns the theme enum used by the chart. |
|
194 | 214 | \sa ChartTheme, setChartTheme() |
|
195 | 215 | */ |
|
196 | 216 | QChart::ChartTheme QChart::theme() const |
|
197 | 217 | { |
|
198 | 218 | return d_ptr->m_presenter->theme(); |
|
199 | 219 | } |
|
200 | 220 | |
|
201 | 221 | /*! |
|
202 | 222 | Zooms in the view by a factor of 2 |
|
203 | 223 | */ |
|
204 | 224 | void QChart::zoomIn() |
|
205 | 225 | { |
|
206 | 226 | d_ptr->m_presenter->zoomIn(); |
|
207 | 227 | } |
|
208 | 228 | |
|
209 | 229 | /*! |
|
210 | 230 | Zooms in the view to a maximum level at which \a rect is still fully visible. |
|
211 | 231 | */ |
|
212 | 232 | void QChart::zoomIn(const QRectF& rect) |
|
213 | 233 | { |
|
214 | 234 | if (!rect.isValid()) return; |
|
215 | 235 | d_ptr->m_presenter->zoomIn(rect); |
|
216 | 236 | } |
|
217 | 237 | |
|
218 | 238 | /*! |
|
219 | 239 | Restores the view zoom level to the previous one. |
|
220 | 240 | */ |
|
221 | 241 | void QChart::zoomOut() |
|
222 | 242 | { |
|
223 | 243 | d_ptr->m_presenter->zoomOut(); |
|
224 | 244 | } |
|
225 | 245 | |
|
226 | 246 | /*! |
|
227 | 247 | Returns the pointer to the x axis object of the chart |
|
228 | 248 | */ |
|
229 | 249 | QChartAxis* QChart::axisX() const |
|
230 | 250 | { |
|
231 | 251 | return d_ptr->m_dataset->axisX(); |
|
232 | 252 | } |
|
233 | 253 | |
|
234 | 254 | /*! |
|
235 | 255 | Returns the pointer to the y axis object of the chart |
|
236 | 256 | */ |
|
237 | 257 | QChartAxis* QChart::axisY() const |
|
238 | 258 | { |
|
239 | 259 | return d_ptr->m_dataset->axisY(); |
|
240 | 260 | } |
|
241 | 261 | |
|
242 | 262 | /*! |
|
243 | 263 | Returns the legend object of the chart. Ownership stays in chart. |
|
244 | 264 | */ |
|
245 | 265 | QLegend& QChart::legend() const |
|
246 | 266 | { |
|
247 | 267 | return *d_ptr->m_legend; |
|
248 | 268 | } |
|
249 | 269 | |
|
250 | 270 | /*! |
|
251 | 271 | Gives ownership of legend to user. |
|
252 | 272 | */ |
|
253 | 273 | QLegend* QChart::takeLegend() |
|
254 | 274 | { |
|
255 | 275 | QLegend* l = d_ptr->m_legend; |
|
256 | 276 | d_ptr->m_legend = 0; |
|
257 | 277 | return l; |
|
258 | 278 | } |
|
259 | 279 | |
|
260 | 280 | /*! |
|
261 | 281 | Gives ownership of legend back to chart. QChart takes ownership of \a legend and deletes existing one |
|
262 | 282 | */ |
|
263 | 283 | void QChart::giveLegend(QLegend *legend) |
|
264 | 284 | { |
|
265 | 285 | if (d_ptr->m_legend) { |
|
266 | 286 | // Should not happen. |
|
267 | 287 | qDebug() << "Warning! Giving more than one legend to chart."; |
|
268 | 288 | delete d_ptr->m_legend; |
|
269 | 289 | } |
|
270 | 290 | |
|
271 | 291 | d_ptr->m_legend = legend; |
|
272 | 292 | |
|
273 | 293 | // Reconnect legend, in case not already connected. |
|
274 | 294 | disconnect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); |
|
275 | 295 | disconnect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*))); |
|
276 | 296 | connect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); |
|
277 | 297 | connect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*))); |
|
278 | 298 | } |
|
279 | 299 | |
|
280 | 300 | /*! |
|
281 | 301 | Resizes and updates the chart area using the \a event data |
|
282 | 302 | */ |
|
283 | 303 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
284 | 304 | { |
|
285 | 305 | d_ptr->m_rect = QRectF(QPoint(0,0),event->newSize()); |
|
286 | 306 | d_ptr->updateLayout(); |
|
287 | 307 | QGraphicsWidget::resizeEvent(event); |
|
288 | 308 | update(); |
|
289 | 309 | } |
|
290 | 310 | |
|
291 | 311 | /*! |
|
292 | 312 | Sets animation \a options for the chart |
|
293 | 313 | */ |
|
294 | 314 | void QChart::setAnimationOptions(AnimationOptions options) |
|
295 | 315 | { |
|
296 | 316 | d_ptr->m_presenter->setAnimationOptions(options); |
|
297 | 317 | } |
|
298 | 318 | |
|
299 | 319 | /*! |
|
300 | 320 | Returns animation options for the chart |
|
301 | 321 | */ |
|
302 | 322 | QChart::AnimationOptions QChart::animationOptions() const |
|
303 | 323 | { |
|
304 | 324 | return d_ptr->m_presenter->animationOptions(); |
|
305 | 325 | } |
|
306 | 326 | |
|
307 | 327 | void QChart::scrollLeft() |
|
308 | 328 | { |
|
309 | 329 | d_ptr->m_presenter->scroll(-d_ptr->m_presenter->geometry().width()/(axisX()->ticksCount()-1),0); |
|
310 | 330 | } |
|
311 | 331 | |
|
312 | 332 | void QChart::scrollRight() |
|
313 | 333 | { |
|
314 | 334 | d_ptr->m_presenter->scroll(d_ptr->m_presenter->geometry().width()/(axisX()->ticksCount()-1),0); |
|
315 | 335 | } |
|
316 | 336 | |
|
317 | 337 | void QChart::scrollUp() |
|
318 | 338 | { |
|
319 | 339 | d_ptr->m_presenter->scroll(0,d_ptr->m_presenter->geometry().width()/(axisY()->ticksCount()-1)); |
|
320 | 340 | } |
|
321 | 341 | |
|
322 | 342 | void QChart::scrollDown() |
|
323 | 343 | { |
|
324 | 344 | d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->geometry().width()/(axisY()->ticksCount()-1)); |
|
325 | 345 | } |
|
326 | 346 | |
|
327 | 347 | void QChart::setBackgroundVisible(bool visible) |
|
328 | 348 | { |
|
329 | 349 | d_ptr->createChartBackgroundItem(); |
|
330 | 350 | d_ptr->m_backgroundItem->setVisible(visible); |
|
331 | 351 | } |
|
332 | 352 | |
|
333 | 353 | bool QChart::isBackgroundVisible() const |
|
334 | 354 | { |
|
335 | 355 | if (!d_ptr->m_backgroundItem) return false; |
|
336 | 356 | return d_ptr->m_backgroundItem->isVisible(); |
|
337 | 357 | } |
|
338 | 358 | |
|
339 | 359 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
340 | 360 | |
|
341 | 361 | QChartPrivate::QChartPrivate(QChart *parent): |
|
342 | 362 | q_ptr(parent), |
|
343 | 363 | m_backgroundItem(0), |
|
344 | 364 | m_titleItem(0), |
|
345 | 365 | m_legend(0), |
|
346 | 366 | m_dataset(0), |
|
347 | 367 | m_presenter(0) |
|
348 | 368 | { |
|
349 | 369 | |
|
350 | 370 | } |
|
351 | 371 | |
|
352 | 372 | QChartPrivate::~QChartPrivate() |
|
353 | 373 | { |
|
354 | 374 | |
|
355 | 375 | } |
|
356 | 376 | |
|
357 | 377 | void QChartPrivate::createChartBackgroundItem() |
|
358 | 378 | { |
|
359 | 379 | if (!m_backgroundItem) { |
|
360 | 380 | m_backgroundItem = new ChartBackground(q_ptr); |
|
361 | 381 | m_backgroundItem->setPen(Qt::NoPen); |
|
362 | 382 | m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); |
|
363 | 383 | } |
|
364 | 384 | } |
|
365 | 385 | |
|
366 | 386 | void QChartPrivate::createChartTitleItem() |
|
367 | 387 | { |
|
368 | 388 | if (!m_titleItem) { |
|
369 | 389 | m_titleItem = new QGraphicsSimpleTextItem(q_ptr); |
|
370 | 390 | m_titleItem->setZValue(ChartPresenter::BackgroundZValue); |
|
371 | 391 | } |
|
372 | 392 | } |
|
373 | 393 | |
|
374 | 394 | void QChartPrivate::updateLegendLayout() |
|
375 | 395 | { |
|
376 | 396 | int padding = m_presenter->padding(); |
|
377 | 397 | QRectF plotRect = m_rect.adjusted(padding,padding,-padding,-padding); |
|
378 | 398 | QRectF legendRect; |
|
379 | 399 | |
|
380 | 400 | switch (m_legend->preferredLayout()) |
|
381 | 401 | { |
|
382 | 402 | case QLegend::PreferredLayoutTop: { |
|
383 | 403 | // legendRect = plotRect.adjusted(m_padding,0,-m_padding,-m_padding - plotRect.height()); |
|
384 | 404 | legendRect = plotRect.adjusted(0,0,0,-padding - plotRect.height()); |
|
385 | 405 | break; |
|
386 | 406 | } |
|
387 | 407 | case QLegend::PreferredLayoutBottom: { |
|
388 | 408 | legendRect = plotRect.adjusted(padding,padding + plotRect.height(),-padding,0); |
|
389 | 409 | break; |
|
390 | 410 | } |
|
391 | 411 | case QLegend::PreferredLayoutLeft: { |
|
392 | 412 | legendRect = plotRect.adjusted(0,padding,-padding - plotRect.width(),-padding); |
|
393 | 413 | break; |
|
394 | 414 | } |
|
395 | 415 | case QLegend::PreferredLayoutRight: { |
|
396 | 416 | legendRect = plotRect.adjusted(padding + plotRect.width(),padding,0,-padding); |
|
397 | 417 | break; |
|
398 | 418 | } |
|
399 | 419 | default: { |
|
400 | 420 | legendRect = plotRect; |
|
401 | 421 | break; |
|
402 | 422 | } |
|
403 | 423 | } |
|
404 | 424 | |
|
405 | 425 | m_legend->setMaximumSize(legendRect.size()); |
|
406 | 426 | m_legend->setPos(legendRect.topLeft()); |
|
407 | 427 | } |
|
408 | 428 | |
|
409 | 429 | void QChartPrivate::updateLayout() |
|
410 | 430 | { |
|
411 | 431 | if (!m_rect.isValid()) return; |
|
412 | 432 | |
|
413 | 433 | int padding = m_presenter->padding(); |
|
414 | 434 | int backgroundPadding = m_presenter->backgroundPadding(); |
|
415 | 435 | |
|
416 | 436 | QRectF rect = m_rect.adjusted(padding,padding,-padding,-padding); |
|
417 | 437 | |
|
418 | 438 | // recalculate title position |
|
419 | 439 | if (m_titleItem) { |
|
420 | 440 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); |
|
421 | 441 | m_titleItem->setPos(center.x(),m_rect.top()/2 + padding/2); |
|
422 | 442 | } |
|
423 | 443 | |
|
424 | 444 | //recalculate background gradient |
|
425 | 445 | if (m_backgroundItem) { |
|
426 | 446 | m_backgroundItem->setRect(m_rect.adjusted(backgroundPadding,backgroundPadding, -backgroundPadding, -backgroundPadding)); |
|
427 | 447 | } |
|
428 | 448 | |
|
429 | 449 | // recalculate legend position |
|
430 | 450 | if (m_legend) { |
|
431 | 451 | if (m_legend->parentObject() == q_ptr) { |
|
432 | 452 | updateLegendLayout(); |
|
433 | 453 | } |
|
434 | 454 | } |
|
435 | 455 | } |
|
436 | 456 | |
|
437 | 457 | #include "moc_qchart.cpp" |
|
438 | 458 | |
|
439 | 459 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,117 +1,117 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #ifndef QCHART_H |
|
22 | 22 | #define QCHART_H |
|
23 | 23 | |
|
24 | 24 | #include <QSeries> |
|
25 | 25 | #include <QGraphicsWidget> |
|
26 | 26 | |
|
27 | 27 | class QGraphicsSceneResizeEvent; |
|
28 | 28 | |
|
29 | 29 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
30 | 30 | |
|
31 | 31 | class QSeries; |
|
32 | 32 | class QChartAxis; |
|
33 | 33 | class QLegend; |
|
34 | 34 | class QChartPrivate; |
|
35 | 35 | |
|
36 | 36 | class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget |
|
37 | 37 | { |
|
38 | 38 | Q_OBJECT |
|
39 | 39 | public: |
|
40 | 40 | enum ChartTheme { |
|
41 | 41 | ChartThemeDefault, |
|
42 | 42 | ChartThemeLight, |
|
43 | 43 | ChartThemeBlueCerulean, |
|
44 | 44 | ChartThemeDark, |
|
45 | 45 | ChartThemeBrownSand, |
|
46 | 46 | ChartThemeBlueNcs, |
|
47 | 47 | ChartThemeIcy, |
|
48 | 48 | ChartThemeScientific, |
|
49 | 49 | ChartThemeCount |
|
50 | 50 | }; |
|
51 | 51 | |
|
52 | 52 | enum AnimationOption { |
|
53 | 53 | NoAnimation = 0x0, |
|
54 | 54 | GridAxisAnimations = 0x1, |
|
55 | 55 | SeriesAnimations =0x2, |
|
56 | 56 | AllAnimations = 0x3 |
|
57 | 57 | }; |
|
58 | 58 | |
|
59 | 59 | Q_DECLARE_FLAGS(AnimationOptions, AnimationOption) |
|
60 | 60 | |
|
61 | 61 | public: |
|
62 | QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0); | |
|
62 | explicit QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0); | |
|
63 | 63 | ~QChart(); |
|
64 | 64 | |
|
65 | 65 | void addSeries(QSeries *series, QChartAxis *axisY = 0); |
|
66 | 66 | void removeSeries(QSeries *series); |
|
67 | 67 | void removeAllSeries(); |
|
68 | 68 | |
|
69 | 69 | void setTheme(QChart::ChartTheme theme); |
|
70 | 70 | QChart::ChartTheme theme() const; |
|
71 | 71 | |
|
72 | 72 | void setTitle(const QString& title); |
|
73 | 73 | QString title() const; |
|
74 | 74 | void setTitleFont(const QFont& font); |
|
75 | 75 | QFont titleFont() const; |
|
76 | 76 | void setTitleBrush(const QBrush &brush); |
|
77 | 77 | QBrush titleBrush() const; |
|
78 | 78 | void setBackgroundBrush(const QBrush& brush); |
|
79 | 79 | QBrush backgroundBrush() const; |
|
80 | 80 | void setBackgroundPen(const QPen& pen); |
|
81 | 81 | QPen backgroundPen() const; |
|
82 | 82 | |
|
83 | 83 | void setBackgroundVisible(bool visible); |
|
84 | 84 | bool isBackgroundVisible() const; |
|
85 | 85 | |
|
86 | 86 | void setAnimationOptions(AnimationOptions options); |
|
87 | 87 | AnimationOptions animationOptions() const; |
|
88 | 88 | |
|
89 | 89 | void zoomIn(); |
|
90 | 90 | void zoomIn(const QRectF& rect); |
|
91 | 91 | void zoomOut(); |
|
92 | 92 | void scrollLeft(); |
|
93 | 93 | void scrollRight(); |
|
94 | 94 | void scrollUp(); |
|
95 | 95 | void scrollDown(); |
|
96 | 96 | |
|
97 | 97 | QChartAxis* axisX() const; |
|
98 | 98 | QChartAxis* axisY() const; |
|
99 | 99 | |
|
100 | 100 | QLegend& legend() const; |
|
101 | 101 | QLegend* takeLegend(); |
|
102 | 102 | void giveLegend(QLegend* legend); |
|
103 | 103 | |
|
104 | 104 | protected: |
|
105 | 105 | void resizeEvent(QGraphicsSceneResizeEvent *event); |
|
106 | 106 | |
|
107 | 107 | protected: |
|
108 | 108 | QScopedPointer<QChartPrivate> d_ptr; |
|
109 | 109 | friend class QChartView; |
|
110 | 110 | Q_DISABLE_COPY(QChart); |
|
111 | 111 | }; |
|
112 | 112 | |
|
113 | 113 | QTCOMMERCIALCHART_END_NAMESPACE |
|
114 | 114 | |
|
115 | 115 | Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions) |
|
116 | 116 | |
|
117 | 117 | #endif |
@@ -1,38 +1,43 | |||
|
1 | // W A R N I N G | |
|
2 | // ------------- | |
|
3 | // | |
|
4 | // This file is not part of the QtCommercial Chart API. It exists purely as an | |
|
5 | // implementation detail. This header file may change from version to | |
|
6 | // version without notice, or even be removed. | |
|
7 | // | |
|
8 | // We mean it. | |
|
9 | ||
|
1 | 10 | #ifndef QCHART_P_H |
|
2 | 11 | #define QCHART_P_H |
|
3 | 12 | |
|
4 | #include "private/qgraphicswidget_p.h" | |
|
5 | 13 | #include "qchartaxis.h" |
|
6 | 14 | #include "qlegend.h" |
|
7 | 15 | #include "chartpresenter_p.h" |
|
8 | 16 | #include "chartdataset_p.h" |
|
9 | 17 | #include "chartbackground_p.h" |
|
10 | 18 | |
|
11 | 19 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
12 | 20 | |
|
13 | 21 | class QChart; |
|
14 | 22 | |
|
15 |
|
|
|
23 | struct QChartPrivate | |
|
16 | 24 | { |
|
17 | public: | |
|
18 | 25 | QChartPrivate(QChart *parent); |
|
19 | 26 | ~QChartPrivate(); |
|
20 | 27 | |
|
21 | 28 | void createChartBackgroundItem(); |
|
22 | 29 | void createChartTitleItem(); |
|
23 | 30 | void updateLayout(); |
|
24 | 31 | void updateLegendLayout(); |
|
25 | 32 | |
|
26 | 33 | QChart *q_ptr; |
|
27 | 34 | ChartBackground* m_backgroundItem; |
|
28 | 35 | QGraphicsSimpleTextItem* m_titleItem; |
|
29 | 36 | QRectF m_rect; |
|
30 | 37 | QLegend* m_legend; |
|
31 | 38 | ChartDataSet *m_dataset; |
|
32 | 39 | ChartPresenter *m_presenter; |
|
33 | ||
|
34 | Q_DECLARE_PUBLIC(QChart); | |
|
35 | 40 | }; |
|
36 | 41 | |
|
37 | 42 | QTCOMMERCIALCHART_END_NAMESPACE |
|
38 | 43 | #endif |
@@ -1,416 +1,233 | |||
|
1 | /**************************************************************************** | |
|
2 | ** | |
|
3 | ** Copyright (C) 2012 Digia Plc | |
|
4 | ** All rights reserved. | |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
|
6 | ** | |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
|
8 | ** | |
|
9 | ** $QT_BEGIN_LICENSE$ | |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
|
13 | ** a written agreement between you and Digia. | |
|
14 | ** | |
|
15 | ** If you have questions regarding the use of this file, please use | |
|
16 | ** contact form at http://qt.digia.com | |
|
17 | ** $QT_END_LICENSE$ | |
|
18 | ** | |
|
19 | ****************************************************************************/ | |
|
20 | ||
|
1 | 21 | #include "qchartview.h" |
|
2 | #include "qchart.h" | |
|
3 | 22 | #include "qchart_p.h" |
|
4 |
#include "qchart |
|
|
5 | #include <QGraphicsView> | |
|
23 | #include "qchartview_p.h" | |
|
6 | 24 | #include <QGraphicsScene> |
|
7 | 25 | #include <QRubberBand> |
|
8 | #include <QResizeEvent> | |
|
26 | ||
|
9 | 27 | |
|
10 | 28 | /*! |
|
11 | 29 | \enum QChartView::RubberBandPolicy |
|
12 | 30 | |
|
13 | 31 | This enum describes the different types of rubber bands that can be used for zoom rect selection |
|
14 | 32 | |
|
15 | 33 | \value NoRubberBand |
|
16 | 34 | \value VerticalRubberBand |
|
17 | 35 | \value HorizonalRubberBand |
|
18 | 36 | \value RectangleRubberBand |
|
19 | 37 | */ |
|
20 | 38 | |
|
21 | 39 | /*! |
|
22 | 40 | \class QChartView |
|
23 | 41 | \brief Standalone charting widget. |
|
24 | 42 | |
|
25 | 43 | QChartView is a standalone widget that can display charts. It does not require separate |
|
26 | 44 | QGraphicsScene to work. It manages the graphical representation of different types of |
|
27 | 45 | QChartSeries and other chart related objects like QChartAxis and QChartLegend. If you want to |
|
28 | 46 | display a chart in your existing QGraphicsScene, you can use the QChart class instead. |
|
29 | 47 | |
|
30 | 48 | \sa QChart |
|
31 | 49 | */ |
|
32 | 50 | |
|
33 | 51 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
34 | 52 | |
|
35 | 53 | /*! |
|
36 | 54 | Constructs a chartView object which is a child of a\a parent. |
|
37 | 55 | */ |
|
38 | QChartView::QChartView(QWidget *parent) : | |
|
56 | QChartView::QChartView(QChart *chart,QWidget *parent) : | |
|
39 | 57 | QGraphicsView(parent), |
|
40 | m_scene(new QGraphicsScene(this)), | |
|
41 | m_chart(new QChart()), | |
|
42 | m_rubberBand(0), | |
|
43 | m_verticalRubberBand(false), | |
|
44 | m_horizonalRubberBand(false) | |
|
58 | d_ptr(new QChartViewPrivate()) | |
|
45 | 59 | { |
|
60 | d_ptr->m_scene = new QGraphicsScene(this); | |
|
61 | d_ptr->m_chart = chart; | |
|
62 | d_ptr->m_presenter = chart->d_ptr->m_presenter; | |
|
63 | ||
|
46 | 64 | setFrameShape(QFrame::NoFrame); |
|
47 | 65 | setBackgroundRole(QPalette::Window); |
|
48 | 66 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
49 | 67 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
50 | setScene(m_scene); | |
|
51 |
m_scene->addItem( |
|
|
68 | setScene(d_ptr->m_scene); | |
|
69 | d_ptr->m_scene->addItem(chart); | |
|
52 | 70 | setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
|
53 | 71 | } |
|
54 | 72 | |
|
55 | 73 | |
|
56 | 74 | /*! |
|
57 | 75 | Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. |
|
58 | 76 | */ |
|
59 | 77 | QChartView::~QChartView() |
|
60 | 78 | { |
|
61 | 79 | } |
|
62 | 80 | |
|
63 | /*! | |
|
64 | Resizes and updates the chart area using the \a event data | |
|
65 | */ | |
|
66 | void QChartView::resizeEvent(QResizeEvent *event) | |
|
67 | { | |
|
68 | m_chart->resize(size()); | |
|
69 | QGraphicsView::resizeEvent(event); | |
|
70 | } | |
|
71 | ||
|
72 | /*! | |
|
73 | Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects. | |
|
74 | If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and | |
|
75 | the y axis). | |
|
76 | \sa removeSeries(), removeAllSeries() | |
|
77 | */ | |
|
78 | void QChartView::addSeries(QSeries* series,QChartAxis *axisY) | |
|
79 | { | |
|
80 | m_chart->addSeries(series,axisY); | |
|
81 | } | |
|
82 | ||
|
83 | /*! | |
|
84 | Removes the \a series specified in a perameter from the QChartView. | |
|
85 | It releses its ownership of the specified QChartSeries object. | |
|
86 | It does not delete the pointed QChartSeries data object | |
|
87 | \sa addSeries(), removeAllSeries() | |
|
88 | */ | |
|
89 | void QChartView::removeSeries(QSeries* series) | |
|
90 | { | |
|
91 | m_chart->removeSeries(series); | |
|
92 | } | |
|
93 | ||
|
94 | /*! | |
|
95 | Removes all the QChartSeries that have been added to the QChartView | |
|
96 | It also deletes the pointed QChartSeries data objects | |
|
97 | \sa addSeries(), removeSeries() | |
|
98 | */ | |
|
99 | void QChartView::removeAllSeries() | |
|
100 | { | |
|
101 | m_chart->removeAllSeries(); | |
|
102 | } | |
|
103 | ||
|
104 | /*! | |
|
105 | Zooms in the view by a factor of 2 | |
|
106 | */ | |
|
107 | void QChartView::zoomIn() | |
|
108 | { | |
|
109 | m_chart->zoomIn(); | |
|
110 | } | |
|
111 | ||
|
112 | /*! | |
|
113 | Zooms in the view to a maximum level at which \a rect is still fully visible. | |
|
114 | */ | |
|
115 | void QChartView::zoomIn(const QRect& rect) | |
|
116 | { | |
|
117 | m_chart->zoomIn(rect); | |
|
118 | } | |
|
119 | ||
|
120 | /*! | |
|
121 | Restores the view zoom level to the previous one. | |
|
122 | */ | |
|
123 | void QChartView::zoomOut() | |
|
124 | { | |
|
125 | m_chart->zoomOut(); | |
|
126 | } | |
|
127 | ||
|
128 | /*! | |
|
129 | Sets the chart \a title. A description text that is drawn above the chart. | |
|
130 | */ | |
|
131 | void QChartView::setChartTitle(const QString& title) | |
|
132 | { | |
|
133 | m_chart->setTitle(title); | |
|
134 | } | |
|
135 | ||
|
136 | /*! | |
|
137 | Returns the chart's title. A description text that is drawn above the chart. | |
|
138 | */ | |
|
139 | QString QChartView::chartTitle() const | |
|
140 | { | |
|
141 | return m_chart->title(); | |
|
142 | } | |
|
143 | ||
|
144 | /*! | |
|
145 | Sets the \a font that is used for rendering the description text that is rendered above the chart. | |
|
146 | */ | |
|
147 | void QChartView::setChartTitleFont(const QFont& font) | |
|
148 | { | |
|
149 | m_chart->setTitleFont(font); | |
|
150 | } | |
|
151 | ||
|
152 | /*! | |
|
153 | Sets the \a brush used for rendering the title text. | |
|
154 | */ | |
|
155 | void QChartView::setChartTitleBrush(const QBrush &brush) | |
|
156 | { | |
|
157 | m_chart->setTitleBrush(brush); | |
|
158 | } | |
|
159 | ||
|
160 | /*! | |
|
161 | Returns the brush used for rendering the title text. | |
|
162 | */ | |
|
163 | QBrush QChartView::chartTitleBrush() | |
|
164 | { | |
|
165 | return m_chart->titleBrush(); | |
|
166 | } | |
|
167 | ||
|
168 | /*! | |
|
169 | Sets the \a brush that is used for painting the background of the chart area of the QChartView widget. | |
|
170 | */ | |
|
171 | void QChartView::setChartBackgroundBrush(const QBrush& brush) | |
|
172 | { | |
|
173 | m_chart->setBackgroundBrush(brush); | |
|
174 | } | |
|
175 | ||
|
176 | /*! | |
|
177 | Sets the \a pen that is used for painting the background of the chart area of the QChartView widget. | |
|
178 | */ | |
|
179 | void QChartView::setChartBackgroundPen(const QPen& pen) | |
|
81 | QChart* QChartView::chart() const | |
|
180 | 82 | { |
|
181 | m_chart->setBackgroundPen(pen); | |
|
83 | return d_ptr->m_chart; | |
|
182 | 84 | } |
|
183 | 85 | |
|
184 | 86 | /*! |
|
185 | 87 | Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed. |
|
186 | 88 | */ |
|
187 |
void QChartView::setRubberBand |
|
|
89 | void QChartView::setRubberBand(const RubberBands& rubberBand) | |
|
188 | 90 | { |
|
189 | switch(policy) { | |
|
190 | case VerticalRubberBand: | |
|
191 | m_verticalRubberBand = true; | |
|
192 | m_horizonalRubberBand = false; | |
|
193 | break; | |
|
194 | case HorizonalRubberBand: | |
|
195 | m_verticalRubberBand = false; | |
|
196 | m_horizonalRubberBand = true; | |
|
197 | break; | |
|
198 | case RectangleRubberBand: | |
|
199 | m_verticalRubberBand = true; | |
|
200 | m_horizonalRubberBand = true; | |
|
201 | break; | |
|
202 | case NoRubberBand: | |
|
203 | default: | |
|
204 | delete m_rubberBand; | |
|
205 | m_rubberBand=0; | |
|
206 | m_horizonalRubberBand = false; | |
|
207 | m_verticalRubberBand = false; | |
|
91 | d_ptr->m_rubberBandFlags=rubberBand; | |
|
92 | ||
|
93 | if (!d_ptr->m_rubberBandFlags) { | |
|
94 | delete d_ptr->m_rubberBand; | |
|
95 | d_ptr->m_rubberBand=0; | |
|
208 | 96 | return; |
|
209 | 97 | } |
|
210 | if(!m_rubberBand) { | |
|
211 | m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this); | |
|
212 | m_rubberBand->setEnabled(true); | |
|
98 | ||
|
99 | if (!d_ptr->m_rubberBand) { | |
|
100 | d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this); | |
|
101 | d_ptr->m_rubberBand->setEnabled(true); | |
|
213 | 102 | } |
|
214 | 103 | } |
|
215 | 104 | |
|
216 | 105 | /*! |
|
217 | 106 | Returns the RubberBandPolicy that is currently being used by the widget. |
|
218 | 107 | */ |
|
219 |
QChartView::RubberBand |
|
|
108 | QChartView::RubberBands QChartView::rubberBand() const | |
|
220 | 109 | { |
|
221 | if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand; | |
|
222 | if(m_horizonalRubberBand) return HorizonalRubberBand; | |
|
223 | if(m_verticalRubberBand) return VerticalRubberBand; | |
|
224 | return NoRubberBand; | |
|
110 | return d_ptr->m_rubberBandFlags; | |
|
225 | 111 | } |
|
226 | 112 | |
|
227 | 113 | /*! |
|
228 | 114 | If Left mouse button is pressed and the RubberBandPolicy is enabled the \a event is accepted and the rubber band is displayed on the screen allowing the user to select the zoom area. |
|
229 | 115 | If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation. |
|
230 | 116 | */ |
|
231 | 117 | void QChartView::mousePressEvent(QMouseEvent *event) |
|
232 | 118 | { |
|
233 | if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) { | |
|
119 | if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) { | |
|
234 | 120 | |
|
235 |
int padding = |
|
|
121 | int padding = d_ptr->m_presenter->padding(); | |
|
236 | 122 | QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding); |
|
237 | 123 | |
|
238 | 124 | if (rect.contains(event->pos())) { |
|
239 | m_rubberBandOrigin = event->pos(); | |
|
240 | m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize())); | |
|
241 | m_rubberBand->show(); | |
|
125 | d_ptr->m_rubberBandOrigin = event->pos(); | |
|
126 | d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize())); | |
|
127 | d_ptr->m_rubberBand->show(); | |
|
242 | 128 | event->accept(); |
|
243 | 129 | } |
|
244 | 130 | } |
|
245 | 131 | else { |
|
246 | 132 | QGraphicsView::mousePressEvent(event); |
|
247 | 133 | } |
|
248 | 134 | } |
|
249 | 135 | |
|
250 | 136 | /*! |
|
251 | 137 | If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry. |
|
252 | 138 | In other case the defualt QGraphicsView::mouseMoveEvent implementation is called. |
|
253 | 139 | */ |
|
254 | 140 | void QChartView::mouseMoveEvent(QMouseEvent *event) |
|
255 | 141 | { |
|
256 | if(m_rubberBand && m_rubberBand->isVisible()) { | |
|
257 |
int padding = |
|
|
142 | if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) { | |
|
143 | int padding = d_ptr->m_presenter->padding(); | |
|
258 | 144 | QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding); |
|
259 | int width = event->pos().x() - m_rubberBandOrigin.x(); | |
|
260 | int height = event->pos().y() - m_rubberBandOrigin.y(); | |
|
261 |
if(! |
|
|
262 | m_rubberBandOrigin.setY(rect.top()); | |
|
145 | int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x(); | |
|
146 | int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y(); | |
|
147 | if (!d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand)) { | |
|
148 | d_ptr->m_rubberBandOrigin.setY(rect.top()); | |
|
263 | 149 | height = rect.height(); |
|
264 | 150 | } |
|
265 |
if(! |
|
|
266 | m_rubberBandOrigin.setX(rect.left()); | |
|
151 | if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) { | |
|
152 | d_ptr->m_rubberBandOrigin.setX(rect.left()); | |
|
267 | 153 | width= rect.width(); |
|
268 | 154 | } |
|
269 | m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized()); | |
|
155 | d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(),d_ptr->m_rubberBandOrigin.y(), width,height).normalized()); | |
|
270 | 156 | } |
|
271 | 157 | else { |
|
272 | 158 | QGraphicsView::mouseMoveEvent(event); |
|
273 | 159 | } |
|
274 | 160 | } |
|
275 | 161 | |
|
276 | 162 | /*! |
|
277 | 163 | If left mouse button is release and RubberBand is enabled then \a event is accepted and the view is zoomed in to rect specified by RubberBand |
|
278 | 164 | If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled. |
|
279 | 165 | */ |
|
280 | 166 | void QChartView::mouseReleaseEvent(QMouseEvent *event) |
|
281 | 167 | { |
|
282 | if(m_rubberBand) { | |
|
283 | if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) { | |
|
284 | m_rubberBand->hide(); | |
|
285 | QRect rect = m_rubberBand->geometry(); | |
|
286 | m_chart->zoomIn(rect); | |
|
168 | if(d_ptr->m_rubberBand) { | |
|
169 | if (event->button() == Qt::LeftButton && d_ptr->m_rubberBand->isVisible()) { | |
|
170 | d_ptr->m_rubberBand->hide(); | |
|
171 | QRect rect = d_ptr->m_rubberBand->geometry(); | |
|
172 | d_ptr->m_chart->zoomIn(rect); | |
|
287 | 173 | event->accept(); |
|
288 | 174 | } |
|
289 | 175 | |
|
290 | 176 | if(event->button()==Qt::RightButton){ |
|
291 | m_chart->zoomOut(); | |
|
177 | d_ptr->m_chart->zoomOut(); | |
|
292 | 178 | event->accept(); |
|
293 | 179 | } |
|
294 | 180 | } |
|
295 | 181 | else { |
|
296 | 182 | QGraphicsView::mouseReleaseEvent(event); |
|
297 | 183 | } |
|
298 | 184 | } |
|
299 | 185 | |
|
300 | 186 | /*! |
|
301 | 187 | Pressing + and - keys performs zoomIn() and zoomOut() respectivly. |
|
302 | 188 | In other \a event is passed to the QGraphicsView::keyPressEvent() implementation |
|
303 | 189 | */ |
|
304 | 190 | void QChartView::keyPressEvent(QKeyEvent *event) |
|
305 | 191 | { |
|
306 | 192 | switch (event->key()) { |
|
307 | 193 | case Qt::Key_Plus: |
|
308 | zoomIn(); | |
|
194 | d_ptr->m_chart->zoomIn(); | |
|
309 | 195 | break; |
|
310 | 196 | case Qt::Key_Minus: |
|
311 | zoomOut(); | |
|
197 | d_ptr->m_chart->zoomOut(); | |
|
312 | 198 | break; |
|
313 | 199 | default: |
|
314 | 200 | QGraphicsView::keyPressEvent(event); |
|
315 | 201 | break; |
|
316 | 202 | } |
|
317 | 203 | } |
|
318 | 204 | |
|
319 | 205 | /*! |
|
320 | Sets the \a theme used by the chart for rendering the graphical representation of the data | |
|
321 | \sa QChart::ChartTheme, chartTheme() | |
|
322 | */ | |
|
323 | void QChartView::setChartTheme(QChart::ChartTheme theme) | |
|
324 | { | |
|
325 | m_chart->setTheme(theme); | |
|
326 | } | |
|
327 | ||
|
328 | /*! | |
|
329 | Returns the theme enum used by the chart. | |
|
330 | \sa setChartTheme() | |
|
331 | */ | |
|
332 | QChart::ChartTheme QChartView::chartTheme() const | |
|
333 | { | |
|
334 | return m_chart->theme(); | |
|
335 | } | |
|
336 | ||
|
337 | /*! | |
|
338 | Returns the pointer to the x axis object of the chart | |
|
339 | */ | |
|
340 | QChartAxis* QChartView::axisX() const | |
|
341 | { | |
|
342 | return m_chart->axisX(); | |
|
343 | } | |
|
344 | ||
|
345 | /*! | |
|
346 | Returns the pointer to the y axis object of the chart | |
|
347 | */ | |
|
348 | QChartAxis* QChartView::axisY() const | |
|
349 | { | |
|
350 | return m_chart->axisY(); | |
|
351 | } | |
|
352 | ||
|
353 | /*! | |
|
354 | Returns the pointer to legend object of the chart | |
|
355 | */ | |
|
356 | QLegend& QChartView::legend() const | |
|
357 | { | |
|
358 | return m_chart->legend(); | |
|
359 | } | |
|
360 | ||
|
361 | /*! | |
|
362 | Gives ownership of legend to user. | |
|
363 | */ | |
|
364 | QLegend* QChartView::takeLegend() | |
|
365 | { | |
|
366 | return m_chart->takeLegend(); | |
|
367 | } | |
|
368 | ||
|
369 | /*! | |
|
370 | Gives ownership of legend back to chart. QChart takes ownership of \a legend and deletes existing one | |
|
371 | */ | |
|
372 | void QChartView::giveLegend(QLegend* legend) | |
|
373 | { | |
|
374 | m_chart->giveLegend(legend); | |
|
375 | } | |
|
376 | ||
|
377 | /*! | |
|
378 | Sets animation \a options for the chart | |
|
206 | Resizes and updates the chart area using the \a event data | |
|
379 | 207 | */ |
|
380 | void QChartView::setAnimationOptions(QChart::AnimationOptions options) | |
|
208 | void QChartView::resizeEvent(QResizeEvent *event) | |
|
381 | 209 | { |
|
382 | m_chart->setAnimationOptions(options); | |
|
210 | d_ptr->m_chart->resize(size()); | |
|
211 | QGraphicsView::resizeEvent(event); | |
|
383 | 212 | } |
|
384 | 213 | |
|
385 | /*! | |
|
386 | Returns animation options for the chart | |
|
387 | */ | |
|
388 | QChart::AnimationOptions QChartView::animationOptions() const | |
|
389 | { | |
|
390 | return m_chart->animationOptions(); | |
|
391 | } | |
|
214 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
|
392 | 215 | |
|
393 | void QChartView::scrollLeft() | |
|
216 | QChartViewPrivate::QChartViewPrivate(): | |
|
217 | m_scene(0), | |
|
218 | m_chart(0), | |
|
219 | m_presenter(0), | |
|
220 | m_rubberBand(0), | |
|
221 | m_rubberBandFlags(QChartView::NoRubberBand) | |
|
394 | 222 | { |
|
395 | m_chart->scrollLeft(); | |
|
396 | } | |
|
397 | 223 | |
|
398 | void QChartView::scrollRight() | |
|
399 | { | |
|
400 | m_chart->scrollRight(); | |
|
401 | 224 | } |
|
402 | 225 | |
|
403 | void QChartView::scrollUp() | |
|
226 | QChartViewPrivate::~QChartViewPrivate() | |
|
404 | 227 | { |
|
405 | m_chart->scrollUp(); | |
|
406 | } | |
|
407 | 228 | |
|
408 | void QChartView::scrollDown() | |
|
409 | { | |
|
410 | m_chart->scrollDown(); | |
|
411 | 229 | } |
|
412 | 230 | |
|
413 | ||
|
414 | 231 | #include "moc_qchartview.cpp" |
|
415 | 232 | |
|
416 | 233 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,84 +1,72 | |||
|
1 | #ifndef QCHARTWIDGET_H | |
|
2 | #define QCHARTWIDGET_H | |
|
3 | ||
|
4 | #include <qchartglobal.h> | |
|
5 | #include <qchartaxis.h> | |
|
6 | #include <qseries.h> | |
|
7 | #include <qchart.h> | |
|
1 | /**************************************************************************** | |
|
2 | ** | |
|
3 | ** Copyright (C) 2012 Digia Plc | |
|
4 | ** All rights reserved. | |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
|
6 | ** | |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
|
8 | ** | |
|
9 | ** $QT_BEGIN_LICENSE$ | |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
|
13 | ** a written agreement between you and Digia. | |
|
14 | ** | |
|
15 | ** If you have questions regarding the use of this file, please use | |
|
16 | ** contact form at http://qt.digia.com | |
|
17 | ** $QT_END_LICENSE$ | |
|
18 | ** | |
|
19 | ****************************************************************************/ | |
|
20 | ||
|
21 | #ifndef QCHARTVIEW_H | |
|
22 | #define QCHARTVIEW_H | |
|
23 | ||
|
24 | #include <QChartAxis> | |
|
25 | #include <QSeries> | |
|
26 | #include <QChart> | |
|
8 | 27 | #include <QGraphicsView> |
|
9 | 28 | |
|
10 | 29 | class QGraphicsScene; |
|
11 | 30 | class QRubberBand; |
|
12 | 31 | |
|
13 | 32 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
14 | 33 | |
|
15 | class QChart; | |
|
34 | class QChartViewPrivate; | |
|
16 | 35 | |
|
17 | 36 | class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView |
|
18 | 37 | { |
|
19 | 38 | Q_OBJECT |
|
20 | 39 | |
|
21 | 40 | public: |
|
22 | enum RubberBandPolicy { NoRubberBand, VerticalRubberBand, HorizonalRubberBand, RectangleRubberBand }; | |
|
23 | ||
|
24 | explicit QChartView(QWidget *parent = 0); | |
|
25 | ~QChartView(); | |
|
26 | ||
|
27 | //implement from QWidget | |
|
28 | void resizeEvent(QResizeEvent *event); | |
|
29 | ||
|
30 | void addSeries(QSeries* series,QChartAxis* axisY=0);// takes series ownership , takes axis ownership | |
|
31 | void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached | |
|
32 | void removeAllSeries(); // deletes series and axis | |
|
33 | 41 | |
|
34 | void setChartTitle(const QString& title); | |
|
35 | QString chartTitle() const; | |
|
36 | void setChartTitleFont(const QFont& font); | |
|
37 | void setChartTitleBrush(const QBrush &brush); | |
|
38 | QBrush chartTitleBrush(); | |
|
39 | void setChartBackgroundBrush(const QBrush& brush); | |
|
40 | void setChartBackgroundPen(const QPen& pen); | |
|
42 | enum RubberBand{ | |
|
43 | NoRubberBand = 0x0, | |
|
44 | VerticalRubberBand = 0x1, | |
|
45 | HorizonalRubberBand = 0x2, | |
|
46 | RectangleRubberBand = 0x3 | |
|
47 | }; | |
|
41 | 48 | |
|
42 | void zoomIn(); | |
|
43 | void zoomIn(const QRect& rect); | |
|
44 | void zoomOut(); | |
|
45 | void scrollLeft(); | |
|
46 | void scrollRight(); | |
|
47 | void scrollUp(); | |
|
48 | void scrollDown(); | |
|
49 | Q_DECLARE_FLAGS(RubberBands, RubberBand) | |
|
49 | 50 | |
|
50 | void setRubberBandPolicy(const RubberBandPolicy ); | |
|
51 | RubberBandPolicy rubberBandPolicy() const; | |
|
52 | ||
|
53 | void setChartTheme(QChart::ChartTheme theme); | |
|
54 | QChart::ChartTheme chartTheme() const; | |
|
55 | ||
|
56 | void setAnimationOptions(QChart::AnimationOptions options); | |
|
57 | QChart::AnimationOptions animationOptions() const; | |
|
58 | ||
|
59 | QChartAxis* axisX() const; | |
|
60 | QChartAxis* axisY() const; | |
|
51 | explicit QChartView(QChart *chart,QWidget *parent = 0); | |
|
52 | ~QChartView(); | |
|
61 | 53 | |
|
62 | QLegend &legend() const; | |
|
63 | QLegend* takeLegend(); | |
|
64 | void giveLegend(QLegend* legend); | |
|
54 | void setRubberBand(const RubberBands& rubberBands); | |
|
55 | RubberBands rubberBand() const; | |
|
56 | QChart* chart() const; | |
|
65 | 57 | |
|
66 | 58 | protected: |
|
59 | void resizeEvent(QResizeEvent *event); | |
|
67 | 60 | void mousePressEvent(QMouseEvent *event); |
|
68 | 61 | void mouseMoveEvent(QMouseEvent *event); |
|
69 | 62 | void mouseReleaseEvent(QMouseEvent *event); |
|
70 | 63 | void keyPressEvent(QKeyEvent *event); |
|
71 | 64 | |
|
72 | private: | |
|
73 | QGraphicsScene *m_scene; | |
|
74 | QChart* m_chart; | |
|
75 | QPoint m_rubberBandOrigin; | |
|
76 | QRubberBand* m_rubberBand; | |
|
77 | bool m_verticalRubberBand; | |
|
78 | bool m_horizonalRubberBand; | |
|
65 | protected: | |
|
66 | QScopedPointer<QChartViewPrivate> d_ptr; | |
|
79 | 67 | Q_DISABLE_COPY(QChartView) |
|
80 | 68 | }; |
|
81 | 69 | |
|
82 | 70 | QTCOMMERCIALCHART_END_NAMESPACE |
|
83 | 71 | |
|
84 | 72 | #endif // QCHARTWIDGET_H |
@@ -1,158 +1,159 | |||
|
1 | 1 | !include( ../common.pri ):error( Couldn't find the common.pri file! ) |
|
2 | 2 | TARGET = QtCommercialChart |
|
3 | 3 | DESTDIR = $$CHART_BUILD_LIB_DIR |
|
4 | 4 | TEMPLATE = lib |
|
5 | 5 | QT += core \ |
|
6 | 6 | gui |
|
7 | 7 | win32-msvc*: LIBS += User32.lib |
|
8 | 8 | CONFIG += debug_and_release |
|
9 | 9 | CONFIG(debug, debug|release):TARGET = QtCommercialChartd |
|
10 | 10 | SOURCES += \ |
|
11 | 11 | $$PWD/chartdataset.cpp \ |
|
12 | 12 | $$PWD/chartpresenter.cpp \ |
|
13 | 13 | $$PWD/charttheme.cpp \ |
|
14 | 14 | $$PWD/domain.cpp \ |
|
15 | 15 | $$PWD/qchart.cpp \ |
|
16 | 16 | $$PWD/qchartview.cpp \ |
|
17 | 17 | $$PWD/qseries.cpp \ |
|
18 | 18 | $$PWD/qlegend.cpp \ |
|
19 | 19 | $$PWD/legendmarker.cpp \ |
|
20 | 20 | $$PWD/legendscrollbutton.cpp \ |
|
21 | 21 | $$PWD/chartbackground.cpp \ |
|
22 | 22 | $$PWD/chart.cpp |
|
23 | 23 | PRIVATE_HEADERS += \ |
|
24 | 24 | $$PWD/chartdataset_p.h \ |
|
25 | 25 | $$PWD/chartitem_p.h \ |
|
26 | 26 | $$PWD/chartpresenter_p.h \ |
|
27 | 27 | $$PWD/charttheme_p.h \ |
|
28 | 28 | $$PWD/domain_p.h \ |
|
29 | 29 | $$PWD/legendmarker_p.h \ |
|
30 | 30 | $$PWD/legendscrollbutton_p.h \ |
|
31 | 31 | $$PWD/chartbackground_p.h \ |
|
32 | 32 | $$PWD/chart_p.h \ |
|
33 | 33 | $$PWD/chartconfig_p.h \ |
|
34 | $$PWD/qchart_p.h | |
|
34 | $$PWD/qchart_p.h \ | |
|
35 | $$PWD/qchartview_p.h | |
|
35 | 36 | PUBLIC_HEADERS += \ |
|
36 | 37 | $$PWD/qchart.h \ |
|
37 | 38 | $$PWD/qchartglobal.h \ |
|
38 | 39 | $$PWD/qseries.h \ |
|
39 | 40 | $$PWD/qchartview.h \ |
|
40 | 41 | $$PWD/qlegend.h |
|
41 | 42 | |
|
42 | 43 | include(animations/animations.pri) |
|
43 | 44 | include(axis/axis.pri) |
|
44 | 45 | include(xychart/xychart.pri) |
|
45 | 46 | include(linechart/linechart.pri) |
|
46 | 47 | include(areachart/areachart.pri) |
|
47 | 48 | include(barchart/barchart.pri) |
|
48 | 49 | include(piechart/piechart.pri) |
|
49 | 50 | include(scatterseries/scatter.pri) |
|
50 | 51 | include(splinechart/splinechart.pri) |
|
51 | 52 | include(themes/themes.pri) |
|
52 | 53 | |
|
53 | 54 | |
|
54 | 55 | HEADERS += $$PUBLIC_HEADERS |
|
55 | 56 | HEADERS += $$PRIVATE_HEADERS |
|
56 | 57 | HEADERS += $$THEMES |
|
57 | 58 | INCLUDEPATH += ../include . |
|
58 | 59 | |
|
59 | 60 | OBJECTS_DIR = $$CHART_BUILD_DIR/lib |
|
60 | 61 | MOC_DIR = $$CHART_BUILD_DIR/lib |
|
61 | 62 | UI_DIR = $$CHART_BUILD_DIR/lib |
|
62 | 63 | RCC_DIR = $$CHART_BUILD_DIR/lib |
|
63 | 64 | DEFINES += QTCOMMERCIALCHART_LIBRARY |
|
64 | 65 | |
|
65 | 66 | #qt public headers |
|
66 | 67 | #this is very primitive and lame parser , TODO: make perl script insted |
|
67 | 68 | !exists($$CHART_BUILD_PUBLIC_HEADER_DIR/QChartGlobal) |
|
68 | 69 | { |
|
69 | 70 | system($$QMAKE_MKDIR $$CHART_BUILD_PUBLIC_HEADER_DIR) |
|
70 | 71 | win32:{ |
|
71 | 72 | command = "echo $${LITERAL_HASH}include \"qchartglobal.h\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/QChartGlobal" |
|
72 | 73 | }else{ |
|
73 | 74 | command = "echo \"$${LITERAL_HASH}include \\\"qchartglobal.h\\\"\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/QChartGlobal" |
|
74 | 75 | } |
|
75 | 76 | system($$command) |
|
76 | 77 | } |
|
77 | 78 | |
|
78 | 79 | for(file, PUBLIC_HEADERS) { |
|
79 | 80 | name = $$split(file,'/') |
|
80 | 81 | name = $$last(name) |
|
81 | 82 | class = "$$cat($$file)" |
|
82 | 83 | class = $$find(class,class) |
|
83 | 84 | !isEmpty(class){ |
|
84 | 85 | class = $$split(class,QTCOMMERCIALCHART_EXPORT) |
|
85 | 86 | class = $$member(class,1) |
|
86 | 87 | class = $$split(class,' ') |
|
87 | 88 | class = $$replace(class,' ','') |
|
88 | 89 | class = $$member(class,0) |
|
89 | 90 | win32:{ |
|
90 | 91 | command = "echo $${LITERAL_HASH}include \"$$name\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class" |
|
91 | 92 | }else{ |
|
92 | 93 | command = "echo \"$${LITERAL_HASH}include \\\"$$name\\\"\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class" |
|
93 | 94 | } |
|
94 | 95 | PUBLIC_QT_HEADERS += $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class |
|
95 | 96 | system($$command) |
|
96 | 97 | } |
|
97 | 98 | } |
|
98 | 99 | |
|
99 | 100 | public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart |
|
100 | 101 | public_headers.files = $$PUBLIC_HEADERS $$PUBLIC_QT_HEADERS |
|
101 | 102 | |
|
102 | 103 | target.path = $$[QT_INSTALL_LIBS] |
|
103 | 104 | INSTALLS += target public_headers |
|
104 | 105 | |
|
105 | 106 | install_build_public_headers.name = build_public_headers |
|
106 | 107 | install_build_public_headers.output = $$CHART_BUILD_PUBLIC_HEADER_DIR/${QMAKE_FILE_BASE}.h |
|
107 | 108 | install_build_public_headers.input = PUBLIC_HEADERS |
|
108 | 109 | install_build_public_headers.commands = $$QMAKE_COPY \ |
|
109 | 110 | ${QMAKE_FILE_NAME} \ |
|
110 | 111 | $$CHART_BUILD_PUBLIC_HEADER_DIR |
|
111 | 112 | install_build_public_headers.CONFIG += target_predeps \ |
|
112 | 113 | no_link |
|
113 | 114 | |
|
114 | 115 | install_build_private_headers.name = buld_private_headers |
|
115 | 116 | install_build_private_headers.output = $$CHART_BUILD_PRIVATE_HEADER_DIR/${QMAKE_FILE_BASE}.h |
|
116 | 117 | install_build_private_headers.input = PRIVATE_HEADERS |
|
117 | 118 | install_build_private_headers.commands = $$QMAKE_COPY \ |
|
118 | 119 | ${QMAKE_FILE_NAME} \ |
|
119 | 120 | $$CHART_BUILD_PRIVATE_HEADER_DIR |
|
120 | 121 | install_build_private_headers.CONFIG += target_predeps \ |
|
121 | 122 | no_link |
|
122 | 123 | |
|
123 | 124 | QMAKE_EXTRA_COMPILERS += install_build_public_headers \ |
|
124 | 125 | install_build_private_headers \ |
|
125 | 126 | |
|
126 | 127 | |
|
127 | 128 | chartversion.target = $$PWD/qchartversion_p.h |
|
128 | 129 | unix:{ |
|
129 | 130 | chartversion.commands = @echo \ |
|
130 | 131 | "const char *buildTime = \\\"`date +'%y%m%d%H%M'`\\\" \\; \ |
|
131 | 132 | const char *gitHead = \\\"`git rev-parse HEAD`\\\" \\; " \ |
|
132 | 133 | > \ |
|
133 | 134 | $$chartversion.target; |
|
134 | 135 | }else{ |
|
135 | 136 | chartversion.commands = @echo \ |
|
136 | 137 | "const char *buildTime = \"%date%_%time%\" ; \ |
|
137 | 138 | const char *gitHead = \"unknown\" ; " \ |
|
138 | 139 | > \ |
|
139 | 140 | $$chartversion.target |
|
140 | 141 | } |
|
141 | 142 | chartversion.depends = $$HEADERS \ |
|
142 | 143 | $$SOURCES |
|
143 | 144 | PRE_TARGETDEPS += $$PWD/qchartversion_p.h |
|
144 | 145 | QMAKE_CLEAN += $$PWD/qchartversion_p.h |
|
145 | 146 | QMAKE_EXTRA_TARGETS += chartversion |
|
146 | 147 | unix:QMAKE_DISTCLEAN += -r \ |
|
147 | 148 | $$CHART_BUILD_HEADER_DIR \ |
|
148 | 149 | $$CHART_BUILD_LIB_DIR |
|
149 | 150 | win32:QMAKE_DISTCLEAN += /Q \ |
|
150 | 151 | $$CHART_BUILD_HEADER_DIR \ |
|
151 | 152 | $$CHART_BUILD_LIB_DIR |
|
152 | 153 | |
|
153 | 154 | # treat warnings as errors |
|
154 | 155 | win32-msvc*: { |
|
155 | 156 | QMAKE_CXXFLAGS += /WX |
|
156 | 157 | } else { |
|
157 | 158 | QMAKE_CXXFLAGS += -Werror |
|
158 | 159 | } |
General Comments 0
You need to be logged in to leave comments.
Login now