SetupView.cc 5.08 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 28 29
/*=====================================================================
 
 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"

#include "UASManager.h"
30
#include "AutoPilotPluginManager.h"
31 32
#include "VehicleComponent.h"
#include "PX4FirmwareUpgrade.h"
33 34 35
#include "ParameterEditor.h"
#include "SetupWidgetHolder.h"
#include "MainWindow.h"
36
#include "QGCMessageBox.h"
37 38 39 40 41 42

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

SetupView::SetupView(QWidget* parent) :
43
    QGCQuickWidget(parent),
44 45
    _uasCurrent(NULL),
    _initComplete(false),
46
    _autoPilotPlugin(NULL)
47 48 49 50 51
{
    bool fSucceeded = connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(_setActiveUAS(UASInterface*)));
    Q_UNUSED(fSucceeded);
    Q_ASSERT(fSucceeded);
    
52 53
    setResizeMode(SizeRootObjectToView);
    
54
    _setActiveUAS(UASManager::instance()->getActiveUAS());
55 56 57 58
}

SetupView::~SetupView()
{
59

60 61 62 63 64
}

void SetupView::_setActiveUAS(UASInterface* uas)
{
    if (_uasCurrent) {
65 66 67
        Q_ASSERT(_autoPilotPlugin);
        disconnect(_autoPilotPlugin);
        _autoPilotPlugin = NULL;
68
    }
69

70 71 72
    _uasCurrent = uas;
    
    if (_uasCurrent) {
73
        _autoPilotPlugin = AutoPilotPluginManager::instance()->getInstanceForAutoPilotPlugin(_uasCurrent);
74
        
75 76 77
        connect(_autoPilotPlugin, &AutoPilotPlugin::pluginReady, this, &SetupView::_pluginReady);
        if (_autoPilotPlugin->pluginIsReady()) {
            _setConnectedView();
78
        }
79 80
    } else {
        _setDisconnectedView();
81 82 83
    }
}

84
void SetupView::_pluginReady(void)
85
{
86
    _setConnectedView();
87 88
}

89
void SetupView::_setViewConnections(void)
90
{
91 92 93
    QObject*button = rootObject()->findChild<QObject*>("firmwareButton");
    Q_ASSERT(button);
    connect(button, SIGNAL(clicked()), this, SLOT(_firmwareButtonClicked()));
94
    
95 96 97 98
    button = rootObject()->findChild<QObject*>("parametersButton");
    if (button) {
        connect(button, SIGNAL(clicked()), this, SLOT(_parametersButtonClicked()));
    }
99 100
}

101
void SetupView::_setDisconnectedView(void)
102
{
103 104
    setSource(QUrl::fromUserInput("qrc:qml/SetupViewDisconnected.qml"));
    _setViewConnections();
105 106
}

107
void SetupView::_setConnectedView(void)
108
{
109 110
    Q_ASSERT(_uasCurrent);
    Q_ASSERT(_autoPilotPlugin);
111
    
112
    setAutoPilot(_autoPilotPlugin);
113
    
114 115 116
    setSource(QUrl::fromUserInput("qrc:qml/SetupViewConnected.qml"));
    disconnect(_autoPilotPlugin);
    _setViewConnections();
117
    
118
    connect((QObject*)rootObject(), SIGNAL(buttonClicked(QVariant)), this, SLOT(_setupButtonClicked(QVariant)));
119 120
}

121
void SetupView::_firmwareButtonClicked(void)
122
{
Don Gagne's avatar
Don Gagne committed
123
    if (_uasCurrent && _uasCurrent->isArmed()) {
124 125 126 127
        QGCMessageBox::warning("Setup", "Firmware Update cannot be performed while vehicle is armed.");
        return;
    }
    
128 129 130
    SetupWidgetHolder* dialog = new SetupWidgetHolder(MainWindow::instance());
    dialog->setModal(true);
    dialog->setWindowTitle("Firmware Upgrade");
131
    
132 133 134
    PX4FirmwareUpgrade* setup = new PX4FirmwareUpgrade(dialog);
    dialog->setInnerWidget(setup);
    dialog->exec();
135 136
}

137
void SetupView::_parametersButtonClicked(void)
138
{
139 140 141
    SetupWidgetHolder* dialog = new SetupWidgetHolder(MainWindow::instance());
    dialog->setModal(true);
    dialog->setWindowTitle("Parameter Editor");
142
    
143 144 145
    ParameterEditor* setup = new ParameterEditor(_uasCurrent, QStringList(), dialog);
    dialog->setInnerWidget(setup);
    dialog->exec();
146 147
}

148
void SetupView::_setupButtonClicked(const QVariant& component)
149
{
150 151 152 153 154
    if (_uasCurrent->isArmed()) {
        QGCMessageBox::warning("Setup", "Setup cannot be performed while vehicle is armed.");
        return;
    }

155 156 157
    VehicleComponent* vehicle = qobject_cast<VehicleComponent*>(component.value<QObject*>());
    Q_ASSERT(vehicle);
    
158 159 160 161 162 163
    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
164 165 166
    SetupWidgetHolder dialog(MainWindow::instance());
    dialog.setModal(true);
    dialog.setWindowTitle(vehicle->name());
167 168
    
    QWidget* setupWidget = vehicle->setupWidget();
Don Gagne's avatar
Don Gagne committed
169 170 171 172 173 174
    
    dialog.resize(setupWidget->minimumSize());
    dialog.setInnerWidget(setupWidget);
    dialog.exec();
    
    delete setupWidget;
175
}