From 33494ffc7c2e59cac3965f9f24accd53d8635fb0 Mon Sep 17 00:00:00 2001 From: edouard Date: Sun, 19 Sep 2010 08:49:48 +0000 Subject: [PATCH] OP-160 Curve widget: 1st working version. Use "initCurve" with a list of double to create a curve, and connect to the curveUpdated signal to read it (or call getCurve manuall). git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1687 ebee16cc-31ac-478f-84a7-5cbb03baadba --- ground/src/plugins/config/mixercurveline.cpp | 19 ---- .../src/plugins/config/mixercurvewidget.cpp | 95 +++++++++++++------ ground/src/plugins/config/mixercurvewidget.h | 12 ++- 3 files changed, 75 insertions(+), 51 deletions(-) diff --git a/ground/src/plugins/config/mixercurveline.cpp b/ground/src/plugins/config/mixercurveline.cpp index 03daf08ae..98d955536 100644 --- a/ground/src/plugins/config/mixercurveline.cpp +++ b/ground/src/plugins/config/mixercurveline.cpp @@ -119,23 +119,4 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter->drawLine(line); - // Draw the arrows -/* - double angle = ::acos(line.dx() / line.length()); - if (line.dy() >= 0) - angle = TwoPi - angle; - - QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi / 3) * arrowSize, - cos(angle + Pi / 3) * arrowSize); - QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi / 3) * arrowSize, - cos(angle + Pi - Pi / 3) * arrowSize); - QPointF destArrowP1 = destPoint + QPointF(sin(angle - Pi / 3) * arrowSize, - cos(angle - Pi / 3) * arrowSize); - QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize, - cos(angle - Pi + Pi / 3) * arrowSize); - - painter->setBrush(Qt::black); - painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2); - painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2); - */ } diff --git a/ground/src/plugins/config/mixercurvewidget.cpp b/ground/src/plugins/config/mixercurvewidget.cpp index d2bc59b63..d9edfea93 100644 --- a/ground/src/plugins/config/mixercurvewidget.cpp +++ b/ground/src/plugins/config/mixercurvewidget.cpp @@ -67,35 +67,11 @@ MixerCurveWidget::MixerCurveWidget(QWidget *parent) : QGraphicsView(parent) scene->setSceneRect(plot->boundingRect()); setScene(scene); - Node *node1 = new Node(this); - Node *node2 = new Node(this); - Node *node3 = new Node(this); - Node *node4 = new Node(this); - Node *node5 = new Node(this); - scene->addItem(node1); - scene->addItem(node2); - scene->addItem(node3); - scene->addItem(node4); - scene->addItem(node5); + QList list; + list << 0 << 0.3 << 0.6 << -0.4 << -0.8; + initCurve(list); - scene->addItem(new Edge(node1, node2)); - scene->addItem(new Edge(node2, node3)); - scene->addItem(new Edge(node3, node4)); - scene->addItem(new Edge(node4, node5)); - - qreal w = plot->boundingRect().width()/4; - - node1->setPos(0,50); - node2->setPos(w, 50); - node3->setPos(2*w, 50); - node4->setPos(3*w, 50); - node5->setPos(4*w, 50); - - node1->verticalMove(true); - node2->verticalMove(true); - node3->verticalMove(true); - node4->verticalMove(true); - node5->verticalMove(true); + qDebug() << getCurve(); } @@ -104,6 +80,66 @@ MixerCurveWidget::~MixerCurveWidget() } +/** + Init curve: create a (flat) curve with a specified number of points. + + If a curve exists already, resets it. + Points should be between -1 and 1. + */ +void MixerCurveWidget::initCurve(QList points) +{ + + if (points.length() < 2) + return; // We need at least 2 points on a curve! + + // First of all, reset the list + // TODO: we probably need to actually delete some objects too there? + foreach (Node *node, nodeList ) { + QList edges = node->edges(); + foreach(Edge *edge, edges) + scene()->removeItem(edge); + scene()->removeItem(node); + } + nodeList.clear(); + + // Create the nodes + qreal w = plot->boundingRect().width()/(points.length()-1); + qreal h = plot->boundingRect().height()/2; + for (int i=0; iaddItem(node); + nodeList.append(node); + double val = points.at(i); + if (val>1) + val=1; + if (val<-1) + val=-1; + node->setPos(w*i,h-val*h); + node->verticalMove(true); + } + + // ... and link them together: + for (int i=0; i<(points.length()-1); i++) { + scene()->addItem(new Edge(nodeList.at(i),nodeList.at(i+1))); + } + +} + + +/** + Returns the current curve settings + */ +QList MixerCurveWidget::getCurve() { + QList list; + + qreal h = plot->boundingRect().height()/2; + foreach(Node *node, nodeList) { + list.append((h-node->pos().y())/h); + } + + return list; +} + void MixerCurveWidget::showEvent(QShowEvent *event) { Q_UNUSED(event) @@ -124,5 +160,6 @@ void MixerCurveWidget::resizeEvent(QResizeEvent* event) void MixerCurveWidget::itemMoved() { - qDebug() << "Moved ball"; + QList list = getCurve(); + emit curveUpdated(list); } diff --git a/ground/src/plugins/config/mixercurvewidget.h b/ground/src/plugins/config/mixercurvewidget.h index 8f305bf09..4009d4219 100644 --- a/ground/src/plugins/config/mixercurvewidget.h +++ b/ground/src/plugins/config/mixercurvewidget.h @@ -31,6 +31,8 @@ #include #include #include +#include "mixercurvepoint.h" +#include "mixercurveline.h" class MixerCurveWidget : public QGraphicsView @@ -40,15 +42,19 @@ class MixerCurveWidget : public QGraphicsView public: MixerCurveWidget(QWidget *parent = 0); ~MixerCurveWidget(); - void itemMoved(); - -public slots: + void itemMoved(); // Callback when a point is moved, to be updated + void initCurve (QList points); + QList getCurve(); +signals: + // Shall create a "curve updated" signal here, maybe ? + void curveUpdated(QList ); private slots: private: QGraphicsSvgItem *plot; + QList nodeList; protected: void showEvent(QShowEvent *event);