dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
positionfield.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 "positionfield.h"
29 #include <QDebug>
30 #include <QStringList>
31 #include <QWidget>
32 #include <QTextEdit>
33 #include <QVBoxLayout>
34 #include <QPushButton>
35 #include <QMouseEvent>
36 #include <QtGlobal>
37 
42  : QGraphicsView(parent)
43 {
44  setMinimumSize(64, 64);
45  setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
46  setScene(new QGraphicsScene(this));
47  setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
48 
49  m_renderer = new QSvgRenderer();
50  bool retval = m_renderer->load(QString(":/magicwaypoint/images/positionfield.svg"));
51  Q_ASSERT(retval);
52  if (!retval)
53  return;
54 
55  m_background = new QGraphicsSvgItem();
56  m_background->setSharedRenderer(m_renderer);
57  m_background->setElementId(QString("background"));
58 
59  m_positiondesired = new QGraphicsSvgItem();
60  m_positiondesired->setSharedRenderer(m_renderer);
61  m_positiondesired->setElementId(QString("desiredPosition"));
62  m_positiondesired->setPos(0, 0);
63 
64  m_positionactual = new QGraphicsSvgItem();
65  m_positionactual->setSharedRenderer(m_renderer);
66  m_positionactual->setElementId(QString("actualPosition"));
67  m_positionactual->setPos(0, 0);
68 
69  QGraphicsScene *l_scene = scene();
70  l_scene->clear(); // This also deletes all items contained in the scene.
71  l_scene->addItem(m_background);
72  l_scene->addItem(m_positiondesired);
73  l_scene->addItem(m_positionactual);
74  l_scene->setSceneRect(m_background->boundingRect());
75 }
76 
78 {
79 }
80 
84 void PositionField::updateDesiredIndicator(double north, double east)
85 {
86  QRectF sceneSize = scene()->sceneRect();
87 
88  m_positiondesired->setPos(
89  (east + 1) / 2 * sceneSize.width() - m_positiondesired->boundingRect().width() / 2,
90  (-north + 1) / 2 * sceneSize.height() - m_positiondesired->boundingRect().height() / 2);
91 }
92 
93 void PositionField::updateActualIndicator(double north, double east)
94 {
95  QRectF sceneSize = scene()->sceneRect();
96 
97  m_positionactual->setPos(
98  (east + 1) / 2 * sceneSize.width() - m_positionactual->boundingRect().width() / 2,
99  (-north + 1) / 2 * sceneSize.height() - m_positionactual->boundingRect().height() / 2);
100 }
101 
105 void PositionField::mouseMoveEvent(QMouseEvent *event)
106 {
107  QPointF point = mapToScene(event->pos());
108  QRectF sceneSize = scene()->sceneRect();
109 
110  double north = -(point.y() / sceneSize.height() - .5) * 2;
111  double east = (point.x() / sceneSize.width() - .5) * 2;
112  emit positionClicked(north, east);
113 }
114 
118 void PositionField::mousePressEvent(QMouseEvent *event)
119 {
120  if (event->button() == Qt::LeftButton) {
121  mouseMoveEvent(event);
122  }
123 }
124 
126 {
127  update();
128 }
129 
130 void PositionField::paintEvent(QPaintEvent *event)
131 {
132  // Skip painting until the dial file is loaded
133  if (!m_renderer->isValid()) {
134  qDebug() << "Image file not loaded, not rendering";
135  }
136 
137  QGraphicsView::paintEvent(event);
138 }
139 
140 void PositionField::resizeEvent(QResizeEvent *event)
141 {
142  Q_UNUSED(event);
143  fitInView(m_background, Qt::IgnoreAspectRatio);
144 }
145 
void mousePressEvent(QMouseEvent *event)
Redirect mouse move clicks to control position.
void updateDesiredIndicator(double north, double east)
Update aircraft position on image (values go from -1 to 1)
void updateActualIndicator(double north, double east)
void paintEvent(QPaintEvent *event)
void mouseMoveEvent(QMouseEvent *event)
Redirect mouse move events to control position.
void resizeEvent(QResizeEvent *event)
PositionField(QWidget *parent=nullptr)
Constructor for JoystickControl widget. Sets up the image of a joystick.
void positionClicked(double north, double east)