UASView.cc 21.2 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11
/*=====================================================================
PIXHAWK Micro Air Vehicle Flying Robotics Toolkit

(c) 2009, 2010 PIXHAWK PROJECT  <http://pixhawk.ethz.ch>

This file is part of the PIXHAWK project

    PIXHAWK 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.
12

pixhawk's avatar
pixhawk committed
13 14 15 16
    PIXHAWK 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.
17

pixhawk's avatar
pixhawk committed
18 19
    You should have received a copy of the GNU General Public License
    along with PIXHAWK. If not, see <http://www.gnu.org/licenses/>.
20

pixhawk's avatar
pixhawk committed
21 22 23 24
======================================================================*/

/**
 * @file
pixhawk's avatar
pixhawk committed
25
 *   @brief Implementation of one airstrip
pixhawk's avatar
pixhawk committed
26 27 28 29 30 31 32 33
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#include <cmath>
#include <QDateTime>
#include <QDebug>
34
#include <QMenu>
35
#include <QInputDialog>
pixhawk's avatar
pixhawk committed
36

37
#include "QGC.h"
pixhawk's avatar
pixhawk committed
38 39 40
#include "MG.h"
#include "UASManager.h"
#include "UASView.h"
41
#include "UASWaypointManager.h"
42
#include "MainWindow.h"
pixhawk's avatar
pixhawk committed
43 44 45
#include "ui_UASView.h"

UASView::UASView(UASInterface* uas, QWidget *parent) :
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
        QWidget(parent),
        startTime(0),
        timeout(false),
        iconIsRed(true),
        timeRemaining(0),
        chargeLevel(0),
        uas(uas),
        load(0),
        state("UNKNOWN"),
        stateDesc(tr("Unknown state")),
        mode("MAV_MODE_UNKNOWN"),
        thrust(0),
        isActive(false),
        x(0),
        y(0),
        z(0),
        totalSpeed(0),
        lat(0),
        lon(0),
        alt(0),
        groundDistance(0),
        localFrame(false),
        removeAction(new QAction("Delete this system", this)),
        renameAction(new QAction("Rename..", this)),
        selectAction(new QAction("Control this system", this )),
lm's avatar
lm committed
71
        hilAction(new QAction("Enable Hardware-in-the-Loop Simulation", this )),
72 73
        selectAirframeAction(new QAction("Choose Airframe", this)),
        setBatterySpecsAction(new QAction("Set Battery Options", this)),
74
        lowPowerModeEnabled(true),
75 76 77 78 79
        m_ui(new Ui::UASView)
{
    // FIXME XXX
    lowPowerModeEnabled = MainWindow::instance()->lowPowerModeEnabled();

lm's avatar
lm committed
80
    hilAction->setCheckable(true);
81

pixhawk's avatar
pixhawk committed
82
    m_ui->setupUi(this);
83

pixhawk's avatar
pixhawk committed
84
    // Setup communication
pixhawk's avatar
pixhawk committed
85
    //connect(uas, SIGNAL(valueChanged(int,QString,double,quint64)), this, SLOT(receiveValue(int,QString,double,quint64)));
pixhawk's avatar
pixhawk committed
86 87 88 89 90 91 92
    connect(uas, SIGNAL(batteryChanged(UASInterface*, double, double, int)), this, SLOT(updateBattery(UASInterface*, double, double, int)));
    connect(uas, SIGNAL(heartbeat(UASInterface*)), this, SLOT(receiveHeartbeat(UASInterface*)));
    connect(uas, SIGNAL(thrustChanged(UASInterface*, double)), this, SLOT(updateThrust(UASInterface*, double)));
    connect(uas, SIGNAL(localPositionChanged(UASInterface*,double,double,double,quint64)), this, SLOT(updateLocalPosition(UASInterface*,double,double,double,quint64)));
    connect(uas, SIGNAL(globalPositionChanged(UASInterface*,double,double,double,quint64)), this, SLOT(updateGlobalPosition(UASInterface*,double,double,double,quint64)));
    connect(uas, SIGNAL(speedChanged(UASInterface*,double,double,double,quint64)), this, SLOT(updateSpeed(UASInterface*,double,double,double,quint64)));
    connect(uas, SIGNAL(statusChanged(UASInterface*,QString,QString)), this, SLOT(updateState(UASInterface*,QString,QString)));
pixhawk's avatar
pixhawk committed
93
    connect(uas, SIGNAL(modeChanged(int,QString,QString)), this, SLOT(updateMode(int,QString,QString)));
pixhawk's avatar
pixhawk committed
94
    connect(uas, SIGNAL(loadChanged(UASInterface*, double)), this, SLOT(updateLoad(UASInterface*, double)));
95
    connect(uas, SIGNAL(heartbeatTimeout()), this, SLOT(heartbeatTimeout()));
pixhawk's avatar
pixhawk committed
96
    connect(uas, SIGNAL(waypointSelected(int,int)), this, SLOT(selectWaypoint(int,int)));
pixhawk's avatar
pixhawk committed
97
    connect(uas->getWaypointManager(), SIGNAL(currentWaypointChanged(quint16)), this, SLOT(currentWaypointUpdated(quint16)));
pixhawk's avatar
pixhawk committed
98
    connect(uas, SIGNAL(systemTypeSet(UASInterface*,uint)), this, SLOT(setSystemType(UASInterface*,uint)));
99
    connect(UASManager::instance(), SIGNAL(activeUASStatusChanged(UASInterface*,bool)), this, SLOT(updateActiveUAS(UASInterface*,bool)));
100 101 102
    connect(uas, SIGNAL(textMessageReceived(int,int,int,QString)), this, SLOT(showStatusText(int, int, int, QString)));
    connect(uas, SIGNAL(navModeChanged(int, int, QString)), this, SLOT(updateNavMode(int, int, QString)));

pixhawk's avatar
pixhawk committed
103
    // Setup UAS selection
104
    connect(m_ui->uasViewFrame, SIGNAL(clicked(bool)), this, SLOT(setUASasActive(bool)));
105

pixhawk's avatar
pixhawk committed
106 107 108 109
    // Setup user interaction
    connect(m_ui->liftoffButton, SIGNAL(clicked()), uas, SLOT(launch()));
    connect(m_ui->haltButton, SIGNAL(clicked()), uas, SLOT(halt()));
    connect(m_ui->continueButton, SIGNAL(clicked()), uas, SLOT(go()));
pixhawk's avatar
pixhawk committed
110 111
    connect(m_ui->landButton, SIGNAL(clicked()), uas, SLOT(home()));
    connect(m_ui->abortButton, SIGNAL(clicked()), uas, SLOT(emergencySTOP()));
pixhawk's avatar
pixhawk committed
112 113
    connect(m_ui->killButton, SIGNAL(clicked()), uas, SLOT(emergencyKILL()));
    connect(m_ui->shutdownButton, SIGNAL(clicked()), uas, SLOT(shutdown()));
114 115 116

    // Allow to delete this widget
    connect(removeAction, SIGNAL(triggered()), this, SLOT(deleteLater()));
117
    connect(renameAction, SIGNAL(triggered()), this, SLOT(rename()));
118
    connect(selectAction, SIGNAL(triggered()), uas, SLOT(setSelected()));
lm's avatar
lm committed
119
    connect(hilAction, SIGNAL(triggered(bool)), uas, SLOT(enableHil(bool)));
120
    connect(selectAirframeAction, SIGNAL(triggered()), this, SLOT(selectAirframe()));
121
    connect(setBatterySpecsAction, SIGNAL(triggered()), this, SLOT(setBatterySpecs()));
122
    connect(uas, SIGNAL(systemRemoved()), this, SLOT(deleteLater()));
123 124 125

    // Name changes
    connect(uas, SIGNAL(nameChanged(QString)), this, SLOT(updateName(QString)));
126

pixhawk's avatar
pixhawk committed
127
    // Set static values
128

pixhawk's avatar
pixhawk committed
129
    // Name
130
    if (uas->getUASName() == "") {
pixhawk's avatar
pixhawk committed
131
        m_ui->nameLabel->setText(tr("UAS") + QString::number(uas->getUASID()));
132
    } else {
pixhawk's avatar
pixhawk committed
133 134
        m_ui->nameLabel->setText(uas->getUASName());
    }
135

136
    setBackgroundColor();
137

pixhawk's avatar
pixhawk committed
138 139 140
    // Heartbeat fade
    refreshTimer = new QTimer(this);
    connect(refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
141 142
    if (lowPowerModeEnabled)
    {
143
        refreshTimer->start(updateInterval*3);
144 145 146
    } else {
        refreshTimer->start(updateInterval);
    }
147 148 149 150 151

    // Hide kill and shutdown buttons per default
    m_ui->killButton->hide();
    m_ui->shutdownButton->hide();

152 153 154
    // Set state and mode
    updateMode(uas->getUASID(), uas->getShortMode(), "");
    updateState(uas, uas->getShortState(), "");
155
    setSystemType(uas, uas->getSystemType());
pixhawk's avatar
pixhawk committed
156 157 158 159 160
}

UASView::~UASView()
{
    delete m_ui;
161 162 163
    delete removeAction;
    delete renameAction;
    delete selectAction;
pixhawk's avatar
pixhawk committed
164 165
}

166 167 168 169
void UASView::heartbeatTimeout()
{
    timeout = true;
}
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

void UASView::updateNavMode(int uasid, int mode, const QString& text)
{
    Q_UNUSED(uasid);
    Q_UNUSED(mode);
    m_ui->navLabel->setText(text);
}

void UASView::showStatusText(int uasid, int componentid, int severity, QString text)
{
    Q_UNUSED(uasid);
    Q_UNUSED(componentid);
    Q_UNUSED(severity);
    //m_ui->statusTextLabel->setText(text);
    stateDesc = text;
}
186

187 188 189 190 191 192 193 194 195 196
/**
 * Set the background color based on the MAV color. If the MAV is selected as the
 * currently actively controlled system, the frame color is highlighted
 */
void UASView::setBackgroundColor()
{
    // UAS color
    QColor uasColor = uas->getColor();
    QString colorstyle;
    QString borderColor = "#4A4A4F";
197
    if (isActive) {
198 199
        borderColor = "#FA4A4F";
        uasColor = uasColor.darker(475);
200
    } else {
201 202
        uasColor = uasColor.darker(675);
    }
203
    colorstyle = colorstyle.sprintf("QGroupBox { border-radius: 12px; padding: 0px; margin: 0px; background-color: #%02X%02X%02X; border: 2px solid %s; }",
204 205 206 207 208
                                    uasColor.red(), uasColor.green(), uasColor.blue(), borderColor.toStdString().c_str());
    m_ui->uasViewFrame->setStyleSheet(colorstyle);
}

void UASView::setUASasActive(bool active)
pixhawk's avatar
pixhawk committed
209
{
210
    if (active) {
211 212 213 214 215 216
        UASManager::instance()->setActiveUAS(this->uas);
    }
}

void UASView::updateActiveUAS(UASInterface* uas, bool active)
{
217
    if (uas == this->uas) {
218 219 220
        this->isActive = active;
        setBackgroundColor();
    }
pixhawk's avatar
pixhawk committed
221 222
}

pixhawk's avatar
pixhawk committed
223 224
void UASView::updateMode(int sysId, QString status, QString description)
{
225
    Q_UNUSED(description);
226 227

    //int aa=this->uas->getUASID();
pixhawk's avatar
pixhawk committed
228
    if (sysId == this->uas->getUASID()) m_ui->modeLabel->setText(status);
229 230

    m_ui->modeLabel->setText(status);
pixhawk's avatar
pixhawk committed
231 232
}

pixhawk's avatar
pixhawk committed
233 234
void UASView::mouseDoubleClickEvent (QMouseEvent * event)
{
235
    Q_UNUSED(event);
pixhawk's avatar
pixhawk committed
236
    UASManager::instance()->setActiveUAS(uas);
237
    // qDebug() << __FILE__ << __LINE__ << "DOUBLECLICKED";
pixhawk's avatar
pixhawk committed
238 239 240 241
}

void UASView::enterEvent(QEvent* event)
{
242
    if (event->type() == QEvent::MouseMove) {
243
        emit uasInFocus(uas);
244
        if (uas != UASManager::instance()->getActiveUAS()) {
245 246 247
            grabMouse(QCursor(Qt::PointingHandCursor));
        }
    }
248
    // qDebug() << __FILE__ << __LINE__ << "IN FOCUS";
249 250

    if (event->type() == QEvent::MouseButtonDblClick) {
251
        // qDebug() << __FILE__ << __LINE__ << "UAS CLICKED!";
252
    }
pixhawk's avatar
pixhawk committed
253 254 255 256
}

void UASView::leaveEvent(QEvent* event)
{
257
    if (event->type() == QEvent::MouseMove) {
258 259 260 261 262 263 264 265 266
        emit uasOutFocus(uas);
        releaseMouse();
    }
}

void UASView::showEvent(QShowEvent* event)
{
    // React only to internal (pre-display)
    // events
267
    Q_UNUSED(event);
268
    refreshTimer->start(updateInterval*10);
269 270 271 272 273 274 275 276
}

void UASView::hideEvent(QHideEvent* event)
{
    // React only to internal (pre-display)
    // events
    Q_UNUSED(event);
    refreshTimer->stop();
pixhawk's avatar
pixhawk committed
277 278 279 280
}

void UASView::receiveHeartbeat(UASInterface* uas)
{
281
    Q_UNUSED(uas);
282
    heartbeatColor = QColor(20, 200, 20);
lm's avatar
lm committed
283
    QString colorstyle("QGroupBox { border-radius: 5px; padding: 2px; margin: 0px; border: 0px; background-color: %1; }");
284
    m_ui->heartbeatIcon->setStyleSheet(colorstyle.arg(heartbeatColor.name()));
285 286 287 288 289 290
    if (timeout) setBackgroundColor();
    timeout = false;
}

void UASView::updateName(const QString& name)
{
291
    if (uas) m_ui->nameLabel->setText(name);
pixhawk's avatar
pixhawk committed
292 293 294 295 296 297 298 299 300 301 302
}

/**
 * The current system type is represented through the system icon.
 *
 * @param uas Source system, has to be the same as this->uas
 * @param systemType type ID, following the MAVLink system type conventions
 * @see http://pixhawk.ethz.ch/software/mavlink
 */
void UASView::setSystemType(UASInterface* uas, unsigned int systemType)
{
303
    if (uas == this->uas) {
pixhawk's avatar
pixhawk committed
304
        // Set matching icon
305
        switch (systemType) {
pixhawk's avatar
pixhawk committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
        case 0:
            m_ui->typeButton->setIcon(QIcon(":/images/mavs/generic.svg"));
            break;
        case 1:
            m_ui->typeButton->setIcon(QIcon(":/images/mavs/fixed-wing.svg"));
            break;
        case 2:
            m_ui->typeButton->setIcon(QIcon(":/images/mavs/quadrotor.svg"));
            break;
        case 3:
            m_ui->typeButton->setIcon(QIcon(":/images/mavs/coaxial.svg"));
            break;
        case 4:
            m_ui->typeButton->setIcon(QIcon(":/images/mavs/helicopter.svg"));
            break;
        case 5:
lm's avatar
lm committed
322 323
            m_ui->typeButton->setIcon(QIcon(":/images/mavs/unknown.svg"));
            break;
324
        case 6: {
325 326
                // A groundstation is a special system type, update widget
                QString result;
lm's avatar
lm committed
327
                m_ui->nameLabel->setText(tr("GCS ") + result.sprintf("%03d", uas->getUASID()));
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
                m_ui->waypointLabel->setText("");
                m_ui->timeRemainingLabel->setText("Online:");
                m_ui->batteryBar->hide();
                m_ui->thrustBar->hide();
                m_ui->stateLabel->hide();
                m_ui->statusTextLabel->hide();
                m_ui->waypointLabel->hide();
                m_ui->liftoffButton->hide();
                m_ui->haltButton->hide();
                m_ui->landButton->hide();
                m_ui->shutdownButton->hide();
                m_ui->abortButton->hide();
                m_ui->typeButton->setIcon(QIcon(":/images/mavs/groundstation.svg"));
            }
            break;
pixhawk's avatar
pixhawk committed
343 344 345 346 347 348 349 350 351
        default:
            m_ui->typeButton->setIcon(QIcon(":/images/mavs/unknown.svg"));
            break;
        }
    }
}

void UASView::updateLocalPosition(UASInterface* uas, double x, double y, double z, quint64 usec)
{
352
    Q_UNUSED(usec);
353 354 355 356
    Q_UNUSED(uas);
    this->x = x;
    this->y = y;
    this->z = z;
357
    if (!localFrame) {
358
        localFrame = true;
pixhawk's avatar
pixhawk committed
359 360 361
    }
}

362
void UASView::updateGlobalPosition(UASInterface* uas, double lon, double lat, double alt, quint64 usec)
pixhawk's avatar
pixhawk committed
363
{
364 365
    Q_UNUSED(uas);
    Q_UNUSED(usec);
pixhawk's avatar
pixhawk committed
366 367 368
    this->lon = lon;
    this->lat = lat;
    this->alt = alt;
pixhawk's avatar
pixhawk committed
369 370 371 372
}

void UASView::updateSpeed(UASInterface*, double x, double y, double z, quint64 usec)
{
373
    Q_UNUSED(usec);
374
    totalSpeed = sqrt(x*x + y*y + z*z);
pixhawk's avatar
pixhawk committed
375 376
}

377 378 379 380 381
void UASView::currentWaypointUpdated(quint16 waypoint)
{
    m_ui->waypointLabel->setText(tr("WP") + QString::number(waypoint));
}

pixhawk's avatar
pixhawk committed
382 383
void UASView::setWaypoint(int uasId, int id, double x, double y, double z, double yaw, bool autocontinue, bool current)
{
384 385 386 387 388
    Q_UNUSED(x);
    Q_UNUSED(y);
    Q_UNUSED(z);
    Q_UNUSED(yaw);
    Q_UNUSED(autocontinue);
389 390
    if (uasId == this->uas->getUASID()) {
        if (current) {
pixhawk's avatar
pixhawk committed
391 392 393 394 395 396 397
            m_ui->waypointLabel->setText(tr("WP") + QString::number(id));
        }
    }
}

void UASView::selectWaypoint(int uasId, int id)
{
398
    if (uasId == this->uas->getUASID()) {
pixhawk's avatar
pixhawk committed
399 400 401 402 403 404
        m_ui->waypointLabel->setText(tr("WP") + QString::number(id));
    }
}

void UASView::updateThrust(UASInterface* uas, double thrust)
{
405
    if (this->uas == uas) {
pixhawk's avatar
pixhawk committed
406
        this->thrust = thrust;
pixhawk's avatar
pixhawk committed
407 408 409 410 411
    }
}

void UASView::updateBattery(UASInterface* uas, double voltage, double percent, int seconds)
{
412
    Q_UNUSED(voltage);
413
    if (this->uas == uas) {
pixhawk's avatar
pixhawk committed
414 415 416 417 418 419 420
        timeRemaining = seconds;
        chargeLevel = percent;
    }
}

void UASView::updateState(UASInterface* uas, QString uasState, QString stateDescription)
{
421
    if (this->uas == uas) {
pixhawk's avatar
pixhawk committed
422 423 424 425 426 427 428
        state = uasState;
        stateDesc = stateDescription;
    }
}

void UASView::updateLoad(UASInterface* uas, double load)
{
429
    if (this->uas == uas) {
pixhawk's avatar
pixhawk committed
430 431 432 433
        this->load = load;
    }
}

434 435
void UASView::contextMenuEvent (QContextMenuEvent* event)
{
436
    QMenu menu(this);
lm's avatar
lm committed
437 438
    menu.addAction(selectAction);
    menu.addSeparator();
439
    menu.addAction(renameAction);
440
    if (timeout) {
441
        menu.addAction(removeAction);
442
    }
lm's avatar
lm committed
443
    menu.addAction(hilAction);
444
    menu.addAction(selectAirframeAction);
445
    menu.addAction(setBatterySpecsAction);
446 447 448
    menu.exec(event->globalPos());
}

449 450
void UASView::setBatterySpecs()
{
451
    if (uas) {
452 453
        bool ok;
        QString newName = QInputDialog::getText(this, tr("Set Battery Specifications for %1").arg(uas->getUASName()),
454
                                                tr("Specs: (empty,warn,full), e.g. (9V,9.5V,12.6V) or just warn level in percent (e.g. 15%) to use estimate from MAV"), QLineEdit::Normal,
455 456 457 458 459 460
                                                uas->getBatterySpecs(), &ok);

        if (ok && !newName.isEmpty()) uas->setBatterySpecs(newName);
    }
}

461 462
void UASView::rename()
{
463
    if (uas) {
464 465 466 467 468 469
        bool ok;
        QString newName = QInputDialog::getText(this, tr("Rename System %1").arg(uas->getUASName()),
                                                tr("System Name:"), QLineEdit::Normal,
                                                uas->getUASName(), &ok);

        if (ok && !newName.isEmpty()) uas->setUASName(newName);
470 471 472
    }
}

473 474
void UASView::selectAirframe()
{
475
    if (uas) {
476 477 478
        // Get list of airframes from UAS
        QStringList airframes;
        airframes << "Generic"
479 480 481 482 483 484 485 486 487
                << "Multiplex Easystar"
                << "Multiplex Twinstar"
                << "Multiplex Merlin"
                << "Pixhawk Cheetah"
                << "Mikrokopter"
                << "Reaper"
                << "Predator"
                << "Coaxial"
                << "Pteryx";
488 489 490 491

        bool ok;
        QString item = QInputDialog::getItem(this, tr("Select Airframe for %1").arg(uas->getUASName()),
                                             tr("Airframe"), airframes, uas->getAirframe(), false, &ok);
492
        if (ok && !item.isEmpty()) {
493 494 495 496 497 498
            // Set this airframe as UAS airframe
            uas->setAirframe(airframes.indexOf(item));
        }
    }
}

pixhawk's avatar
pixhawk committed
499 500
void UASView::refresh()
{
501
    //setUpdatesEnabled(false);
502 503
    //setUpdatesEnabled(true);
    //repaint();
504 505

    static quint64 lastupdate = 0;
506
    //// qDebug() << "UASVIEW update diff: " << MG::TIME::getGroundTimeNow() - lastupdate;
507 508 509 510 511
    lastupdate = MG::TIME::getGroundTimeNow();

    // FIXME
    static int generalUpdateCount = 0;

512
    if (generalUpdateCount == 4) {
513
#if (QGC_EVENTLOOP_DEBUG)
514
        // qDebug() << "EVENTLOOP:" << __FILE__ << __LINE__;
515
#endif
516
        generalUpdateCount = 0;
517
        //// qDebug() << "UPDATING EVERYTHING";
518 519 520 521 522 523 524 525 526 527 528
        // State
        m_ui->stateLabel->setText(state);
        m_ui->statusTextLabel->setText(stateDesc);

        // Battery
        m_ui->batteryBar->setValue(static_cast<int>(this->chargeLevel));
        //m_ui->loadBar->setValue(static_cast<int>(this->load));
        m_ui->thrustBar->setValue(this->thrust);

        // Position
        QString position;
529
        position = position.sprintf("%05.1f %05.1f %06.1f m", x, y, z);
530 531 532
        m_ui->positionLabel->setText(position);
        QString globalPosition;
        QString latIndicator;
533
        if (lat > 0) {
534
            latIndicator = "N";
535
        } else {
536 537 538
            latIndicator = "S";
        }
        QString lonIndicator;
539
        if (lon > 0) {
540
            lonIndicator = "E";
541
        } else {
542 543
            lonIndicator = "W";
        }
544 545
        globalPosition = globalPosition.sprintf("%05.1f%s %05.1f%s %06.1f m", lon, lonIndicator.toStdString().c_str(), lat, latIndicator.toStdString().c_str(), alt);
        m_ui->positionLabel->setText(globalPosition);
pixhawk's avatar
pixhawk committed
546

547
        // Altitude
548
        if (groundDistance == 0 && alt != 0) {
549
            m_ui->groundDistanceLabel->setText(QString("%1 m").arg(alt, 6, 'f', 1, '0'));
550
        } else {
551
            m_ui->groundDistanceLabel->setText(QString("%1 m").arg(groundDistance, 6, 'f', 1, '0'));
552
        }
553

554
        // Speed
555 556
        QString speed("%1 m/s");
        m_ui->speedLabel->setText(speed.arg(totalSpeed, 4, 'f', 1, '0'));
pixhawk's avatar
pixhawk committed
557

558 559
        // Thrust
        m_ui->thrustBar->setValue(thrust * 100);
pixhawk's avatar
pixhawk committed
560

561
        if(this->timeRemaining > 1 && this->timeRemaining < QGC::MAX_FLIGHT_TIME) {
562 563 564 565 566 567 568 569 570 571
            // Filter output to get a higher stability
            static double filterTime = static_cast<int>(this->timeRemaining);
            filterTime = 0.8 * filterTime + 0.2 * static_cast<int>(this->timeRemaining);
            int sec = static_cast<int>(filterTime - static_cast<int>(filterTime / 60.0f) * 60);
            int min = static_cast<int>(filterTime / 60);
            int hours = static_cast<int>(filterTime - min * 60 - sec);

            QString timeText;
            timeText = timeText.sprintf("%02d:%02d:%02d", hours, min, sec);
            m_ui->timeRemainingLabel->setText(timeText);
572
        } else {
573
            m_ui->timeRemainingLabel->setText(tr("Calc.."));
574 575 576 577 578 579 580
        }

        // Time Elapsed
        //QDateTime time = MG::TIME::msecToQDateTime(uas->getUptime());

        quint64 filterTime = uas->getUptime() / 1000;
        int sec = static_cast<int>(filterTime - static_cast<int>(filterTime / 60) * 60);
pixhawk's avatar
pixhawk committed
581 582 583 584
        int min = static_cast<int>(filterTime / 60);
        int hours = static_cast<int>(filterTime - min * 60 - sec);
        QString timeText;
        timeText = timeText.sprintf("%02d:%02d:%02d", hours, min, sec);
585
        m_ui->timeElapsedLabel->setText(timeText);
pixhawk's avatar
pixhawk committed
586
    }
587
    generalUpdateCount++;
pixhawk's avatar
pixhawk committed
588

lm's avatar
lm committed
589
    QString colorstyle("QGroupBox { border-radius: 5px; padding: 2px; margin: 0px; border: 0px; background-color: %1; }");
590

591
    if (timeout) {
592 593
        // CRITICAL CONDITION, NO HEARTBEAT

594
        QString borderColor = "#FFFF00";
595
        if (isActive) {
596 597 598
            borderColor = "#FA4A4F";
        }

599
        if (iconIsRed) {
600 601
            QColor warnColor(Qt::red);
            m_ui->heartbeatIcon->setStyleSheet(colorstyle.arg(warnColor.name()));
602
            QString style = QString("QGroupBox { border-radius: 12px; padding: 0px; margin: 0px; border: 2px solid %1; background-color: %2; }").arg(borderColor, warnColor.name());
603
            m_ui->uasViewFrame->setStyleSheet(style);
604
        } else {
605 606
            QColor warnColor(Qt::black);
            m_ui->heartbeatIcon->setStyleSheet(colorstyle.arg(warnColor.name()));
607
            QString style = QString("QGroupBox { border-radius: 12px; padding: 0px; margin: 0px; border: 2px solid %1; background-color: %2; }").arg(borderColor, warnColor.name());
608
            m_ui->uasViewFrame->setStyleSheet(style);
LM's avatar
LM committed
609 610

            refreshTimer->setInterval(errorUpdateInterval);
611 612
        }
        iconIsRed = !iconIsRed;
613
    } else {
614 615 616 617
        if (!lowPowerModeEnabled)
        {
            // Fade heartbeat icon
            // Make color darker
LM's avatar
LM committed
618
            heartbeatColor = heartbeatColor.darker(210);
619 620 621

            //m_ui->heartbeatIcon->setAutoFillBackground(true);
            m_ui->heartbeatIcon->setStyleSheet(colorstyle.arg(heartbeatColor.name()));
LM's avatar
LM committed
622
            refreshTimer->setInterval(updateInterval);
623
        }
624
    }
625
    //setUpdatesEnabled(true);
626 627

    //setUpdatesEnabled(false);
pixhawk's avatar
pixhawk committed
628 629 630 631 632 633 634 635 636 637 638 639 640
}

void UASView::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}