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