dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
joystickcontrol.cpp
Go to the documentation of this file.
1 
12 /*
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.
17  *
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.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, see <http://www.gnu.org/licenses/>
25  */
26 
27 #include "joystickcontrol.h"
29 #include <QDebug>
30 #include <QWidget>
31 #include <QMouseEvent>
32 
37  : QGraphicsView(parent)
38 {
39  setMinimumSize(64, 64);
40  setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
41  setScene(new QGraphicsScene(this));
42  setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
43 
44  m_renderer = new QSvgRenderer();
45  bool test = m_renderer->load(QString(":/gcscontrol/images/joystick.svg"));
46  Q_UNUSED(test);
47  Q_ASSERT(test);
48 
49  m_background = new QGraphicsSvgItem();
50  m_background->setSharedRenderer(m_renderer);
51  m_background->setElementId(QString("background"));
52 
53  m_joystickEnd = new QGraphicsSvgItem();
54  m_joystickEnd->setSharedRenderer(m_renderer);
55  m_joystickEnd->setElementId(QString("joystickEnd"));
56 
57  m_joystickArea = new QGraphicsSvgItem();
58  m_joystickArea->setSharedRenderer(m_renderer);
59  m_joystickArea->setElementId(QString("joystickArea"));
60  m_joystickArea->setPos(
61  (m_background->boundingRect().width() - m_joystickArea->boundingRect().width()) * 0.5,
62  (m_background->boundingRect().height() - m_joystickArea->boundingRect().height()) * 0.5);
63  m_joystickArea->setVisible(false);
64 
65  QGraphicsScene *l_scene = scene();
66  l_scene->clear(); // This also deletes all items contained in the scene.
67  l_scene->addItem(m_background);
68  l_scene->addItem(m_joystickArea);
69  l_scene->addItem(m_joystickEnd);
70  l_scene->setSceneRect(m_background->boundingRect());
71 
72  changePosition(0.0, 0.0);
73 }
74 
76 {
77  // Do nothing
78 }
79 
83 void JoystickControl::changePosition(double x, double y)
84 {
85  QRectF areaSize = m_joystickArea->boundingRect();
86  QPointF point(((1.0 + x) * areaSize.width() - m_joystickEnd->boundingRect().width()) * 0.5,
87  ((1.0 - y) * areaSize.height() - m_joystickEnd->boundingRect().height()) * 0.5);
88  m_joystickEnd->setPos(m_joystickArea->mapToScene(point));
89 }
90 
94 void JoystickControl::mouseMoveEvent(QMouseEvent *event)
95 {
96  QPointF point = m_joystickArea->mapFromScene(mapToScene(event->pos()));
97  QSizeF areaSize = m_joystickArea->boundingRect().size();
98 
99  double y = -(point.y() / areaSize.height() - 0.5) * 2.0;
100  double x = (point.x() / areaSize.width() - 0.5) * 2.0;
101  if (y < -1.0)
102  y = -1.0;
103  if (y > 1.0)
104  y = 1.0;
105  if (x < -1.0)
106  x = -1.0;
107  if (x > 1.0)
108  x = 1.0;
109 
110  emit positionClicked(x, y);
111 }
112 
116 void JoystickControl::mousePressEvent(QMouseEvent *event)
117 {
118  if (event->button() == Qt::LeftButton) {
119  mouseMoveEvent(event);
120  }
121 }
122 
124 {
125  update();
126 }
127 
128 void JoystickControl::paintEvent(QPaintEvent *event)
129 {
130  // Skip painting until the image file is loaded
131  if (!m_renderer->isValid()) {
132  qDebug() << "Image file not loaded, not rendering";
133  }
134 
135  QGraphicsView::paintEvent(event);
136 }
137 
138 void JoystickControl::resizeEvent(QResizeEvent *event)
139 {
140  Q_UNUSED(event);
141  fitInView(m_background, Qt::KeepAspectRatio);
142 }
143 
void mouseMoveEvent(QMouseEvent *event)
Redirect mouse move events to control position.
void positionClicked(double x, double y)
JoystickControl(QWidget *parent=nullptr)
Constructor for JoystickControl widget. Sets up the image of a joystick.
void paintEvent(QPaintEvent *event)
void mousePressEvent(QMouseEvent *event)
Redirect mouse move clicks to control position.
void resizeEvent(QResizeEvent *event)
x
Definition: OPPlots.m:100
void changePosition(double x, double y)
Update the displayed position based on an MCC update.
y
Definition: OPPlots.m:101