@@ -1,53 +1,67 | |||||
1 | #include "splinewidget.h" |
|
1 | #include "splinewidget.h" | |
2 | #include "qchartview.h" |
|
2 | #include "qchartview.h" | |
3 | #include "qsplineseries.h" |
|
3 | #include "qsplineseries.h" | |
4 | #include "qlineseries.h" |
|
4 | #include "qlineseries.h" | |
5 | #include <QGridLayout> |
|
5 | #include <QGridLayout> | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
7 | QTCOMMERCIALCHART_USE_NAMESPACE | |
8 |
|
8 | |||
9 | SplineWidget::SplineWidget(QWidget *parent) |
|
9 | SplineWidget::SplineWidget(QWidget *parent) | |
10 | : QWidget(parent) |
|
10 | : QWidget(parent) | |
11 | { |
|
11 | { | |
12 | //create QSplineSeries |
|
12 | //create QSplineSeries | |
13 | QSplineSeries* series = new QSplineSeries(this); |
|
13 | QSplineSeries* series = new QSplineSeries(this); | |
14 | series->add(QPointF(150, 100)); |
|
14 | // series->add(QPointF(150, 100)); | |
15 |
series->add(QPointF(200, 1 |
|
15 | // series->add(QPointF(200, 130)); | |
16 |
series->add(QPointF(2 |
|
16 | // series->add(QPointF(250, 120)); | |
17 |
series->add(QPointF( |
|
17 | // series->add(QPointF(300, 140)); | |
18 |
series->add(QPointF(3 |
|
18 | // series->add(QPointF(350, 100)); | |
19 |
series->add(QPointF(4 |
|
19 | // series->add(QPointF(400, 120)); | |
20 |
series->add(QPointF( |
|
20 | // series->add(QPointF(450, 150)); | |
|
21 | //// series->add(QPointF(600, 150)); | |||
|
22 | // series->add(QPointF(500, 145)); | |||
|
23 | // series->add(QPointF(550, 170)); | |||
|
24 | // series->add(QPointF(600, 190)); | |||
|
25 | // series->add(QPointF(650, 210)); | |||
|
26 | // series->add(QPointF(700, 190)); | |||
|
27 | // series->add(QPointF(750, 180)); | |||
|
28 | // series->add(QPointF(800, 170)); | |||
|
29 | ||||
|
30 | qsrand(time(NULL)); | |||
|
31 | for (int i = 0; i < 100; i++) | |||
|
32 | { | |||
|
33 | series->add(QPointF(i*7, qrand()%600)); | |||
|
34 | } | |||
21 |
|
35 | |||
22 | series->calculateControlPoints(); |
|
36 | series->calculateControlPoints(); | |
23 |
|
37 | |||
24 | // QLineSeries* lineSeries = new QLineSeries; |
|
38 | // QLineSeries* lineSeries = new QLineSeries; | |
25 | // for (int i = 0; i < series->count() - 1; i++) |
|
39 | // for (int i = 0; i < series->count() - 1; i++) | |
26 | // { |
|
40 | // { | |
27 | // lineSeries->add(series->at(i).x(), series->at(i).y()); |
|
41 | // lineSeries->add(series->at(i).x(), series->at(i).y()); | |
28 | // lineSeries->add(series->controlPoint(2*i).x(), series->controlPoint(2*i).y()); |
|
42 | // lineSeries->add(series->controlPoint(2*i).x(), series->controlPoint(2*i).y()); | |
29 | // lineSeries->add(series->controlPoint(2*i + 1).x(), series->controlPoint(2*i + 1).y()); |
|
43 | // lineSeries->add(series->controlPoint(2*i + 1).x(), series->controlPoint(2*i + 1).y()); | |
30 | // } |
|
44 | // } | |
31 |
|
45 | |||
32 | // QLineChartSeries* lineSeries2 = new QLineChartSeries; |
|
46 | // QLineChartSeries* lineSeries2 = new QLineChartSeries; | |
33 | // lineSeries2->add(10, 50); |
|
47 | // lineSeries2->add(10, 50); | |
34 | // lineSeries2->add(30, 15); |
|
48 | // lineSeries2->add(30, 15); | |
35 | // lineSeries2->add(60, 40); |
|
49 | // lineSeries2->add(60, 40); | |
36 | // lineSeries2->add(90, 70); |
|
50 | // lineSeries2->add(90, 70); | |
37 | // lineSeries2->add(100, 20); |
|
51 | // lineSeries2->add(100, 20); | |
38 |
|
52 | |||
39 | //create chart view |
|
53 | //create chart view | |
40 | QChartView* chart = new QChartView; |
|
54 | QChartView* chart = new QChartView; | |
41 | chart->setMinimumSize(800,600); |
|
55 | chart->setMinimumSize(800,600); | |
42 | // chart->setGeometry(50, 50, 400, 300); |
|
56 | // chart->setGeometry(50, 50, 400, 300); | |
43 | chart->addSeries(series); |
|
57 | chart->addSeries(series); | |
44 |
|
58 | |||
45 | QGridLayout* mainLayout = new QGridLayout; |
|
59 | QGridLayout* mainLayout = new QGridLayout; | |
46 | mainLayout->addWidget(chart); |
|
60 | mainLayout->addWidget(chart); | |
47 | setLayout(mainLayout); |
|
61 | setLayout(mainLayout); | |
48 | } |
|
62 | } | |
49 |
|
63 | |||
50 | SplineWidget::~SplineWidget() |
|
64 | SplineWidget::~SplineWidget() | |
51 | { |
|
65 | { | |
52 |
|
66 | |||
53 | } |
|
67 | } |
@@ -1,106 +1,74 | |||||
1 | #include "splinepresenter_p.h" |
|
1 | #include "splinepresenter_p.h" | |
2 | #include <QPainter> |
|
2 | #include <QPainter> | |
3 |
|
3 | |||
4 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
4 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
5 |
|
5 | |||
6 | SplinePresenter::SplinePresenter(QSplineSeries* series, QGraphicsObject *parent) : |
|
6 | SplinePresenter::SplinePresenter(QSplineSeries* series, QGraphicsObject *parent) : | |
7 | LineChartItem(0, series, parent)//,m_boundingRect() |
|
7 | LineChartItem(0, series, parent)//,m_boundingRect() | |
8 | { |
|
8 | { | |
9 | // if (parent) |
|
9 | // | |
10 | // m_boundingRect = parent->boundingRect(); |
|
|||
11 | // else |
|
|||
12 | // m_boundingRect = QRectF(10,50, 250, 250); |
|
|||
13 | } |
|
10 | } | |
14 |
|
11 | |||
15 | //void SplinePresenter::handleGeometryChanged(const QRectF&) |
|
|||
16 | //{ |
|
|||
17 | // update(); |
|
|||
18 | //} |
|
|||
19 |
|
12 | |||
20 | //void SplinePresenter::handleDomainChanged(const Domain& domain) |
|
|||
21 | //{ |
|
|||
22 | // // |
|
|||
23 | //} |
|
|||
24 |
|
13 | |||
25 | QPointF SplinePresenter::calculateGeometryControlPoint(int index) const |
|
14 | QPointF SplinePresenter::calculateGeometryControlPoint(int index) const | |
26 | { |
|
15 | { | |
27 | QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series); |
|
16 | QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series); | |
28 | const qreal deltaX = m_size.width()/m_domain.spanX(); |
|
17 | const qreal deltaX = m_size.width()/m_domain.spanX(); | |
29 | const qreal deltaY = m_size.height()/m_domain.spanY(); |
|
18 | const qreal deltaY = m_size.height()/m_domain.spanY(); | |
30 | qreal x = (splineSeries->controlPoint(index).x() - m_domain.m_minX)* deltaX; |
|
19 | qreal x = (splineSeries->controlPoint(index).x() - m_domain.m_minX)* deltaX; | |
31 | qreal y = (splineSeries->controlPoint(index).y() - m_domain.m_minY)*-deltaY + m_size.height(); |
|
20 | qreal y = (splineSeries->controlPoint(index).y() - m_domain.m_minY)*-deltaY + m_size.height(); | |
32 | return QPointF(x,y); |
|
21 | return QPointF(x,y); | |
33 | } |
|
22 | } | |
34 |
|
23 | |||
35 | void SplinePresenter::applyGeometry(QVector<QPointF>& points) |
|
24 | void SplinePresenter::applyGeometry(QVector<QPointF>& points) | |
36 | { |
|
25 | { | |
37 | if(points.size()==0) return; |
|
26 | if(points.size()==0) return; | |
38 |
|
27 | |||
39 | // QList<QGraphicsItem*> items = m_items.childItems(); |
|
|||
40 |
|
||||
41 | QPainterPath splinePath; |
|
28 | QPainterPath splinePath; | |
42 | const QPointF& point = points.at(0); |
|
29 | const QPointF& point = points.at(0); | |
43 | splinePath.moveTo(point); |
|
30 | splinePath.moveTo(point); | |
44 | // QGraphicsItem* item = items.at(0); |
|
|||
45 | // item->setPos(point.x()-1,point.y()-1); |
|
|||
46 | // if(!m_clipRect.contains(point)) item->setVisible(false); |
|
|||
47 |
|
31 | |||
48 | QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series); |
|
32 | QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series); | |
49 | for (int i = 0; i < splineSeries->count() - 1; i++) |
|
33 | for (int i = 0; i < splineSeries->count() - 1; i++) | |
50 |
|
|
34 | { | |
51 | const QPointF& point = points.at(i + 1); |
|
35 | const QPointF& point = points.at(i + 1); | |
52 | // painter->setPen(Qt::red); |
|
|||
53 | // splinePath.cubicTo(qobject_cast<QSplineSeries*>(m_series)->controlPoint(2 * i), qobject_cast<QSplineSeries*>(m_series)->controlPoint(2 * i + 1), QPointF(m_series->x(i + 1), m_series->y(i + 1))); |
|
|||
54 | splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point); |
|
36 | splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point); | |
55 | // painter->drawEllipse(m_series->at(i), 4, 4); |
|
|||
56 |
|
||||
57 | // painter->setPen(Qt::blue); |
|
|||
58 | // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i)); |
|
|||
59 | // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1)); |
|
|||
60 | // painter->drawEllipse(m_series->controlPoint(2 * i), 4, 4); |
|
|||
61 | // painter->drawEllipse(m_series->controlPoint(2 * i + 1), 4, 4); |
|
|||
62 |
|
|
37 | } | |
63 |
|
38 | |||
64 | // for(int i=1 ; i< points.size();i++) { |
|
39 | ||
65 | // QGraphicsItem* item = items.at(i); |
|
|||
66 | // const QPointF& point = points.at(i); |
|
|||
67 | // item->setPos(point.x()-1,point.y()-1); |
|
|||
68 | // if(!m_clipRect.contains(point)) item->setVisible(false); |
|
|||
69 | // path.lineTo(point); |
|
|||
70 | // } |
|
|||
71 |
|
40 | |||
72 | prepareGeometryChange(); |
|
41 | prepareGeometryChange(); | |
73 | m_path = splinePath; |
|
42 | m_path = splinePath; | |
74 | m_rect = splinePath.boundingRect(); |
|
43 | m_rect = splinePath.boundingRect(); | |
75 | } |
|
44 | } | |
76 |
|
45 | |||
77 |
|
|
46 | void SplinePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
78 | //{ |
|
47 | { | |
79 |
|
|
48 | Q_UNUSED(widget); | |
80 |
|
|
49 | Q_UNUSED(option); | |
81 |
|
|
50 | painter->save(); | |
82 |
|
51 | painter->setPen(m_pen); | ||
83 | // painter->setPen(Qt::SolidLine); |
|
52 | painter->setClipRect(m_clipRect); | |
|
53 | painter->drawPath(m_path); | |||
84 |
|
54 | |||
85 | // QPainterPath splinePath; |
|
55 | QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series); | |
86 | // splinePath.moveTo(m_series->at(0)); |
|
56 | for (int i = 0; i < splineSeries->count() - 1; i++) | |
87 | // for (int i = 0; i < m_series->count() - 1; i++) |
|
57 | { | |
88 | // { |
|
58 | painter->setPen(Qt::red); | |
89 | // painter->setPen(Qt::red); |
|
59 | painter->drawEllipse(m_points[i], 4, 4); | |
90 | // splinePath.cubicTo(m_series->controlPoint(2 * i), m_series->controlPoint(2 * i + 1), m_series->at(i + 1)); |
|
|||
91 | // painter->drawEllipse(m_series->at(i), 4, 4); |
|
|||
92 |
|
60 | |||
93 |
|
|
61 | painter->setPen(Qt::blue); | |
94 | // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i)); |
|
62 | // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i)); | |
95 | // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1)); |
|
63 | // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1)); | |
96 |
|
|
64 | painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4); | |
97 |
|
|
65 | painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4); | |
98 |
|
|
66 | } | |
99 |
|
|
67 | painter->setPen(Qt::red); | |
100 | // painter->drawPath(splinePath); |
|
68 | painter->drawEllipse(m_points[m_points.count() - 1], 4, 4); | |
101 |
|
|
69 | painter->restore(); | |
102 | //} |
|
70 | } | |
103 |
|
71 | |||
104 | #include "moc_splinepresenter_p.cpp" |
|
72 | #include "moc_splinepresenter_p.cpp" | |
105 |
|
73 | |||
106 | QTCOMMERCIALCHART_END_NAMESPACE |
|
74 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,40 +1,33 | |||||
1 | #ifndef SPLINEPRESENTER_P_H |
|
1 | #ifndef SPLINEPRESENTER_P_H | |
2 | #define SPLINEPRESENTER_P_H |
|
2 | #define SPLINEPRESENTER_P_H | |
3 |
|
3 | |||
4 | #include "chartitem_p.h" |
|
4 | #include "chartitem_p.h" | |
5 | #include <QObject> |
|
5 | #include <QObject> | |
6 | #include "qsplineseries.h" |
|
6 | #include "qsplineseries.h" | |
7 | #include "linechartitem_p.h" |
|
7 | #include "linechartitem_p.h" | |
8 |
|
8 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
10 | |||
11 | class SplinePresenter : public LineChartItem |
|
11 | class SplinePresenter : public LineChartItem | |
12 | { |
|
12 | { | |
13 | Q_OBJECT |
|
13 | Q_OBJECT | |
14 | public: |
|
14 | public: | |
15 | SplinePresenter(QSplineSeries* series, QGraphicsObject *parent = 0); |
|
15 | SplinePresenter(QSplineSeries* series, QGraphicsObject *parent = 0); | |
16 |
|
16 | |||
17 | void updateGeometry(); |
|
17 | void updateGeometry(); | |
18 |
|
18 | |||
19 | void applyGeometry(QVector<QPointF>& points); |
|
19 | void applyGeometry(QVector<QPointF>& points); | |
20 |
|
20 | |||
21 | QPointF calculateGeometryControlPoint(int index) const; |
|
21 | QPointF calculateGeometryControlPoint(int index) const; | |
22 |
|
22 | |||
23 | // QRectF boundingRect() const { return m_boundingRect; } |
|
23 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
24 | // void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
|||
25 |
|
24 | |||
26 | signals: |
|
25 | signals: | |
27 |
|
26 | |||
28 | public slots: |
|
27 | public slots: | |
29 | // void handleDomainChanged(const Domain& domain); |
|
|||
30 | // void handleGeometryChanged(const QRectF& rect); |
|
|||
31 |
|
||||
32 | private: |
|
|||
33 | // QSplineSeries* m_series; |
|
|||
34 | // QRectF m_boundingRect; |
|
|||
35 |
|
28 | |||
36 | }; |
|
29 | }; | |
37 |
|
30 | |||
38 | QTCOMMERCIALCHART_END_NAMESPACE |
|
31 | QTCOMMERCIALCHART_END_NAMESPACE | |
39 |
|
32 | |||
40 | #endif // SPLINEPRESENTER_P_H |
|
33 | #endif // SPLINEPRESENTER_P_H |
General Comments 0
You need to be logged in to leave comments.
Login now