From 575ceaee424c94bcff55c6110dd9365970717752 2012-03-02 09:56:05 From: Marek Rosa Date: 2012-03-02 09:56:05 Subject: [PATCH] Added automatic refresh of control points on add/remove point. Spline example updated --- diff --git a/example/splinechart/splinewidget.cpp b/example/splinechart/splinewidget.cpp index c8a4347..80a5f99 100644 --- a/example/splinechart/splinewidget.cpp +++ b/example/splinechart/splinewidget.cpp @@ -1,8 +1,8 @@ #include "splinewidget.h" #include "qchartview.h" -#include "qsplineseries.h" #include "qlineseries.h" #include +#include QTCOMMERCIALCHART_USE_NAMESPACE @@ -10,28 +10,31 @@ SplineWidget::SplineWidget(QWidget *parent) : QWidget(parent) { //create QSplineSeries - QSplineSeries* series = new QSplineSeries(this); -// series->add(QPointF(150, 100)); -// series->add(QPointF(200, 130)); -// series->add(QPointF(250, 120)); -// series->add(QPointF(300, 140)); -// series->add(QPointF(350, 100)); -// series->add(QPointF(400, 120)); -// series->add(QPointF(450, 150)); -//// series->add(QPointF(600, 150)); -// series->add(QPointF(500, 145)); -// series->add(QPointF(550, 170)); -// series->add(QPointF(600, 190)); -// series->add(QPointF(650, 210)); -// series->add(QPointF(700, 190)); -// series->add(QPointF(750, 180)); -// series->add(QPointF(800, 170)); + series = new QSplineSeries(this); + series->add(QPointF(150, 100)); + series->add(QPointF(200, 130)); + series->add(QPointF(250, 120)); + series->add(QPointF(300, 140)); + series->add(QPointF(350, 100)); + series->add(QPointF(400, 120)); + series->add(QPointF(450, 150)); + // series->add(QPointF(600, 150)); + series->add(QPointF(500, 145)); + series->add(QPointF(550, 170)); + series->add(QPointF(600, 190)); + series->add(QPointF(650, 210)); + series->add(QPointF(700, 190)); + series->add(QPointF(750, 180)); + series->add(QPointF(800, 170)); -// qsrand(time(NULL)); -// for (int i = 0; i < 100; i++) -// { -// series->add(QPointF(i*7, qrand()%600)); -// } +// series->calculateControlPoints(); + + QSplineSeries* series2 = new QSplineSeries(this); + qsrand(time(NULL)); + // for (int i = 0; i < 100; i++) + // { + // series2->add(QPointF(i*7, qrand()%600)); + // } int k = 10; for (int i = 0; i < 25; i++) { @@ -39,38 +42,67 @@ SplineWidget::SplineWidget(QWidget *parent) { k = 10; } - series->add(QPointF(i*50, k)); + series2->add(QPointF(i*50, k)); k +=10; } - series->calculateControlPoints(); +// series2->calculateControlPoints(); -// QLineSeries* lineSeries = new QLineSeries; -// for (int i = 0; i < series->count() - 1; i++) -// { -// lineSeries->add(series->at(i).x(), series->at(i).y()); -// lineSeries->add(series->controlPoint(2*i).x(), series->controlPoint(2*i).y()); -// lineSeries->add(series->controlPoint(2*i + 1).x(), series->controlPoint(2*i + 1).y()); -// } + // QLineSeries* lineSeries = new QLineSeries; + // for (int i = 0; i < series->count() - 1; i++) + // { + // lineSeries->add(series->at(i).x(), series->at(i).y()); + // lineSeries->add(series->controlPoint(2*i).x(), series->controlPoint(2*i).y()); + // lineSeries->add(series->controlPoint(2*i + 1).x(), series->controlPoint(2*i + 1).y()); + // } -// QLineChartSeries* lineSeries2 = new QLineChartSeries; -// lineSeries2->add(10, 50); -// lineSeries2->add(30, 15); -// lineSeries2->add(60, 40); -// lineSeries2->add(90, 70); -// lineSeries2->add(100, 20); + // QLineChartSeries* lineSeries2 = new QLineChartSeries; + // lineSeries2->add(10, 50); + // lineSeries2->add(30, 15); + // lineSeries2->add(60, 40); + // lineSeries2->add(90, 70); + // lineSeries2->add(100, 20); //create chart view QChartView* chart = new QChartView; chart->setMinimumSize(800,600); -// chart->setGeometry(50, 50, 400, 300); + // chart->setGeometry(50, 50, 400, 300); chart->addSeries(series); + chart->addSeries(series2); + + //add new item + QPushButton* addButton = new QPushButton("Add new point"); + connect(addButton, SIGNAL(clicked()), this, SLOT(addNewPoint())); + + QPushButton* removeButton = new QPushButton("Remove point from the end"); + connect(removeButton, SIGNAL(clicked()), this, SLOT(removePoint())); + + //butttons layout + QVBoxLayout* buttonsLayout = new QVBoxLayout; + buttonsLayout->addWidget(addButton); + buttonsLayout->addWidget(removeButton); + buttonsLayout->addStretch(); QGridLayout* mainLayout = new QGridLayout; - mainLayout->addWidget(chart); + mainLayout->addWidget(chart, 1, 0); + mainLayout->addLayout(buttonsLayout, 1, 1); setLayout(mainLayout); } +void SplineWidget::addNewPoint() +{ + if (series->count() > 0) + series->add(QPointF(series->x(series->count() - 1) + 50, series->y(series->count() - 1) - 50 + qrand()%100)); + else + series->add(QPointF(50, 50 + qrand()%50)); +} + +void SplineWidget::removePoint() +{ + if (series->count() > 0) + series->remove(QPointF(series->x(series->count() - 1), series->y(series->count() - 1))); +} + SplineWidget::~SplineWidget() { diff --git a/example/splinechart/splinewidget.h b/example/splinechart/splinewidget.h index 2dfae26..ad24d20 100644 --- a/example/splinechart/splinewidget.h +++ b/example/splinechart/splinewidget.h @@ -2,6 +2,9 @@ #define SPLINEWIDGET_H #include +#include "qsplineseries.h" + +QTCOMMERCIALCHART_USE_NAMESPACE class SplineWidget : public QWidget { @@ -10,6 +13,13 @@ class SplineWidget : public QWidget public: SplineWidget(QWidget *parent = 0); ~SplineWidget(); + +public slots: + void addNewPoint(); + void removePoint(); + +private: + QSplineSeries* series; }; #endif // SPLINEWIDGET_H diff --git a/src/splinechart/qsplineseries.cpp b/src/splinechart/qsplineseries.cpp index 1091a58..2a97231 100644 --- a/src/splinechart/qsplineseries.cpp +++ b/src/splinechart/qsplineseries.cpp @@ -5,21 +5,11 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE QSplineSeries::QSplineSeries(QObject *parent) : QLineSeries(parent) { + connect(this,SIGNAL(pointAdded(int)), this, SLOT(updateControlPoints())); + connect(this,SIGNAL(pointRemoved(int)), this, SLOT(updateControlPoints())); + connect(this,SIGNAL(pointReplaced(int)), this, SLOT(updateControlPoints())); } -//QSplineSeries& QSplineSeries::operator << (const QPointF &value) -//{ -//// d->m_data.append(value); -// m_data.append(value); -//// emit changed(); -// return *this; -//} - -//void QSplineSeries::addData(QPointF value) -//{ -// m_data.append(value); -//} - void QSplineSeries::calculateControlPoints() { @@ -72,8 +62,6 @@ void QSplineSeries::calculateControlPoints() QList y = getFirstControlPoints(rhs); // Fill output arrays. - // firstControlPoints = new Point[n]; - // secondControlPoints = new Point[n]; for (int i = 0; i < n; ++i) { // First control point @@ -105,6 +93,16 @@ QList QSplineSeries::getFirstControlPoints(QList rhs) return x; } + +void QSplineSeries::updateControlPoints() +{ + if(m_x.size() > 1) + { + m_controlPoints.clear(); + calculateControlPoints(); + } +} + #include "moc_qsplineseries.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/splinechart/qsplineseries.h b/src/splinechart/qsplineseries.h index 9a37010..b945f00 100644 --- a/src/splinechart/qsplineseries.h +++ b/src/splinechart/qsplineseries.h @@ -16,22 +16,18 @@ class QSplineSeries : public QLineSeries QSplineSeries(QObject *parent = 0); QSeriesType type() const { return QSeries::SeriesTypeSpline; } -// void addData(QPointF value); -// QSplineSeries& operator << (const QPointF &value); - void calculateControlPoints(); - QList getFirstControlPoints(QList rhs); int count() const { return m_x.size(); } - -// QPointF at(int index) const { return m_data[index]; } QPointF controlPoint(int index) const { return m_controlPoints[index]; } - signals: - - public slots: + private: + void calculateControlPoints(); + QList getFirstControlPoints(QList rhs); + + private slots: + void updateControlPoints(); private: -// QList m_data; QList m_controlPoints; }; diff --git a/src/splinechart/splinepresenter.cpp b/src/splinechart/splinepresenter.cpp index a50bb4f..f6b658a 100644 --- a/src/splinechart/splinepresenter.cpp +++ b/src/splinechart/splinepresenter.cpp @@ -6,7 +6,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE SplinePresenter::SplinePresenter(QSplineSeries* series, QGraphicsObject *parent) : LineChartItem(0, series, parent)//,m_boundingRect() { - // + // } @@ -61,11 +61,14 @@ void SplinePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *o painter->setPen(Qt::blue); // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i)); // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1)); - painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4); - painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4); + // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4); + // painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4); + } + if (m_points.count() > 0) + { + painter->setPen(Qt::red); + painter->drawEllipse(m_points[m_points.count() - 1], 4, 4); } - painter->setPen(Qt::red); - painter->drawEllipse(m_points[m_points.count() - 1], 4, 4); painter->restore(); }