Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / plugins / gcscontrol / joystickcontrol.cpp
blob6d0a44ce7c162bca8d9c8f25c7933b3d27a63424
1 /**
2 ******************************************************************************
4 * @file joystickcontrol.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @addtogroup GCSPlugins GCS Plugins
7 * @{
8 * @addtogroup GCSControlGadgetPlugin GCSControl Gadget Plugin
9 * @{
10 * @brief The plugin that mimics a transmitter joystick and updates the MCC
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 "joystickcontrol.h"
29 #include "extensionsystem/pluginmanager.h"
30 #include <QDebug>
31 #include <QWidget>
32 #include <QOpenGLWidget>
33 #include <QMouseEvent>
35 /**
36 * @brief Constructor for JoystickControl widget. Sets up the image of a joystick
38 JoystickControl::JoystickControl(QWidget *parent) : QGraphicsView(parent)
40 setMinimumSize(64, 64);
41 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
42 setScene(new QGraphicsScene(this));
43 setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
45 m_renderer = new QSvgRenderer();
46 bool test = m_renderer->load(QString(":/gcscontrol/images/joystick.svg"));
47 Q_UNUSED(test);
48 Q_ASSERT(test);
50 m_background = new QGraphicsSvgItem();
51 m_background->setSharedRenderer(m_renderer);
52 m_background->setElementId(QString("background"));
54 m_joystickEnd = new QGraphicsSvgItem();
55 m_joystickEnd->setSharedRenderer(m_renderer);
56 m_joystickEnd->setElementId(QString("joystickEnd"));
58 m_joystickArea = new QGraphicsSvgItem();
59 m_joystickArea->setSharedRenderer(m_renderer);
60 m_joystickArea->setElementId(QString("joystickArea"));
61 m_joystickArea->setPos(
62 (m_background->boundingRect().width() - m_joystickArea->boundingRect().width()) * 0.5,
63 (m_background->boundingRect().height() - m_joystickArea->boundingRect().height()) * 0.5
65 m_joystickArea->setVisible(false);
67 QGraphicsScene *l_scene = scene();
68 l_scene->clear(); // This also deletes all items contained in the scene.
69 l_scene->addItem(m_background);
70 l_scene->addItem(m_joystickArea);
71 l_scene->addItem(m_joystickEnd);
72 l_scene->setSceneRect(m_background->boundingRect());
74 changePosition(0.0, 0.0);
77 JoystickControl::~JoystickControl()
79 // Do nothing
82 /**
83 * @brief Enables/Disables OpenGL
85 void JoystickControl::enableOpenGL(bool flag)
87 if (flag) {
88 setViewport(new QOpenGLWidget()); // QGLFormat(QGL::SampleBuffers)));
89 } else {
90 setViewport(new QWidget);
94 /**
95 * @brief Update the displayed position based on an MCC update
97 void JoystickControl::changePosition(double x, double y)
99 QRectF areaSize = m_joystickArea->boundingRect();
100 QPointF point(
101 ((1.0 + x) * areaSize.width() - m_joystickEnd->boundingRect().width()) * 0.5,
102 ((1.0 - y) * areaSize.height() - m_joystickEnd->boundingRect().height()) * 0.5
105 m_joystickEnd->setPos(m_joystickArea->mapToScene(point));
109 * @brief Redirect mouse move events to control position
111 void JoystickControl::mouseMoveEvent(QMouseEvent *event)
113 QPointF point = m_joystickArea->mapFromScene(mapToScene(event->pos()));
114 QSizeF areaSize = m_joystickArea->boundingRect().size();
116 double y = -(point.y() / areaSize.height() - 0.5) * 2.0;
117 double x = (point.x() / areaSize.width() - 0.5) * 2.0;
119 if (y < -1.0) {
120 y = -1.0;
122 if (y > 1.0) {
123 y = 1.0;
125 if (x < -1.0) {
126 x = -1.0;
128 if (x > 1.0) {
129 x = 1.0;
132 emit positionClicked(x, y);
136 * @brief Redirect mouse move clicks to control position
138 void JoystickControl::mousePressEvent(QMouseEvent *event)
140 if (event->button() == Qt::LeftButton) {
141 mouseMoveEvent(event);
145 void JoystickControl::paint()
147 update();
150 void JoystickControl::paintEvent(QPaintEvent *event)
152 // Skip painting until the image file is loaded
153 if (!m_renderer->isValid()) {
154 qDebug() << "Image file not loaded, not rendering";
157 QGraphicsView::paintEvent(event);
160 void JoystickControl::resizeEvent(QResizeEvent *event)
162 Q_UNUSED(event);
163 fitInView(m_background, Qt::KeepAspectRatio);
167 * @}
168 * @}