2011-03-29 22:32:09 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file mixercurvewidget.cpp
|
2012-02-05 21:07:19 +01:00
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
2011-03-29 22:32:09 +02:00
|
|
|
* @addtogroup GCSPlugins GCS Plugins
|
|
|
|
* @{
|
2012-02-05 21:07:19 +01:00
|
|
|
* @addtogroup UAVObjectWidgetUtils Plugin
|
2011-03-29 22:32:09 +02:00
|
|
|
* @{
|
2012-02-05 21:07:19 +01:00
|
|
|
* @brief Utility plugin for UAVObject to Widget relation management
|
2011-03-29 22:32:09 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mixercurvewidget.h"
|
|
|
|
#include "mixercurveline.h"
|
|
|
|
#include "mixercurvepoint.h"
|
|
|
|
|
|
|
|
#include <QtGui>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the widget
|
|
|
|
*/
|
|
|
|
MixerCurveWidget::MixerCurveWidget(QWidget *parent) : QGraphicsView(parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
// Create a layout, add a QGraphicsView and put the SVG inside.
|
|
|
|
// The Mixer Curve widget looks like this:
|
|
|
|
// |--------------------|
|
|
|
|
// | |
|
|
|
|
// | |
|
|
|
|
// | Graph |
|
|
|
|
// | |
|
|
|
|
// | |
|
|
|
|
// | |
|
|
|
|
// |--------------------|
|
|
|
|
|
|
|
|
|
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
setRenderHint(QPainter::Antialiasing);
|
|
|
|
|
|
|
|
curveMin=0.0;
|
|
|
|
curveMax=1.0;
|
|
|
|
|
|
|
|
|
2011-05-02 22:56:01 +02:00
|
|
|
setFrameStyle(QFrame::NoFrame);
|
|
|
|
setStyleSheet("background:transparent");
|
2011-03-29 22:32:09 +02:00
|
|
|
|
|
|
|
QGraphicsScene *scene = new QGraphicsScene(this);
|
|
|
|
QSvgRenderer *renderer = new QSvgRenderer();
|
|
|
|
plot = new QGraphicsSvgItem();
|
|
|
|
renderer->load(QString(":/configgadget/images/curve-bg.svg"));
|
|
|
|
plot->setSharedRenderer(renderer);
|
|
|
|
//plot->setElementId("map");
|
|
|
|
scene->addItem(plot);
|
|
|
|
plot->setZValue(-1);
|
2012-06-23 02:01:33 +02:00
|
|
|
|
2011-03-29 22:32:09 +02:00
|
|
|
scene->setSceneRect(plot->boundingRect());
|
|
|
|
setScene(scene);
|
|
|
|
|
2012-06-19 19:28:42 +02:00
|
|
|
initNodes(MixerCurveWidget::NODE_NUMELEM);
|
2011-03-29 22:32:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MixerCurveWidget::~MixerCurveWidget()
|
|
|
|
{
|
2012-06-20 21:46:07 +02:00
|
|
|
while (!nodePool.isEmpty())
|
|
|
|
delete nodePool.takeFirst();
|
2012-06-19 19:28:42 +02:00
|
|
|
|
2012-06-20 21:46:07 +02:00
|
|
|
while (!edgePool.isEmpty())
|
|
|
|
delete edgePool.takeFirst();
|
2012-06-19 19:28:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Node* MixerCurveWidget::getNode(int index)
|
|
|
|
{
|
|
|
|
Node* node;
|
|
|
|
|
|
|
|
if (index >= 0 && index < nodePool.count())
|
|
|
|
{
|
|
|
|
node = nodePool.at(index);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node = new Node(this);
|
|
|
|
nodePool.append(node);
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Edge* MixerCurveWidget::getEdge(int index, Node* sourceNode, Node* destNode)
|
|
|
|
{
|
|
|
|
Edge* edge;
|
2011-03-29 22:32:09 +02:00
|
|
|
|
2012-06-19 19:28:42 +02:00
|
|
|
if (index >= 0 && index < edgePool.count())
|
|
|
|
{
|
|
|
|
edge = edgePool.at(index);
|
|
|
|
edge->setSourceNode(sourceNode);
|
|
|
|
edge->setDestNode(destNode);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
edge = new Edge(sourceNode,destNode);
|
|
|
|
edgePool.append(edge);
|
|
|
|
}
|
|
|
|
return edge;
|
2011-03-29 22:32:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Init curve: create a (flat) curve with a specified number of points.
|
|
|
|
|
|
|
|
If a curve exists already, resets it.
|
|
|
|
Points should be between 0 and 1.
|
|
|
|
*/
|
2012-06-29 00:56:40 +02:00
|
|
|
void MixerCurveWidget::initCurve(const QList<double>* points)
|
2011-03-29 22:32:09 +02:00
|
|
|
{
|
2012-06-29 00:56:40 +02:00
|
|
|
if (points->length() < 2)
|
2011-03-29 22:32:09 +02:00
|
|
|
return; // We need at least 2 points on a curve!
|
|
|
|
|
2012-06-19 19:28:42 +02:00
|
|
|
// finally, set node positions
|
|
|
|
setCurve(points);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MixerCurveWidget::initNodes(int numPoints)
|
|
|
|
{
|
2012-06-20 00:22:11 +02:00
|
|
|
// First of all, clear any existing list
|
2012-06-19 19:28:42 +02:00
|
|
|
if (nodeList.count()) {
|
2011-03-29 22:32:09 +02:00
|
|
|
foreach (Node *node, nodeList ) {
|
2012-06-23 02:01:33 +02:00
|
|
|
foreach(Edge *edge, node->edges()) {
|
|
|
|
if (edge->sourceNode() == node) {
|
2012-06-20 00:22:11 +02:00
|
|
|
scene()->removeItem(edge);
|
|
|
|
}
|
2011-03-29 22:32:09 +02:00
|
|
|
}
|
2012-06-19 19:28:42 +02:00
|
|
|
scene()->removeItem(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeList.clear();
|
2011-03-29 22:32:09 +02:00
|
|
|
}
|
|
|
|
|
2012-06-20 00:22:11 +02:00
|
|
|
// Create the nodes and edges
|
|
|
|
Node* prevNode = 0;
|
2012-06-19 19:28:42 +02:00
|
|
|
for (int i=0; i<numPoints; i++) {
|
|
|
|
|
|
|
|
Node *node = getNode(i);
|
|
|
|
|
2011-03-29 22:32:09 +02:00
|
|
|
nodeList.append(node);
|
2012-06-19 19:28:42 +02:00
|
|
|
scene()->addItem(node);
|
2011-03-29 22:32:09 +02:00
|
|
|
|
2012-06-21 20:53:51 +02:00
|
|
|
node->setPos(0,0);
|
|
|
|
|
2012-06-20 00:22:11 +02:00
|
|
|
if (prevNode) {
|
|
|
|
scene()->addItem(getEdge(i, prevNode, node));
|
|
|
|
}
|
|
|
|
|
|
|
|
prevNode = node;
|
2011-03-29 22:32:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the current curve settings
|
|
|
|
*/
|
|
|
|
QList<double> MixerCurveWidget::getCurve() {
|
2012-06-20 01:51:22 +02:00
|
|
|
|
2011-03-29 22:32:09 +02:00
|
|
|
QList<double> list;
|
|
|
|
|
|
|
|
foreach(Node *node, nodeList) {
|
2012-06-21 20:53:51 +02:00
|
|
|
list.append(node->value());
|
2011-03-29 22:32:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2011-06-12 01:49:05 +02:00
|
|
|
/**
|
|
|
|
Sets a linear graph
|
|
|
|
*/
|
2012-06-23 02:01:33 +02:00
|
|
|
void MixerCurveWidget::initLinearCurve(int numPoints, double maxValue, double minValue)
|
2011-06-12 01:49:05 +02:00
|
|
|
{
|
2012-06-24 20:11:53 +02:00
|
|
|
double range = setRange(minValue, maxValue);
|
2012-06-20 00:22:11 +02:00
|
|
|
|
2011-06-12 01:49:05 +02:00
|
|
|
QList<double> points;
|
2012-06-23 02:01:33 +02:00
|
|
|
for (double i=0; i < (double)numPoints; i++) {
|
2012-06-24 20:11:53 +02:00
|
|
|
double val = (range * ( i / (double)(numPoints-1) ) ) + minValue;
|
2012-06-20 00:22:11 +02:00
|
|
|
points.append(val);
|
2011-06-12 01:49:05 +02:00
|
|
|
}
|
2012-06-29 00:56:40 +02:00
|
|
|
initCurve(&points);
|
2011-06-12 01:49:05 +02:00
|
|
|
}
|
2011-03-29 22:32:09 +02:00
|
|
|
/**
|
|
|
|
Setd the current curve settings
|
|
|
|
*/
|
2012-06-29 00:56:40 +02:00
|
|
|
void MixerCurveWidget::setCurve(const QList<double>* points)
|
2011-03-29 22:32:09 +02:00
|
|
|
{
|
2012-06-19 19:28:42 +02:00
|
|
|
curveUpdating = true;
|
|
|
|
|
2012-06-29 00:56:40 +02:00
|
|
|
int ptCnt = points->count();
|
2012-06-23 02:31:14 +02:00
|
|
|
if (nodeList.count() != ptCnt)
|
|
|
|
initNodes(ptCnt);
|
2012-06-19 19:28:42 +02:00
|
|
|
|
2012-06-23 02:31:14 +02:00
|
|
|
double range = curveMax - curveMin;
|
2012-06-21 20:53:51 +02:00
|
|
|
|
2012-06-23 02:31:14 +02:00
|
|
|
qreal w = plot->boundingRect().width()/(ptCnt-1);
|
|
|
|
qreal h = plot->boundingRect().height();
|
|
|
|
for (int i=0; i<ptCnt; i++) {
|
2012-06-19 19:28:42 +02:00
|
|
|
|
2012-06-29 00:56:40 +02:00
|
|
|
double val = (points->at(i) < curveMin) ? curveMin : (points->at(i) > curveMax) ? curveMax : points->at(i);
|
2012-06-19 19:28:42 +02:00
|
|
|
|
2012-06-23 02:31:14 +02:00
|
|
|
val += range;
|
|
|
|
val -= (curveMin + range);
|
|
|
|
val /= range;
|
2012-06-19 19:28:42 +02:00
|
|
|
|
2012-06-21 20:53:51 +02:00
|
|
|
Node* node = nodeList.at(i);
|
|
|
|
node->setPos(w*i, h - (val*h));
|
|
|
|
node->verticalMove(true);
|
2012-06-30 23:35:38 +02:00
|
|
|
node->update();
|
2012-06-21 20:53:51 +02:00
|
|
|
}
|
2012-06-19 19:28:42 +02:00
|
|
|
curveUpdating = false;
|
|
|
|
|
2012-06-21 20:53:51 +02:00
|
|
|
update();
|
|
|
|
|
2012-06-30 23:35:38 +02:00
|
|
|
emit curveUpdated();
|
2011-03-29 22:32:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MixerCurveWidget::showEvent(QShowEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event)
|
|
|
|
// Thit fitInView method should only be called now, once the
|
|
|
|
// widget is shown, otherwise it cannot compute its values and
|
|
|
|
// the result is usually a ahrsbargraph that is way too small.
|
|
|
|
|
2012-07-06 20:44:16 +02:00
|
|
|
QRectF rect = plot->boundingRect();
|
|
|
|
|
|
|
|
fitInView(rect.adjusted(-12,-12,12,12), Qt::KeepAspectRatio);
|
2011-03-29 22:32:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MixerCurveWidget::resizeEvent(QResizeEvent* event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
2012-07-06 20:28:46 +02:00
|
|
|
|
2012-07-06 20:44:16 +02:00
|
|
|
QRectF rect = plot->boundingRect();
|
|
|
|
|
|
|
|
fitInView(rect.adjusted(-12,-12,12,12), Qt::KeepAspectRatio);
|
2011-03-29 22:32:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MixerCurveWidget::itemMoved(double itemValue)
|
|
|
|
{
|
2012-06-19 19:28:42 +02:00
|
|
|
if (!curveUpdating) {
|
2012-06-30 23:35:38 +02:00
|
|
|
emit curveUpdated();
|
2012-06-19 19:28:42 +02:00
|
|
|
}
|
2011-03-29 22:32:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MixerCurveWidget::setMin(double value)
|
|
|
|
{
|
2012-06-30 23:35:38 +02:00
|
|
|
if (curveMin != value)
|
|
|
|
emit curveMinChanged(value);
|
|
|
|
|
2011-03-29 22:32:09 +02:00
|
|
|
curveMin = value;
|
|
|
|
}
|
2012-06-30 23:35:38 +02:00
|
|
|
|
2011-03-29 22:32:09 +02:00
|
|
|
void MixerCurveWidget::setMax(double value)
|
|
|
|
{
|
2012-06-30 23:35:38 +02:00
|
|
|
if (curveMax != value)
|
|
|
|
emit curveMaxChanged(value);
|
|
|
|
|
2011-03-29 22:32:09 +02:00
|
|
|
curveMax = value;
|
|
|
|
}
|
2012-06-30 23:35:38 +02:00
|
|
|
|
2012-06-20 01:51:22 +02:00
|
|
|
double MixerCurveWidget::getMin()
|
|
|
|
{
|
|
|
|
return curveMin;
|
|
|
|
}
|
|
|
|
double MixerCurveWidget::getMax()
|
|
|
|
{
|
|
|
|
return curveMax;
|
|
|
|
}
|
2012-06-24 20:11:53 +02:00
|
|
|
double MixerCurveWidget::setRange(double min, double max)
|
2011-03-29 22:32:09 +02:00
|
|
|
{
|
|
|
|
curveMin = min;
|
|
|
|
curveMax = max;
|
2012-06-24 20:11:53 +02:00
|
|
|
return curveMax - curveMin;
|
2011-03-29 22:32:09 +02:00
|
|
|
}
|