##// END OF EJS Templates
adds QChartView PIMPL, refactor public API
Michal Klocek -
r746:646ccd5c026b
parent child
Show More
@@ -1,3 +1,23
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>
@@ -59,7 +59,7 public:
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);
@@ -1,7 +1,15
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"
@@ -12,9 +20,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 20
13 21 class QChart;
14 22
15 class QChartPrivate
23 struct QChartPrivate
16 24 {
17 public:
18 25 QChartPrivate(QChart *parent);
19 26 ~QChartPrivate();
20 27
@@ -30,8 +37,6 public:
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
@@ -1,11 +1,29
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 "qchartaxis.h"
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
@@ -35,20 +53,20 QTCOMMERCIALCHART_BEGIN_NAMESPACE
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(m_chart);
68 setScene(d_ptr->m_scene);
69 d_ptr->m_scene->addItem(chart);
52 70 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
53 71 }
54 72
@@ -60,168 +78,36 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::setRubberBandPolicy(const RubberBandPolicy policy)
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::RubberBandPolicy QChartView::rubberBandPolicy() const
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 /*!
@@ -230,15 +116,15 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
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 = m_chart->d_ptr->m_presenter->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 }
@@ -253,20 +139,20 void QChartView::mousePressEvent(QMouseEvent *event)
253 139 */
254 140 void QChartView::mouseMoveEvent(QMouseEvent *event)
255 141 {
256 if(m_rubberBand && m_rubberBand->isVisible()) {
257 int padding = m_chart->d_ptr->m_presenter->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(!m_verticalRubberBand) {
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(!m_horizonalRubberBand) {
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);
@@ -279,16 +165,16 void QChartView::mouseMoveEvent(QMouseEvent *event)
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 }
@@ -305,10 +191,10 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);
@@ -317,100 +203,31 void QChartView::keyPressEvent(QKeyEvent *event)
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,10 +1,29
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;
@@ -12,70 +31,39 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
@@ -31,7 +31,8 PRIVATE_HEADERS += \
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 \
General Comments 0
You need to be logged in to leave comments. Login now