SetupView.cc 5.62 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    //setResizeMode(SizeRootObjectToView);
    
    _ui->buttonHolder->setAutoPilot(NULL);
    _ui->buttonHolder->setSource(QUrl::fromUserInput("qrc:/qml/SetupViewButtons.qml"));
    
    QObject* rootObject = (QObject*)_ui->buttonHolder->rootObject();
    Q_ASSERT(rootObject);
    
    fSucceeded = connect(rootObject, SIGNAL(setupButtonClicked(QVariant)), this, SLOT(_setupButtonClicked(QVariant)));
    Q_ASSERT(fSucceeded);
    
    fSucceeded = connect(rootObject, SIGNAL(firmwareButtonClicked()), this, SLOT(_firmwareButtonClicked()));
    Q_ASSERT(fSucceeded);
    
    fSucceeded = connect(rootObject, SIGNAL(parametersButtonClicked()), this, SLOT(_parametersButtonClicked()));
    Q_ASSERT(fSucceeded);
    
    fSucceeded = connect(rootObject, SIGNAL(summaryButtonClicked()), this, SLOT(_summaryButtonClicked()));
    Q_ASSERT(fSucceeded);
76
    
77
    _setActiveUAS(UASManager::instance()->getActiveUAS());
78 79 80 81
}

SetupView::~SetupView()
{
82

83 84 85 86 87
}

void SetupView::_setActiveUAS(UASInterface* uas)
{
    if (_uasCurrent) {
88
        Q_ASSERT(_autoPilotPlugin);
Don Gagne's avatar
Don Gagne committed
89
        disconnect(_autoPilotPlugin, &AutoPilotPlugin::pluginReady, this, &SetupView::_pluginReady);
90
    }
91

Don Gagne's avatar
Don Gagne committed
92 93 94 95 96 97 98
    _autoPilotPlugin = NULL;
    _ui->buttonHolder->setAutoPilot(NULL);
    _firmwareButtonClicked();
    QObject* button = _ui->buttonHolder->rootObject()->findChild<QObject*>("firmwareButton");
    Q_ASSERT(button);
    button->setProperty("checked", true);
    
99 100 101
    _uasCurrent = uas;
    
    if (_uasCurrent) {
102
        _autoPilotPlugin = AutoPilotPluginManager::instance()->getInstanceForAutoPilotPlugin(_uasCurrent);
103
        
104 105
        connect(_autoPilotPlugin, &AutoPilotPlugin::pluginReady, this, &SetupView::_pluginReady);
        if (_autoPilotPlugin->pluginIsReady()) {
Don Gagne's avatar
Don Gagne committed
106
            _pluginReady();
107 108 109 110
        }
    }
}

111
void SetupView::_pluginReady(void)
112
{
Don Gagne's avatar
Don Gagne committed
113 114 115
    _ui->buttonHolder->setAutoPilot(_autoPilotPlugin);
    _summaryButtonClicked();
    QObject* button = _ui->buttonHolder->rootObject()->findChild<QObject*>("summaryButton");
116
    Q_ASSERT(button);
Don Gagne's avatar
Don Gagne committed
117
    button->setProperty("checked", true);
118 119
}

Don Gagne's avatar
Don Gagne committed
120
void SetupView::_changeSetupWidget(QWidget* newWidget)
121
{
Don Gagne's avatar
Don Gagne committed
122 123 124 125 126
    if (_currentSetupWidget) {
        delete _currentSetupWidget;
    }
    _currentSetupWidget = newWidget;
    _ui->setupWidgetLayout->addWidget(newWidget);
127 128
}

129
void SetupView::_firmwareButtonClicked(void)
130
{
Don Gagne's avatar
Don Gagne committed
131
    if (_uasCurrent && _uasCurrent->isArmed()) {
132 133 134
        QGCMessageBox::warning("Setup", "Firmware Update cannot be performed while vehicle is armed.");
        return;
    }
135 136

#if 1
Don Gagne's avatar
Don Gagne committed
137
    PX4FirmwareUpgrade* setup = new PX4FirmwareUpgrade(this);
138 139 140 141 142 143 144 145
#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
146
    _changeSetupWidget(setup);
147 148
}

149
void SetupView::_parametersButtonClicked(void)
150
{
Don Gagne's avatar
Don Gagne committed
151 152 153 154 155 156 157
    ParameterEditor* setup = new ParameterEditor(_uasCurrent, QStringList(), this);
    _changeSetupWidget(setup);
}

void SetupView::_summaryButtonClicked(void)
{
    Q_ASSERT(_autoPilotPlugin);
158
    
Don Gagne's avatar
Don Gagne committed
159 160 161 162 163 164 165
    QGCQmlWidgetHolder* summary = new QGCQmlWidgetHolder;
    Q_CHECK_PTR(summary);

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

    _changeSetupWidget(summary);
166 167
}

168
void SetupView::_setupButtonClicked(const QVariant& component)
169
{
170 171 172 173 174
    if (_uasCurrent->isArmed()) {
        QGCMessageBox::warning("Setup", "Setup cannot be performed while vehicle is armed.");
        return;
    }

175 176 177
    VehicleComponent* vehicle = qobject_cast<VehicleComponent*>(component.value<QObject*>());
    Q_ASSERT(vehicle);
    
178 179 180 181 182 183
    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
184
    _changeSetupWidget(vehicle->setupWidget());
185
}