@@ -1,101 +1,103 | |||||
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 <QValuesAxis> |
|
21 | #include <QValuesAxis> | |
22 | #include <QAbstractAxis> |
|
22 | #include <QAbstractAxis> | |
|
23 | #include <cmath> | |||
23 | #include <QDebug> |
|
24 | #include <QDebug> | |
24 |
|
25 | |||
25 | #include "chart.h" |
|
26 | #include "chart.h" | |
26 |
|
27 | |||
27 | Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags, QLineSeries *series) |
|
28 | Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags, QLineSeries *series) | |
28 | : QChart(parent, wFlags), m_series(series) |
|
29 | : QChart(parent, wFlags), m_series(series) | |
29 | { |
|
30 | { | |
30 | m_clicked = false; |
|
31 | m_clicked = false; | |
31 | } |
|
32 | } | |
32 |
|
33 | |||
33 | Chart::~Chart() |
|
34 | Chart::~Chart() | |
34 | { |
|
35 | { | |
35 | } |
|
36 | } | |
36 |
|
37 | |||
37 | void Chart::clickPoint(const QPointF &point) |
|
38 | void Chart::clickPoint(const QPointF &point) | |
38 | { |
|
39 | { | |
39 | //Get all points from the series. |
|
40 | // Find the closes data point | |
40 | QList<QPointF> points = m_series->points(); |
|
41 | m_movingPoint = QPoint(); | |
41 | //Construct a small rectangle around the clicked point |
|
42 | m_clicked = false; | |
42 | //to identify the real point clicked from the series. |
|
43 | foreach (QPointF p, m_series->points()) { | |
43 | QRectF clickRect(point.x() - 0.5, point.y() - 0.5, 1.0, 1.0); |
|
44 | if (distance(p, point) < distance(m_movingPoint, point)) { | |
44 |
|
||||
45 | //Find the clicked point to be moved. |
|
|||
46 | foreach (QPointF p, points) { |
|
|||
47 | if (clickRect.contains(p)) { |
|
|||
48 | m_movingPoint = p; |
|
45 | m_movingPoint = p; | |
49 | m_clicked = true; |
|
46 | m_clicked = true; | |
50 | return; |
|
|||
51 | } |
|
47 | } | |
52 | } |
|
48 | } | |
53 | } |
|
49 | } | |
54 |
|
50 | |||
|
51 | qreal Chart::distance(const QPointF &p1, const QPointF &p2) | |||
|
52 | { | |||
|
53 | return sqrt((p1.x() - p2.x()) * (p1.x() - p2.x()) | |||
|
54 | + (p1.y() - p2.y()) * (p1.y() - p2.y())); | |||
|
55 | } | |||
|
56 | ||||
55 | void Chart::setPointClicked(bool clicked) |
|
57 | void Chart::setPointClicked(bool clicked) | |
56 | { |
|
58 | { | |
57 | m_clicked = clicked; |
|
59 | m_clicked = clicked; | |
58 | } |
|
60 | } | |
59 |
|
61 | |||
60 | void Chart::handlePointMove(const QPoint &point) |
|
62 | void Chart::handlePointMove(const QPoint &point) | |
61 | { |
|
63 | { | |
62 | if (m_clicked) { |
|
64 | if (m_clicked) { | |
63 | //Map the point clicked from the ChartView |
|
65 | //Map the point clicked from the ChartView | |
64 | //to the area occupied by the chart. |
|
66 | //to the area occupied by the chart. | |
65 | QPoint mappedPoint = point; |
|
67 | QPoint mappedPoint = point; | |
66 | mappedPoint.setX(point.x()-this->plotArea().x()); |
|
68 | mappedPoint.setX(point.x()-this->plotArea().x()); | |
67 | mappedPoint.setY(point.y()-this->plotArea().y()); |
|
69 | mappedPoint.setY(point.y()-this->plotArea().y()); | |
68 |
|
70 | |||
69 | //Get the x- and y axis to be able to convert the mapped |
|
71 | //Get the x- and y axis to be able to convert the mapped | |
70 | //coordinate point to the charts scale. |
|
72 | //coordinate point to the charts scale. | |
71 | QAbstractAxis * axisx = this->axisX(); |
|
73 | QAbstractAxis * axisx = this->axisX(); | |
72 | QValuesAxis* haxis = 0; |
|
74 | QValuesAxis* haxis = 0; | |
73 | if (axisx->type() == QAbstractAxis::AxisTypeValues) |
|
75 | if (axisx->type() == QAbstractAxis::AxisTypeValues) | |
74 | haxis = qobject_cast<QValuesAxis*>(axisx); |
|
76 | haxis = qobject_cast<QValuesAxis*>(axisx); | |
75 |
|
77 | |||
76 | QAbstractAxis * axisy = this->axisY(); |
|
78 | QAbstractAxis * axisy = this->axisY(); | |
77 | QValuesAxis* vaxis = 0; |
|
79 | QValuesAxis* vaxis = 0; | |
78 | if (axisy->type() == QAbstractAxis::AxisTypeValues) |
|
80 | if (axisy->type() == QAbstractAxis::AxisTypeValues) | |
79 | vaxis = qobject_cast<QValuesAxis*>(axisy); |
|
81 | vaxis = qobject_cast<QValuesAxis*>(axisy); | |
80 |
|
82 | |||
81 | if (haxis && vaxis) { |
|
83 | if (haxis && vaxis) { | |
82 | //Calculate the "unit" between points on the x |
|
84 | //Calculate the "unit" between points on the x | |
83 | //y axis. |
|
85 | //y axis. | |
84 | double xUnit = this->plotArea().width()/haxis->max(); |
|
86 | double xUnit = this->plotArea().width()/haxis->max(); | |
85 | double yUnit = this->plotArea().height()/vaxis->max(); |
|
87 | double yUnit = this->plotArea().height()/vaxis->max(); | |
86 |
|
88 | |||
87 | //Convert the mappedPoint to the actual chart scale. |
|
89 | //Convert the mappedPoint to the actual chart scale. | |
88 | double x = mappedPoint.x()/xUnit; |
|
90 | double x = mappedPoint.x()/xUnit; | |
89 | double y = vaxis->max() - mappedPoint.y()/yUnit; |
|
91 | double y = vaxis->max() - mappedPoint.y()/yUnit; | |
90 |
|
92 | |||
91 | //Replace the old point with the new one. |
|
93 | //Replace the old point with the new one. | |
92 | m_series->replace(m_movingPoint, QPointF(x, y)); |
|
94 | m_series->replace(m_movingPoint, QPointF(x, y)); | |
93 |
|
95 | |||
94 | //Update the m_movingPoint so we are able to |
|
96 | //Update the m_movingPoint so we are able to | |
95 | //do the replace also during mousemoveEvent. |
|
97 | //do the replace also during mousemoveEvent. | |
96 | m_movingPoint.setX(x); |
|
98 | m_movingPoint.setX(x); | |
97 | m_movingPoint.setY(y); |
|
99 | m_movingPoint.setY(y); | |
98 | } |
|
100 | } | |
99 | } |
|
101 | } | |
100 | } |
|
102 | } | |
101 |
|
103 |
@@ -1,52 +1,53 | |||||
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 CHART_H |
|
21 | #ifndef CHART_H | |
22 | #define CHART_H |
|
22 | #define CHART_H | |
23 |
|
23 | |||
24 | #include <QChart> |
|
24 | #include <QChart> | |
25 | #include <QLineSeries> |
|
25 | #include <QLineSeries> | |
26 |
|
26 | |||
27 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
27 | QTCOMMERCIALCHART_USE_NAMESPACE | |
28 |
|
28 | |||
29 | class Chart : public QChart |
|
29 | class Chart : public QChart | |
30 | { |
|
30 | { | |
31 | Q_OBJECT |
|
31 | Q_OBJECT | |
32 | public: |
|
32 | public: | |
33 | explicit Chart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0, QLineSeries *series = 0); |
|
33 | explicit Chart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0, QLineSeries *series = 0); | |
34 | ~Chart(); |
|
34 | ~Chart(); | |
35 |
|
35 | |||
36 | public slots: |
|
36 | public slots: | |
37 | void clickPoint(const QPointF &point); |
|
37 | void clickPoint(const QPointF &point); | |
38 |
|
38 | |||
39 | public: |
|
39 | public: | |
40 | void handlePointMove(const QPoint &point); |
|
40 | void handlePointMove(const QPoint &point); | |
41 | void setPointClicked(bool clicked); |
|
41 | void setPointClicked(bool clicked); | |
42 |
|
42 | |||
43 | private: |
|
43 | private: | |
|
44 | qreal distance(const QPointF &p1, const QPointF &p2); | |||
44 | QLineSeries *m_series; |
|
45 | QLineSeries *m_series; | |
45 | QPointF m_movingPoint; |
|
46 | QPointF m_movingPoint; | |
46 |
|
47 | |||
47 | //Boolean value to determine if an actual point in the |
|
48 | //Boolean value to determine if an actual point in the | |
48 | //series is clicked. |
|
49 | //series is clicked. | |
49 | bool m_clicked; |
|
50 | bool m_clicked; | |
50 | }; |
|
51 | }; | |
51 |
|
52 | |||
52 | #endif // CHART_H |
|
53 | #endif // CHART_H |
@@ -1,71 +1,74 | |||||
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 <QApplication> |
|
21 | #include <QApplication> | |
22 | #include <QMainWindow> |
|
22 | #include <QMainWindow> | |
23 | #include <QLineSeries> |
|
23 | #include <QLineSeries> | |
24 |
|
24 | |||
25 | #include <QValuesAxis> |
|
25 | #include <QValuesAxis> | |
26 |
|
26 | |||
27 | #include "chart.h" |
|
27 | #include "chart.h" | |
28 | #include "chartview.h" |
|
28 | #include "chartview.h" | |
29 |
|
29 | |||
30 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
30 | QTCOMMERCIALCHART_USE_NAMESPACE | |
31 |
|
31 | |||
32 | int main(int argc, char *argv[]) |
|
32 | int main(int argc, char *argv[]) | |
33 | { |
|
33 | { | |
34 | QApplication a(argc, argv); |
|
34 | QApplication a(argc, argv); | |
35 |
|
35 | |||
36 | QLineSeries* series = new QLineSeries(); |
|
36 | QLineSeries* series = new QLineSeries(); | |
37 |
|
37 | |||
38 | series->append(0, 6); |
|
38 | series->append(0, 6); | |
39 | series->append(1, 3); |
|
39 | series->append(1, 3); | |
40 | series->append(2, 4); |
|
40 | series->append(2, 4); | |
41 | series->append(3, 8); |
|
41 | series->append(3, 8); | |
42 | series->append(7, 13); |
|
42 | series->append(7, 13); | |
43 | series->append(10, 5); |
|
43 | series->append(10, 5); | |
44 | *series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2); |
|
44 | *series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2); | |
45 |
|
45 | |||
46 | Chart* chart = new Chart(0, 0, series); |
|
46 | Chart* chart = new Chart(0, 0, series); | |
47 | chart->legend()->hide(); |
|
47 | chart->legend()->hide(); | |
48 | chart->addSeries(series); |
|
48 | chart->addSeries(series); | |
|
49 | QPen p = series->pen(); | |||
|
50 | p.setWidth(5); | |||
|
51 | series->setPen(p); | |||
49 | chart->createDefaultAxes(); |
|
52 | chart->createDefaultAxes(); | |
50 | chart->setTitle("Drag'n drop to move data points"); |
|
53 | chart->setTitle("Drag'n drop to move data points"); | |
51 |
|
54 | |||
52 | QValuesAxis *axisX = new QValuesAxis(); |
|
55 | QValuesAxis *axisX = new QValuesAxis(); | |
53 | chart->setAxisX(axisX, series); |
|
56 | chart->setAxisX(axisX, series); | |
54 | axisX->setRange(0, 20); |
|
57 | axisX->setRange(0, 20); | |
55 |
|
58 | |||
56 | QValuesAxis *axisY = new QValuesAxis(); |
|
59 | QValuesAxis *axisY = new QValuesAxis(); | |
57 | chart->setAxisY(axisY, series); |
|
60 | chart->setAxisY(axisY, series); | |
58 | axisY->setRange(0, 13); |
|
61 | axisY->setRange(0, 13); | |
59 |
|
62 | |||
60 | QObject::connect(series, SIGNAL(clicked(QPointF)), chart, SLOT(clickPoint(QPointF))); |
|
63 | QObject::connect(series, SIGNAL(clicked(QPointF)), chart, SLOT(clickPoint(QPointF))); | |
61 |
|
64 | |||
62 | ChartView* chartView = new ChartView(chart); |
|
65 | ChartView* chartView = new ChartView(chart); | |
63 | chartView->setRenderHint(QPainter::Antialiasing); |
|
66 | chartView->setRenderHint(QPainter::Antialiasing); | |
64 |
|
67 | |||
65 | QMainWindow window; |
|
68 | QMainWindow window; | |
66 | window.setCentralWidget(chartView); |
|
69 | window.setCentralWidget(chartView); | |
67 | window.resize(400, 300); |
|
70 | window.resize(400, 300); | |
68 | window.show(); |
|
71 | window.show(); | |
69 |
|
72 | |||
70 | return a.exec(); |
|
73 | return a.exec(); | |
71 | } |
|
74 | } |
@@ -1,119 +1,132 | |||||
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 "linechartitem_p.h" |
|
21 | #include "linechartitem_p.h" | |
22 | #include "qlineseries.h" |
|
22 | #include "qlineseries.h" | |
23 | #include "qlineseries_p.h" |
|
23 | #include "qlineseries_p.h" | |
24 | #include "chartpresenter_p.h" |
|
24 | #include "chartpresenter_p.h" | |
25 | #include <QPainter> |
|
25 | #include <QPainter> | |
26 | #include <QGraphicsSceneMouseEvent> |
|
26 | #include <QGraphicsSceneMouseEvent> | |
27 |
|
27 | |||
28 |
|
||||
29 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
28 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
30 |
|
29 | |||
31 | //TODO: optimize : remove points which are not visible |
|
30 | const qreal mouseEventMinWidth(14); | |
32 |
|
31 | |||
33 | LineChartItem::LineChartItem(QLineSeries* series,ChartPresenter *presenter): |
|
32 | LineChartItem::LineChartItem(QLineSeries* series,ChartPresenter *presenter): | |
34 | XYChart(series, presenter), |
|
33 | XYChart(series, presenter), | |
35 | QGraphicsItem(presenter ? presenter->rootItem() : 0), |
|
34 | QGraphicsItem(presenter ? presenter->rootItem() : 0), | |
36 | m_series(series), |
|
35 | m_series(series), | |
37 | m_pointsVisible(false) |
|
36 | m_pointsVisible(false) | |
38 | { |
|
37 | { | |
39 | setZValue(ChartPresenter::LineChartZValue); |
|
38 | setZValue(ChartPresenter::LineChartZValue); | |
40 | QObject::connect(series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated())); |
|
39 | QObject::connect(series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated())); | |
41 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); |
|
40 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); | |
42 | handleUpdated(); |
|
41 | handleUpdated(); | |
43 | } |
|
42 | } | |
44 |
|
43 | |||
45 | QRectF LineChartItem::boundingRect() const |
|
44 | QRectF LineChartItem::boundingRect() const | |
46 | { |
|
45 | { | |
47 | return m_rect; |
|
46 | return m_rect; | |
48 | } |
|
47 | } | |
49 |
|
48 | |||
50 | QPainterPath LineChartItem::shape() const |
|
49 | QPainterPath LineChartItem::shape() const | |
51 | { |
|
50 | { | |
52 | return m_path; |
|
51 | // Increase the size of the path slightly to make mouse interactions more natural | |
|
52 | QPainterPathStroker s; | |||
|
53 | s.setCapStyle(Qt::RoundCap); | |||
|
54 | s.setJoinStyle(Qt::RoundJoin); | |||
|
55 | qreal spacing = qMax(mouseEventMinWidth, (qreal) m_linePen.width()); | |||
|
56 | s.setWidth(spacing); | |||
|
57 | return s.createStroke(m_path); | |||
53 | } |
|
58 | } | |
54 |
|
59 | |||
55 | void LineChartItem::updateGeometry() |
|
60 | void LineChartItem::updateGeometry() | |
56 | { |
|
61 | { | |
57 | const QVector<QPointF>& points = geometryPoints(); |
|
62 | const QVector<QPointF>& points = geometryPoints(); | |
58 |
|
63 | |||
59 | if(points.size()==0) |
|
64 | if(points.size()==0) | |
60 | { |
|
65 | { | |
61 | prepareGeometryChange(); |
|
66 | prepareGeometryChange(); | |
62 | m_path = QPainterPath(); |
|
67 | m_path = QPainterPath(); | |
63 | m_rect = QRect(); |
|
68 | m_rect = QRect(); | |
64 | return; |
|
69 | return; | |
65 | } |
|
70 | } | |
66 |
|
71 | |||
67 | QPainterPath linePath(points.at(0)); |
|
72 | QPainterPath linePath(points.at(0)); | |
68 |
|
73 | |||
69 | for(int i=1; i< points.size();i++) { |
|
74 | for(int i=1; i< points.size();i++) { | |
70 | linePath.lineTo(points.at(i)); |
|
75 | linePath.lineTo(points.at(i)); | |
71 | } |
|
76 | } | |
72 |
|
77 | |||
73 | prepareGeometryChange(); |
|
78 | prepareGeometryChange(); | |
|
79 | ||||
74 | m_path = linePath; |
|
80 | m_path = linePath; | |
75 | m_rect = linePath.boundingRect(); |
|
81 | ||
|
82 | // When defining bounding rectangle, | |||
|
83 | // 1. take the line width into account (otherwise you will get drawing artifacts) and | |||
|
84 | // 2. take the shape into account (otherwise you will not get mouse events through on border | |||
|
85 | // areas). | |||
|
86 | const qreal sqrtOf2 = 1.414214; | |||
|
87 | const qreal spacing = qMax(mouseEventMinWidth / 2.0, | |||
|
88 | sqrtOf2 * (qreal) m_linePen.width() / 2.0); | |||
|
89 | m_rect = m_path.boundingRect().adjusted(-spacing, -spacing, spacing, spacing); | |||
|
90 | ||||
76 | setPos(origin()); |
|
91 | setPos(origin()); | |
77 | } |
|
92 | } | |
78 |
|
93 | |||
79 | void LineChartItem::handleUpdated() |
|
94 | void LineChartItem::handleUpdated() | |
80 | { |
|
95 | { | |
81 | setVisible(m_series->isVisible()); |
|
96 | setVisible(m_series->isVisible()); | |
82 | m_pointsVisible = m_series->pointsVisible(); |
|
97 | m_pointsVisible = m_series->pointsVisible(); | |
83 | m_linePen = m_series->pen(); |
|
98 | m_linePen = m_series->pen(); | |
84 | m_pointPen = m_series->pen(); |
|
99 | m_pointPen = m_series->pen(); | |
85 | m_pointPen.setWidthF(2*m_pointPen.width()); |
|
100 | m_pointPen.setWidthF(2*m_pointPen.width()); | |
86 | update(); |
|
101 | update(); | |
87 | } |
|
102 | } | |
88 |
|
103 | |||
89 | //painter |
|
|||
90 |
|
||||
91 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
104 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
92 | { |
|
105 | { | |
93 | Q_UNUSED(widget) |
|
106 | Q_UNUSED(widget) | |
94 | Q_UNUSED(option) |
|
107 | Q_UNUSED(option) | |
95 |
|
108 | |||
96 | painter->save(); |
|
109 | painter->save(); | |
97 | painter->setPen(m_linePen); |
|
110 | painter->setPen(m_linePen); | |
98 | painter->setClipRect(clipRect()); |
|
111 | painter->setClipRect(clipRect()); | |
99 | // Draw lines |
|
112 | // Draw lines | |
100 | const QVector<QPointF> &points = geometryPoints(); |
|
113 | const QVector<QPointF> &points = geometryPoints(); | |
101 | for (int i(1); i < points.size();i++) |
|
114 | for (int i(1); i < points.size();i++) | |
102 | painter->drawLine(points.at(i-1), points.at(i)); |
|
115 | painter->drawLine(points.at(i-1), points.at(i)); | |
103 | // Draw points |
|
116 | // Draw points | |
104 | if (m_pointsVisible){ |
|
117 | if (m_pointsVisible){ | |
105 | painter->setPen(m_pointPen); |
|
118 | painter->setPen(m_pointPen); | |
106 | painter->drawPoints(geometryPoints()); |
|
119 | painter->drawPoints(geometryPoints()); | |
107 | } |
|
120 | } | |
108 | painter->restore(); |
|
121 | painter->restore(); | |
109 | } |
|
122 | } | |
110 |
|
123 | |||
111 | void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
124 | void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) | |
112 | { |
|
125 | { | |
113 | emit XYChart::clicked(calculateDomainPoint(event->pos())); |
|
126 | emit XYChart::clicked(calculateDomainPoint(event->pos())); | |
114 | QGraphicsItem::mousePressEvent(event); |
|
127 | QGraphicsItem::mousePressEvent(event); | |
115 | } |
|
128 | } | |
116 |
|
129 | |||
117 | #include "moc_linechartitem_p.cpp" |
|
130 | #include "moc_linechartitem_p.cpp" | |
118 |
|
131 | |||
119 | QTCOMMERCIALCHART_END_NAMESPACE |
|
132 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,73 +1,73 | |||||
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 | // W A R N I N G |
|
21 | // W A R N I N G | |
22 | // ------------- |
|
22 | // ------------- | |
23 | // |
|
23 | // | |
24 | // This file is not part of the QtCommercial Chart API. It exists purely as an |
|
24 | // This file is not part of the QtCommercial Chart API. It exists purely as an | |
25 | // implementation detail. This header file may change from version to |
|
25 | // implementation detail. This header file may change from version to | |
26 | // version without notice, or even be removed. |
|
26 | // version without notice, or even be removed. | |
27 | // |
|
27 | // | |
28 | // We mean it. |
|
28 | // We mean it. | |
29 |
|
29 | |||
30 | #ifndef LINECHARTITEM_H |
|
30 | #ifndef LINECHARTITEM_H | |
31 | #define LINECHARTITEM_H |
|
31 | #define LINECHARTITEM_H | |
32 |
|
32 | |||
33 | #include "qchartglobal.h" |
|
33 | #include "qchartglobal.h" | |
34 | #include "xychart_p.h" |
|
34 | #include "xychart_p.h" | |
35 | #include <QPen> |
|
35 | #include <QPen> | |
36 |
|
36 | |||
37 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
37 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
38 |
|
38 | |||
39 | class QLineSeries; |
|
39 | class QLineSeries; | |
40 | class ChartPresenter; |
|
40 | class ChartPresenter; | |
41 |
|
41 | |||
42 | class LineChartItem : public XYChart , public QGraphicsItem |
|
42 | class LineChartItem : public XYChart , public QGraphicsItem | |
43 | { |
|
43 | { | |
44 | Q_OBJECT |
|
44 | Q_OBJECT | |
45 | Q_INTERFACES(QGraphicsItem) |
|
45 | Q_INTERFACES(QGraphicsItem) | |
46 | public: |
|
46 | public: | |
47 | explicit LineChartItem(QLineSeries *series,ChartPresenter *presenter); |
|
47 | explicit LineChartItem(QLineSeries *series,ChartPresenter *presenter); | |
48 |
~LineChartItem() {} |
|
48 | ~LineChartItem() {} | |
49 |
|
49 | |||
50 | //from QGraphicsItem |
|
50 | //from QGraphicsItem | |
51 | QRectF boundingRect() const; |
|
51 | QRectF boundingRect() const; | |
52 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
52 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
53 | QPainterPath shape() const; |
|
53 | QPainterPath shape() const; | |
54 |
|
54 | |||
55 | public Q_SLOTS: |
|
55 | public Q_SLOTS: | |
56 | void handleUpdated(); |
|
56 | void handleUpdated(); | |
57 | protected: |
|
57 | protected: | |
58 | void updateGeometry(); |
|
58 | void updateGeometry(); | |
59 | void mousePressEvent(QGraphicsSceneMouseEvent *event); |
|
59 | void mousePressEvent(QGraphicsSceneMouseEvent *event); | |
60 |
|
60 | |||
61 | private: |
|
61 | private: | |
62 | QLineSeries* m_series; |
|
62 | QLineSeries* m_series; | |
63 | QPainterPath m_path; |
|
63 | QPainterPath m_path; | |
64 | QRectF m_rect; |
|
64 | QRectF m_rect; | |
65 | QPen m_linePen; |
|
65 | QPen m_linePen; | |
66 | QPen m_pointPen; |
|
66 | QPen m_pointPen; | |
67 | bool m_pointsVisible; |
|
67 | bool m_pointsVisible; | |
68 |
|
68 | |||
69 | }; |
|
69 | }; | |
70 |
|
70 | |||
71 | QTCOMMERCIALCHART_END_NAMESPACE |
|
71 | QTCOMMERCIALCHART_END_NAMESPACE | |
72 |
|
72 | |||
73 | #endif |
|
73 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now