##// END OF EJS Templates
Added automatic refresh of control points on add/remove point. Spline example updated
Marek Rosa -
r431:575ceaee424c
parent child
Show More
@@ -1,8 +1,8
1 #include "splinewidget.h"
1 #include "splinewidget.h"
2 #include "qchartview.h"
2 #include "qchartview.h"
3 #include "qsplineseries.h"
4 #include "qlineseries.h"
3 #include "qlineseries.h"
5 #include <QGridLayout>
4 #include <QGridLayout>
5 #include <QPushButton>
6
6
7 QTCOMMERCIALCHART_USE_NAMESPACE
7 QTCOMMERCIALCHART_USE_NAMESPACE
8
8
@@ -10,27 +10,30 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 series = new QSplineSeries(this);
14 // series->add(QPointF(150, 100));
14 series->add(QPointF(150, 100));
15 // series->add(QPointF(200, 130));
15 series->add(QPointF(200, 130));
16 // series->add(QPointF(250, 120));
16 series->add(QPointF(250, 120));
17 // series->add(QPointF(300, 140));
17 series->add(QPointF(300, 140));
18 // series->add(QPointF(350, 100));
18 series->add(QPointF(350, 100));
19 // series->add(QPointF(400, 120));
19 series->add(QPointF(400, 120));
20 // series->add(QPointF(450, 150));
20 series->add(QPointF(450, 150));
21 //// series->add(QPointF(600, 150));
21 // series->add(QPointF(600, 150));
22 // series->add(QPointF(500, 145));
22 series->add(QPointF(500, 145));
23 // series->add(QPointF(550, 170));
23 series->add(QPointF(550, 170));
24 // series->add(QPointF(600, 190));
24 series->add(QPointF(600, 190));
25 // series->add(QPointF(650, 210));
25 series->add(QPointF(650, 210));
26 // series->add(QPointF(700, 190));
26 series->add(QPointF(700, 190));
27 // series->add(QPointF(750, 180));
27 series->add(QPointF(750, 180));
28 // series->add(QPointF(800, 170));
28 series->add(QPointF(800, 170));
29
29
30 // qsrand(time(NULL));
30 // series->calculateControlPoints();
31
32 QSplineSeries* series2 = new QSplineSeries(this);
33 qsrand(time(NULL));
31 // for (int i = 0; i < 100; i++)
34 // for (int i = 0; i < 100; i++)
32 // {
35 // {
33 // series->add(QPointF(i*7, qrand()%600));
36 // series2->add(QPointF(i*7, qrand()%600));
34 // }
37 // }
35 int k = 10;
38 int k = 10;
36 for (int i = 0; i < 25; i++)
39 for (int i = 0; i < 25; i++)
@@ -39,11 +42,11 SplineWidget::SplineWidget(QWidget *parent)
39 {
42 {
40 k = 10;
43 k = 10;
41 }
44 }
42 series->add(QPointF(i*50, k));
45 series2->add(QPointF(i*50, k));
43 k +=10;
46 k +=10;
44 }
47 }
45
48
46 series->calculateControlPoints();
49 // series2->calculateControlPoints();
47
50
48 // QLineSeries* lineSeries = new QLineSeries;
51 // QLineSeries* lineSeries = new QLineSeries;
49 // for (int i = 0; i < series->count() - 1; i++)
52 // for (int i = 0; i < series->count() - 1; i++)
@@ -65,12 +68,41 SplineWidget::SplineWidget(QWidget *parent)
65 chart->setMinimumSize(800,600);
68 chart->setMinimumSize(800,600);
66 // chart->setGeometry(50, 50, 400, 300);
69 // chart->setGeometry(50, 50, 400, 300);
67 chart->addSeries(series);
70 chart->addSeries(series);
71 chart->addSeries(series2);
72
73 //add new item
74 QPushButton* addButton = new QPushButton("Add new point");
75 connect(addButton, SIGNAL(clicked()), this, SLOT(addNewPoint()));
76
77 QPushButton* removeButton = new QPushButton("Remove point from the end");
78 connect(removeButton, SIGNAL(clicked()), this, SLOT(removePoint()));
79
80 //butttons layout
81 QVBoxLayout* buttonsLayout = new QVBoxLayout;
82 buttonsLayout->addWidget(addButton);
83 buttonsLayout->addWidget(removeButton);
84 buttonsLayout->addStretch();
68
85
69 QGridLayout* mainLayout = new QGridLayout;
86 QGridLayout* mainLayout = new QGridLayout;
70 mainLayout->addWidget(chart);
87 mainLayout->addWidget(chart, 1, 0);
88 mainLayout->addLayout(buttonsLayout, 1, 1);
71 setLayout(mainLayout);
89 setLayout(mainLayout);
72 }
90 }
73
91
92 void SplineWidget::addNewPoint()
93 {
94 if (series->count() > 0)
95 series->add(QPointF(series->x(series->count() - 1) + 50, series->y(series->count() - 1) - 50 + qrand()%100));
96 else
97 series->add(QPointF(50, 50 + qrand()%50));
98 }
99
100 void SplineWidget::removePoint()
101 {
102 if (series->count() > 0)
103 series->remove(QPointF(series->x(series->count() - 1), series->y(series->count() - 1)));
104 }
105
74 SplineWidget::~SplineWidget()
106 SplineWidget::~SplineWidget()
75 {
107 {
76
108
@@ -2,6 +2,9
2 #define SPLINEWIDGET_H
2 #define SPLINEWIDGET_H
3
3
4 #include <QtGui/QWidget>
4 #include <QtGui/QWidget>
5 #include "qsplineseries.h"
6
7 QTCOMMERCIALCHART_USE_NAMESPACE
5
8
6 class SplineWidget : public QWidget
9 class SplineWidget : public QWidget
7 {
10 {
@@ -10,6 +13,13 class SplineWidget : public QWidget
10 public:
13 public:
11 SplineWidget(QWidget *parent = 0);
14 SplineWidget(QWidget *parent = 0);
12 ~SplineWidget();
15 ~SplineWidget();
16
17 public slots:
18 void addNewPoint();
19 void removePoint();
20
21 private:
22 QSplineSeries* series;
13 };
23 };
14
24
15 #endif // SPLINEWIDGET_H
25 #endif // SPLINEWIDGET_H
@@ -5,21 +5,11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QSplineSeries::QSplineSeries(QObject *parent) :
5 QSplineSeries::QSplineSeries(QObject *parent) :
6 QLineSeries(parent)
6 QLineSeries(parent)
7 {
7 {
8 connect(this,SIGNAL(pointAdded(int)), this, SLOT(updateControlPoints()));
9 connect(this,SIGNAL(pointRemoved(int)), this, SLOT(updateControlPoints()));
10 connect(this,SIGNAL(pointReplaced(int)), this, SLOT(updateControlPoints()));
8 }
11 }
9
12
10 //QSplineSeries& QSplineSeries::operator << (const QPointF &value)
11 //{
12 //// d->m_data.append(value);
13 // m_data.append(value);
14 //// emit changed();
15 // return *this;
16 //}
17
18 //void QSplineSeries::addData(QPointF value)
19 //{
20 // m_data.append(value);
21 //}
22
23 void QSplineSeries::calculateControlPoints()
13 void QSplineSeries::calculateControlPoints()
24 {
14 {
25
15
@@ -72,8 +62,6 void QSplineSeries::calculateControlPoints()
72 QList<qreal> y = getFirstControlPoints(rhs);
62 QList<qreal> y = getFirstControlPoints(rhs);
73
63
74 // Fill output arrays.
64 // Fill output arrays.
75 // firstControlPoints = new Point[n];
76 // secondControlPoints = new Point[n];
77 for (int i = 0; i < n; ++i)
65 for (int i = 0; i < n; ++i)
78 {
66 {
79 // First control point
67 // First control point
@@ -105,6 +93,16 QList<qreal> QSplineSeries::getFirstControlPoints(QList<qreal> rhs)
105
93
106 return x;
94 return x;
107 }
95 }
96
97 void QSplineSeries::updateControlPoints()
98 {
99 if(m_x.size() > 1)
100 {
101 m_controlPoints.clear();
102 calculateControlPoints();
103 }
104 }
105
108 #include "moc_qsplineseries.cpp"
106 #include "moc_qsplineseries.cpp"
109
107
110 QTCOMMERCIALCHART_END_NAMESPACE
108 QTCOMMERCIALCHART_END_NAMESPACE
@@ -16,22 +16,18 class QSplineSeries : public QLineSeries
16
16
17 QSplineSeries(QObject *parent = 0);
17 QSplineSeries(QObject *parent = 0);
18 QSeriesType type() const { return QSeries::SeriesTypeSpline; }
18 QSeriesType type() const { return QSeries::SeriesTypeSpline; }
19 // void addData(QPointF value);
20 // QSplineSeries& operator << (const QPointF &value);
21 void calculateControlPoints();
22 QList<qreal> getFirstControlPoints(QList<qreal> rhs);
23
19
24 int count() const { return m_x.size(); }
20 int count() const { return m_x.size(); }
25
26 // QPointF at(int index) const { return m_data[index]; }
27 QPointF controlPoint(int index) const { return m_controlPoints[index]; }
21 QPointF controlPoint(int index) const { return m_controlPoints[index]; }
28
22
29 signals:
23 private:
24 void calculateControlPoints();
25 QList<qreal> getFirstControlPoints(QList<qreal> rhs);
30
26
31 public slots:
27 private slots:
28 void updateControlPoints();
32
29
33 private:
30 private:
34 // QList<QPointF> m_data;
35 QList<QPointF> m_controlPoints;
31 QList<QPointF> m_controlPoints;
36
32
37 };
33 };
@@ -61,11 +61,14 void SplinePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *o
61 painter->setPen(Qt::blue);
61 painter->setPen(Qt::blue);
62 // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i));
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));
63 // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1));
64 painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4);
64 // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4);
65 painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4);
65 // painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4);
66 }
66 }
67 if (m_points.count() > 0)
68 {
67 painter->setPen(Qt::red);
69 painter->setPen(Qt::red);
68 painter->drawEllipse(m_points[m_points.count() - 1], 4, 4);
70 painter->drawEllipse(m_points[m_points.count() - 1], 4, 4);
71 }
69 painter->restore();
72 painter->restore();
70 }
73 }
71
74
General Comments 0
You need to be logged in to leave comments. Login now