LP-56 - Better txpid option namings, fix tabs-spaces, tooltips. headers, variable...
[librepilot.git] / ground / openpilotgcs / src / plugins / uavobjectwidgetutils / mixercurveline.cpp
blob13c83bdca2320b5753a8154a33d96aa514a8bda6
1 /**
2 ******************************************************************************
4 * @file mixercurveline.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 <QPainter>
30 #include "mixercurveline.h"
31 #include "mixercurvepoint.h"
33 #include <qmath.h>
36 Edge::Edge(MixerNode *sourceNode, MixerNode *destNode)
37 : arrowSize(10)
39 setAcceptedMouseButtons(0);
40 source = sourceNode;
41 dest = destNode;
42 source->addEdge(this);
43 dest->addEdge(this);
44 adjust();
47 Edge::~Edge()
50 MixerNode *Edge::sourceNode() const
52 return source;
55 void Edge::setSourceNode(MixerNode *node)
57 source = node;
58 adjust();
61 MixerNode *Edge::destNode() const
63 return dest;
66 void Edge::setDestNode(MixerNode *node)
68 dest = node;
69 adjust();
72 void Edge::adjust()
74 if (!source || !dest) {
75 return;
78 QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
79 qreal length = line.length();
81 prepareGeometryChange();
83 if (length > qreal(20.)) {
84 QPointF edgeOffset((line.dx() * 13) / length, (line.dy() * 13) / length);
85 sourcePoint = line.p1() + edgeOffset;
86 destPoint = line.p2() - edgeOffset;
87 } else {
88 sourcePoint = destPoint = line.p1();
92 QRectF Edge::boundingRect() const
94 if (!source || !dest) {
95 return QRectF();
98 qreal penWidth = 1;
99 qreal extra = (penWidth + arrowSize) / 2.0;
101 return QRectF(sourcePoint, QSizeF(destPoint.x() - sourcePoint.x(),
102 destPoint.y() - sourcePoint.y()))
103 .normalized()
104 .adjusted(-extra, -extra, extra, extra);
107 void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
109 if (!source || !dest) {
110 return;
113 QLineF line(sourcePoint, destPoint);
114 if (qFuzzyCompare(line.length(), qreal(0.))) {
115 return;
118 // Draw the line itself
119 painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
120 painter->drawLine(line);