##// END OF EJS Templates
Add gestures support for zoomlinechart example...
Jani Honkonen -
r1187:5f42a207d0d0
parent child
Show More
@@ -0,0 +1,61
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 #include "chart.h"
22 #include <QGesture>
23 #include <QGraphicsScene>
24 #include <QGraphicsView>
25
26 Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
27 :QChart(parent, wFlags)
28 {
29 // Seems that QGraphicsView (QChartView) does not grab gestures.
30 // They can only be grabbed here in the QGraphicsWidget (QChart).
31 grabGesture(Qt::PanGesture);
32 grabGesture(Qt::PinchGesture);
33 }
34
35 Chart::~Chart()
36 {
37
38 }
39
40 bool Chart::sceneEvent(QEvent *event)
41 {
42 if (event->type() == QEvent::Gesture)
43 return gestureEvent(static_cast<QGestureEvent*>(event));
44 return QChart::event(event);
45 }
46
47 bool Chart::gestureEvent(QGestureEvent* event)
48 {
49 if (QGesture *gesture = event->gesture(Qt::PanGesture)) {
50 QPanGesture *pan = static_cast<QPanGesture *>(gesture);
51 scroll(pan->delta());
52 }
53
54 if (QGesture *gesture = event->gesture(Qt::PinchGesture)) {
55 QPinchGesture *pinch = static_cast<QPinchGesture *>(gesture);
56 if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged)
57 zoom(pinch->scaleFactor());
58 }
59
60 return true;
61 }
@@ -0,0 +1,44
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 CHART_H
22 #define CHART_H
23
24 #include <QChart>
25
26 QTCOMMERCIALCHART_USE_NAMESPACE
27
28 class Chart : public QChart
29 {
30 public:
31 explicit Chart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
32 ~Chart();
33
34 protected:
35 bool sceneEvent(QEvent *event);
36
37 private:
38 bool gestureEvent(QGestureEvent* event);
39
40 private:
41
42 };
43
44 #endif // CHART_H
@@ -1,89 +1,100
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 "chartview.h"
22 22 #include <QMouseEvent>
23 23
24 24 ChartView::ChartView(QChart *chart, QWidget *parent) :
25 QChartView(chart, parent), m_rubberBand(QRubberBand::Rectangle, this), m_chart(chart)
25 QChartView(chart, parent),
26 m_isTouching(false)
26 27 {
28 setRubberBand(QChartView::RectangleRubberBand);
27 29 }
28 30
29 void ChartView::mousePressEvent(QMouseEvent *event)
31 bool ChartView::viewportEvent(QEvent *event)
30 32 {
31 if (event->button() != Qt::LeftButton)
32 return;
33 if (event->type() == QEvent::TouchBegin) {
34 // By default touch events are converted to mouse events. So
35 // after this event we will get a mouse event also but we want
36 // to handle touch events as gestures only. So we need this safeguard
37 // to block mouse events that are actually generated from touch.
38 m_isTouching = true;
33 39
34 m_origin = event->pos();
35 m_rubberBand.setGeometry(QRect(m_origin, QSize()));
36 m_rubberBand.show();
40 // Turn off animations when handling gestures they
41 // will only slow us down.
42 chart()->setAnimationOptions(QChart::NoAnimation);
43 }
44 return QChartView::viewportEvent(event);
45 }
37 46
38 event->accept();
47 void ChartView::mousePressEvent(QMouseEvent *event)
48 {
49 if (m_isTouching)
50 return;
51 QChartView::mousePressEvent(event);
39 52 }
40 53
41 54 void ChartView::mouseMoveEvent(QMouseEvent *event)
42 55 {
43 if (m_rubberBand.isVisible())
44 m_rubberBand.setGeometry(QRect(m_origin, event->pos()).normalized());
56 if (m_isTouching)
57 return;
58 QChartView::mouseMoveEvent(event);
45 59 }
46 60
47 61 void ChartView::mouseReleaseEvent(QMouseEvent *event)
48 62 {
49 if (event->button() == Qt::LeftButton && m_rubberBand.isVisible()) {
50 m_rubberBand.hide();
63 if (m_isTouching)
64 m_isTouching = false;
51 65
52 QRect rect = m_rubberBand.geometry();
53 m_chart->zoomIn(rect);
54 event->accept();
55 }
66 // Because we disabled animations when touch event was detected
67 // we must put them back on.
68 chart()->setAnimationOptions(QChart::SeriesAnimations);
56 69
57 if (event->button() == Qt::RightButton) {
58 m_chart->zoomOut();
59 }
70 QChartView::mouseReleaseEvent(event);
60 71 }
61 72
62 73 //![1]
63 74 void ChartView::keyPressEvent(QKeyEvent *event)
64 75 {
65 76 switch (event->key()) {
66 77 case Qt::Key_Plus:
67 m_chart->zoomIn();
78 chart()->zoomIn();
68 79 break;
69 80 case Qt::Key_Minus:
70 m_chart->zoomOut();
81 chart()->zoomOut();
71 82 break;
72 83 //![1]
73 84 case Qt::Key_Left:
74 m_chart->scrollLeft();
85 chart()->scrollLeft();
75 86 break;
76 87 case Qt::Key_Right:
77 m_chart->scrollRight();
88 chart()->scrollRight();
78 89 break;
79 90 case Qt::Key_Up:
80 m_chart->scrollUp();
91 chart()->scrollUp();
81 92 break;
82 93 case Qt::Key_Down:
83 m_chart->scrollDown();
94 chart()->scrollDown();
84 95 break;
85 96 default:
86 97 QGraphicsView::keyPressEvent(event);
87 98 break;
88 99 }
89 100 }
@@ -1,49 +1,49
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 CHARTVIEW_H
22 22 #define CHARTVIEW_H
23
23 24 #include <QChartView>
24 25 #include <QRubberBand>
25 26
26 27 QTCOMMERCIALCHART_USE_NAMESPACE
27 28
28 29 //![1]
29 30 class ChartView : public QChartView
30 31 //![1]
31 32 {
32 33 public:
33 34 ChartView(QChart *chart, QWidget *parent = 0);
34 35
35 36 //![2]
36 37 protected:
38 bool viewportEvent(QEvent *event);
37 39 void mousePressEvent(QMouseEvent *event);
38 40 void mouseMoveEvent(QMouseEvent *event);
39 41 void mouseReleaseEvent(QMouseEvent *event);
40 42 void keyPressEvent(QKeyEvent *event);
41 43 //![2]
42 44
43 45 private:
44 QRubberBand m_rubberBand;
45 QPoint m_origin;
46 QChart* m_chart;
46 bool m_isTouching;
47 47 };
48 48
49 49 #endif
@@ -1,56 +1,59
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 #include "chart.h"
21 22 #include "chartview.h"
22 23 #include <QApplication>
23 24 #include <QMainWindow>
24 25 #include <QLineSeries>
25 26
26 27 QTCOMMERCIALCHART_USE_NAMESPACE
27 28
28 29 int main(int argc, char *argv[])
29 30 {
30 31 QApplication a(argc, argv);
31 32
32 33 //![1]
33 34 QLineSeries* series = new QLineSeries();
34 35 qreal yValue = 0.0;
35 36 for (int i(0); i < 500; i++) {
36 37 yValue = yValue + (qreal) (qrand() % 10) / 500.0;
37 38 QPointF value((i + (qreal) rand() / (qreal) RAND_MAX) * (10.0 / 500.0), yValue);
38 39 *series << value;
39 40 }
40 41 //![1]
41 42
42 QChart* chart = new QChart();
43 Chart* chart = new Chart();
43 44 chart->addSeries(series);
44 45 chart->setTitle("Zoom in/out example");
45 chart->setAnimationOptions(QChart::AllAnimations);
46 chart->setAnimationOptions(QChart::SeriesAnimations);
46 47
47 48 ChartView* chartView = new ChartView(chart);
48 49 chartView->setRenderHint(QPainter::Antialiasing);
49 50
50 51 QMainWindow window;
51 52 window.setCentralWidget(chartView);
52 53 window.resize(400, 300);
54 window.grabGesture(Qt::PanGesture);
55 window.grabGesture(Qt::PinchGesture);
53 56 window.show();
54 57
55 58 return a.exec();
56 59 }
@@ -1,8 +1,9
1 1 !include( ../examples.pri ) {
2 2 error( "Couldn't find the examples.pri file!" )
3 3 }
4 4 TARGET = zoomlinechart
5 HEADERS += chartview.h
6 SOURCES += main.cpp chartview.cpp
5 HEADERS += chart.h chartview.h
6
7 SOURCES += main.cpp chart.cpp chartview.cpp
7 8
8 9 !system_build:mac: QMAKE_POST_LINK += "$$MAC_POST_LINK_PREFIX $$MAC_EXAMPLES_BIN_DIR"
@@ -1,411 +1,414
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 #include "chartpresenter_p.h"
21 21 #include "qchart.h"
22 22 #include "qchart_p.h"
23 23 #include "qaxis.h"
24 24 #include "chartdataset_p.h"
25 25 #include "charttheme_p.h"
26 26 #include "chartanimator_p.h"
27 27 #include "qabstractseries_p.h"
28 28 #include "qareaseries.h"
29 29 #include "chartaxis_p.h"
30 30 #include "areachartitem_p.h"
31 31 #include "chartbackground_p.h"
32 32
33 33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 34
35 35 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
36 36 m_chart(chart),
37 37 m_animator(0),
38 38 m_dataset(dataset),
39 39 m_chartTheme(0),
40 40 m_chartRect(QRectF(QPoint(0,0),m_chart->size())),
41 41 m_options(QChart::NoAnimation),
42 42 m_minLeftMargin(0),
43 43 m_minBottomMargin(0),
44 44 m_backgroundItem(0),
45 45 m_titleItem(0),
46 46 m_marginBig(60),
47 47 m_marginSmall(20),
48 48 m_marginTiny(10),
49 49 m_chartMargins(QRect(m_marginBig,m_marginBig,0,0))
50 50 {
51 51 }
52 52
53 53 ChartPresenter::~ChartPresenter()
54 54 {
55 55 delete m_chartTheme;
56 56 }
57 57
58 58 void ChartPresenter::setGeometry(const QRectF& rect)
59 59 {
60 60 m_rect = rect;
61 61 Q_ASSERT(m_rect.isValid());
62 62 updateLayout();
63 63 }
64 64
65 65 void ChartPresenter::setMinimumMarginWidth(ChartAxis* axis, qreal width)
66 66 {
67 67 switch(axis->axisType()){
68 68 case ChartAxis::X_AXIS:
69 69 {
70 70 if(width>m_chartRect.width()+ m_chartMargins.left()) {
71 71 m_minLeftMargin= width - m_chartRect.width();
72 72 updateLayout();
73 73 }
74 74 break;
75 75 }
76 76 case ChartAxis::Y_AXIS:
77 77 {
78 78
79 79 if(m_minLeftMargin!=width){
80 80 m_minLeftMargin= width;
81 81 updateLayout();
82 82 }
83 83 break;
84 84 }
85 85
86 86 }
87 87 }
88 88
89 89 void ChartPresenter::setMinimumMarginHeight(ChartAxis* axis, qreal height)
90 90 {
91 91 switch(axis->axisType()){
92 92 case ChartAxis::X_AXIS:
93 93 {
94 94 if(m_minBottomMargin!=height) {
95 95 m_minBottomMargin= height;
96 96 updateLayout();
97 97 }
98 98 break;
99 99 }
100 100 case ChartAxis::Y_AXIS:
101 101 {
102 102
103 103 if(height>m_chartMargins.bottom()+m_chartRect.height()){
104 104 m_minBottomMargin= height - m_chartRect.height();
105 105 updateLayout();
106 106 }
107 107 break;
108 108 }
109 109
110 110 }
111 111 }
112 112
113 113 void ChartPresenter::handleAxisAdded(QAxis* axis,Domain* domain)
114 114 {
115 115 ChartAxis* item = new ChartAxis(axis,this,axis==m_dataset->axisX()?ChartAxis::X_AXIS : ChartAxis::Y_AXIS);
116 116
117 117 if(m_options.testFlag(QChart::GridAxisAnimations)){
118 118 m_animator->addAnimation(item);
119 119 }
120 120
121 121 if(axis==m_dataset->axisX()){
122 122 m_chartTheme->decorate(axis,true);
123 123 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
124 124 //initialize
125 125 item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount());
126 126
127 127 }
128 128 else{
129 129 m_chartTheme->decorate(axis,false);
130 130 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
131 131 //initialize
132 132 item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount());
133 133 }
134 134
135 135 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
136 136 //initialize
137 137 item->handleGeometryChanged(m_chartRect);
138 138 m_axisItems.insert(axis, item);
139 139 }
140 140
141 141 void ChartPresenter::handleAxisRemoved(QAxis* axis)
142 142 {
143 143 ChartAxis* item = m_axisItems.take(axis);
144 144 Q_ASSERT(item);
145 145 if(m_animator) m_animator->removeAnimation(item);
146 146 delete item;
147 147 }
148 148
149 149
150 150 void ChartPresenter::handleSeriesAdded(QAbstractSeries* series,Domain* domain)
151 151 {
152 152 Chart *item = series->d_ptr->createGraphics(this);
153 153 Q_ASSERT(item);
154 154 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
155 155 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
156 156 //initialize
157 157 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
158 158 if(m_chartRect.isValid()) item->handleGeometryChanged(m_chartRect);
159 159 m_chartItems.insert(series,item);
160 160 }
161 161
162 162 void ChartPresenter::handleSeriesRemoved(QAbstractSeries* series)
163 163 {
164 164 Chart* item = m_chartItems.take(series);
165 165 Q_ASSERT(item);
166 166 if(m_animator) {
167 167 //small hack to handle area animations
168 168 if(series->type() == QAbstractSeries::SeriesTypeArea){
169 169 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
170 170 AreaChartItem* area = static_cast<AreaChartItem*>(item);
171 171 m_animator->removeAnimation(area->upperLineItem());
172 172 if(areaSeries->lowerSeries()) m_animator->removeAnimation(area->lowerLineItem());
173 173 }else
174 174 m_animator->removeAnimation(item);
175 175 }
176 176 delete item;
177 177 }
178 178
179 179 void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force)
180 180 {
181 181 if(m_chartTheme && m_chartTheme->id() == theme) return;
182 182 delete m_chartTheme;
183 183 m_chartTheme = ChartTheme::createTheme(theme);
184 184 m_chartTheme->setForced(force);
185 185 m_chartTheme->decorate(m_chart);
186 186 m_chartTheme->decorate(m_chart->legend());
187 187 resetAllElements();
188 188
189 189 // We do not want "force" to stay on.
190 190 // Bar/pie are calling decorate when adding/removing slices/bars which means
191 191 // that to preserve users colors "force" must not be on.
192 192 m_chartTheme->setForced(false);
193 193 }
194 194
195 195 QChart::ChartTheme ChartPresenter::theme()
196 196 {
197 197 return m_chartTheme->id();
198 198 }
199 199
200 200 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
201 201 {
202 202 if(m_options!=options) {
203 203
204 204 m_options=options;
205 205
206 206 if(m_options!=QChart::NoAnimation && !m_animator) {
207 207 m_animator= new ChartAnimator(this);
208 208 }
209 209 resetAllElements();
210 210 }
211 211
212 212 }
213 213
214 214 void ChartPresenter::resetAllElements()
215 215 {
216 216 QList<QAxis *> axisList = m_axisItems.uniqueKeys();
217 217 QList<QAbstractSeries *> seriesList = m_chartItems.uniqueKeys();
218 218
219 219 foreach(QAxis *axis, axisList) {
220 220 handleAxisRemoved(axis);
221 221 handleAxisAdded(axis,m_dataset->domain(axis));
222 222 }
223 223 foreach(QAbstractSeries *series, seriesList) {
224 224 handleSeriesRemoved(series);
225 225 handleSeriesAdded(series,m_dataset->domain(series));
226 226 // m_dataset->removeSeries(series);
227 227 // m_dataset->addSeries(series);
228 228 }
229 229 }
230 230
231 void ChartPresenter::zoomIn()
231 void ChartPresenter::zoomIn(qreal factor)
232 232 {
233 233 QRectF rect = chartGeometry();
234 rect.setWidth(rect.width()/2);
235 rect.setHeight(rect.height()/2);
234 rect.setWidth(rect.width()/factor);
235 rect.setHeight(rect.height()/factor);
236 236 rect.moveCenter(chartGeometry().center());
237 237 zoomIn(rect);
238 238 }
239 239
240 240 void ChartPresenter::zoomIn(const QRectF& rect)
241 241 {
242 242 QRectF r = rect.normalized();
243 243 r.translate(-m_chartMargins.topLeft());
244 if(!r.isValid()) return;
245 if(m_animator) {
244 if (!r.isValid())
245 return;
246 246
247 if (m_animator) {
247 248 QPointF point(r.center().x()/chartGeometry().width(),r.center().y()/chartGeometry().height());
248 249 m_animator->setState(ChartAnimator::ZoomInState,point);
249 250 }
251
250 252 m_dataset->zoomInDomain(r,chartGeometry().size());
251 if(m_animator) {
253
254 if (m_animator)
252 255 m_animator->setState(ChartAnimator::ShowState);
253 }
254 256 }
255 257
256 void ChartPresenter::zoomOut()
258 void ChartPresenter::zoomOut(qreal factor)
257 259 {
258 if(m_animator)
259 {
260 if (m_animator)
260 261 m_animator->setState(ChartAnimator::ZoomOutState);
261 }
262 262
263 QSizeF size = chartGeometry().size();
264 QRectF rect = chartGeometry();
265 rect.translate(-m_chartMargins.topLeft());
266 if(!rect.isValid()) return;
267 m_dataset->zoomOutDomain(rect.adjusted(size.width()/4,size.height()/4,-size.width()/4,-size.height()/4),size);
268 //m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size());
263 QRectF chartRect;
264 chartRect.setSize(chartGeometry().size());
269 265
270 if(m_animator){
266 QRectF rect;
267 rect.setSize(chartRect.size()/factor);
268 rect.moveCenter(chartRect.center());
269 if (!rect.isValid())
270 return;
271
272 m_dataset->zoomOutDomain(rect, chartRect.size());
273
274 if (m_animator)
271 275 m_animator->setState(ChartAnimator::ShowState);
272 }
273 276 }
274 277
275 278 void ChartPresenter::scroll(int dx,int dy)
276 279 {
277 280 if(m_animator){
278 281 if(dx<0) m_animator->setState(ChartAnimator::ScrollLeftState,QPointF());
279 282 if(dx>0) m_animator->setState(ChartAnimator::ScrollRightState,QPointF());
280 283 if(dy<0) m_animator->setState(ChartAnimator::ScrollUpState,QPointF());
281 284 if(dy>0) m_animator->setState(ChartAnimator::ScrollDownState,QPointF());
282 285 }
283 286
284 287 m_dataset->scrollDomain(dx,dy,chartGeometry().size());
285 288
286 289 if(m_animator){
287 290 m_animator->setState(ChartAnimator::ShowState);
288 291 }
289 292 }
290 293
291 294 QChart::AnimationOptions ChartPresenter::animationOptions() const
292 295 {
293 296 return m_options;
294 297 }
295 298
296 299 void ChartPresenter::updateLayout()
297 300 {
298 301 if (!m_rect.isValid()) return;
299 302
300 303 // recalculate title size
301 304
302 305 QSize titleSize;
303 306 int titlePadding=0;
304 307
305 308 if (m_titleItem) {
306 309 titleSize= m_titleItem->boundingRect().size().toSize();
307 310 }
308 311
309 312 //defaults
310 313 m_chartMargins = QRect(QPoint(m_minLeftMargin>m_marginBig?m_minLeftMargin:m_marginBig,m_marginBig),QPoint(m_marginBig,m_minBottomMargin>m_marginBig?m_minBottomMargin:m_marginBig));
311 314 titlePadding = m_chartMargins.top()/2;
312 315
313 316 QLegend* legend = m_chart->d_ptr->m_legend;
314 317
315 318 // recalculate legend position
316 319 if (legend->isAttachedToChart() && legend->isEnabled()) {
317 320
318 321 QRect legendRect;
319 322
320 323 // Reserve some space for legend
321 324 switch (legend->alignment()) {
322 325
323 326 case QLegend::AlignmentTop: {
324 327 int ledgendSize = legend->minHeight();
325 328 int topPadding = 2*m_marginTiny + titleSize.height() + ledgendSize + m_marginTiny;
326 329 m_chartMargins = QRect(QPoint(m_chartMargins.left(),topPadding),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
327 330 m_legendMargins = QRect(QPoint(m_chartMargins.left(),topPadding - (ledgendSize + m_marginTiny)),QPoint(m_chartMargins.right(),m_rect.height()-topPadding + m_marginTiny));
328 331 titlePadding = m_marginTiny + m_marginTiny;
329 332 break;
330 333 }
331 334 case QLegend::AlignmentBottom: {
332 335 int ledgendSize = legend->minHeight();
333 336 int bottomPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minBottomMargin;
334 337 m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(m_chartMargins.right(),bottomPadding));
335 338 m_legendMargins = QRect(QPoint(m_chartMargins.left(),m_rect.height()-bottomPadding + m_marginTiny + m_minBottomMargin),QPoint(m_chartMargins.right(),m_marginTiny + m_marginSmall));
336 339 titlePadding = m_chartMargins.top()/2;
337 340 break;
338 341 }
339 342 case QLegend::AlignmentLeft: {
340 343 int ledgendSize = legend->minWidth();
341 344 int leftPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minLeftMargin;
342 345 m_chartMargins = QRect(QPoint(leftPadding,m_chartMargins.top()),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
343 346 m_legendMargins = QRect(QPoint(m_marginTiny + m_marginSmall,m_chartMargins.top()),QPoint(m_rect.width()-leftPadding + m_marginTiny + m_minLeftMargin,m_chartMargins.bottom()));
344 347 titlePadding = m_chartMargins.top()/2;
345 348 break;
346 349 }
347 350 case QLegend::AlignmentRight: {
348 351 int ledgendSize = legend->minWidth();
349 352 int rightPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny;
350 353 m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(rightPadding,m_chartMargins.bottom()));
351 354 m_legendMargins = QRect(QPoint(m_rect.width()- rightPadding+ m_marginTiny ,m_chartMargins.top()),QPoint(m_marginTiny + m_marginSmall,m_chartMargins.bottom()));
352 355 titlePadding = m_chartMargins.top()/2;
353 356 break;
354 357 }
355 358 default: {
356 359 break;
357 360 }
358 361 }
359 362 }
360 363
361 364 if(m_rect.width()<2*(m_chartMargins.top()+m_chartMargins.bottom()) || m_rect.height()< 2*(m_chartMargins.top() + m_chartMargins.bottom()))
362 365 {
363 366 m_chart->setMinimumSize(2*(m_chartMargins.top()+m_chartMargins.bottom()),2*(m_chartMargins.top() + m_chartMargins.bottom()));
364 367 return;
365 368 }
366 369
367 370
368 371 // recalculate title position
369 372 if (m_titleItem) {
370 373 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
371 374 m_titleItem->setPos(center.x(),titlePadding);
372 375 }
373 376
374 377 //recalculate background gradient
375 378 if (m_backgroundItem) {
376 379 m_backgroundItem->setRect(m_rect.adjusted(m_marginTiny,m_marginTiny, -m_marginTiny, -m_marginTiny));
377 380 }
378 381
379 382
380 383 QRectF chartRect = m_rect.adjusted(m_chartMargins.left(),m_chartMargins.top(),-m_chartMargins.right(),-m_chartMargins.bottom());
381 384
382 385 legend->setGeometry(m_rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom()));
383 386
384 387 if(m_chartRect!=chartRect){
385 388 m_chartRect=chartRect;
386 389 emit geometryChanged(m_chartRect);
387 390 }
388 391
389 392
390 393 }
391 394
392 395 void ChartPresenter::createChartBackgroundItem()
393 396 {
394 397 if (!m_backgroundItem) {
395 398 m_backgroundItem = new ChartBackground(rootItem());
396 399 m_backgroundItem->setPen(Qt::NoPen);
397 400 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
398 401 }
399 402 }
400 403
401 404 void ChartPresenter::createChartTitleItem()
402 405 {
403 406 if (!m_titleItem) {
404 407 m_titleItem = new QGraphicsSimpleTextItem(rootItem());
405 408 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
406 409 }
407 410 }
408 411
409 412 #include "moc_chartpresenter_p.cpp"
410 413
411 414 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,124 +1,124
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 CHARTPRESENTER_H
22 22 #define CHARTPRESENTER_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include "qchart.h" //becouse of QChart::ChartThemeId //TODO
26 26 #include <QRectF>
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 30 class Chart;
31 31 class QAbstractSeries;
32 32 class ChartDataSet;
33 33 class Domain;
34 34 class ChartAxis;
35 35 class ChartTheme;
36 36 class ChartAnimator;
37 37 class ChartBackground;
38 38
39 39 class ChartPresenter: public QObject
40 40 {
41 41 Q_OBJECT
42 42 public:
43 43 enum ZValues {
44 44 BackgroundZValue = -1,
45 45 ShadesZValue,
46 46 GridZValue,
47 47 LineChartZValue,
48 48 BarSeriesZValue,
49 49 ScatterSeriesZValue,
50 50 PieSeriesZValue,
51 51 AxisZValue,
52 52 LegendZValue
53 53 };
54 54
55 55 ChartPresenter(QChart* chart,ChartDataSet *dataset);
56 56 virtual ~ChartPresenter();
57 57
58 58 ChartAnimator* animator() const { return m_animator; }
59 59 ChartTheme *chartTheme() const { return m_chartTheme; }
60 60 ChartDataSet *dataSet() const { return m_dataset; }
61 61 QGraphicsItem* rootItem() const { return m_chart; }
62 62
63 63 void setTheme(QChart::ChartTheme theme,bool force = true);
64 64 QChart::ChartTheme theme();
65 65
66 66 void setAnimationOptions(QChart::AnimationOptions options);
67 67 QChart::AnimationOptions animationOptions() const;
68 68
69 void zoomIn();
69 void zoomIn(qreal factor);
70 70 void zoomIn(const QRectF& rect);
71 void zoomOut();
71 void zoomOut(qreal factor);
72 72 void scroll(int dx,int dy);
73 73
74 74 void setGeometry(const QRectF& rect);
75 75 QRectF chartGeometry() const { return m_chartRect; }
76 76
77 77 void setMinimumMarginHeight(ChartAxis* axis, qreal height);
78 78 void setMinimumMarginWidth(ChartAxis* axis, qreal width);
79 79 qreal minimumLeftMargin() const { return m_minLeftMargin; }
80 80 qreal minimumBottomMargin() const { return m_minBottomMargin; }
81 81
82 82 public: //TODO: fix me
83 83 void resetAllElements();
84 84 void createChartBackgroundItem();
85 85 void createChartTitleItem();
86 86 QRectF margins() const { return m_chartMargins;}
87 87
88 88 public Q_SLOTS:
89 89 void handleSeriesAdded(QAbstractSeries* series,Domain* domain);
90 90 void handleSeriesRemoved(QAbstractSeries* series);
91 91 void handleAxisAdded(QAxis* axis,Domain* domain);
92 92 void handleAxisRemoved(QAxis* axis);
93 93 void updateLayout();
94 94
95 95 Q_SIGNALS:
96 96 void geometryChanged(const QRectF& rect);
97 97
98 98
99 99 private:
100 100 QChart* m_chart;
101 101 ChartAnimator* m_animator;
102 102 ChartDataSet* m_dataset;
103 103 ChartTheme *m_chartTheme;
104 104 QMap<QAbstractSeries *, Chart *> m_chartItems;
105 105 QMap<QAxis *, ChartAxis *> m_axisItems;
106 106 QRectF m_rect;
107 107 QRectF m_chartRect;
108 108 QChart::AnimationOptions m_options;
109 109 qreal m_minLeftMargin;
110 110 qreal m_minBottomMargin;
111 111 public: //TODO: fixme
112 112 ChartBackground* m_backgroundItem;
113 113 QGraphicsSimpleTextItem* m_titleItem;
114 114 int m_marginBig;
115 115 int m_marginSmall;
116 116 int m_marginTiny;
117 117 QRectF m_chartMargins;
118 118 QRectF m_legendMargins;
119 119
120 120 };
121 121
122 122 QTCOMMERCIALCHART_END_NAMESPACE
123 123
124 124 #endif /* CHARTPRESENTER_H_ */
@@ -1,440 +1,470
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 "legendscroller_p.h"
24 24 #include "qlegend_p.h"
25 25 #include "chartbackground_p.h"
26 26 #include "qaxis.h"
27 27 #include <QGraphicsScene>
28 28 #include <QGraphicsSceneResizeEvent>
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
32 32 /*!
33 33 \enum QChart::ChartTheme
34 34
35 35 This enum describes the theme used by the chart.
36 36
37 37 \value ChartThemeLight The default theme
38 38 \value ChartThemeBlueCerulean
39 39 \value ChartThemeDark
40 40 \value ChartThemeBrownSand
41 41 \value ChartThemeBlueNcs
42 42 \value ChartThemeHighContrast
43 43 \value ChartThemeBlueIcy
44 44 */
45 45
46 46 /*!
47 47 \enum QChart::AnimationOption
48 48
49 49 For enabling/disabling animations. Defaults to NoAnimation.
50 50
51 51 \value NoAnimation
52 52 \value GridAxisAnimations
53 53 \value SeriesAnimations
54 54 \value AllAnimations
55 55 */
56 56
57 57 /*!
58 58 \class QChart
59 59 \brief QtCommercial chart API.
60 60
61 61 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
62 62 representation of different types of series and other chart related objects like
63 63 QAxis and QLegend. If you simply want to show a chart in a layout, you can use the
64 64 convenience class QChartView instead of QChart.
65 65 \sa QChartView
66 66 */
67 67
68 68 /*!
69 69 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
70 70 */
71 71 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
72 72 d_ptr(new QChartPrivate())
73 73 {
74 74 d_ptr->m_dataset = new ChartDataSet(this);
75 75 d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset);
76 76 d_ptr->createConnections();
77 77 d_ptr->m_legend = new LegendScroller(this);
78 78 d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false);
79 79 }
80 80
81 81 /*!
82 82 Destroys the object and it's children, like series and axis objects added to it.
83 83 */
84 84 QChart::~QChart()
85 85 {
86 86 //delete first presenter , since this is a root of all the graphical items
87 87 delete d_ptr->m_presenter;
88 88 d_ptr->m_presenter=0;
89 89 }
90 90
91 91 /*!
92 92 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
93 93 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
94 94 the y axis).
95 95 */
96 96 void QChart::addSeries(QAbstractSeries *series, QAxis *axisY)
97 97 {
98 98 Q_ASSERT(series);
99 99 d_ptr->m_dataset->addSeries(series, axisY);
100 100 }
101 101
102 102 /*!
103 103 Removes the \a series specified in a perameter from the QChartView.
104 104 It releses its ownership of the specified QChartSeries object.
105 105 It does not delete the pointed QChartSeries data object
106 106 \sa addSeries(), removeAllSeries()
107 107 */
108 108 void QChart::removeSeries(QAbstractSeries *series)
109 109 {
110 110 Q_ASSERT(series);
111 111 d_ptr->m_dataset->removeSeries(series);
112 112 }
113 113
114 114 /*!
115 115 Removes all the QChartSeries that have been added to the QChartView
116 116 It also deletes the pointed QChartSeries data objects
117 117 \sa addSeries(), removeSeries()
118 118 */
119 119 void QChart::removeAllSeries()
120 120 {
121 121 d_ptr->m_dataset->removeAllSeries();
122 122 }
123 123
124 124 /*!
125 125 Sets the \a brush that is used for painting the background of the chart area.
126 126 */
127 127 void QChart::setBackgroundBrush(const QBrush& brush)
128 128 {
129 129 //TODO: refactor me
130 130 d_ptr->m_presenter->createChartBackgroundItem();
131 131 d_ptr->m_presenter->m_backgroundItem->setBrush(brush);
132 132 d_ptr->m_presenter->m_backgroundItem->update();
133 133 }
134 134
135 135 /*!
136 136 Gets the brush that is used for painting the background of the chart area.
137 137 */
138 138 QBrush QChart::backgroundBrush() const
139 139 {
140 140 //TODO: refactor me
141 141 if (!d_ptr->m_presenter->m_backgroundItem) return QBrush();
142 142 return (d_ptr->m_presenter->m_backgroundItem)->brush();
143 143 }
144 144
145 145 /*!
146 146 Sets the \a pen that is used for painting the background of the chart area.
147 147 */
148 148 void QChart::setBackgroundPen(const QPen& pen)
149 149 {
150 150 //TODO: refactor me
151 151 d_ptr->m_presenter->createChartBackgroundItem();
152 152 d_ptr->m_presenter->m_backgroundItem->setPen(pen);
153 153 d_ptr->m_presenter->m_backgroundItem->update();
154 154 }
155 155
156 156 /*!
157 157 Gets the pen that is used for painting the background of the chart area.
158 158 */
159 159 QPen QChart::backgroundPen() const
160 160 {
161 161 //TODO: refactor me
162 162 if (!d_ptr->m_presenter->m_backgroundItem) return QPen();
163 163 return d_ptr->m_presenter->m_backgroundItem->pen();
164 164 }
165 165
166 166 /*!
167 167 Sets the chart \a title. The description text that is drawn above the chart.
168 168 */
169 169 void QChart::setTitle(const QString& title)
170 170 {
171 171 //TODO: refactor me
172 172 d_ptr->m_presenter->createChartTitleItem();
173 173 d_ptr->m_presenter->m_titleItem->setText(title);
174 174 d_ptr->m_presenter->updateLayout();
175 175 }
176 176
177 177 /*!
178 178 Returns the chart title. The description text that is drawn above the chart.
179 179 */
180 180 QString QChart::title() const
181 181 {
182 182 //TODO: refactor me
183 183 if (d_ptr->m_presenter->m_titleItem)
184 184 return d_ptr->m_presenter->m_titleItem->text();
185 185 else
186 186 return QString();
187 187 }
188 188
189 189 /*!
190 190 Sets the \a font that is used for drawing the chart description text that is rendered above the chart.
191 191 */
192 192 void QChart::setTitleFont(const QFont& font)
193 193 {
194 194 //TODO: refactor me
195 195 d_ptr->m_presenter->createChartTitleItem();
196 196 d_ptr->m_presenter->m_titleItem->setFont(font);
197 197 d_ptr->m_presenter->updateLayout();
198 198 }
199 199
200 200 /*!
201 201 Gets the font that is used for drawing the chart description text that is rendered above the chart.
202 202 */
203 203 QFont QChart::titleFont() const
204 204 {
205 205 if (d_ptr->m_presenter->m_titleItem)
206 206 return d_ptr->m_presenter->m_titleItem->font();
207 207 else
208 208 return QFont();
209 209 }
210 210
211 211 /*!
212 212 Sets the \a brush used for rendering the title text.
213 213 */
214 214 void QChart::setTitleBrush(const QBrush &brush)
215 215 {
216 216 //TODO: refactor me
217 217 d_ptr->m_presenter->createChartTitleItem();
218 218 d_ptr->m_presenter->m_titleItem->setBrush(brush);
219 219 d_ptr->m_presenter->updateLayout();
220 220 }
221 221
222 222 /*!
223 223 Returns the brush used for rendering the title text.
224 224 */
225 225 QBrush QChart::titleBrush() const
226 226 {
227 227 //TODO: refactor me
228 228 if (!d_ptr->m_presenter->m_titleItem) return QBrush();
229 229 return d_ptr->m_presenter->m_titleItem->brush();
230 230 }
231 231
232 232 /*!
233 233 Sets the \a theme used by the chart for rendering the graphical representation of the data
234 234 \sa theme()
235 235 */
236 236 void QChart::setTheme(QChart::ChartTheme theme)
237 237 {
238 238 d_ptr->m_presenter->setTheme(theme);
239 239 }
240 240
241 241 /*!
242 242 Returns the theme enum used by the chart.
243 243 \sa ChartTheme, setTheme()
244 244 */
245 245 QChart::ChartTheme QChart::theme() const
246 246 {
247 247 return d_ptr->m_presenter->theme();
248 248 }
249 249
250 250 /*!
251 251 Zooms in the view by a factor of 2
252 252 */
253 253 void QChart::zoomIn()
254 254 {
255 d_ptr->m_presenter->zoomIn();
255 d_ptr->m_presenter->zoomIn(2.0);
256 256 }
257 257
258 258 /*!
259 259 Zooms in the view to a maximum level at which \a rect is still fully visible.
260 260 */
261 261 void QChart::zoomIn(const QRectF& rect)
262 262 {
263 263 if (!rect.isValid()) return;
264 264 d_ptr->m_presenter->zoomIn(rect);
265 265 }
266 266
267 267 /*!
268 268 Restores the view zoom level to the previous one.
269 269 */
270 270 void QChart::zoomOut()
271 271 {
272 d_ptr->m_presenter->zoomOut();
272 d_ptr->m_presenter->zoomOut(2.0);
273 }
274
275 /*!
276 Zooms in the view by a \a factor.
277
278 A factor over 1.0 zooms the view in and factor between 0.0 and 1.0 zooms out.
279 */
280 void QChart::zoom(qreal factor)
281 {
282 if (qFuzzyIsNull(factor))
283 return;
284
285 if (qFuzzyCompare(factor, 1.0))
286 return;
287
288 if (factor < 0)
289 return;
290
291 if (factor > 1.0)
292 d_ptr->m_presenter->zoomIn(factor);
293 else
294 d_ptr->m_presenter->zoomOut(1.0 / factor);
273 295 }
274 296
275 297 /*!
276 298 Returns the pointer to the x axis object of the chart
277 299 */
278 300 QAxis* QChart::axisX() const
279 301 {
280 302 return d_ptr->m_dataset->axisX();
281 303 }
282 304
283 305 /*!
284 306 Returns the pointer to the y axis object of the \a series
285 307 If no \a series is provided then default Y axis of the chart is returned.
286 308 */
287 309 QAxis* QChart::axisY(QAbstractSeries *series) const
288 310 {
289 311 return d_ptr->m_dataset->axisY(series);
290 312 }
291 313
292 314 /*!
293 315 Returns the legend object of the chart. Ownership stays in chart.
294 316 */
295 317 QLegend* QChart::legend() const
296 318 {
297 319 return d_ptr->m_legend;
298 320 }
299 321
300 322 /*!
301 323 Returns the rect that contains information about margins (distance between chart widget edge and axes).
302 324 Individual margins can be obtained by calling left, top, right, bottom on the returned rect.
303 325 */
304 326 QRectF QChart::margins() const
305 327 {
306 328 return d_ptr->m_presenter->margins();
307 329 }
308 330
309 331
310 332 /*!
311 333 Resizes and updates the chart area using the \a event data
312 334 */
313 335 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
314 336 {
315 337 d_ptr->m_rect = QRectF(QPoint(0,0),event->newSize());
316 338 QGraphicsWidget::resizeEvent(event);
317 339 d_ptr->m_presenter->setGeometry(d_ptr->m_rect);
318 340 }
319 341
320 342 /*!
321 343 Sets animation \a options for the chart
322 344 */
323 345 void QChart::setAnimationOptions(AnimationOptions options)
324 346 {
325 347 d_ptr->m_presenter->setAnimationOptions(options);
326 348 }
327 349
328 350 /*!
329 351 Returns animation options for the chart
330 352 */
331 353 QChart::AnimationOptions QChart::animationOptions() const
332 354 {
333 355 return d_ptr->m_presenter->animationOptions();
334 356 }
335 357
336 358 /*!
337 359 Scrolls the visible area of the chart to the left by the distance between two x axis ticks
338 360 */
339 361 void QChart::scrollLeft()
340 362 {
341 363 d_ptr->m_presenter->scroll(-d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0);
342 364 }
343 365
344 366 /*!
345 367 Scrolls the visible area of the chart to the right by the distance between two x axis ticks
346 368 */
347 369 void QChart::scrollRight()
348 370 {
349 371 d_ptr->m_presenter->scroll(d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0);
350 372 }
351 373
352 374 /*!
353 375 Scrolls the visible area of the chart up by the distance between two y axis ticks
354 376 */
355 377 void QChart::scrollUp()
356 378 {
357 379 d_ptr->m_presenter->scroll(0,d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1));
358 380 }
359 381
360 382 /*!
361 383 Scrolls the visible area of the chart down by the distance between two y axis ticks
362 384 */
363 385 void QChart::scrollDown()
364 386 {
365 387 d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1));
366 388 }
367 389
368 390 /*!
391 Scrolls the visible area of the chart by the distance defined in the \a delta.
392 */
393 void QChart::scroll(const QPointF &delta)
394 {
395 d_ptr->m_presenter->scroll(-delta.x(), delta.y());
396 }
397
398 /*!
369 399 Sets the chart background visibility state to \a visible
370 400 */
371 401 void QChart::setBackgroundVisible(bool visible)
372 402 {
373 403 //TODO: refactor me
374 404 d_ptr->m_presenter->createChartBackgroundItem();
375 405 d_ptr->m_presenter->m_backgroundItem->setVisible(visible);
376 406 }
377 407
378 408 /*!
379 409 Returns the chart's background visibility state
380 410 */
381 411 bool QChart::isBackgroundVisible() const
382 412 {
383 413 //TODO: refactor me
384 414 if (!d_ptr->m_presenter->m_backgroundItem)
385 415 return false;
386 416
387 417 return d_ptr->m_presenter->m_backgroundItem->isVisible();
388 418 }
389 419
390 420 /*!
391 421 Sets the background drop shadow effect state to \a enabled.
392 422 */
393 423 void QChart::setBackgroundDropShadowEnabled(bool enabled)
394 424 {
395 425 d_ptr->m_presenter->createChartBackgroundItem();
396 426 d_ptr->m_presenter->m_backgroundItem->setDropShadowEnabled(enabled);
397 427 }
398 428
399 429 /*!
400 430 Returns true if the drop shadow effect is enabled for the chart background.
401 431 */
402 432 bool QChart::isBackgroundDropShadowEnabled() const
403 433 {
404 434 if (!d_ptr->m_presenter->m_backgroundItem)
405 435 return false;
406 436
407 437 return d_ptr->m_presenter->m_backgroundItem->isDropShadowEnabled();
408 438 }
409 439
410 440 QList<QAbstractSeries*> QChart::series() const
411 441 {
412 442 return d_ptr->m_dataset->series();
413 443 }
414 444
415 445 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
416 446
417 447 QChartPrivate::QChartPrivate():
418 448 m_legend(0),
419 449 m_dataset(0),
420 450 m_presenter(0)
421 451 {
422 452
423 453 }
424 454
425 455 QChartPrivate::~QChartPrivate()
426 456 {
427 457
428 458 }
429 459
430 460 void QChartPrivate::createConnections()
431 461 {
432 462 QObject::connect(m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),m_presenter,SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
433 463 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),m_presenter,SLOT(handleSeriesRemoved(QAbstractSeries*)));
434 464 QObject::connect(m_dataset,SIGNAL(axisAdded(QAxis*,Domain*)),m_presenter,SLOT(handleAxisAdded(QAxis*,Domain*)));
435 465 QObject::connect(m_dataset,SIGNAL(axisRemoved(QAxis*)),m_presenter,SLOT(handleAxisRemoved(QAxis*)));
436 466 }
437 467
438 468 #include "moc_qchart.cpp"
439 469
440 470 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,122 +1,124
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 <QAbstractSeries>
25 25 #include <QLegend>
26 26 #include <QGraphicsWidget>
27 27
28 28 class QGraphicsSceneResizeEvent;
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
32 32 class QAbstractSeries;
33 33 class QAxis;
34 34 class QLegend;
35 35 struct QChartPrivate;
36 36
37 37 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
38 38 {
39 39 Q_OBJECT
40 40 Q_ENUMS(ChartTheme)
41 41 Q_ENUMS(AnimationOption)
42 42
43 43 public:
44 44 enum ChartTheme {
45 45 ChartThemeLight = 0,
46 46 ChartThemeBlueCerulean,
47 47 ChartThemeDark,
48 48 ChartThemeBrownSand,
49 49 ChartThemeBlueNcs,
50 50 ChartThemeHighContrast,
51 51 ChartThemeBlueIcy
52 52 };
53 53
54 54 enum AnimationOption {
55 55 NoAnimation = 0x0,
56 56 GridAxisAnimations = 0x1,
57 57 SeriesAnimations =0x2,
58 58 AllAnimations = 0x3
59 59 };
60 60
61 61 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
62 62
63 63 public:
64 64 explicit QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
65 65 ~QChart();
66 66
67 67 void addSeries(QAbstractSeries *series, QAxis *axisY = 0);
68 68 void removeSeries(QAbstractSeries *series);
69 69 void removeAllSeries();
70 70 QList<QAbstractSeries*> series() const;
71 71
72 72 void setTheme(QChart::ChartTheme theme);
73 73 QChart::ChartTheme theme() const;
74 74
75 75 void setTitle(const QString& title);
76 76 QString title() const;
77 77 void setTitleFont(const QFont& font);
78 78 QFont titleFont() const;
79 79 void setTitleBrush(const QBrush &brush);
80 80 QBrush titleBrush() const;
81 81
82 82 void setBackgroundBrush(const QBrush &brush);
83 83 QBrush backgroundBrush() const;
84 84 void setBackgroundPen(const QPen &pen);
85 85 QPen backgroundPen() const;
86 86 void setBackgroundVisible(bool visible = true);
87 87 bool isBackgroundVisible() const;
88 88 void setBackgroundDropShadowEnabled(bool enabled = true);
89 89 bool isBackgroundDropShadowEnabled() const;
90 90
91 91 void setAnimationOptions(AnimationOptions options);
92 92 AnimationOptions animationOptions() const;
93 93
94 94 void zoomIn();
95 95 void zoomIn(const QRectF &rect);
96 96 void zoomOut();
97 void zoom(qreal factor);
97 98 void scrollLeft();
98 99 void scrollRight();
99 100 void scrollUp();
100 101 void scrollDown();
102 void scroll(const QPointF &delta);
101 103
102 104 QAxis* axisX() const;
103 105 QAxis* axisY(QAbstractSeries* series = 0) const;
104 106
105 107 QLegend* legend() const;
106 108 QRectF margins() const;
107 109
108 110 protected:
109 111 void resizeEvent(QGraphicsSceneResizeEvent *event);
110 112
111 113 protected:
112 114 QScopedPointer<QChartPrivate> d_ptr;
113 115 friend class QLegend;
114 116 friend class ChartPresenter;
115 117 Q_DISABLE_COPY(QChart)
116 118 };
117 119
118 120 QTCOMMERCIALCHART_END_NAMESPACE
119 121
120 122 Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions)
121 123
122 124 #endif
General Comments 0
You need to be logged in to leave comments. Login now