SetupView.cc 5 KB
Newer Older
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
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 This file is part of the QGROUNDCONTROL project
 
 QGROUNDCONTROL 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.
 
 QGROUNDCONTROL 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 QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/

/// @file
///     @author Don Gagne <don@thegagnes.com>

#include "SetupView.h"
Don Gagne's avatar
Don Gagne committed
28
#include "ui_SetupView.h"
29 30

#include "UASManager.h"
31
#include "AutoPilotPluginManager.h"
32
#include "VehicleComponent.h"
33
#include "ParameterEditor.h"
Don Gagne's avatar
Don Gagne committed
34
#include "QGCQmlWidgetHolder.h"
35
#include "MainWindow.h"
36
#include "QGCMessageBox.h"
37
#include "FirmwareUpgradeController.h"
38 39 40 41 42 43

#include <QQmlError>
#include <QQmlContext>
#include <QDebug>

SetupView::SetupView(QWidget* parent) :
Don Gagne's avatar
Don Gagne committed
44
    QWidget(parent),
45 46
    _uasCurrent(NULL),
    _initComplete(false),
Don Gagne's avatar
Don Gagne committed
47 48 49
    _autoPilotPlugin(NULL),
    _currentSetupWidget(NULL),
    _ui(new Ui::SetupView)
50
{
Don Gagne's avatar
Don Gagne committed
51 52
    _ui->setupUi(this);

53 54 55 56
    bool fSucceeded = connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(_setActiveUAS(UASInterface*)));
    Q_UNUSED(fSucceeded);
    Q_ASSERT(fSucceeded);
    
Don Gagne's avatar
Don Gagne committed
57 58 59
    _ui->buttonHolder->setAutoPilot(NULL);
    _ui->buttonHolder->setSource(QUrl::fromUserInput("qrc:/qml/SetupViewButtons.qml"));
    
Don Gagne's avatar
Don Gagne committed
60
    _ui->buttonHolder->rootContext()->setContextProperty("controller", this);
61
    
62 63
    qmlRegisterType<FirmwareUpgradeController>("QGroundControl.FirmwareUpgradeController", 1, 0, "FirmwareUpgradeController");
    
64
    _setActiveUAS(UASManager::instance()->getActiveUAS());
65 66 67 68
}

SetupView::~SetupView()
{
69

70 71 72 73 74
}

void SetupView::_setActiveUAS(UASInterface* uas)
{
    if (_uasCurrent) {
75
        Q_ASSERT(_autoPilotPlugin);
Don Gagne's avatar
Don Gagne committed
76
        disconnect(_autoPilotPlugin, &AutoPilotPlugin::pluginReady, this, &SetupView::_pluginReady);
77
    }
78

Don Gagne's avatar
Don Gagne committed
79 80
    _autoPilotPlugin = NULL;
    _ui->buttonHolder->setAutoPilot(NULL);
Don Gagne's avatar
Don Gagne committed
81
    firmwareButtonClicked();
Don Gagne's avatar
Don Gagne committed
82 83 84 85
    QObject* button = _ui->buttonHolder->rootObject()->findChild<QObject*>("firmwareButton");
    Q_ASSERT(button);
    button->setProperty("checked", true);
    
86 87 88
    _uasCurrent = uas;
    
    if (_uasCurrent) {
89
        _autoPilotPlugin = AutoPilotPluginManager::instance()->getInstanceForAutoPilotPlugin(_uasCurrent);
90
        
91 92
        connect(_autoPilotPlugin, &AutoPilotPlugin::pluginReady, this, &SetupView::_pluginReady);
        if (_autoPilotPlugin->pluginIsReady()) {
Don Gagne's avatar
Don Gagne committed
93
            _pluginReady();
94 95 96 97
        }
    }
}

98
void SetupView::_pluginReady(void)
99
{
Don Gagne's avatar
Don Gagne committed
100
    _ui->buttonHolder->setAutoPilot(_autoPilotPlugin);
Don Gagne's avatar
Don Gagne committed
101
    summaryButtonClicked();
Don Gagne's avatar
Don Gagne committed
102
    QObject* button = _ui->buttonHolder->rootObject()->findChild<QObject*>("summaryButton");
103
    Q_ASSERT(button);
Don Gagne's avatar
Don Gagne committed
104
    button->setProperty("checked", true);
105 106
}

Don Gagne's avatar
Don Gagne committed
107
void SetupView::_changeSetupWidget(QWidget* newWidget)
108
{
Don Gagne's avatar
Don Gagne committed
109 110 111 112 113
    if (_currentSetupWidget) {
        delete _currentSetupWidget;
    }
    _currentSetupWidget = newWidget;
    _ui->setupWidgetLayout->addWidget(newWidget);
114 115
}

Don Gagne's avatar
Don Gagne committed
116
void SetupView::firmwareButtonClicked(void)
117
{
Don Gagne's avatar
Don Gagne committed
118
    if (_uasCurrent && _uasCurrent->isArmed()) {
119 120 121
        QGCMessageBox::warning("Setup", "Firmware Update cannot be performed while vehicle is armed.");
        return;
    }
122 123 124 125 126

    QGCQmlWidgetHolder* setup = new QGCQmlWidgetHolder;
    Q_CHECK_PTR(setup);
    
    setup->setSource(QUrl::fromUserInput("qrc:/qml/FirmwareUpgrade.qml"));
127

Don Gagne's avatar
Don Gagne committed
128
    _changeSetupWidget(setup);
129 130
}

Don Gagne's avatar
Don Gagne committed
131
void SetupView::parametersButtonClicked(void)
132
{
Don Gagne's avatar
Don Gagne committed
133 134 135 136
    ParameterEditor* setup = new ParameterEditor(_uasCurrent, QStringList(), this);
    _changeSetupWidget(setup);
}

Don Gagne's avatar
Don Gagne committed
137
void SetupView::summaryButtonClicked(void)
Don Gagne's avatar
Don Gagne committed
138 139
{
    Q_ASSERT(_autoPilotPlugin);
140
    
Don Gagne's avatar
Don Gagne committed
141 142 143 144 145 146 147
    QGCQmlWidgetHolder* summary = new QGCQmlWidgetHolder;
    Q_CHECK_PTR(summary);

    summary->setAutoPilot(_autoPilotPlugin);
    summary->setSource(QUrl::fromUserInput("qrc:/qml/VehicleSummary.qml"));

    _changeSetupWidget(summary);
148 149
}

Don Gagne's avatar
Don Gagne committed
150
void SetupView::setupButtonClicked(const QVariant& component)
151
{
152 153 154 155 156
    if (_uasCurrent->isArmed()) {
        QGCMessageBox::warning("Setup", "Setup cannot be performed while vehicle is armed.");
        return;
    }

157 158 159
    VehicleComponent* vehicle = qobject_cast<VehicleComponent*>(component.value<QObject*>());
    Q_ASSERT(vehicle);
    
160 161 162 163 164 165
    QString setupPrereq = vehicle->prerequisiteSetup();
    if (!setupPrereq.isEmpty()) {
        QGCMessageBox::warning("Setup", QString("%1 setup must be completed prior to %2 setup.").arg(setupPrereq).arg(vehicle->name()));
        return;
    }
    
Don Gagne's avatar
Don Gagne committed
166
    _changeSetupWidget(vehicle->setupWidget());
167
}