mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-03-01 18:29:16 +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:
parent
88de8f789a
commit
33494ffc7c
@ -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->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
|
||||||
painter->drawLine(line);
|
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);
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
@ -67,35 +67,11 @@ MixerCurveWidget::MixerCurveWidget(QWidget *parent) : QGraphicsView(parent)
|
|||||||
scene->setSceneRect(plot->boundingRect());
|
scene->setSceneRect(plot->boundingRect());
|
||||||
setScene(scene);
|
setScene(scene);
|
||||||
|
|
||||||
Node *node1 = new Node(this);
|
QList<double> list;
|
||||||
Node *node2 = new Node(this);
|
list << 0 << 0.3 << 0.6 << -0.4 << -0.8;
|
||||||
Node *node3 = new Node(this);
|
initCurve(list);
|
||||||
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);
|
|
||||||
|
|
||||||
scene->addItem(new Edge(node1, node2));
|
qDebug() << getCurve();
|
||||||
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);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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)
|
void MixerCurveWidget::showEvent(QShowEvent *event)
|
||||||
{
|
{
|
||||||
Q_UNUSED(event)
|
Q_UNUSED(event)
|
||||||
@ -124,5 +160,6 @@ void MixerCurveWidget::resizeEvent(QResizeEvent* event)
|
|||||||
|
|
||||||
void MixerCurveWidget::itemMoved()
|
void MixerCurveWidget::itemMoved()
|
||||||
{
|
{
|
||||||
qDebug() << "Moved ball";
|
QList<double> list = getCurve();
|
||||||
|
emit curveUpdated(list);
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,8 @@
|
|||||||
#include <QGraphicsView>
|
#include <QGraphicsView>
|
||||||
#include <QtSvg/QSvgRenderer>
|
#include <QtSvg/QSvgRenderer>
|
||||||
#include <QtSvg/QGraphicsSvgItem>
|
#include <QtSvg/QGraphicsSvgItem>
|
||||||
|
#include "mixercurvepoint.h"
|
||||||
|
#include "mixercurveline.h"
|
||||||
|
|
||||||
|
|
||||||
class MixerCurveWidget : public QGraphicsView
|
class MixerCurveWidget : public QGraphicsView
|
||||||
@ -40,15 +42,19 @@ class MixerCurveWidget : public QGraphicsView
|
|||||||
public:
|
public:
|
||||||
MixerCurveWidget(QWidget *parent = 0);
|
MixerCurveWidget(QWidget *parent = 0);
|
||||||
~MixerCurveWidget();
|
~MixerCurveWidget();
|
||||||
void itemMoved();
|
void itemMoved(); // Callback when a point is moved, to be updated
|
||||||
|
void initCurve (QList<double> points);
|
||||||
public slots:
|
QList<double> getCurve();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
// Shall create a "curve updated" signal here, maybe ?
|
||||||
|
void curveUpdated(QList<double> );
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QGraphicsSvgItem *plot;
|
QGraphicsSvgItem *plot;
|
||||||
|
QList<Node*> nodeList;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void showEvent(QShowEvent *event);
|
void showEvent(QShowEvent *event);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user