@@ -1,372 +1,382 | |||
|
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 "qchart.h" |
|
22 | 22 | #include "qchart_p.h" |
|
23 | 23 | #include <QGraphicsScene> |
|
24 | 24 | #include <QGraphicsSceneResizeEvent> |
|
25 | 25 | |
|
26 | 26 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
27 | 27 | |
|
28 | 28 | /*! |
|
29 | 29 | \enum QChart::ChartTheme |
|
30 | 30 | |
|
31 | 31 | This enum describes the theme used by the chart. |
|
32 | 32 | |
|
33 | 33 | \value ChartThemeLight The default theme |
|
34 | 34 | \value ChartThemeBlueCerulean |
|
35 | 35 | \value ChartThemeDark |
|
36 | 36 | \value ChartThemeBrownSand |
|
37 | 37 | \value ChartThemeBlueNcs |
|
38 | 38 | \value ChartThemeHighContrast |
|
39 | 39 | \value ChartThemeBlueIcy |
|
40 | 40 | \value ChartThemeCount Not really a theme; the total count of themes. |
|
41 | 41 | */ |
|
42 | 42 | |
|
43 | 43 | /*! |
|
44 | 44 | \enum QChart::AnimationOption |
|
45 | 45 | |
|
46 | 46 | For enabling/disabling animations. Defaults to NoAnimation. |
|
47 | 47 | |
|
48 | 48 | \value NoAnimation |
|
49 | 49 | \value GridAxisAnimations |
|
50 | 50 | \value SeriesAnimations |
|
51 | 51 | \value AllAnimations |
|
52 | 52 | */ |
|
53 | 53 | |
|
54 | 54 | /*! |
|
55 | 55 | \class QChart |
|
56 | 56 | \brief QtCommercial chart API. |
|
57 | 57 | |
|
58 | 58 | QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical |
|
59 | 59 | representation of different types of QChartSeries and other chart related objects like |
|
60 | 60 | QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the |
|
61 | 61 | convenience class QChartView instead of QChart. |
|
62 | 62 | \sa QChartView |
|
63 | 63 | */ |
|
64 | 64 | |
|
65 | 65 | /*! |
|
66 | 66 | Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor. |
|
67 | 67 | */ |
|
68 | 68 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), |
|
69 | 69 | d_ptr(new QChartPrivate()) |
|
70 | 70 | { |
|
71 | 71 | d_ptr->m_legend = new ScrolledQLegend(this); |
|
72 | 72 | d_ptr->m_legend->setVisible(false); |
|
73 | 73 | d_ptr->m_dataset = new ChartDataSet(this); |
|
74 | 74 | d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset); |
|
75 | 75 | d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false); |
|
76 | 76 | d_ptr->createConnections(); |
|
77 | 77 | //TODO:fix me setMinimumSize(d_ptr->m_padding.left() * 3, d_ptr->m_padding.top() * 3); |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | /*! |
|
81 | 81 | Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. |
|
82 | 82 | */ |
|
83 | 83 | QChart::~QChart() |
|
84 | 84 | { |
|
85 | 85 | //delete first presenter , since this is a root of all the graphical items |
|
86 | 86 | delete d_ptr->m_presenter; |
|
87 | 87 | d_ptr->m_presenter=0; |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | /*! |
|
91 | 91 | Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects. |
|
92 | 92 | If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and |
|
93 | 93 | the y axis). |
|
94 | 94 | */ |
|
95 | 95 | void QChart::addSeries(QSeries* series, QChartAxis* axisY) |
|
96 | 96 | { |
|
97 | Q_ASSERT(series); | |
|
97 | 98 | d_ptr->m_dataset->addSeries(series, axisY); |
|
98 | 99 | } |
|
99 | 100 | |
|
100 | 101 | /*! |
|
101 | 102 | Removes the \a series specified in a perameter from the QChartView. |
|
102 | 103 | It releses its ownership of the specified QChartSeries object. |
|
103 | 104 | It does not delete the pointed QChartSeries data object |
|
104 | 105 | \sa addSeries(), removeAllSeries() |
|
105 | 106 | */ |
|
106 | 107 | void QChart::removeSeries(QSeries* series) |
|
107 | 108 | { |
|
109 | Q_ASSERT(series); | |
|
108 | 110 | d_ptr->m_dataset->removeSeries(series); |
|
109 | 111 | } |
|
110 | 112 | |
|
111 | 113 | /*! |
|
112 | 114 | Removes all the QChartSeries that have been added to the QChartView |
|
113 | 115 | It also deletes the pointed QChartSeries data objects |
|
114 | 116 | \sa addSeries(), removeSeries() |
|
115 | 117 | */ |
|
116 | 118 | void QChart::removeAllSeries() |
|
117 | 119 | { |
|
118 | 120 | d_ptr->m_dataset->removeAllSeries(); |
|
119 | 121 | } |
|
120 | 122 | |
|
121 | 123 | /*! |
|
122 | 124 | Sets the \a brush that is used for painting the background of the chart area. |
|
123 | 125 | */ |
|
124 | 126 | void QChart::setBackgroundBrush(const QBrush& brush) |
|
125 | 127 | { |
|
126 | 128 | //TODO: refactor me |
|
127 | 129 | d_ptr->m_presenter->createChartBackgroundItem(); |
|
128 | 130 | d_ptr->m_presenter->m_backgroundItem->setBrush(brush); |
|
129 | 131 | d_ptr->m_presenter->m_backgroundItem->update(); |
|
130 | 132 | } |
|
131 | 133 | |
|
132 | 134 | QBrush QChart::backgroundBrush() const |
|
133 | 135 | { |
|
134 | 136 | //TODO: refactor me |
|
135 | 137 | if (!d_ptr->m_presenter->m_backgroundItem) return QBrush(); |
|
136 | 138 | return (d_ptr->m_presenter->m_backgroundItem)->brush(); |
|
137 | 139 | } |
|
138 | 140 | |
|
139 | 141 | /*! |
|
140 | 142 | Sets the \a pen that is used for painting the background of the chart area. |
|
141 | 143 | */ |
|
142 | 144 | void QChart::setBackgroundPen(const QPen& pen) |
|
143 | 145 | { |
|
144 | 146 | //TODO: refactor me |
|
145 | 147 | d_ptr->m_presenter->createChartBackgroundItem(); |
|
146 | 148 | d_ptr->m_presenter->m_backgroundItem->setPen(pen); |
|
147 | 149 | d_ptr->m_presenter->m_backgroundItem->update(); |
|
148 | 150 | } |
|
149 | 151 | |
|
150 | 152 | QPen QChart::backgroundPen() const |
|
151 | 153 | { |
|
152 | 154 | //TODO: refactor me |
|
153 | 155 | if (!d_ptr->m_presenter->m_backgroundItem) return QPen(); |
|
154 | 156 | return d_ptr->m_presenter->m_backgroundItem->pen(); |
|
155 | 157 | } |
|
156 | 158 | |
|
157 | 159 | /*! |
|
158 | 160 | Sets the chart \a title. The description text that is drawn above the chart. |
|
159 | 161 | */ |
|
160 | 162 | void QChart::setTitle(const QString& title) |
|
161 | 163 | { |
|
162 | 164 | //TODO: refactor me |
|
163 | 165 | d_ptr->m_presenter->createChartTitleItem(); |
|
164 | 166 | d_ptr->m_presenter->m_titleItem->setText(title); |
|
165 | 167 | d_ptr->m_presenter->updateLayout(); |
|
166 | 168 | } |
|
167 | 169 | |
|
168 | 170 | /*! |
|
169 | 171 | Returns the chart title. The description text that is drawn above the chart. |
|
170 | 172 | */ |
|
171 | 173 | QString QChart::title() const |
|
172 | 174 | { |
|
173 | 175 | //TODO: refactor me |
|
174 | 176 | if (d_ptr->m_presenter->m_titleItem) |
|
175 | return d_ptr->m_presenter->m_titleItem->text(); | |
|
177 | return d_ptr->m_presenter->m_titleItem->text(); | |
|
176 | 178 | else |
|
177 | return QString(); | |
|
179 | return QString(); | |
|
178 | 180 | } |
|
179 | 181 | |
|
180 | 182 | /*! |
|
181 | 183 | Sets the \a font that is used for rendering the description text that is rendered above the chart. |
|
182 | 184 | */ |
|
183 | 185 | void QChart::setTitleFont(const QFont& font) |
|
184 | 186 | { |
|
185 | 187 | //TODO: refactor me |
|
186 | 188 | d_ptr->m_presenter->createChartTitleItem(); |
|
187 | 189 | d_ptr->m_presenter->m_titleItem->setFont(font); |
|
188 | 190 | d_ptr->m_presenter->updateLayout(); |
|
189 | 191 | } |
|
190 | 192 | |
|
193 | QFont QChart::titleFont() const | |
|
194 | { | |
|
195 | if (d_ptr->m_presenter->m_titleItem) | |
|
196 | return d_ptr->m_presenter->m_titleItem->font(); | |
|
197 | else | |
|
198 | return QFont(); | |
|
199 | } | |
|
200 | ||
|
191 | 201 | /*! |
|
192 | 202 | Sets the \a brush used for rendering the title text. |
|
193 | 203 | */ |
|
194 | 204 | void QChart::setTitleBrush(const QBrush &brush) |
|
195 | 205 | { |
|
196 | 206 | //TODO: refactor me |
|
197 | 207 | d_ptr->m_presenter->createChartTitleItem(); |
|
198 | 208 | d_ptr->m_presenter->m_titleItem->setBrush(brush); |
|
199 | 209 | d_ptr->m_presenter->updateLayout(); |
|
200 | 210 | } |
|
201 | 211 | |
|
202 | 212 | /*! |
|
203 | 213 | Returns the brush used for rendering the title text. |
|
204 | 214 | */ |
|
205 | 215 | QBrush QChart::titleBrush() const |
|
206 | 216 | { |
|
207 | 217 | //TODO: refactor me |
|
208 | 218 | if (!d_ptr->m_presenter->m_titleItem) return QBrush(); |
|
209 | 219 | return d_ptr->m_presenter->m_titleItem->brush(); |
|
210 | 220 | } |
|
211 | 221 | |
|
212 | 222 | /*! |
|
213 | 223 | Sets the \a theme used by the chart for rendering the graphical representation of the data |
|
214 | 224 | \sa ChartTheme, chartTheme() |
|
215 | 225 | */ |
|
216 | 226 | void QChart::setTheme(QChart::ChartTheme theme) |
|
217 | 227 | { |
|
218 | 228 | d_ptr->m_presenter->setTheme(theme); |
|
219 | 229 | } |
|
220 | 230 | |
|
221 | 231 | /*! |
|
222 | 232 | Returns the theme enum used by the chart. |
|
223 | 233 | \sa ChartTheme, setChartTheme() |
|
224 | 234 | */ |
|
225 | 235 | QChart::ChartTheme QChart::theme() const |
|
226 | 236 | { |
|
227 | 237 | return d_ptr->m_presenter->theme(); |
|
228 | 238 | } |
|
229 | 239 | |
|
230 | 240 | /*! |
|
231 | 241 | Zooms in the view by a factor of 2 |
|
232 | 242 | */ |
|
233 | 243 | void QChart::zoomIn() |
|
234 | 244 | { |
|
235 | 245 | d_ptr->m_presenter->zoomIn(); |
|
236 | 246 | } |
|
237 | 247 | |
|
238 | 248 | /*! |
|
239 | 249 | Zooms in the view to a maximum level at which \a rect is still fully visible. |
|
240 | 250 | */ |
|
241 | 251 | void QChart::zoomIn(const QRectF& rect) |
|
242 | 252 | { |
|
243 | 253 | if (!rect.isValid()) return; |
|
244 | 254 | d_ptr->m_presenter->zoomIn(rect); |
|
245 | 255 | } |
|
246 | 256 | |
|
247 | 257 | /*! |
|
248 | 258 | Restores the view zoom level to the previous one. |
|
249 | 259 | */ |
|
250 | 260 | void QChart::zoomOut() |
|
251 | 261 | { |
|
252 | 262 | d_ptr->m_presenter->zoomOut(); |
|
253 | 263 | } |
|
254 | 264 | |
|
255 | 265 | /*! |
|
256 | 266 | Returns the pointer to the x axis object of the chart |
|
257 | 267 | */ |
|
258 | 268 | QChartAxis* QChart::axisX() const |
|
259 | 269 | { |
|
260 | 270 | return d_ptr->m_dataset->axisX(); |
|
261 | 271 | } |
|
262 | 272 | |
|
263 | 273 | /*! |
|
264 | 274 | Returns the pointer to the y axis object of the chart |
|
265 | 275 | */ |
|
266 | 276 | QChartAxis* QChart::axisY() const |
|
267 | 277 | { |
|
268 | 278 | return d_ptr->m_dataset->axisY(); |
|
269 | 279 | } |
|
270 | 280 | |
|
271 | 281 | /*! |
|
272 | 282 | Returns the legend object of the chart. Ownership stays in chart. |
|
273 | 283 | */ |
|
274 | 284 | QLegend* QChart::legend() const |
|
275 | 285 | { |
|
276 | 286 | return d_ptr->m_legend; |
|
277 | 287 | } |
|
278 | 288 | |
|
279 | 289 | QRectF QChart::margins() const |
|
280 | 290 | { |
|
281 | 291 | return d_ptr->m_presenter->margins(); |
|
282 | 292 | } |
|
283 | 293 | |
|
284 | 294 | |
|
285 | 295 | /*! |
|
286 | 296 | Resizes and updates the chart area using the \a event data |
|
287 | 297 | */ |
|
288 | 298 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
289 | 299 | { |
|
290 | 300 | d_ptr->m_rect = QRectF(QPoint(0,0),event->newSize()); |
|
291 | 301 | QGraphicsWidget::resizeEvent(event); |
|
292 | 302 | d_ptr->m_presenter->setGeometry(d_ptr->m_rect); |
|
293 | 303 | } |
|
294 | 304 | |
|
295 | 305 | /*! |
|
296 | 306 | Sets animation \a options for the chart |
|
297 | 307 | */ |
|
298 | 308 | void QChart::setAnimationOptions(AnimationOptions options) |
|
299 | 309 | { |
|
300 | 310 | d_ptr->m_presenter->setAnimationOptions(options); |
|
301 | 311 | } |
|
302 | 312 | |
|
303 | 313 | /*! |
|
304 | 314 | Returns animation options for the chart |
|
305 | 315 | */ |
|
306 | 316 | QChart::AnimationOptions QChart::animationOptions() const |
|
307 | 317 | { |
|
308 | 318 | return d_ptr->m_presenter->animationOptions(); |
|
309 | 319 | } |
|
310 | 320 | |
|
311 | 321 | void QChart::scrollLeft() |
|
312 | 322 | { |
|
313 | 323 | d_ptr->m_presenter->scroll(-d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0); |
|
314 | 324 | } |
|
315 | 325 | |
|
316 | 326 | void QChart::scrollRight() |
|
317 | 327 | { |
|
318 | 328 | d_ptr->m_presenter->scroll(d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0); |
|
319 | 329 | } |
|
320 | 330 | |
|
321 | 331 | void QChart::scrollUp() |
|
322 | 332 | { |
|
323 | 333 | d_ptr->m_presenter->scroll(0,d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1)); |
|
324 | 334 | } |
|
325 | 335 | |
|
326 | 336 | void QChart::scrollDown() |
|
327 | 337 | { |
|
328 | 338 | d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1)); |
|
329 | 339 | } |
|
330 | 340 | |
|
331 | 341 | void QChart::setBackgroundVisible(bool visible) |
|
332 | 342 | { |
|
333 | 343 | //TODO: refactor me |
|
334 | 344 | d_ptr->m_presenter->createChartBackgroundItem(); |
|
335 | 345 | d_ptr->m_presenter->m_backgroundItem->setVisible(visible); |
|
336 | 346 | } |
|
337 | 347 | |
|
338 | 348 | bool QChart::isBackgroundVisible() const |
|
339 | 349 | { |
|
340 | 350 | //TODO: refactor me |
|
341 | 351 | if (!d_ptr->m_presenter->m_backgroundItem) return false; |
|
342 | 352 | return d_ptr->m_presenter->m_backgroundItem->isVisible(); |
|
343 | 353 | } |
|
344 | 354 | |
|
345 | 355 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
346 | 356 | |
|
347 | 357 | QChartPrivate::QChartPrivate(): |
|
348 | 358 | m_legend(0), |
|
349 | 359 | m_dataset(0), |
|
350 | 360 | m_presenter(0) |
|
351 | 361 | { |
|
352 | 362 | |
|
353 | 363 | } |
|
354 | 364 | |
|
355 | 365 | QChartPrivate::~QChartPrivate() |
|
356 | 366 | { |
|
357 | 367 | |
|
358 | 368 | } |
|
359 | 369 | |
|
360 | 370 | void QChartPrivate::createConnections() |
|
361 | 371 | { |
|
362 | 372 | QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); |
|
363 | 373 | QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*))); |
|
364 | 374 | QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_presenter,SLOT(handleSeriesAdded(QSeries*,Domain*))); |
|
365 | 375 | QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_presenter,SLOT(handleSeriesRemoved(QSeries*))); |
|
366 | 376 | QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*,Domain*)),m_presenter,SLOT(handleAxisAdded(QChartAxis*,Domain*))); |
|
367 | 377 | QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),m_presenter,SLOT(handleAxisRemoved(QChartAxis*))); |
|
368 | 378 | } |
|
369 | 379 | |
|
370 | 380 | #include "moc_qchart.cpp" |
|
371 | 381 | |
|
372 | 382 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,245 +1,246 | |||
|
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 "qchartview.h" |
|
22 | 22 | #include "qchart_p.h" |
|
23 | 23 | #include "qchartview_p.h" |
|
24 | 24 | #include <QGraphicsScene> |
|
25 | 25 | #include <QRubberBand> |
|
26 | 26 | |
|
27 | 27 | |
|
28 | 28 | /*! |
|
29 | 29 | \enum QChartView::RubberBandPolicy |
|
30 | 30 | |
|
31 | 31 | This enum describes the different types of rubber bands that can be used for zoom rect selection |
|
32 | 32 | |
|
33 | 33 | \value NoRubberBand |
|
34 | 34 | \value VerticalRubberBand |
|
35 | 35 | \value HorizonalRubberBand |
|
36 | 36 | \value RectangleRubberBand |
|
37 | 37 | */ |
|
38 | 38 | |
|
39 | 39 | /*! |
|
40 | 40 | \class QChartView |
|
41 | 41 | \brief Standalone charting widget. |
|
42 | 42 | |
|
43 | 43 | QChartView is a standalone widget that can display charts. It does not require separate |
|
44 | 44 | QGraphicsScene to work. It manages the graphical representation of different types of |
|
45 | 45 | QChartSeries and other chart related objects like QChartAxis and QChartLegend. If you want to |
|
46 | 46 | display a chart in your existing QGraphicsScene, you can use the QChart class instead. |
|
47 | 47 | |
|
48 | 48 | \sa QChart |
|
49 | 49 | */ |
|
50 | 50 | |
|
51 | 51 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
52 | 52 | |
|
53 | 53 | /*! |
|
54 | 54 | Constructs a chartView object which is a child of a\a parent. |
|
55 | 55 | */ |
|
56 | 56 | QChartView::QChartView(QChart *chart,QWidget *parent) : |
|
57 | 57 | QGraphicsView(parent), |
|
58 | 58 | d_ptr(new QChartViewPrivate()) |
|
59 | 59 | { |
|
60 | Q_ASSERT(chart); | |
|
60 | 61 | d_ptr->m_scene = new QGraphicsScene(this); |
|
61 | 62 | d_ptr->m_chart = chart; |
|
62 | 63 | setFrameShape(QFrame::NoFrame); |
|
63 | 64 | setBackgroundRole(QPalette::Window); |
|
64 | 65 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
65 | 66 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
66 | 67 | setScene(d_ptr->m_scene); |
|
67 | 68 | d_ptr->m_scene->addItem(chart); |
|
68 | 69 | setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
|
69 | 70 | } |
|
70 | 71 | |
|
71 | 72 | |
|
72 | 73 | /*! |
|
73 | 74 | Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. |
|
74 | 75 | */ |
|
75 | 76 | QChartView::~QChartView() |
|
76 | 77 | { |
|
77 | 78 | } |
|
78 | 79 | |
|
79 | 80 | QChart* QChartView::chart() const |
|
80 | 81 | { |
|
81 | 82 | return d_ptr->m_chart; |
|
82 | 83 | } |
|
83 | 84 | |
|
84 | 85 | /*! |
|
85 | 86 | Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed. |
|
86 | 87 | */ |
|
87 | 88 | void QChartView::setRubberBand(const RubberBands& rubberBand) |
|
88 | 89 | { |
|
89 | 90 | d_ptr->m_rubberBandFlags=rubberBand; |
|
90 | 91 | |
|
91 | 92 | if (!d_ptr->m_rubberBandFlags) { |
|
92 | 93 | delete d_ptr->m_rubberBand; |
|
93 | 94 | d_ptr->m_rubberBand=0; |
|
94 | 95 | return; |
|
95 | 96 | } |
|
96 | 97 | |
|
97 | 98 | if (!d_ptr->m_rubberBand) { |
|
98 | 99 | d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this); |
|
99 | 100 | d_ptr->m_rubberBand->setEnabled(true); |
|
100 | 101 | } |
|
101 | 102 | } |
|
102 | 103 | |
|
103 | 104 | /*! |
|
104 | 105 | Returns the RubberBandPolicy that is currently being used by the widget. |
|
105 | 106 | */ |
|
106 | 107 | QChartView::RubberBands QChartView::rubberBand() const |
|
107 | 108 | { |
|
108 | 109 | return d_ptr->m_rubberBandFlags; |
|
109 | 110 | } |
|
110 | 111 | |
|
111 | 112 | /*! |
|
112 | 113 | 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. |
|
113 | 114 | If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation. |
|
114 | 115 | */ |
|
115 | 116 | void QChartView::mousePressEvent(QMouseEvent *event) |
|
116 | 117 | { |
|
117 | 118 | if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) { |
|
118 | 119 | |
|
119 | 120 | int padding = d_ptr->m_chart->margins().top(); |
|
120 | 121 | QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding); |
|
121 | 122 | |
|
122 | 123 | if (rect.contains(event->pos())) { |
|
123 | 124 | d_ptr->m_rubberBandOrigin = event->pos(); |
|
124 | 125 | d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize())); |
|
125 | 126 | d_ptr->m_rubberBand->show(); |
|
126 | 127 | event->accept(); |
|
127 | 128 | } |
|
128 | 129 | } |
|
129 | 130 | else { |
|
130 | 131 | QGraphicsView::mousePressEvent(event); |
|
131 | 132 | } |
|
132 | 133 | } |
|
133 | 134 | |
|
134 | 135 | /*! |
|
135 | 136 | If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry. |
|
136 | 137 | In other case the defualt QGraphicsView::mouseMoveEvent implementation is called. |
|
137 | 138 | */ |
|
138 | 139 | void QChartView::mouseMoveEvent(QMouseEvent *event) |
|
139 | 140 | { |
|
140 | 141 | if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) { |
|
141 | 142 | QRectF margins = d_ptr->m_chart->margins(); |
|
142 | 143 | QRectF geometry = d_ptr->m_chart->geometry(); |
|
143 | 144 | QRectF rect =geometry.adjusted(margins.left(),margins.top(),-margins.right(),-margins.bottom()); |
|
144 | 145 | int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x(); |
|
145 | 146 | int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y(); |
|
146 | 147 | if (!d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand)) { |
|
147 | 148 | d_ptr->m_rubberBandOrigin.setY(rect.top()); |
|
148 | 149 | height = rect.height(); |
|
149 | 150 | } |
|
150 | 151 | if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) { |
|
151 | 152 | d_ptr->m_rubberBandOrigin.setX(rect.left()); |
|
152 | 153 | width= rect.width(); |
|
153 | 154 | } |
|
154 | 155 | d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(),d_ptr->m_rubberBandOrigin.y(), width,height).normalized()); |
|
155 | 156 | } |
|
156 | 157 | else { |
|
157 | 158 | QGraphicsView::mouseMoveEvent(event); |
|
158 | 159 | } |
|
159 | 160 | } |
|
160 | 161 | |
|
161 | 162 | /*! |
|
162 | 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 |
|
163 | 164 | If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled. |
|
164 | 165 | */ |
|
165 | 166 | void QChartView::mouseReleaseEvent(QMouseEvent *event) |
|
166 | 167 | { |
|
167 | 168 | if(d_ptr->m_rubberBand) { |
|
168 | 169 | if (event->button() == Qt::LeftButton && d_ptr->m_rubberBand->isVisible()) { |
|
169 | 170 | d_ptr->m_rubberBand->hide(); |
|
170 | 171 | QRect rect = d_ptr->m_rubberBand->geometry(); |
|
171 | 172 | d_ptr->m_chart->zoomIn(rect); |
|
172 | 173 | event->accept(); |
|
173 | 174 | } |
|
174 | 175 | |
|
175 | 176 | if(event->button()==Qt::RightButton){ |
|
176 | 177 | d_ptr->m_chart->zoomOut(); |
|
177 | 178 | event->accept(); |
|
178 | 179 | } |
|
179 | 180 | } |
|
180 | 181 | else { |
|
181 | 182 | QGraphicsView::mouseReleaseEvent(event); |
|
182 | 183 | } |
|
183 | 184 | } |
|
184 | 185 | |
|
185 | 186 | /*! |
|
186 | 187 | Pressing + and - keys performs zoomIn() and zoomOut() respectivly. |
|
187 | 188 | In other \a event is passed to the QGraphicsView::keyPressEvent() implementation |
|
188 | 189 | */ |
|
189 | 190 | void QChartView::keyPressEvent(QKeyEvent *event) |
|
190 | 191 | { |
|
191 | 192 | switch (event->key()) { |
|
192 | 193 | case Qt::Key_Plus: |
|
193 | 194 | d_ptr->m_chart->zoomIn(); |
|
194 | 195 | break; |
|
195 | 196 | case Qt::Key_Minus: |
|
196 | 197 | d_ptr->m_chart->zoomOut(); |
|
197 | 198 | break; |
|
198 | 199 | case Qt::Key_Left: |
|
199 | 200 | d_ptr->m_chart->scrollLeft(); |
|
200 | 201 | break; |
|
201 | 202 | case Qt::Key_Right: |
|
202 | 203 | d_ptr->m_chart->scrollRight(); |
|
203 | 204 | break; |
|
204 | 205 | case Qt::Key_Up: |
|
205 | 206 | d_ptr->m_chart->scrollUp(); |
|
206 | 207 | break; |
|
207 | 208 | case Qt::Key_Down: |
|
208 | 209 | d_ptr->m_chart->scrollDown(); |
|
209 | 210 | break; |
|
210 | 211 | default: |
|
211 | 212 | QGraphicsView::keyPressEvent(event); |
|
212 | 213 | break; |
|
213 | 214 | } |
|
214 | 215 | } |
|
215 | 216 | |
|
216 | 217 | /*! |
|
217 | 218 | Resizes and updates the chart area using the \a event data |
|
218 | 219 | */ |
|
219 | 220 | void QChartView::resizeEvent(QResizeEvent *event) |
|
220 | 221 | { |
|
221 | 222 | QGraphicsView::resizeEvent(event); |
|
222 | 223 | d_ptr->m_chart->resize(size()); |
|
223 | 224 | setSceneRect(d_ptr->m_chart->geometry()); |
|
224 | 225 | } |
|
225 | 226 | |
|
226 | 227 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
227 | 228 | |
|
228 | 229 | QChartViewPrivate::QChartViewPrivate(): |
|
229 | 230 | m_scene(0), |
|
230 | 231 | m_chart(0), |
|
231 | 232 | m_presenter(0), |
|
232 | 233 | m_rubberBand(0), |
|
233 | 234 | m_rubberBandFlags(QChartView::NoRubberBand) |
|
234 | 235 | { |
|
235 | 236 | |
|
236 | 237 | } |
|
237 | 238 | |
|
238 | 239 | QChartViewPrivate::~QChartViewPrivate() |
|
239 | 240 | { |
|
240 | 241 | |
|
241 | 242 | } |
|
242 | 243 | |
|
243 | 244 | #include "moc_qchartview.cpp" |
|
244 | 245 | |
|
245 | 246 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now