@@ -0,0 +1,101 | |||||
|
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 <QValuesAxis> | |||
|
22 | #include <QAbstractAxis> | |||
|
23 | #include <QDebug> | |||
|
24 | ||||
|
25 | #include "chart.h" | |||
|
26 | ||||
|
27 | Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags, QLineSeries *series) | |||
|
28 | : QChart(parent, wFlags), m_series(series) | |||
|
29 | { | |||
|
30 | m_clicked = false; | |||
|
31 | } | |||
|
32 | ||||
|
33 | Chart::~Chart() | |||
|
34 | { | |||
|
35 | } | |||
|
36 | ||||
|
37 | void Chart::clickPoint(const QPointF &point) | |||
|
38 | { | |||
|
39 | //Get all points from the series. | |||
|
40 | QList<QPointF> points = m_series->points(); | |||
|
41 | //Construct a small rectangle around the clicked point | |||
|
42 | //to identify the real point clicked from the series. | |||
|
43 | QRectF clickRect(point.x() - 0.5, point.y() - 0.5, 1.0, 1.0); | |||
|
44 | ||||
|
45 | //Find the clicked point to be moved. | |||
|
46 | foreach (QPointF p, points) { | |||
|
47 | if (clickRect.contains(p)) { | |||
|
48 | m_movingPoint = p; | |||
|
49 | m_clicked = true; | |||
|
50 | return; | |||
|
51 | } | |||
|
52 | } | |||
|
53 | } | |||
|
54 | ||||
|
55 | void Chart::setPointClicked(bool clicked) | |||
|
56 | { | |||
|
57 | m_clicked = clicked; | |||
|
58 | } | |||
|
59 | ||||
|
60 | void Chart::handlePointMove(const QPoint &point) | |||
|
61 | { | |||
|
62 | if (m_clicked) { | |||
|
63 | //Map the point clicked from the ChartView | |||
|
64 | //to the area occupied by the chart. | |||
|
65 | QPoint mappedPoint = point; | |||
|
66 | mappedPoint.setX(point.x()-this->plotArea().x()); | |||
|
67 | mappedPoint.setY(point.y()-this->plotArea().y()); | |||
|
68 | ||||
|
69 | //Get the x- and y axis to be able to convert the mapped | |||
|
70 | //coordinate point to the charts scale. | |||
|
71 | QAbstractAxis * axisx = this->axisX(); | |||
|
72 | QValuesAxis* haxis = 0; | |||
|
73 | if (axisx->type() == QAbstractAxis::AxisTypeValues) | |||
|
74 | haxis = qobject_cast<QValuesAxis*>(axisx); | |||
|
75 | ||||
|
76 | QAbstractAxis * axisy = this->axisY(); | |||
|
77 | QValuesAxis* vaxis = 0; | |||
|
78 | if (axisy->type() == QAbstractAxis::AxisTypeValues) | |||
|
79 | vaxis = qobject_cast<QValuesAxis*>(axisy); | |||
|
80 | ||||
|
81 | if (haxis && vaxis) { | |||
|
82 | //Calculate the "unit" between points on the x | |||
|
83 | //y axis. | |||
|
84 | double xUnit = this->plotArea().width()/haxis->max(); | |||
|
85 | double yUnit = this->plotArea().height()/vaxis->max(); | |||
|
86 | ||||
|
87 | //Convert the mappedPoint to the actual chart scale. | |||
|
88 | double x = mappedPoint.x()/xUnit; | |||
|
89 | double y = vaxis->max() - mappedPoint.y()/yUnit; | |||
|
90 | ||||
|
91 | //Replace the old point with the new one. | |||
|
92 | m_series->replace(m_movingPoint, QPointF(x, y)); | |||
|
93 | ||||
|
94 | //Update the m_movingPoint so we are able to | |||
|
95 | //do the replace also during mousemoveEvent. | |||
|
96 | m_movingPoint.setX(x); | |||
|
97 | m_movingPoint.setY(y); | |||
|
98 | } | |||
|
99 | } | |||
|
100 | } | |||
|
101 |
@@ -0,0 +1,52 | |||||
|
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 | #include <QLineSeries> | |||
|
26 | ||||
|
27 | QTCOMMERCIALCHART_USE_NAMESPACE | |||
|
28 | ||||
|
29 | class Chart : public QChart | |||
|
30 | { | |||
|
31 | Q_OBJECT | |||
|
32 | public: | |||
|
33 | explicit Chart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0, QLineSeries *series = 0); | |||
|
34 | ~Chart(); | |||
|
35 | ||||
|
36 | public slots: | |||
|
37 | void clickPoint(const QPointF &point); | |||
|
38 | ||||
|
39 | public: | |||
|
40 | void handlePointMove(const QPoint &point); | |||
|
41 | void setPointClicked(bool clicked); | |||
|
42 | ||||
|
43 | private: | |||
|
44 | QLineSeries *m_series; | |||
|
45 | QPointF m_movingPoint; | |||
|
46 | ||||
|
47 | //Boolean value to determine if an actual point in the | |||
|
48 | //series is clicked. | |||
|
49 | bool m_clicked; | |||
|
50 | }; | |||
|
51 | ||||
|
52 | #endif // CHART_H |
@@ -0,0 +1,9 | |||||
|
1 | !include( ../demos.pri ):error( "Couldn't find the demos.pri file!" ) | |||
|
2 | ||||
|
3 | QT += core gui | |||
|
4 | ||||
|
5 | TARGET = chartinteractions | |||
|
6 | TEMPLATE = app | |||
|
7 | ||||
|
8 | HEADERS += chart.h chartview.h | |||
|
9 | SOURCES += main.cpp chart.cpp chartview.cpp |
@@ -0,0 +1,50 | |||||
|
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 <QMouseEvent> | |||
|
22 | #include "chartview.h" | |||
|
23 | #include "chart.h" | |||
|
24 | ||||
|
25 | ChartView::ChartView(Chart *chart, QWidget *parent) : | |||
|
26 | QChartView(chart, parent) | |||
|
27 | { | |||
|
28 | m_chart = chart; | |||
|
29 | } | |||
|
30 | ||||
|
31 | void ChartView::mousePressEvent(QMouseEvent *event) | |||
|
32 | { | |||
|
33 | m_mousePos = event->pos(); | |||
|
34 | QChartView::mousePressEvent(event); | |||
|
35 | } | |||
|
36 | ||||
|
37 | void ChartView::mouseMoveEvent(QMouseEvent *event) | |||
|
38 | { | |||
|
39 | m_chart->handlePointMove(event->pos()); | |||
|
40 | QChartView::mouseMoveEvent(event); | |||
|
41 | } | |||
|
42 | ||||
|
43 | void ChartView::mouseReleaseEvent(QMouseEvent *event) | |||
|
44 | { | |||
|
45 | if (event->pos() != m_mousePos) { | |||
|
46 | m_chart->handlePointMove(event->pos()); | |||
|
47 | m_chart->setPointClicked(false); | |||
|
48 | } | |||
|
49 | QChartView::mouseReleaseEvent(event); | |||
|
50 | } |
@@ -0,0 +1,45 | |||||
|
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 CHARTVIEW_H | |||
|
22 | #define CHARTVIEW_H | |||
|
23 | ||||
|
24 | #include <QChartView> | |||
|
25 | ||||
|
26 | class Chart; | |||
|
27 | ||||
|
28 | QTCOMMERCIALCHART_USE_NAMESPACE | |||
|
29 | ||||
|
30 | class ChartView : public QChartView | |||
|
31 | { | |||
|
32 | public: | |||
|
33 | ChartView(Chart *chart, QWidget *parent = 0); | |||
|
34 | ||||
|
35 | protected: | |||
|
36 | void mousePressEvent(QMouseEvent *event); | |||
|
37 | void mouseMoveEvent(QMouseEvent *event); | |||
|
38 | void mouseReleaseEvent(QMouseEvent *event); | |||
|
39 | ||||
|
40 | private: | |||
|
41 | Chart *m_chart; | |||
|
42 | QPoint m_mousePos; | |||
|
43 | }; | |||
|
44 | ||||
|
45 | #endif |
@@ -0,0 +1,71 | |||||
|
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 <QApplication> | |||
|
22 | #include <QMainWindow> | |||
|
23 | #include <QLineSeries> | |||
|
24 | ||||
|
25 | #include <QValuesAxis> | |||
|
26 | ||||
|
27 | #include "chart.h" | |||
|
28 | #include "chartview.h" | |||
|
29 | ||||
|
30 | QTCOMMERCIALCHART_USE_NAMESPACE | |||
|
31 | ||||
|
32 | int main(int argc, char *argv[]) | |||
|
33 | { | |||
|
34 | QApplication a(argc, argv); | |||
|
35 | ||||
|
36 | QLineSeries* series = new QLineSeries(); | |||
|
37 | ||||
|
38 | series->append(0, 6); | |||
|
39 | series->append(1, 3); | |||
|
40 | series->append(2, 4); | |||
|
41 | series->append(3, 8); | |||
|
42 | series->append(7, 13); | |||
|
43 | series->append(10, 5); | |||
|
44 | *series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2); | |||
|
45 | ||||
|
46 | Chart* chart = new Chart(0, 0, series); | |||
|
47 | chart->legend()->hide(); | |||
|
48 | chart->addSeries(series); | |||
|
49 | chart->createDefaultAxes(); | |||
|
50 | chart->setTitle("Drag'n drop to move data points"); | |||
|
51 | ||||
|
52 | QValuesAxis *axisX = new QValuesAxis(); | |||
|
53 | chart->setAxisX(axisX, series); | |||
|
54 | axisX->setRange(0, 20); | |||
|
55 | ||||
|
56 | QValuesAxis *axisY = new QValuesAxis(); | |||
|
57 | chart->setAxisY(axisY, series); | |||
|
58 | axisY->setRange(0, 13); | |||
|
59 | ||||
|
60 | QObject::connect(series, SIGNAL(clicked(QPointF)), chart, SLOT(clickPoint(QPointF))); | |||
|
61 | ||||
|
62 | ChartView* chartView = new ChartView(chart); | |||
|
63 | chartView->setRenderHint(QPainter::Antialiasing); | |||
|
64 | ||||
|
65 | QMainWindow window; | |||
|
66 | window.setCentralWidget(chartView); | |||
|
67 | window.resize(400, 300); | |||
|
68 | window.show(); | |||
|
69 | ||||
|
70 | return a.exec(); | |||
|
71 | } |
@@ -13,4 +13,5 SUBDIRS += chartthemes \ | |||||
13 | qmlcustomizations \ |
|
13 | qmlcustomizations \ | |
14 | qmlcustommodel \ |
|
14 | qmlcustommodel \ | |
15 | qmloscilloscope \ |
|
15 | qmloscilloscope \ | |
16 | chartviewer |
|
16 | chartviewer \ | |
|
17 | chartinteractions |
@@ -133,7 +133,7 Q_SIGNALS: | |||||
133 |
|
133 | |||
134 | protected: |
|
134 | protected: | |
135 | QScopedPointer<QAbstractAxisPrivate> d_ptr; |
|
135 | QScopedPointer<QAbstractAxisPrivate> d_ptr; | |
136 |
Q_DISABLE_COPY(QAbstractAxis) |
|
136 | Q_DISABLE_COPY(QAbstractAxis) | |
137 | friend class ChartDataSet; |
|
137 | friend class ChartDataSet; | |
138 | friend class ChartAxis; |
|
138 | friend class ChartAxis; | |
139 | friend class ChartPresenter; |
|
139 | friend class ChartPresenter; |
@@ -55,13 +55,13 public: | |||||
55 | BackgroundZValue = -1, |
|
55 | BackgroundZValue = -1, | |
56 | ShadesZValue , |
|
56 | ShadesZValue , | |
57 | GridZValue, |
|
57 | GridZValue, | |
|
58 | AxisZValue, | |||
58 | SeriesZValue, |
|
59 | SeriesZValue, | |
59 | LineChartZValue = SeriesZValue, |
|
60 | LineChartZValue = SeriesZValue, | |
60 | SplineChartZValue = SeriesZValue, |
|
61 | SplineChartZValue = SeriesZValue, | |
61 | BarSeriesZValue = SeriesZValue, |
|
62 | BarSeriesZValue = SeriesZValue, | |
62 | ScatterSeriesZValue = SeriesZValue, |
|
63 | ScatterSeriesZValue = SeriesZValue, | |
63 | PieSeriesZValue = SeriesZValue, |
|
64 | PieSeriesZValue = SeriesZValue, | |
64 | AxisZValue, |
|
|||
65 | LegendZValue, |
|
65 | LegendZValue, | |
66 | TopMostZValue |
|
66 | TopMostZValue | |
67 | }; |
|
67 | }; |
General Comments 0
You need to be logged in to leave comments.
Login now