LP-56 - Better txpid option namings, fix tabs-spaces, tooltips. headers, variable...
[librepilot.git] / ground / openpilotgcs / src / plugins / uavobjectwidgetutils / mixercurvewidget.cpp
blobd6c7f533ab3de093bf07ce0e377ae3a2ca2bcb99
1 /**
2 ******************************************************************************
4 * @file mixercurvewidget.cpp
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 "mixercurvewidget.h"
29 #include "mixercurveline.h"
30 #include "mixercurvepoint.h"
32 #include <QObject>
33 #include <QtGui>
34 #include <QDebug>
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 // |--------------------|
45 // | |
46 // | |
47 // | Graph |
48 // | |
49 // | |
50 // | |
51 // |--------------------|
54 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
55 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
56 setRenderHint(QPainter::Antialiasing);
58 m_curveMin = 0.0;
59 m_curveMax = 1.0;
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());
74 setScene(scene);
76 setupXAxisLabel();
77 setupYAxisLabel();
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
128 setCurve(points);
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);
139 delete edge;
142 scene()->removeItem(node);
143 delete node;
146 m_nodeList.clear();
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);
157 node->setPos(0, 0);
159 if (prevNode) {
160 scene()->addItem(new Edge(prevNode, node));
163 prevNode = node;
167 void MixerCurveWidget::setupXAxisLabel()
169 if (!m_xAxisString.isEmpty()) {
170 if (m_xAxisTextItem) {
171 m_xAxisTextItem->setPlainText(m_xAxisString);
172 } else {
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);
184 } else {
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()
197 QList<double> list;
199 foreach(MixerNode * node, m_nodeList) {
200 list.append(node->value());
203 return list;
206 Sets a linear graph
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;
215 points.append(val);
217 initCurve(&points);
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) {
228 initNodes(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);
238 val += range;
239 val -= (m_curveMin + range);
240 val /= range;
242 MixerNode *node = m_nodeList.at(i);
243 node->setPos(w * i, h - (val * h));
244 node->verticalMove(true);
246 node->update();
248 m_curveUpdating = false;
250 update();
252 emit curveUpdated();
255 void MixerCurveWidget::showEvent(QShowEvent *event)
257 Q_UNUSED(event)
258 positionAxisLabels();
259 setSceneRect(scene()->itemsBoundingRect());
260 fitInView(scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
263 void MixerCurveWidget::resizeEvent(QResizeEvent *event)
265 Q_UNUSED(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) {
277 node->update();
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)
299 Q_UNUSED(itemValue);
301 if (!m_curveUpdating) {
302 emit curveUpdated();
306 void MixerCurveWidget::setMin(double value)
308 if (m_curveMin != value) {
309 emit curveMinChanged(value);
312 m_curveMin = value;
315 void MixerCurveWidget::setMax(double value)
317 if (m_curveMax != value) {
318 emit curveMaxChanged(value);
321 m_curveMax = value;
324 double MixerCurveWidget::getMin()
326 return m_curveMin;
328 double MixerCurveWidget::getMax()
330 return m_curveMax;
332 double MixerCurveWidget::setRange(double min, double max)
334 m_curveMin = min;
335 m_curveMax = max;
336 return m_curveMax - m_curveMin;
339 void MixerCurveWidget::setXAxisLabel(QString label)
341 m_xAxisString = label;
342 setupXAxisLabel();
345 void MixerCurveWidget::setYAxisLabel(QString label)
347 m_yAxisString = label;
348 setupYAxisLabel();