1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-11-30 08:24:11 +01:00

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
This commit is contained in:
edouard 2010-09-19 08:49:48 +00:00 committed by edouard
parent 88de8f789a
commit 33494ffc7c
3 changed files with 75 additions and 51 deletions

View File

@ -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);
*/
}

View File

@ -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<double> 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<double> 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<Edge*> 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; i<points.length(); i++) {
Node *node = new Node(this);
scene()->addItem(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<double> MixerCurveWidget::getCurve() {
QList<double> 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<double> list = getCurve();
emit curveUpdated(list);
}

View File

@ -31,6 +31,8 @@
#include <QGraphicsView>
#include <QtSvg/QSvgRenderer>
#include <QtSvg/QGraphicsSvgItem>
#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<double> points);
QList<double> getCurve();
signals:
// Shall create a "curve updated" signal here, maybe ?
void curveUpdated(QList<double> );
private slots:
private:
QGraphicsSvgItem *plot;
QList<Node*> nodeList;
protected:
void showEvent(QShowEvent *event);