2 ******************************************************************************
4 * @file mixercurvewidget.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @addtogroup GCSPlugins GCS Plugins
8 * @addtogroup UAVObjectWidgetUtils Plugin
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
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 "mixercurvewidget.h"
29 #include "mixercurveline.h"
30 #include "mixercurvepoint.h"
37 * Initialize the widget
39 MixerCurveWidget::MixerCurveWidget(QWidget
*parent
) :
40 QGraphicsView(parent
), m_xAxisTextItem(0), m_yAxisTextItem(0)
42 // Create a layout, add a QGraphicsView and put the SVG inside.
43 // The Mixer Curve widget looks like this:
44 // |--------------------|
51 // |--------------------|
54 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
55 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
56 setRenderHint(QPainter::Antialiasing
);
61 setFrameStyle(QFrame::NoFrame
);
62 setStyleSheet("background:transparent");
63 setRenderHint(QPainter::HighQualityAntialiasing
, true);
64 QGraphicsScene
*scene
= new QGraphicsScene(this);
65 QSvgRenderer
*renderer
= new QSvgRenderer();
66 m_plot
= new QGraphicsSvgItem();
67 renderer
->load(QString(":/uavobjectwidgetutils/images/curve-bg.svg"));
68 m_plot
->setSharedRenderer(renderer
);
70 scene
->addItem(m_plot
);
71 m_plot
->setZValue(-1);
73 scene
->setSceneRect(m_plot
->boundingRect());
78 initNodes(MixerCurveWidget::NODE_NUMELEM
);
81 MixerCurveWidget::~MixerCurveWidget()
83 while (!m_nodeList
.isEmpty()) {
84 delete m_nodeList
.takeFirst();
87 while (!m_edgeList
.isEmpty()) {
88 delete m_edgeList
.takeFirst();
90 if (m_xAxisTextItem
) {
91 delete m_xAxisTextItem
;
92 m_xAxisTextItem
= NULL
;
94 if (m_yAxisTextItem
) {
95 delete m_yAxisTextItem
;
96 m_yAxisTextItem
= NULL
;
100 void MixerCurveWidget::setPositiveColor(QString color
)
102 for (int i
= 0; i
< m_nodeList
.count(); i
++) {
103 MixerNode
*node
= m_nodeList
.at(i
);
104 node
->setPositiveColor(color
);
108 void MixerCurveWidget::setNegativeColor(QString color
)
110 for (int i
= 0; i
< m_nodeList
.count(); i
++) {
111 MixerNode
*node
= m_nodeList
.at(i
);
112 node
->setNegativeColor(color
);
117 Init curve: create a (flat) curve with a specified number of points.
119 If a curve exists already, resets it.
120 Points should be between 0 and 1.
122 void MixerCurveWidget::initCurve(const QList
<double> *points
)
124 if (points
->length() < 2) {
125 return; // We need at least 2 points on a curve!
127 // finally, set node positions
131 void MixerCurveWidget::initNodes(int numPoints
)
133 // First of all, clear any existing list
134 if (m_nodeList
.count()) {
135 foreach(MixerNode
* node
, m_nodeList
) {
136 foreach(Edge
* edge
, node
->edges()) {
137 if (edge
->sourceNode() == node
) {
138 scene()->removeItem(edge
);
142 scene()->removeItem(node
);
149 // Create the nodes and edges
150 MixerNode
*prevNode
= 0;
151 for (int i
= 0; i
< numPoints
; i
++) {
152 MixerNode
*node
= new MixerNode(this, m_plot
);
154 m_nodeList
.append(node
);
155 scene()->addItem(node
);
160 scene()->addItem(new Edge(prevNode
, node
));
167 void MixerCurveWidget::setupXAxisLabel()
169 if (!m_xAxisString
.isEmpty()) {
170 if (m_xAxisTextItem
) {
171 m_xAxisTextItem
->setPlainText(m_xAxisString
);
173 m_xAxisTextItem
= new QGraphicsTextItem(m_xAxisString
, m_plot
);
174 scene()->addItem(m_xAxisTextItem
);
179 void MixerCurveWidget::setupYAxisLabel()
181 if (!m_yAxisString
.isEmpty()) {
182 if (m_yAxisTextItem
) {
183 m_yAxisTextItem
->setPlainText(m_yAxisString
);
185 m_yAxisTextItem
= new QGraphicsTextItem(m_yAxisString
, m_plot
);
186 m_yAxisTextItem
->setRotation(270);
187 scene()->addItem(m_yAxisTextItem
);
193 Returns the current curve settings
195 QList
<double> MixerCurveWidget::getCurve()
199 foreach(MixerNode
* node
, m_nodeList
) {
200 list
.append(node
->value());
208 void MixerCurveWidget::initLinearCurve(int numPoints
, double maxValue
, double minValue
)
210 double range
= maxValue
- minValue
; // setRange(minValue, maxValue);
212 QList
<double> points
;
213 for (double i
= 0; i
< (double)numPoints
; i
++) {
214 double val
= (range
* (i
/ (double)(numPoints
- 1))) + minValue
;
220 Setd the current curve settings
222 void MixerCurveWidget::setCurve(const QList
<double> *points
)
224 m_curveUpdating
= true;
226 int ptCnt
= points
->count();
227 if (m_nodeList
.count() != ptCnt
) {
231 double range
= m_curveMax
- m_curveMin
;
233 qreal w
= m_plot
->boundingRect().width() / (ptCnt
- 1);
234 qreal h
= m_plot
->boundingRect().height();
235 for (int i
= 0; i
< ptCnt
; i
++) {
236 double val
= (points
->at(i
) < m_curveMin
) ? m_curveMin
: (points
->at(i
) > m_curveMax
) ? m_curveMax
: points
->at(i
);
239 val
-= (m_curveMin
+ range
);
242 MixerNode
*node
= m_nodeList
.at(i
);
243 node
->setPos(w
* i
, h
- (val
* h
));
244 node
->verticalMove(true);
248 m_curveUpdating
= false;
255 void MixerCurveWidget::showEvent(QShowEvent
*event
)
258 positionAxisLabels();
259 setSceneRect(scene()->itemsBoundingRect());
260 fitInView(scene()->itemsBoundingRect(), Qt::KeepAspectRatio
);
263 void MixerCurveWidget::resizeEvent(QResizeEvent
*event
)
266 positionAxisLabels();
267 setSceneRect(scene()->itemsBoundingRect());
268 fitInView(scene()->itemsBoundingRect(), Qt::KeepAspectRatio
);
271 void MixerCurveWidget::changeEvent(QEvent
*event
)
273 QGraphicsView::changeEvent(event
);
275 if (event
->type() == QEvent::EnabledChange
) {
276 foreach(MixerNode
* node
, m_nodeList
) {
282 void MixerCurveWidget::positionAxisLabels()
284 QRectF rect
= m_plot
->boundingRect();
286 if (m_xAxisTextItem
) {
287 m_xAxisTextItem
->setPos(rect
.right() -
288 m_xAxisTextItem
->boundingRect().width(), rect
.bottom() - 4);
291 if (m_yAxisTextItem
) {
292 m_yAxisTextItem
->setPos(rect
.left() -
293 m_yAxisTextItem
->boundingRect().height(), m_yAxisTextItem
->boundingRect().width());
297 void MixerCurveWidget::itemMoved(double itemValue
)
301 if (!m_curveUpdating
) {
306 void MixerCurveWidget::setMin(double value
)
308 if (m_curveMin
!= value
) {
309 emit
curveMinChanged(value
);
315 void MixerCurveWidget::setMax(double value
)
317 if (m_curveMax
!= value
) {
318 emit
curveMaxChanged(value
);
324 double MixerCurveWidget::getMin()
328 double MixerCurveWidget::getMax()
332 double MixerCurveWidget::setRange(double min
, double max
)
336 return m_curveMax
- m_curveMin
;
339 void MixerCurveWidget::setXAxisLabel(QString label
)
341 m_xAxisString
= label
;
345 void MixerCurveWidget::setYAxisLabel(QString label
)
347 m_yAxisString
= label
;