2010-09-17 22:49:24 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file mixercurvepoint.h
|
2012-02-05 21:07:19 +01:00
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
2010-09-17 22:49:24 +02:00
|
|
|
* @addtogroup GCSPlugins GCS Plugins
|
|
|
|
* @{
|
2012-02-05 21:07:19 +01:00
|
|
|
* @addtogroup UAVObjectWidgetUtils Plugin
|
2010-09-17 22:49:24 +02:00
|
|
|
* @{
|
2012-02-05 21:07:19 +01:00
|
|
|
* @brief Utility plugin for UAVObject to Widget relation management
|
2010-09-17 22:49:24 +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 <QGraphicsScene>
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QStyleOption>
|
2012-07-12 06:49:05 +02:00
|
|
|
#include <QColor>
|
2010-09-28 20:27:34 +02:00
|
|
|
#include <QDebug>
|
2010-09-17 22:49:24 +02:00
|
|
|
|
|
|
|
#include "mixercurveline.h"
|
|
|
|
#include "mixercurvepoint.h"
|
|
|
|
#include "mixercurvewidget.h"
|
|
|
|
|
2012-09-12 12:09:30 +02:00
|
|
|
MixerNode::MixerNode(MixerCurveWidget *graphWidget)
|
2010-09-17 22:49:24 +02:00
|
|
|
: graph(graphWidget)
|
|
|
|
{
|
|
|
|
setFlag(ItemIsMovable);
|
|
|
|
setFlag(ItemSendsGeometryChanges);
|
|
|
|
setCacheMode(DeviceCoordinateCache);
|
|
|
|
setZValue(-1);
|
2013-05-19 16:37:30 +02:00
|
|
|
vertical = false;
|
|
|
|
drawNode = true;
|
|
|
|
drawText = true;
|
|
|
|
|
|
|
|
positiveColor = "#609FF2"; // blueish?
|
|
|
|
neutralColor = "#14CE24"; // greenish?
|
|
|
|
negativeColor = "#EF5F5F"; // redish?
|
|
|
|
disabledColor = "#dddddd";
|
2013-05-04 00:13:15 +02:00
|
|
|
disabledTextColor = "#aaaaaa";
|
2010-09-17 22:49:24 +02:00
|
|
|
}
|
|
|
|
|
2012-09-12 12:09:30 +02:00
|
|
|
void MixerNode::addEdge(Edge *edge)
|
2010-09-17 22:49:24 +02:00
|
|
|
{
|
|
|
|
edgeList << edge;
|
|
|
|
edge->adjust();
|
|
|
|
}
|
|
|
|
|
2012-09-12 12:09:30 +02:00
|
|
|
QList<Edge *> MixerNode::edges() const
|
2010-09-17 22:49:24 +02:00
|
|
|
{
|
|
|
|
return edgeList;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-12 12:09:30 +02:00
|
|
|
QRectF MixerNode::boundingRect() const
|
2010-09-17 22:49:24 +02:00
|
|
|
{
|
2013-05-04 00:13:15 +02:00
|
|
|
return QRectF(-13, -13, 26, 26);
|
2010-09-17 22:49:24 +02:00
|
|
|
}
|
|
|
|
|
2012-09-12 12:09:30 +02:00
|
|
|
QPainterPath MixerNode::shape() const
|
2010-09-17 22:49:24 +02:00
|
|
|
{
|
|
|
|
QPainterPath path;
|
2013-05-19 16:37:30 +02:00
|
|
|
|
2012-07-12 06:49:05 +02:00
|
|
|
path.addEllipse(boundingRect());
|
2010-09-17 22:49:24 +02:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2012-09-12 12:09:30 +02:00
|
|
|
void MixerNode::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
|
2010-09-17 22:49:24 +02:00
|
|
|
{
|
2013-05-04 00:13:15 +02:00
|
|
|
QString text = QString().sprintf("%.2f", value());
|
2013-05-19 16:37:30 +02:00
|
|
|
|
2012-09-12 12:09:30 +02:00
|
|
|
painter->setFont(graph->font());
|
2012-07-14 07:41:58 +02:00
|
|
|
if (drawNode) {
|
|
|
|
QRadialGradient gradient(-3, -3, 10);
|
2013-05-04 00:13:15 +02:00
|
|
|
|
|
|
|
QColor color;
|
|
|
|
if (value() < 0) {
|
|
|
|
color = negativeColor;
|
2013-05-19 16:37:30 +02:00
|
|
|
} else if (value() == 0) {
|
2013-05-04 00:13:15 +02:00
|
|
|
color = neutralColor;
|
2013-05-19 16:37:30 +02:00
|
|
|
} else {
|
2013-05-04 00:13:15 +02:00
|
|
|
color = positiveColor;
|
|
|
|
}
|
|
|
|
|
2012-07-14 07:41:58 +02:00
|
|
|
if (option->state & QStyle::State_Sunken) {
|
|
|
|
gradient.setCenter(3, 3);
|
|
|
|
gradient.setFocalPoint(3, 3);
|
|
|
|
|
2013-05-04 00:13:15 +02:00
|
|
|
QColor selColor = color.darker();
|
|
|
|
gradient.setColorAt(1, selColor.darker());
|
|
|
|
gradient.setColorAt(0, selColor);
|
2012-07-14 07:41:58 +02:00
|
|
|
} else {
|
2013-05-04 00:13:15 +02:00
|
|
|
gradient.setColorAt(0, graph->isEnabled() ? color : disabledColor);
|
|
|
|
gradient.setColorAt(1, graph->isEnabled() ? color.darker() : disabledColor);
|
2012-07-14 07:41:58 +02:00
|
|
|
}
|
|
|
|
painter->setBrush(gradient);
|
2013-05-04 00:13:15 +02:00
|
|
|
painter->setPen(graph->isEnabled() ? QPen(Qt::black, 0) : QPen(disabledTextColor));
|
2012-07-14 07:41:58 +02:00
|
|
|
painter->drawEllipse(boundingRect());
|
2010-09-17 22:49:24 +02:00
|
|
|
|
2013-05-04 00:13:15 +02:00
|
|
|
if (!image.isNull()) {
|
|
|
|
painter->drawImage(boundingRect().adjusted(1, 1, -1, -1), image);
|
|
|
|
}
|
2012-07-14 07:41:58 +02:00
|
|
|
}
|
2012-07-08 19:38:15 +02:00
|
|
|
|
2012-07-14 07:41:58 +02:00
|
|
|
if (drawText) {
|
2013-05-19 16:37:30 +02:00
|
|
|
if (graph->isEnabled()) {
|
2013-05-04 00:13:15 +02:00
|
|
|
painter->setPen(QPen(drawNode ? Qt::white : Qt::black, 0));
|
|
|
|
} else {
|
|
|
|
painter->setPen(QPen(disabledTextColor));
|
2012-07-08 19:38:15 +02:00
|
|
|
}
|
2013-05-04 00:13:15 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
painter->drawText((value() < 0) ? -10 : -8, 3, text);
|
2010-09-17 22:49:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
void MixerNode::verticalMove(bool flag)
|
|
|
|
{
|
2010-09-19 07:47:18 +02:00
|
|
|
vertical = flag;
|
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
double MixerNode::value()
|
|
|
|
{
|
|
|
|
double h = graph->sceneRect().height();
|
2012-06-21 20:53:51 +02:00
|
|
|
double ratio = (h - pos().y()) / h;
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
return ((graph->getMax() - graph->getMin()) * ratio) + graph->getMin();
|
2012-06-20 01:51:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-12 12:09:30 +02:00
|
|
|
QVariant MixerNode::itemChange(GraphicsItemChange change, const QVariant &val)
|
2010-09-17 22:49:24 +02:00
|
|
|
{
|
2012-06-21 20:53:51 +02:00
|
|
|
QPointF newPos = val.toPointF();
|
2010-10-01 10:40:46 +02:00
|
|
|
double h = graph->sceneRect().height();
|
2012-06-21 20:53:51 +02:00
|
|
|
|
2010-09-17 22:49:24 +02:00
|
|
|
switch (change) {
|
2013-05-19 16:37:30 +02:00
|
|
|
case ItemPositionChange:
|
|
|
|
{
|
|
|
|
if (!vertical) {
|
|
|
|
break;
|
|
|
|
}
|
2012-06-21 20:53:51 +02:00
|
|
|
|
|
|
|
// Force node to move vertically
|
|
|
|
newPos.setX(pos().x());
|
2012-06-23 02:01:33 +02:00
|
|
|
|
2012-06-21 20:53:51 +02:00
|
|
|
// Stay inside graph
|
2013-05-19 16:37:30 +02:00
|
|
|
if (newPos.y() < 0) {
|
2012-06-21 20:53:51 +02:00
|
|
|
newPos.setY(0);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
// qDebug() << h << " - " << newPos.y();
|
|
|
|
if (newPos.y() > h) {
|
2012-06-21 20:53:51 +02:00
|
|
|
newPos.setY(h);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2012-06-23 02:01:33 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return newPos;
|
2010-09-19 07:47:18 +02:00
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
case ItemPositionHasChanged:
|
|
|
|
{
|
|
|
|
foreach(Edge * edge, edgeList)
|
|
|
|
edge->adjust();
|
2012-06-20 01:51:22 +02:00
|
|
|
|
2012-06-21 20:53:51 +02:00
|
|
|
update();
|
2012-06-20 01:51:22 +02:00
|
|
|
|
2012-06-21 20:53:51 +02:00
|
|
|
graph->itemMoved(value());
|
2010-09-17 22:49:24 +02:00
|
|
|
break;
|
2012-06-20 01:51:22 +02:00
|
|
|
}
|
2010-09-17 22:49:24 +02:00
|
|
|
default:
|
|
|
|
break;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
;
|
2010-09-17 22:49:24 +02:00
|
|
|
|
2012-06-21 20:53:51 +02:00
|
|
|
return QGraphicsItem::itemChange(change, val);
|
2010-09-17 22:49:24 +02:00
|
|
|
}
|
|
|
|
|
2012-09-12 12:09:30 +02:00
|
|
|
void MixerNode::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
2010-09-17 22:49:24 +02:00
|
|
|
{
|
|
|
|
update();
|
|
|
|
QGraphicsItem::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
|
2012-09-12 12:09:30 +02:00
|
|
|
void MixerNode::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
2010-09-17 22:49:24 +02:00
|
|
|
{
|
|
|
|
update();
|
|
|
|
QGraphicsItem::mouseReleaseEvent(event);
|
|
|
|
}
|