@@ -1,8 +1,8 | |||
|
1 | 1 | #include "splinewidget.h" |
|
2 | 2 | #include "qchartview.h" |
|
3 | #include "qsplineseries.h" | |
|
4 | 3 | #include "qlineseries.h" |
|
5 | 4 | #include <QGridLayout> |
|
5 | #include <QPushButton> | |
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
8 | 8 | |
@@ -10,28 +10,31 SplineWidget::SplineWidget(QWidget *parent) | |||
|
10 | 10 | : QWidget(parent) |
|
11 | 11 | { |
|
12 | 12 | //create QSplineSeries |
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
|
|
|
13 | series = new QSplineSeries(this); | |
|
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 | 29 | |
|
30 | // qsrand(time(NULL)); | |
|
31 | // for (int i = 0; i < 100; i++) | |
|
32 | // { | |
|
33 | // series->add(QPointF(i*7, qrand()%600)); | |
|
34 | // } | |
|
30 | // series->calculateControlPoints(); | |
|
31 | ||
|
32 | QSplineSeries* series2 = new QSplineSeries(this); | |
|
33 | qsrand(time(NULL)); | |
|
34 | // for (int i = 0; i < 100; i++) | |
|
35 | // { | |
|
36 | // series2->add(QPointF(i*7, qrand()%600)); | |
|
37 | // } | |
|
35 | 38 | int k = 10; |
|
36 | 39 | for (int i = 0; i < 25; i++) |
|
37 | 40 | { |
@@ -39,38 +42,67 SplineWidget::SplineWidget(QWidget *parent) | |||
|
39 | 42 | { |
|
40 | 43 | k = 10; |
|
41 | 44 | } |
|
42 | series->add(QPointF(i*50, k)); | |
|
45 | series2->add(QPointF(i*50, k)); | |
|
43 | 46 | k +=10; |
|
44 | 47 | } |
|
45 | 48 | |
|
46 | series->calculateControlPoints(); | |
|
49 | // series2->calculateControlPoints(); | |
|
47 | 50 | |
|
48 | // QLineSeries* lineSeries = new QLineSeries; | |
|
49 | // for (int i = 0; i < series->count() - 1; i++) | |
|
50 | // { | |
|
51 | // lineSeries->add(series->at(i).x(), series->at(i).y()); | |
|
52 | // lineSeries->add(series->controlPoint(2*i).x(), series->controlPoint(2*i).y()); | |
|
53 | // lineSeries->add(series->controlPoint(2*i + 1).x(), series->controlPoint(2*i + 1).y()); | |
|
54 | // } | |
|
51 | // QLineSeries* lineSeries = new QLineSeries; | |
|
52 | // for (int i = 0; i < series->count() - 1; i++) | |
|
53 | // { | |
|
54 | // lineSeries->add(series->at(i).x(), series->at(i).y()); | |
|
55 | // lineSeries->add(series->controlPoint(2*i).x(), series->controlPoint(2*i).y()); | |
|
56 | // lineSeries->add(series->controlPoint(2*i + 1).x(), series->controlPoint(2*i + 1).y()); | |
|
57 | // } | |
|
55 | 58 | |
|
56 | // QLineChartSeries* lineSeries2 = new QLineChartSeries; | |
|
57 | // lineSeries2->add(10, 50); | |
|
58 | // lineSeries2->add(30, 15); | |
|
59 | // lineSeries2->add(60, 40); | |
|
60 | // lineSeries2->add(90, 70); | |
|
61 | // lineSeries2->add(100, 20); | |
|
59 | // QLineChartSeries* lineSeries2 = new QLineChartSeries; | |
|
60 | // lineSeries2->add(10, 50); | |
|
61 | // lineSeries2->add(30, 15); | |
|
62 | // lineSeries2->add(60, 40); | |
|
63 | // lineSeries2->add(90, 70); | |
|
64 | // lineSeries2->add(100, 20); | |
|
62 | 65 | |
|
63 | 66 | //create chart view |
|
64 | 67 | QChartView* chart = new QChartView; |
|
65 | 68 | chart->setMinimumSize(800,600); |
|
66 | // chart->setGeometry(50, 50, 400, 300); | |
|
69 | // chart->setGeometry(50, 50, 400, 300); | |
|
67 | 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 | 86 | QGridLayout* mainLayout = new QGridLayout; |
|
70 | mainLayout->addWidget(chart); | |
|
87 | mainLayout->addWidget(chart, 1, 0); | |
|
88 | mainLayout->addLayout(buttonsLayout, 1, 1); | |
|
71 | 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 | 106 | SplineWidget::~SplineWidget() |
|
75 | 107 | { |
|
76 | 108 |
@@ -2,6 +2,9 | |||
|
2 | 2 | #define SPLINEWIDGET_H |
|
3 | 3 | |
|
4 | 4 | #include <QtGui/QWidget> |
|
5 | #include "qsplineseries.h" | |
|
6 | ||
|
7 | QTCOMMERCIALCHART_USE_NAMESPACE | |
|
5 | 8 | |
|
6 | 9 | class SplineWidget : public QWidget |
|
7 | 10 | { |
@@ -10,6 +13,13 class SplineWidget : public QWidget | |||
|
10 | 13 | public: |
|
11 | 14 | SplineWidget(QWidget *parent = 0); |
|
12 | 15 | ~SplineWidget(); |
|
16 | ||
|
17 | public slots: | |
|
18 | void addNewPoint(); | |
|
19 | void removePoint(); | |
|
20 | ||
|
21 | private: | |
|
22 | QSplineSeries* series; | |
|
13 | 23 | }; |
|
14 | 24 | |
|
15 | 25 | #endif // SPLINEWIDGET_H |
@@ -5,21 +5,11 QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
5 | 5 | QSplineSeries::QSplineSeries(QObject *parent) : |
|
6 | 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 | 13 | void QSplineSeries::calculateControlPoints() |
|
24 | 14 | { |
|
25 | 15 | |
@@ -72,8 +62,6 void QSplineSeries::calculateControlPoints() | |||
|
72 | 62 | QList<qreal> y = getFirstControlPoints(rhs); |
|
73 | 63 | |
|
74 | 64 | // Fill output arrays. |
|
75 | // firstControlPoints = new Point[n]; | |
|
76 | // secondControlPoints = new Point[n]; | |
|
77 | 65 | for (int i = 0; i < n; ++i) |
|
78 | 66 | { |
|
79 | 67 | // First control point |
@@ -105,6 +93,16 QList<qreal> QSplineSeries::getFirstControlPoints(QList<qreal> rhs) | |||
|
105 | 93 | |
|
106 | 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 | 106 | #include "moc_qsplineseries.cpp" |
|
109 | 107 | |
|
110 | 108 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -16,22 +16,18 class QSplineSeries : public QLineSeries | |||
|
16 | 16 | |
|
17 | 17 | QSplineSeries(QObject *parent = 0); |
|
18 | 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 | 20 | int count() const { return m_x.size(); } |
|
25 | ||
|
26 | // QPointF at(int index) const { return m_data[index]; } | |
|
27 | 21 | QPointF controlPoint(int index) const { return m_controlPoints[index]; } |
|
28 | 22 | |
|
29 | signals: | |
|
30 | ||
|
31 | public slots: | |
|
23 | private: | |
|
24 | void calculateControlPoints(); | |
|
25 | QList<qreal> getFirstControlPoints(QList<qreal> rhs); | |
|
26 | ||
|
27 | private slots: | |
|
28 | void updateControlPoints(); | |
|
32 | 29 | |
|
33 | 30 | private: |
|
34 | // QList<QPointF> m_data; | |
|
35 | 31 | QList<QPointF> m_controlPoints; |
|
36 | 32 | |
|
37 | 33 | }; |
@@ -6,7 +6,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
6 | 6 | SplinePresenter::SplinePresenter(QSplineSeries* series, QGraphicsObject *parent) : |
|
7 | 7 | LineChartItem(0, series, parent)//,m_boundingRect() |
|
8 | 8 | { |
|
9 |
|
|
|
9 | // | |
|
10 | 10 | } |
|
11 | 11 | |
|
12 | 12 | |
@@ -61,11 +61,14 void SplinePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *o | |||
|
61 | 61 | painter->setPen(Qt::blue); |
|
62 | 62 | // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i)); |
|
63 | 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); | |
|
64 | // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4); | |
|
65 | // painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4); | |
|
66 | } | |
|
67 | if (m_points.count() > 0) | |
|
68 | { | |
|
69 | painter->setPen(Qt::red); | |
|
70 | painter->drawEllipse(m_points[m_points.count() - 1], 4, 4); | |
|
66 | 71 | } |
|
67 | painter->setPen(Qt::red); | |
|
68 | painter->drawEllipse(m_points[m_points.count() - 1], 4, 4); | |
|
69 | 72 | painter->restore(); |
|
70 | 73 | } |
|
71 | 74 |
General Comments 0
You need to be logged in to leave comments.
Login now