SetupView.cc 5.55 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
#ifndef __android__
37
#include "FirmwareUpgradeController.h"
38
#endif
Don Gagne's avatar
Don Gagne committed
39
#include "ParameterEditorController.h"
40 41 42 43 44 45

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

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

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

SetupView::~SetupView()
{
72

73 74 75 76 77
}

void SetupView::_setActiveUAS(UASInterface* uas)
{
    if (_uasCurrent) {
78
        Q_ASSERT(_autoPilotPlugin);
79
        disconnect(_autoPilotPlugin, &AutoPilotPlugin::pluginReadyChanged, this, &SetupView::_pluginReadyChanged);
80
    }
81

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

93
void SetupView::_pluginReadyChanged(bool pluginReady)
94
{
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    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);
    }
110 111
}

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

Don Gagne's avatar
Don Gagne committed
121
void SetupView::firmwareButtonClicked(void)
122
{
123 124
#ifndef __android__
    //FIXME: Hack out for android for now
Don Gagne's avatar
Don Gagne committed
125
    if (_uasCurrent && _uasCurrent->isArmed()) {
126 127 128
        QGCMessageBox::warning("Setup", "Firmware Update cannot be performed while vehicle is armed.");
        return;
    }
129 130 131 132 133

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

Don Gagne's avatar
Don Gagne committed
135
    _changeSetupWidget(setup);
136
#endif
137 138
}

Don Gagne's avatar
Don Gagne committed
139
void SetupView::parametersButtonClicked(void)
140
{
Don Gagne's avatar
Don Gagne committed
141 142 143 144 145
	QGCQmlWidgetHolder* setup = new QGCQmlWidgetHolder;
	Q_CHECK_PTR(setup);

	Q_ASSERT(_autoPilotPlugin);
	setup->setAutoPilot(_autoPilotPlugin);
146
	setup->setSource(QUrl::fromUserInput("qrc:/qml/SetupParameterEditor.qml"));
Don Gagne's avatar
Don Gagne committed
147 148
	
	_changeSetupWidget(setup);
Don Gagne's avatar
Don Gagne committed
149 150
}

Don Gagne's avatar
Don Gagne committed
151
void SetupView::summaryButtonClicked(void)
Don Gagne's avatar
Don Gagne committed
152 153
{
    Q_ASSERT(_autoPilotPlugin);
154
    
Don Gagne's avatar
Don Gagne committed
155 156 157 158 159 160 161
    QGCQmlWidgetHolder* summary = new QGCQmlWidgetHolder;
    Q_CHECK_PTR(summary);

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

    _changeSetupWidget(summary);
162 163
}

Don Gagne's avatar
Don Gagne committed
164
void SetupView::setupButtonClicked(const QVariant& component)
165
{
166 167 168 169 170
    if (_uasCurrent->isArmed()) {
        QGCMessageBox::warning("Setup", "Setup cannot be performed while vehicle is armed.");
        return;
    }

171 172 173
    VehicleComponent* vehicle = qobject_cast<VehicleComponent*>(component.value<QObject*>());
    Q_ASSERT(vehicle);
    
174 175 176 177 178 179
    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
180
    _changeSetupWidget(vehicle->setupWidget());
181
}