1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-09 20:46:07 +01:00
LibrePilot/ground/openpilotgcs/src/plugins/uavobjectwidgetutils/mixercurvewidget.cpp

351 lines
8.8 KiB
C++
Raw Normal View History

/**
******************************************************************************
*
* @file mixercurvewidget.cpp
2012-02-05 21:07:19 +01:00
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @addtogroup GCSPlugins GCS Plugins
* @{
2012-02-05 21:07:19 +01:00
* @addtogroup UAVObjectWidgetUtils Plugin
* @{
2012-02-05 21:07:19 +01:00
* @brief Utility plugin for UAVObject to Widget relation management
*****************************************************************************/
/*
* 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"
2012-07-12 06:49:05 +02:00
#include <QObject>
#include <QtGui>
#include <QDebug>
/*
* Initialize the widget
*/
2014-09-21 14:28:07 +02:00
MixerCurveWidget::MixerCurveWidget(QWidget *parent) :
QGraphicsView(parent), m_xAxisTextItem(0), m_yAxisTextItem(0)
{
// 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);
2014-09-21 14:28:07 +02:00
m_curveMin = 0.0;
m_curveMax = 1.0;
setFrameStyle(QFrame::NoFrame);
setStyleSheet("background:transparent");
QGraphicsScene *scene = new QGraphicsScene(this);
QSvgRenderer *renderer = new QSvgRenderer();
2014-09-21 14:28:07 +02:00
m_plot = new QGraphicsSvgItem();
renderer->load(QString(":/uavobjectwidgetutils/images/curve-bg.svg"));
2014-09-21 14:28:07 +02:00
m_plot->setSharedRenderer(renderer);
2014-09-21 14:28:07 +02:00
scene->addItem(m_plot);
m_plot->setZValue(-1);
2014-09-21 14:28:07 +02:00
scene->setSceneRect(m_plot->boundingRect());
setScene(scene);
2014-09-21 14:28:07 +02:00
setupXAxisLabel();
setupYAxisLabel();
initNodes(MixerCurveWidget::NODE_NUMELEM);
}
MixerCurveWidget::~MixerCurveWidget()
{
2014-09-21 14:28:07 +02:00
while (!m_nodeList.isEmpty()) {
delete m_nodeList.takeFirst();
}
2014-09-21 14:28:07 +02:00
while (!m_edgeList.isEmpty()) {
delete m_edgeList.takeFirst();
}
if (m_xAxisTextItem) {
delete m_xAxisTextItem;
m_xAxisTextItem = NULL;
}
if (m_yAxisTextItem) {
delete m_yAxisTextItem;
m_yAxisTextItem = NULL;
}
}
void MixerCurveWidget::setPositiveColor(QString color)
2012-07-12 06:49:05 +02:00
{
2014-09-21 14:28:07 +02:00
for (int i = 0; i < m_nodeList.count(); i++) {
MixerNode *node = m_nodeList.at(i);
node->setPositiveColor(color);
2012-07-12 06:49:05 +02:00
}
}
void MixerCurveWidget::setNegativeColor(QString color)
2012-07-12 06:49:05 +02:00
{
2014-09-21 14:28:07 +02:00
for (int i = 0; i < m_nodeList.count(); i++) {
MixerNode *node = m_nodeList.at(i);
node->setNegativeColor(color);
2012-07-12 06:49:05 +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.
*/
void MixerCurveWidget::initCurve(const QList<double> *points)
{
if (points->length() < 2) {
return; // We need at least 2 points on a curve!
}
// finally, set node positions
setCurve(points);
}
void MixerCurveWidget::initNodes(int numPoints)
{
// First of all, clear any existing list
2014-09-21 14:28:07 +02:00
if (m_nodeList.count()) {
foreach(MixerNode * node, m_nodeList) {
foreach(Edge * edge, node->edges()) {
if (edge->sourceNode() == node) {
scene()->removeItem(edge);
delete edge;
}
}
scene()->removeItem(node);
delete node;
}
2014-09-21 14:28:07 +02:00
m_nodeList.clear();
}
// Create the nodes and edges
MixerNode *prevNode = 0;
for (int i = 0; i < numPoints; i++) {
MixerNode *node = new MixerNode(this);
2014-09-21 14:28:07 +02:00
m_nodeList.append(node);
scene()->addItem(node);
node->setPos(0, 0);
if (prevNode) {
scene()->addItem(new Edge(prevNode, node));
}
prevNode = node;
}
}
2014-09-21 14:28:07 +02:00
void MixerCurveWidget::setupXAxisLabel()
{
if (!m_xAxisString.isEmpty()) {
if (m_xAxisTextItem) {
m_xAxisTextItem->setPlainText(m_xAxisString);
} else {
m_xAxisTextItem = new QGraphicsTextItem(m_xAxisString, m_plot);
scene()->addItem(m_xAxisTextItem);
}
}
}
void MixerCurveWidget::setupYAxisLabel()
{
if (!m_yAxisString.isEmpty()) {
if (m_yAxisTextItem) {
m_yAxisTextItem->setPlainText(m_yAxisString);
} else {
m_yAxisTextItem = new QGraphicsTextItem(m_yAxisString, m_plot);
// m_yAxisTextItem->setTransformOriginPoint(m_yAxisTextItem->boundingRect().height(), m_yAxisTextItem->boundingRect().left());
m_yAxisTextItem->setRotation(270);
scene()->addItem(m_yAxisTextItem);
}
}
}
/**
Returns the current curve settings
*/
QList<double> MixerCurveWidget::getCurve()
{
QList<double> list;
2014-09-21 14:28:07 +02:00
foreach(MixerNode * node, m_nodeList) {
list.append(node->value());
}
return list;
}
2011-06-12 01:49:05 +02:00
/**
Sets a linear graph
*/
void MixerCurveWidget::initLinearCurve(int numPoints, double maxValue, double minValue)
2011-06-12 01:49:05 +02:00
{
double range = maxValue - minValue; // setRange(minValue, maxValue);
2011-06-12 01:49:05 +02:00
QList<double> points;
for (double i = 0; i < (double)numPoints; i++) {
double val = (range * (i / (double)(numPoints - 1))) + minValue;
points.append(val);
2011-06-12 01:49:05 +02:00
}
initCurve(&points);
2011-06-12 01:49:05 +02:00
}
/**
Setd the current curve settings
*/
void MixerCurveWidget::setCurve(const QList<double> *points)
{
2014-09-21 14:28:07 +02:00
m_curveUpdating = true;
int ptCnt = points->count();
2014-09-21 14:28:07 +02:00
if (m_nodeList.count() != ptCnt) {
2012-06-23 02:31:14 +02:00
initNodes(ptCnt);
}
2014-09-21 14:28:07 +02:00
double range = m_curveMax - m_curveMin;
2014-09-21 14:28:07 +02:00
qreal w = m_plot->boundingRect().width() / (ptCnt - 1);
qreal h = m_plot->boundingRect().height();
for (int i = 0; i < ptCnt; i++) {
2014-09-21 14:28:07 +02:00
double val = (points->at(i) < m_curveMin) ? m_curveMin : (points->at(i) > m_curveMax) ? m_curveMax : points->at(i);
2012-06-23 02:31:14 +02:00
val += range;
2014-09-21 14:28:07 +02:00
val -= (m_curveMin + range);
2012-06-23 02:31:14 +02:00
val /= range;
2014-09-21 14:28:07 +02:00
MixerNode *node = m_nodeList.at(i);
node->setPos(w * i, h - (val * h));
node->verticalMove(true);
2012-07-12 06:49:05 +02:00
node->update();
}
2014-09-21 14:28:07 +02:00
m_curveUpdating = false;
update();
emit curveUpdated();
}
void MixerCurveWidget::showEvent(QShowEvent *event)
{
Q_UNUSED(event)
2014-09-21 14:28:07 +02:00
positionAxisLabels();
setSceneRect(scene()->itemsBoundingRect());
fitInView(scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
}
void MixerCurveWidget::resizeEvent(QResizeEvent *event)
{
Q_UNUSED(event);
2014-09-21 14:28:07 +02:00
positionAxisLabels();
setSceneRect(scene()->itemsBoundingRect());
fitInView(scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
}
void MixerCurveWidget::changeEvent(QEvent *event)
2012-07-12 06:49:05 +02:00
{
QGraphicsView::changeEvent(event);
if (event->type() == QEvent::EnabledChange) {
2014-09-21 14:28:07 +02:00
foreach(MixerNode * node, m_nodeList) {
node->update();
}
2012-07-12 06:49:05 +02:00
}
}
2014-09-21 14:28:07 +02:00
void MixerCurveWidget::positionAxisLabels()
{
QRectF rect = m_plot->boundingRect();
if (m_xAxisTextItem) {
m_xAxisTextItem->setPos(rect.right() -
m_xAxisTextItem->boundingRect().width(), rect.bottom() - 4);
}
if (m_yAxisTextItem) {
m_yAxisTextItem->setPos(rect.left() -
m_yAxisTextItem->boundingRect().height(), m_yAxisTextItem->boundingRect().width());
}
}
void MixerCurveWidget::itemMoved(double itemValue)
{
2012-07-12 06:49:05 +02:00
Q_UNUSED(itemValue);
2014-09-21 14:28:07 +02:00
if (!m_curveUpdating) {
emit curveUpdated();
}
}
void MixerCurveWidget::setMin(double value)
{
2014-09-21 14:28:07 +02:00
if (m_curveMin != value) {
emit curveMinChanged(value);
}
2014-09-21 14:28:07 +02:00
m_curveMin = value;
}
void MixerCurveWidget::setMax(double value)
{
2014-09-21 14:28:07 +02:00
if (m_curveMax != value) {
emit curveMaxChanged(value);
}
2014-09-21 14:28:07 +02:00
m_curveMax = value;
}
double MixerCurveWidget::getMin()
{
2014-09-21 14:28:07 +02:00
return m_curveMin;
}
double MixerCurveWidget::getMax()
{
2014-09-21 14:28:07 +02:00
return m_curveMax;
}
double MixerCurveWidget::setRange(double min, double max)
{
2014-09-21 14:28:07 +02:00
m_curveMin = min;
m_curveMax = max;
return m_curveMax - m_curveMin;
}
void MixerCurveWidget::setXAxisLabel(QString label)
{
m_xAxisString = label;
setupXAxisLabel();
}
void MixerCurveWidget::setYAxisLabel(QString label)
{
m_yAxisString = label;
setupYAxisLabel();
}