MultiVehicleDockWidget.cc 2.53 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

10 11 12 13

#include "MultiVehicleDockWidget.h"
#include "ui_MultiVehicleDockWidget.h"
#include "MultiVehicleManager.h"
14
#include "QGCApplication.h"
15

16 17
MultiVehicleDockWidget::MultiVehicleDockWidget(const QString& title, QAction* action, QWidget *parent)
    : QGCDockWidget(title, action, parent)
18 19 20 21
    , _ui(new Ui::MultiVehicleDockWidget)
{
    _ui->setupUi(this);
    
22 23
    setWindowTitle(title);
    
24 25 26
    connect(qgcApp()->toolbox()->multiVehicleManager(), &MultiVehicleManager::activeVehicleChanged, this, &MultiVehicleDockWidget::_activeVehicleChanged);
    connect(qgcApp()->toolbox()->multiVehicleManager(), &MultiVehicleManager::vehicleAdded, this, &MultiVehicleDockWidget::_vehicleAdded);
    connect(qgcApp()->toolbox()->multiVehicleManager(), &MultiVehicleManager::vehicleRemoved, this, &MultiVehicleDockWidget::_vehicleRemoved);
27 28 29 30
}

void MultiVehicleDockWidget::init(void)
{
31 32 33 34
    QmlObjectListModel* vehicles = qgcApp()->toolbox()->multiVehicleManager()->vehicles();

    for (int i=0; i<vehicles->count(); i++) {
        _vehicleAdded(qobject_cast<Vehicle*>(vehicles->get(i)));
Don Gagne's avatar
Don Gagne committed
35 36
    }

37 38
    if (qgcApp()->toolbox()->multiVehicleManager()->activeVehicle()) {
        _activeVehicleChanged(qgcApp()->toolbox()->multiVehicleManager()->activeVehicle());
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
    }
}

MultiVehicleDockWidget::~MultiVehicleDockWidget()
{
    delete _ui;
}

void MultiVehicleDockWidget::_vehicleRemoved(Vehicle* vehicle)
{
    int id = vehicle->id();
    
    if (_vehicleWidgets.contains(id)) {
        _vehicleWidgets[id]->deleteLater();
        _vehicleWidgets.remove(id);
    }
}

void MultiVehicleDockWidget::_vehicleAdded(Vehicle* vehicle)
{
    int id = vehicle->id();
    
    if (!_vehicleWidgets.contains(id)) {
        QWidget* vehicleWidget = _newVehicleWidget(vehicle, _ui->stackedWidget);
        _vehicleWidgets[id] = vehicleWidget;
        _ui->stackedWidget->addWidget(vehicleWidget);
    }
}

void MultiVehicleDockWidget::_activeVehicleChanged(Vehicle* vehicle)
{
    if (vehicle) {
        int id = vehicle->id();
        
        if (!_vehicleWidgets.contains(id)) {
            _vehicleAdded(vehicle);
        }
        
        QWidget* vehicleWidget = _vehicleWidgets[id];
        _ui->stackedWidget->setCurrentWidget(vehicleWidget);
    }
}