SetupView.cc 5.04 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 33
#include "VehicleComponent.h"
#include "PX4FirmwareUpgrade.h"
34
#include "ParameterEditor.h"
Don Gagne's avatar
Don Gagne committed
35
#include "QGCQmlWidgetHolder.h"
36
#include "MainWindow.h"
37
#include "QGCMessageBox.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 60 61
    //setResizeMode(SizeRootObjectToView);
    
    _ui->buttonHolder->setAutoPilot(NULL);
    _ui->buttonHolder->setSource(QUrl::fromUserInput("qrc:/qml/SetupViewButtons.qml"));
    
Don Gagne's avatar
Don Gagne committed
62
    _ui->buttonHolder->rootContext()->setContextProperty("controller", this);
63
    
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

#if 1
Don Gagne's avatar
Don Gagne committed
124
    PX4FirmwareUpgrade* setup = new PX4FirmwareUpgrade(this);
125 126 127 128 129 130 131 132
#else
    // NYI
    QGCQmlWidgetHolder* setup = new QGCQmlWidgetHolder;
    Q_CHECK_PTR(setup);
    
    //setup->setAutoPilot(_autoPilotPlugin);
    setup->setSource(QUrl::fromUserInput("qrc:/qml/FirmwareUpgrade.qml"));
#endif
Don Gagne's avatar
Don Gagne committed
133
    _changeSetupWidget(setup);
134 135
}

Don Gagne's avatar
Don Gagne committed
136
void SetupView::parametersButtonClicked(void)
137
{
Don Gagne's avatar
Don Gagne committed
138 139 140 141
    ParameterEditor* setup = new ParameterEditor(_uasCurrent, QStringList(), this);
    _changeSetupWidget(setup);
}

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

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

    _changeSetupWidget(summary);
153 154
}

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

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