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