-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPrimaryFlightDisplayQML.cpp
More file actions
437 lines (366 loc) · 13.4 KB
/
PrimaryFlightDisplayQML.cpp
File metadata and controls
437 lines (366 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*===================================================================
APM_PLANNER Open Source Ground Control Station
(c) 2014 Bill Bonney <billbonney@communistech.com>
This file is part of the APM_PLANNER project
APM_PLANNER is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
APM_PLANNER is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with APM_PLANNER. If not, see <http://www.gnu.org/licenses/>.
======================================================================*/
#include "PrimaryFlightDisplayQML.h"
#include <QVBoxLayout>
#include <QMessagebox>
#include <QtQml/QQmlContext>
#include <QtQuick/QQuickItem>
#include <QtQml/QQmlEngine>
#include <QGst/Init>
#include <QSettings>
#include <QApplication>
#include <QStandardPaths>
#include <QDesktopServices>
#include <QGst/Quick/VideoItem>
#include <QtAndroidExtras/QAndroidJniObject>
#include "LinkManager1.h"
#include "UASManager1.h"
#define ToRad(x) (x*0.01745329252) // *pi/180
#define ToDeg(x) (x*57.2957795131) // *180/pi
// Callback to update the custom plugin
void* update_node(void* surface, void* node, qreal x, qreal y, qreal w, qreal h);
PrimaryFlightDisplayQML::PrimaryFlightDisplayQML(QObject *parent) :
QObject(parent),
m_declarativeView(NULL),
m_uasInterface(NULL),
m_player(NULL),
m_showToolAction(NULL),
m_surface(NULL),
m_currentState(NULL),
m_enableGStreamer(true),
m_videoEnabled(true),
m_uasConnected(false)
{
m_enableVideoTimer.setSingleShot(true);
connect(&m_enableVideoTimer, SIGNAL(timeout()), this, SLOT(onVideoEnabledTimer()));
m_currentState = new QCurrentState();
m_declarativeView = new QQuickView();
m_surface = new QGst::Quick::VideoSurface;
m_player = new GStreamerPlayer(m_declarativeView);
m_player->setVideoSink(m_surface->videoSink());
m_declarativeView->setResizeMode(QQuickView::SizeRootObjectToView);
connect(m_player, SIGNAL(messageBox(QString)), this,
SLOT(messageBox(QString)), Qt::UniqueConnection);
// Default to video display until user selects
InitializeDisplayWithVideo();
// Connect with UAS
connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this,
SLOT(setActiveUAS(UASInterface*)), Qt::UniqueConnection);
}
PrimaryFlightDisplayQML::~PrimaryFlightDisplayQML()
{
delete m_player;
m_player = NULL;
if (m_currentState != NULL)
{
delete m_currentState;
m_currentState = NULL;
}
}
void PrimaryFlightDisplayQML::setActiveUAS(UASInterface *uas)
{
if (m_uasInterface) {
disconnect(uas,SIGNAL(textMessageReceived(int,int,int,QString)),
this,SLOT(uasTextMessage(int,int,int,QString)));
disconnect(uas, SIGNAL(navModeChanged(int, int, QString)),
this, SLOT(updateNavMode(int, int, QString)));
}
m_uasInterface = uas;
if (m_uasInterface) {
connect(uas,SIGNAL(textMessageReceived(int,int,int,QString)),
this,SLOT(uasTextMessage(int,int,int,QString)));
connect(uas, SIGNAL(navModeChanged(int, int, QString)),
this, SLOT(updateNavMode(int, int, QString)));
VehicleOverview *obj = LinkManager::instance()->getUasObject(uas->getUASID())->getVehicleOverview();
RelPositionOverview *rel = LinkManager::instance()->getUasObject(uas->getUASID())->getRelPositionOverview();
AbsPositionOverview *abs = LinkManager::instance()->getUasObject(uas->getUASID())->getAbsPositionOverview();
if (m_declarativeView)
{
m_declarativeView->rootContext()->setContextProperty("vehicleoverview",obj);
m_declarativeView->rootContext()->setContextProperty("relpositionoverview",rel);
m_declarativeView->rootContext()->setContextProperty("abspositionoverview",abs);
QMetaObject::invokeMethod(m_declarativeView->rootObject(),"activeUasSet");
}
}
}
void PrimaryFlightDisplayQML::uasTextMessage(int uasid, int componentid, int severity, QString text)
{
Q_UNUSED(uasid);
Q_UNUSED(componentid);
if (text.contains("PreArm") || severity == 3)
{
if (m_declarativeView)
{
QObject *root = m_declarativeView->rootObject();
root->setProperty("statusMessage", text);
root->setProperty("showStatusMessage", true);
}
}
qCritical() << text;
}
void PrimaryFlightDisplayQML::messageBox(QString text)
{
if (m_declarativeView)
{
QObject *root = m_declarativeView->rootObject();
root->setProperty("messageBoxText", text);
root->setProperty("showMessageBox", true);
}
qCritical() << text;
}
void PrimaryFlightDisplayQML::updateNavMode(int uasid, int mode, const QString& text)
{
Q_UNUSED(uasid);
Q_UNUSED(mode);
if (m_declarativeView)
{
QObject *root = m_declarativeView->rootObject();
root->setProperty("navMode", text);
}
}
void PrimaryFlightDisplayQML::InitializeDisplayWithVideo()
{
QUrl url = QUrl(QLatin1String("assets:/qml/PrimaryFlightDisplayWithVideoQML.qml"));
m_declarativeView->engine()->clearComponentCache();
m_declarativeView->rootContext()->setContextProperty(QLatin1String("videoSurface1"), m_surface);
m_declarativeView->rootContext()->setContextProperty(QLatin1String("player"), m_player);
m_declarativeView->rootContext()->setContextProperty(QLatin1String("container"), this);
m_declarativeView->rootContext()->setContextProperty(QLatin1String("currentState"), m_currentState);
m_declarativeView->setSource(url);
m_declarativeView->show();
// Wire up video surface manually
QQuickItem *item = m_declarativeView->rootObject();
QQuickItem *videoObj = item->findChild<QQuickItem*>("video");
if (videoObj)
{
qCritical() << "Initializing Video Object in QML";
QGst::Quick::VideoItem *pItem = dynamic_cast<QGst::Quick::VideoItem*>(videoObj);
if (pItem)
{
pItem->setSurface(m_surface);
pItem->setUpdateNodeCallback(&update_node);
}
else
{
qCritical() << "Unable to set VideoItem 'surface' property!";
}
}
else
{
qCritical() << "Failed to find video object in QML!";
}
m_player->play();
setActiveUAS(UASManager::instance()->getActiveUAS());
qCritical() << "Showing Video";
}
void PrimaryFlightDisplayQML::enableVideo(bool enabled)
{
if (enabled) m_player->play();
else m_player->stop();
}
void PrimaryFlightDisplayQML::onVideoEnabledTimer()
{
this->enableVideo(m_videoEnabled);
emit videoEnabledChanged();
}
void PrimaryFlightDisplayQML::setVideoEnabled(bool value) {
this->m_videoEnabled = value;
if (!m_enableVideoTimer.isActive())
{
m_enableVideoTimer.start(1000);
}
}
void PrimaryFlightDisplayQML::setOpenHelp(bool value)
{
this->m_openHelp = value;
emit openHelpChanged();
if (this->m_openHelp)
{
QString appDataDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QString localHelpFile = appDataDir + "/help.html";
qCritical() << "Local Help File =" << localHelpFile;
QString assetsHelpFile = "assets:/resources/help.html";
if (!QFile(localHelpFile).exists())
{
qCritical() << "Copying local help file";
QFile(assetsHelpFile).copy(localHelpFile);
}
if (QFile(localHelpFile).exists())
{
QAndroidJniObject url = QAndroidJniObject::fromString(localHelpFile);
QAndroidJniObject::callStaticMethod<void>("org/qtproject/qt5/android/bindings/QtActivityEx", "openUrl", "(Ljava/lang/String;)V",url.object<jstring>());
}
}
this->m_openHelp = false;
emit openHelpChanged();
}
void PrimaryFlightDisplayQML::applicationStateChanged(Qt::ApplicationState state)
{
QString strState = "Unknown";
switch (state)
{
case Qt::ApplicationState::ApplicationSuspended:
strState = "Suspended";
if (m_player) m_player->stop();
break;
case Qt::ApplicationState::ApplicationHidden:
strState = "Hidden";
if (m_player) m_player->stop();
break;
case Qt::ApplicationState::ApplicationInactive:
strState = "Inactive";
if (m_player) m_player->stop();
break;
case Qt::ApplicationState::ApplicationActive:
strState = "Active";
if (m_player) m_player->play();
break;
}
qDebug() << "Application State Changed to " + strState;
}
void PrimaryFlightDisplayQML::setUasConnected(bool value) {
LinkManager *pLinkMgr = LinkManager::instance();
int iLinkId = -1;
if (m_connectionMap.contains(m_ipOrHost))
{
iLinkId = m_connectionMap.value(m_ipOrHost);
}
if (iLinkId >= 0)
{
if (value) value = pLinkMgr->connectLink(iLinkId);
else pLinkMgr->disconnectLink(iLinkId);
this->m_uasConnected = value;
emit uasConnectedChanged();
return;
}
// Not in list, add new
QStringList split = m_ipOrHost.split(":");
QString token;
QString ipOrHost;
int iToken = 0;
int iPort = 0;
bool isTCP = false;
bool isUDP = false;
QString text;
Q_FOREACH(token, split)
{
switch (iToken)
{
case 0:
if (token.startsWith("TCP"))
{
isTCP = true;
iToken++;
break;
}
if (token.startsWith("UDP"))
{
isUDP = true;
iToken++;
break;
}
// Error if got here, invalid string
text = "Invalid connect string '" + m_ipOrHost + "', missing TCP,UDP";
this->messageBox(text);
value = false;
this->m_uasConnected = value;
emit uasConnectedChanged();
return;
case 1:
if (isTCP)
{
ipOrHost = token;
iToken++;
break;
}
if (isUDP)
{
iPort = token.toInt();
iToken++;
break;
}
break;
case 2:
if (isTCP)
{
iPort = token.toInt();
iToken++;
break;
}
break;
}
}
if (isTCP)
{
QHostAddress addr(ipOrHost);
iLinkId = pLinkMgr->addTcpConnection(addr, iPort, false);
m_connectionMap.insert(m_ipOrHost, iLinkId);
}
else if (isUDP)
{
iLinkId = pLinkMgr->addUdpConnection(QHostAddress::Any, iPort);
m_connectionMap.insert(m_ipOrHost, iLinkId);
}
if (!pLinkMgr->connectLink(iLinkId))
{
text = "Cannot open connection '" + m_ipOrHost + "'!";
this->messageBox(text);
value = false;
}
this->m_uasConnected = value;
emit uasConnectedChanged();
}
void PrimaryFlightDisplayQML::setPipelineString(QString pipelineString)
{
m_pipelineString = pipelineString; emit pipelineStringChanged();
if (m_player) m_player->setPipelineString(m_pipelineString);
qDebug() << "GStreamer Pipeline String = " << m_pipelineString;
}
void PrimaryFlightDisplayQML::setIpOrHost(QString ipOrHost)
{
m_ipOrHost = ipOrHost; emit ipOrHostChanged();
qDebug() << "IP Address or Host Connect String = " << m_ipOrHost;
}
void PrimaryFlightDisplayQML::SetCurrentState(CCurrentState &theState)
{
// This method will update the bound state variables in the QML
m_currentState->setRoll(theState.getRoll());
m_currentState->setPitch(theState.getPitch());
m_currentState->setYaw(theState.getYaw());
m_currentState->setGroundspeed(theState.getGroundspeed());
m_currentState->setAirspeed(theState.getAirspeed());
m_currentState->setBatteryVoltage(theState.getBatteryVoltage());
m_currentState->setBatteryCurrent(theState.getBatteryCurrent());
m_currentState->setBatteryRemaining(theState.getBatteryRemaining());
m_currentState->setAltitude(theState.getAltitude());
m_currentState->setWatts(theState.getWatts());
m_currentState->setGpsstatus(theState.getGpsStatus());
m_currentState->setGpshdop(theState.getGpsHdop());
m_currentState->setSatcount(theState.getSatCount());
m_currentState->setWp_dist(theState.getWpDist());
m_currentState->setCh3percent(theState.getCh3Percent());
m_currentState->setTimeInAir(theState.getTimeInAir());
m_currentState->setDistToHome(theState.getDistToHome());
m_currentState->setDistTraveled(theState.getDistTravled());
m_currentState->setAZToMAV(theState.getAzToMav());
m_currentState->setLat(theState.getLat());
m_currentState->setLng(theState.getLng());
m_currentState->setArmed(theState.getArmed());
m_currentState->setDistUnit(QString::fromWCharArray(theState.getDistUnit()));
m_currentState->setSpeedUnit(QString::fromWCharArray(theState.getSpeedUnit()));
m_currentState->setMessage(QString::fromWCharArray(theState.getMessage()));
m_currentState->setFlightMode(QString::fromWCharArray(theState.getFlightMode()));
}