##// END OF EJS Templates
Small description update
Marek Rosa -
r328:8194a15014e5
parent child
Show More
@@ -1,362 +1,362
1 1 #include "qchartview.h"
2 2 #include "qchart.h"
3 3 #include "qchartaxis.h"
4 4 #include <QGraphicsView>
5 5 #include <QGraphicsScene>
6 6 #include <QRubberBand>
7 7 #include <QResizeEvent>
8 8 #include <QDebug>
9 9
10 10 /*!
11 11 \enum QChartView::RubberBandPolicy
12 12
13 13 This enum describes the different types of rubber bands that can be used for zoom rect selection
14 14
15 15 \value NoRubberBand
16 16 \value VerticalRubberBand
17 17 \value HorizonalRubberBand
18 18 \value RectangleRubberBand
19 19 */
20 20
21 21 /*!
22 22 \class QChartView
23 23 \brief Standalone charting widget.
24 24
25 25 QChartView is a standalone widget that can display charts. It does not require separate QGraphicsScene to work. It manages the graphical
26 26 representation of different types of QChartSeries and other chart related objects like
27 27 QChartAxis and QChartLegend. If you want to display a chart in your existing QGraphicsScene, you can use the QChart class instead.
28 28
29 29 For example, to create an empty chart in a widget based application:
30 30 \snippet ../example/chartview/main.cpp 1
31 31 \image chartview_example.jpg
32 32
33 33 To add a line series:
34 34 \snippet ../example/chartview/main.cpp 2
35 35 \image chartview_example_series.jpg
36 36
37 37 To modify the visual appearance of the chart, you can use the pre-defined themes:
38 38 \snippet ../example/chartview/main.cpp 3
39 39 \image chartview_example_theme.jpg
40 40
41 41 \sa QChart
42 42 */
43 43
44 44 QTCOMMERCIALCHART_BEGIN_NAMESPACE
45 45
46 46 /*!
47 47 Constructs a chartView object which is a child of a\a parent.
48 48 */
49 49 QChartView::QChartView(QWidget *parent) :
50 50 QGraphicsView(parent),
51 51 m_scene(new QGraphicsScene(this)),
52 52 m_chart(new QChart()),
53 53 m_rubberBand(0),
54 54 m_verticalRubberBand(false),
55 55 m_horizonalRubberBand(false)
56 56 {
57 57 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
58 58 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
59 59 setScene(m_scene);
60 60 m_chart->setMargin(50);
61 61 m_scene->addItem(m_chart);
62 62 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
63 63 }
64 64
65 65
66 66 /*!
67 67 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
68 68 */
69 69 QChartView::~QChartView()
70 70 {
71 71 }
72 72
73 73 /*!
74 74 Resizes and updates the chart area using the \a event data
75 75 */
76 76 void QChartView::resizeEvent(QResizeEvent *event)
77 77 {
78 78 m_scene->setSceneRect(0,0,size().width(),size().height());
79 79 m_chart->resize(size());
80 80 QWidget::resizeEvent(event);
81 81 }
82 82
83 83 /*!
84 84 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
85 85 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
86 86 the y axis).
87 87 \sa removeSeries(), removeAllSeries()
88 88 */
89 89 void QChartView::addSeries(QChartSeries* series,QChartAxis *axisY)
90 90 {
91 91 m_chart->addSeries(series,axisY);
92 92 }
93 93
94 94 /*!
95 95 Removes the \a series specified in a perameter from the QChartView.
96 96 It releses its ownership of the specified QChartSeries object.
97 97 It does not delete the pointed QChartSeries data object
98 98 \sa addSeries(), removeAllSeries()
99 99 */
100 100 void QChartView::removeSeries(QChartSeries* series)
101 101 {
102 102 m_chart->removeSeries(series);
103 103 }
104 104
105 105 /*!
106 106 Removes all the QChartSeries that have been added to the QChartView
107 107 It also deletes the pointed QChartSeries data objects
108 108 \sa addSeries(), removeSeries()
109 109 */
110 110 void QChartView::removeAllSeries()
111 111 {
112 112 m_chart->removeAllSeries();
113 113 }
114 114
115 115 /*!
116 116 Zooms in the view by a factor of 2
117 117 */
118 118 void QChartView::zoomIn()
119 119 {
120 120 m_chart->zoomIn();
121 121 }
122 122
123 123 /*!
124 124 Zooms in the view to a maximum level at which \a rect is still fully visible.
125 125 */
126 126 void QChartView::zoomIn(const QRect& rect)
127 127 {
128 128 m_chart->zoomIn(rect);
129 129 }
130 130
131 131 /*!
132 132 Restores the view zoom level to the previous one.
133 133 */
134 134 void QChartView::zoomOut()
135 135 {
136 136 m_chart->zoomOut();
137 137 }
138 138
139 139 /*!
140 140 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
141 141 */
142 142 int QChartView::margin() const
143 143 {
144 144 return m_chart->margin();
145 145 }
146 146
147 147 /*!
148 148 Sets the chart \a title. A description text that is rendered above the chart.
149 149 */
150 150 void QChartView::setChartTitle(const QString& title)
151 151 {
152 152 m_chart->setChartTitle(title);
153 153 }
154 154
155 155 /*!
156 156 Sets the \a font that is used for rendering the description text that is rendered above the chart.
157 157 */
158 158 void QChartView::setChartTitleFont(const QFont& font)
159 159 {
160 160 m_chart->setChartTitleFont(font);
161 161 }
162 162
163 163 /*!
164 164 Sets the \a brush that is used for painting the background of the chart area of the QChartView widget.
165 165 */
166 166 void QChartView::setChartBackgroundBrush(const QBrush& brush)
167 167 {
168 168 m_chart->setChartBackgroundBrush(brush);
169 169 }
170 170
171 171 /*!
172 172 Sets the \a pen that is used for painting the background of the chart area of the QChartView widget.
173 173 */
174 174 void QChartView::setChartBackgroundPen(const QPen& pen)
175 175 {
176 176 m_chart->setChartBackgroundPen(pen);
177 177 }
178 178
179 179 /*!
180 180 Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed.
181 181 */
182 182 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
183 183 {
184 184 switch(policy) {
185 185 case VerticalRubberBand:
186 186 m_verticalRubberBand = true;
187 187 m_horizonalRubberBand = false;
188 188 break;
189 189 case HorizonalRubberBand:
190 190 m_verticalRubberBand = false;
191 191 m_horizonalRubberBand = true;
192 192 break;
193 193 case RectangleRubberBand:
194 194 m_verticalRubberBand = true;
195 195 m_horizonalRubberBand = true;
196 196 break;
197 197 case NoRubberBand:
198 198 default:
199 199 delete m_rubberBand;
200 200 m_rubberBand=0;
201 201 m_horizonalRubberBand = false;
202 202 m_verticalRubberBand = false;
203 203 return;
204 204 }
205 205 if(!m_rubberBand) {
206 206 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
207 207 m_rubberBand->setEnabled(true);
208 208 }
209 209 }
210 210
211 211 /*!
212 212 Returns the RubberBandPolicy that is currently being used by the widget.
213 213 */
214 214 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
215 215 {
216 216 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
217 217 if(m_horizonalRubberBand) return HorizonalRubberBand;
218 218 if(m_verticalRubberBand) return VerticalRubberBand;
219 219 return NoRubberBand;
220 220 }
221 221
222 222 /*!
223 223 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.
224 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation is called.
224 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
225 225 */
226 226 void QChartView::mousePressEvent(QMouseEvent *event)
227 227 {
228 228 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
229 229
230 230 int margin = m_chart->margin();
231 231 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
232 232
233 233 if (rect.contains(event->pos())) {
234 234 m_rubberBandOrigin = event->pos();
235 235 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
236 236 m_rubberBand->show();
237 237 event->accept();
238 238 }
239 239 }
240 240 else {
241 241 QGraphicsView::mousePressEvent(event);
242 242 }
243 243 }
244 244
245 245 /*!
246 246 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
247 247 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
248 248 */
249 249 void QChartView::mouseMoveEvent(QMouseEvent *event)
250 250 {
251 251 if(m_rubberBand && m_rubberBand->isVisible()) {
252 252 int margin = m_chart->margin();
253 253 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
254 254 int width = event->pos().x() - m_rubberBandOrigin.x();
255 255 int height = event->pos().y() - m_rubberBandOrigin.y();
256 256 if(!m_verticalRubberBand) {
257 257 m_rubberBandOrigin.setY(rect.top());
258 258 height = rect.height();
259 259 }
260 260 if(!m_horizonalRubberBand) {
261 261 m_rubberBandOrigin.setX(rect.left());
262 262 width= rect.width();
263 263 }
264 264 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
265 265 }
266 266 else {
267 267 QGraphicsView::mouseMoveEvent(event);
268 268 }
269 269 }
270 270
271 271 /*!
272 272 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
273 273 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
274 274 */
275 275 void QChartView::mouseReleaseEvent(QMouseEvent *event)
276 276 {
277 277 if(m_rubberBand) {
278 278 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
279 279 m_rubberBand->hide();
280 280 QRect rect = m_rubberBand->geometry();
281 281 m_chart->zoomIn(rect);
282 282 event->accept();
283 283 }
284 284
285 285 if(event->button()==Qt::RightButton)
286 286 m_chart->zoomReset();
287 287 }
288 288 else {
289 289 QGraphicsView::mouseReleaseEvent(event);
290 290 }
291 291 }
292 292
293 293 /*!
294 294 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
295 295 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
296 296 */
297 297 void QChartView::keyPressEvent(QKeyEvent *event)
298 298 {
299 299 switch (event->key()) {
300 300 case Qt::Key_Plus:
301 301 zoomIn();
302 302 break;
303 303 case Qt::Key_Minus:
304 304 zoomOut();
305 305 break;
306 306 default:
307 307 QGraphicsView::keyPressEvent(event);
308 308 break;
309 309 }
310 310 }
311 311
312 312 /*!
313 313 Sets the \a theme used by the chart for rendering the graphical representation of the data
314 314 \sa QChart::ChartTheme, chartTheme()
315 315 */
316 316 void QChartView::setChartTheme(QChart::ChartTheme theme)
317 317 {
318 318 m_chart->setChartTheme(theme);
319 319 }
320 320
321 321 /*!
322 322 Returns the theme enum used by the chart.
323 323 \sa setChartTheme()
324 324 */
325 325 QChart::ChartTheme QChartView::chartTheme() const
326 326 {
327 327 return m_chart->chartTheme();
328 328 }
329 329
330 330 /*!
331 331 Returns the pointer to the x axis object of the chart
332 332 */
333 333 QChartAxis* QChartView::axisX() const
334 334 {
335 335 return m_chart->axisX();
336 336 }
337 337
338 338 /*!
339 339 Returns the pointer to the y axis object of the chart
340 340 */
341 341 QChartAxis* QChartView::axisY() const
342 342 {
343 343 return m_chart->axisY();
344 344 }
345 345
346 346 /*!
347 347 Sets animation \a options for the chart
348 348 */
349 349 void QChartView::setAnimationOptions(QChart::AnimationOptions options)
350 350 {
351 351 m_chart->setAnimationOptions(options);
352 352 }
353 353
354 354 /*!
355 355 Returns animation options for the chart
356 356 */
357 357 QChart::AnimationOptions QChartView::animationOptions() const
358 358 {
359 359 return m_chart->animationOptions();
360 360 }
361 361
362 362 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now