LP-56 - Better txpid option namings, fix tabs-spaces, tooltips. headers, variable...
[librepilot.git] / ground / openpilotgcs / src / plugins / uavobjectwidgetutils / mixercurvepoint.cpp
blob2ab2c24781961c6a37fde03679b337e9029392f1
1 /**
2 ******************************************************************************
4 * @file mixercurvepoint.h
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @addtogroup GCSPlugins GCS Plugins
7 * @{
8 * @addtogroup UAVObjectWidgetUtils Plugin
9 * @{
10 * @brief Utility plugin for UAVObject to Widget relation management
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <QGraphicsScene>
29 #include <QGraphicsSceneMouseEvent>
30 #include <QPainter>
31 #include <QStyleOption>
32 #include <QColor>
33 #include <QDebug>
35 #include "mixercurveline.h"
36 #include "mixercurvepoint.h"
37 #include "mixercurvewidget.h"
39 MixerNode::MixerNode(MixerCurveWidget *graphWidget, QGraphicsItem *graphItem)
40 : m_graph(graphWidget), m_graphItem(graphItem)
42 setFlag(ItemIsMovable);
43 setFlag(ItemSendsGeometryChanges);
44 setCacheMode(DeviceCoordinateCache);
45 setZValue(-1);
46 m_vertical = false;
47 m_drawNode = true;
48 m_drawText = true;
50 m_alpha = 0.7;
51 m_positiveColor = "#609FF2"; // blueish?
52 m_neutralColor = "#14CE24"; // greenish?
53 m_negativeColor = "#EF5F5F"; // redish?
54 m_disabledColor = "#dddddd";
55 m_disabledTextColor = "#aaaaaa";
58 void MixerNode::addEdge(Edge *edge)
60 m_edgeList << edge;
61 edge->adjust();
64 QList<Edge *> MixerNode::edges() const
66 return m_edgeList;
70 QRectF MixerNode::boundingRect() const
72 return QRectF(-13, -13, 26, 26);
75 QPainterPath MixerNode::shape() const
77 QPainterPath path;
79 path.addEllipse(boundingRect());
80 return path;
83 void MixerNode::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
85 QString text = QString().sprintf("%.2f", value());
87 painter->setFont(m_graph->font());
88 if (m_drawNode) {
89 QRadialGradient gradient(-3, -3, 10);
91 QColor color;
92 if (value() < 0) {
93 color = m_negativeColor;
94 } else if (value() == 0) {
95 color = m_neutralColor;
96 } else {
97 color = m_positiveColor;
99 color.setAlphaF(m_alpha);
101 if (option->state & QStyle::State_Sunken) {
102 gradient.setCenter(3, 3);
103 gradient.setFocalPoint(3, 3);
105 QColor selColor = color.darker();
106 gradient.setColorAt(1, selColor.darker());
107 gradient.setColorAt(0, selColor);
108 } else {
109 gradient.setColorAt(0, m_graph->isEnabled() ? color : m_disabledColor);
110 gradient.setColorAt(1, m_graph->isEnabled() ? color.darker() : m_disabledColor);
112 painter->setBrush(gradient);
113 painter->setPen(m_graph->isEnabled() ? QPen(Qt::black, 0) : QPen(m_disabledTextColor));
114 painter->drawEllipse(boundingRect());
116 if (!m_image.isNull()) {
117 painter->drawImage(boundingRect().adjusted(1, 1, -1, -1), m_image);
121 if (m_drawText) {
122 if (m_graph->isEnabled()) {
123 painter->setPen(QPen(m_drawNode ? Qt::white : Qt::black, 0));
124 } else {
125 painter->setPen(QPen(m_disabledTextColor));
128 painter->drawText((value() < 0) ? -10 : -8, 3, text);
132 void MixerNode::verticalMove(bool flag)
134 m_vertical = flag;
137 double MixerNode::value()
139 double h = m_graphItem->boundingRect().height();
140 double ratio = (h - pos().y()) / h;
142 return ((m_graph->getMax() - m_graph->getMin()) * ratio) + m_graph->getMin();
146 QVariant MixerNode::itemChange(GraphicsItemChange change, const QVariant &val)
148 QPointF newPos = val.toPointF();
149 double h = m_graphItem->boundingRect().height();
151 switch (change) {
152 case ItemPositionChange:
154 if (!m_vertical) {
155 break;
158 // Force node to move vertically
159 newPos.setX(pos().x());
161 // Stay inside graph
162 if (newPos.y() < 0) {
163 newPos.setY(0);
165 // qDebug() << h << " - " << newPos.y();
166 if (newPos.y() > h) {
167 newPos.setY(h);
170 return newPos;
172 case ItemPositionHasChanged:
174 foreach(Edge * edge, m_edgeList)
175 edge->adjust();
177 update();
179 m_graph->itemMoved(value());
180 break;
182 default:
183 break;
187 return QGraphicsItem::itemChange(change, val);
190 void MixerNode::mousePressEvent(QGraphicsSceneMouseEvent *event)
192 update();
193 QGraphicsItem::mousePressEvent(event);
196 void MixerNode::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
198 update();
199 QGraphicsItem::mouseReleaseEvent(event);