SetupView.cc 5.43 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"
Don Gagne's avatar
Don Gagne committed
33
#include "QGCQmlWidgetHolder.h"
34
#include "MainWindow.h"
35
#include "QGCMessageBox.h"
36
#include "FirmwareUpgradeController.h"
Don Gagne's avatar
Don Gagne committed
37
#include "ParameterEditorController.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
    qmlRegisterType<FirmwareUpgradeController>("QGroundControl.Controllers", 1, 0, "FirmwareUpgradeController");
	
Don Gagne's avatar
Don Gagne committed
59
    _ui->buttonHolder->rootContext()->setContextProperty("controller", this);
60 61
    _ui->buttonHolder->setAutoPilot(NULL);
    _ui->buttonHolder->setSource(QUrl::fromUserInput("qrc:/qml/SetupViewButtonsDisconnected.qml"));
62
    
63
    _setActiveUAS(UASManager::instance()->getActiveUAS());
64 65 66 67
}

SetupView::~SetupView()
{
68

69 70 71 72 73
}

void SetupView::_setActiveUAS(UASInterface* uas)
{
    if (_uasCurrent) {
74
        Q_ASSERT(_autoPilotPlugin);
75
        disconnect(_autoPilotPlugin, &AutoPilotPlugin::pluginReadyChanged, this, &SetupView::_pluginReadyChanged);
76
    }
77

78
    _pluginReadyChanged(false);
Don Gagne's avatar
Don Gagne committed
79
    
80 81 82
    _uasCurrent = uas;
    
    if (_uasCurrent) {
83
        _autoPilotPlugin = AutoPilotPluginManager::instance()->getInstanceForAutoPilotPlugin(_uasCurrent);
84 85
        _pluginReadyChanged(_autoPilotPlugin->pluginReady());
        connect(_autoPilotPlugin, &AutoPilotPlugin::pluginReadyChanged, this, &SetupView::_pluginReadyChanged);
86 87 88
    }
}

89
void SetupView::_pluginReadyChanged(bool pluginReady)
90
{
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    if (pluginReady) {
        _ui->buttonHolder->setAutoPilot(_autoPilotPlugin);
        _ui->buttonHolder->setSource(QUrl::fromUserInput("qrc:/qml/SetupViewButtonsConnected.qml"));
        summaryButtonClicked();
        QObject* button = _ui->buttonHolder->rootObject()->findChild<QObject*>("summaryButton");
        Q_ASSERT(button);
        button->setProperty("checked", true);
    } else {
        _ui->buttonHolder->setSource(QUrl::fromUserInput("qrc:/qml/SetupViewButtonsDisconnected.qml"));
        _ui->buttonHolder->setAutoPilot(NULL);
        firmwareButtonClicked();
        QObject* button = _ui->buttonHolder->rootObject()->findChild<QObject*>("firmwareButton");
        Q_ASSERT(button);
        button->setProperty("checked", true);
    }
106 107
}

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

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

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

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

Don Gagne's avatar
Don Gagne committed
132
void SetupView::parametersButtonClicked(void)
133
{
Don Gagne's avatar
Don Gagne committed
134 135 136 137 138
	QGCQmlWidgetHolder* setup = new QGCQmlWidgetHolder;
	Q_CHECK_PTR(setup);

	Q_ASSERT(_autoPilotPlugin);
	setup->setAutoPilot(_autoPilotPlugin);
139
	setup->setSource(QUrl::fromUserInput("qrc:/qml/SetupParameterEditor.qml"));
Don Gagne's avatar
Don Gagne committed
140 141
	
	_changeSetupWidget(setup);
Don Gagne's avatar
Don Gagne committed
142 143
}

Don Gagne's avatar
Don Gagne committed
144
void SetupView::summaryButtonClicked(void)
Don Gagne's avatar
Don Gagne committed
145 146
{
    Q_ASSERT(_autoPilotPlugin);
147
    
Don Gagne's avatar
Don Gagne committed
148 149 150 151 152 153 154
    QGCQmlWidgetHolder* summary = new QGCQmlWidgetHolder;
    Q_CHECK_PTR(summary);

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

    _changeSetupWidget(summary);
155 156
}

Don Gagne's avatar
Don Gagne committed
157
void SetupView::setupButtonClicked(const QVariant& component)
158
{
159 160 161 162 163
    if (_uasCurrent->isArmed()) {
        QGCMessageBox::warning("Setup", "Setup cannot be performed while vehicle is armed.");
        return;
    }

164 165 166
    VehicleComponent* vehicle = qobject_cast<VehicleComponent*>(component.value<QObject*>());
    Q_ASSERT(vehicle);
    
167 168 169 170 171 172
    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
173
    _changeSetupWidget(vehicle->setupWidget());
174
}