Skip to content
MainWindow.cc 48.7 KiB
Newer Older
void MainWindow::commsWidgetDestroyed(QObject *obj)
{
    // Do not dynamic cast or de-reference QObject, since object is either in destructor or may have already
    // been destroyed.
dogmaphobic's avatar
dogmaphobic committed
    if (_commsWidgetList.contains(obj))
dogmaphobic's avatar
dogmaphobic committed
        _commsWidgetList.removeOne(obj);

void MainWindow::setActiveUAS(UASInterface* uas)
{
    Q_UNUSED(uas);
dogmaphobic's avatar
dogmaphobic committed
    if (settings.contains(_getWindowStateKey()))
dogmaphobic's avatar
dogmaphobic committed
        restoreState(settings.value(_getWindowStateKey()).toByteArray());
}

void MainWindow::UASSpecsChanged(int uas)
{
    Q_UNUSED(uas);
    // TODO: Update UAS properties if its specs change
}

void MainWindow::UASCreated(UASInterface* uas)
{
    // The UAS actions are not enabled without connection to system
dogmaphobic's avatar
dogmaphobic committed
    _ui.actionLiftoff->setEnabled(true);
    _ui.actionLand->setEnabled(true);
    _ui.actionEmergency_Kill->setEnabled(true);
    _ui.actionEmergency_Land->setEnabled(true);
    _ui.actionShutdownMAV->setEnabled(true);
    connect(uas, SIGNAL(systemSpecsChanged(int)), this, SLOT(UASSpecsChanged(int)));
John Tapsell's avatar
John Tapsell committed
    connect(uas, SIGNAL(valueChanged(int,QString,QString,QVariant,quint64)), this, SIGNAL(valueChanged(int,QString,QString,QVariant,quint64)));
    connect(uas, SIGNAL(misconfigurationDetected(UASInterface*)), this, SLOT(handleMisconfiguration(UASInterface*)));
    _showHILConfigurationWidgets();
    if (!linechartWidget)
    {
        linechartWidget = new Linecharts(this);
        linechartWidget->setVisible(false);
    linechartWidget->addSource(mavlinkDecoder);
    if (_engineeringView != linechartWidget)
        _engineeringView = linechartWidget;

    // Reload view state in case new widgets were added
    _loadCurrentViewState();
}

void MainWindow::UASDeleted(UASInterface* uas)
{
    Q_UNUSED(uas);
    // TODO: Update the UI when a UAS is deleted
/// Stores the state of the toolbar, status bar and widgets associated with the current view
void MainWindow::_storeCurrentViewState(void)
    // HIL dock widgets are dynamic and are not part of the saved state
    _hideAllHilDockWidgets();
    // Save list of visible widgets
    bool firstWidget = true;
    QString widgetNames = "";
    foreach(QDockWidget* dockWidget, _mapName2DockWidget) {
        if (dockWidget->isVisible()) {
            if (!firstWidget) {
                widgetNames += ",";
            }
            widgetNames += dockWidget->objectName();
            firstWidget = false;
        }
dogmaphobic's avatar
dogmaphobic committed
    settings.setValue(_getWindowStateKey() + "WIDGETS", widgetNames);
    settings.setValue(_getWindowStateKey(), saveState());
    settings.setValue(_getWindowGeometryKey(), saveGeometry());
/// Restores the state of the toolbar, status bar and widgets associated with the current view
void MainWindow::_loadCurrentViewState(void)
Don Gagne's avatar
Don Gagne committed
    QWidget* centerView = NULL;
    QString defaultWidgets;
    switch (_currentView) {
            _buildSetupView();
            centerView = _setupView;
            _buildEngineeringView();
            centerView = _engineeringView;
            defaultWidgets = "MAVLINK_INSPECTOR_DOCKWIDGET,PARAMETER_INTERFACE_DOCKWIDGET,FILE_VIEW_DOCKWIDGET,HEAD_UP_DISPLAY_DOCKWIDGET";
            _buildPilotView();
            centerView = _pilotView;
            defaultWidgets = "COMMUNICATION_CONSOLE_DOCKWIDGET,UAS_INFO_INFOVIEW_DOCKWIDGET";
            _buildPlannerView();
            centerView = _plannerView;
            defaultWidgets = "UNMANNED_SYSTEM_LIST_DOCKWIDGET,WAYPOINT_LIST_DOCKWIDGET";
            _buildSimView();
            centerView = _simView;
            defaultWidgets = "UNMANNED_SYSTEM_CONTROL_DOCKWIDGET,WAYPOINT_LIST_DOCKWIDGET,PARAMETER_INTERFACE_DOCKWIDGET,PRIMARY_FLIGHT_DISPLAY_DOCKWIDGET";
        case VIEW_TERMINAL:
            _buildTerminalView();
            centerView = _terminalView;
            _buildGoogleEarthView();
            centerView = _googleEarthView;
            _buildLocal3DView();
            centerView = _local3DView;
    // Remove old view
    if (_currentViewWidget) {
        _currentViewWidget->setVisible(false);
        Q_ASSERT(_centralLayout->count() == 1);
        QLayoutItem *child = _centralLayout->takeAt(0);
        Q_ASSERT(child);
        delete child;
    }
    // Add the new one
    Q_ASSERT(centerView);
    Q_ASSERT(_centralLayout->count() == 0);
    _currentViewWidget = centerView;
    _centralLayout->addWidget(_currentViewWidget);
    _currentViewWidget->setVisible(true);
    // Hide all widgets from previous view
    _hideAllDockWidgets();

    // Restore the widgets for the new view
dogmaphobic's avatar
dogmaphobic committed
    QString widgetNames = settings.value(_getWindowStateKey() + "WIDGETS", defaultWidgets).toString();
    if (!widgetNames.isEmpty()) {
        QStringList split = widgetNames.split(",");
        foreach (QString widgetName, split) {
            Q_ASSERT(!widgetName.isEmpty());
            _showDockWidget(widgetName, true);
dogmaphobic's avatar
dogmaphobic committed
    if (settings.contains(_getWindowStateKey())) {
        restoreState(settings.value(_getWindowStateKey()).toByteArray());
    // HIL dock widget are dynamic and don't take part in the saved window state, so this
    // need to happen after we restore state
    _showHILConfigurationWidgets();
}

void MainWindow::_hideAllHilDockWidgets(void)
{
    foreach(QDockWidget* dockWidget, _mapUasId2HilDockWidget) {
        dockWidget->setVisible(false);
    }
}

void MainWindow::_hideAllDockWidgets(void)
{
    foreach(QDockWidget* dockWidget, _mapName2DockWidget) {
        dockWidget->setVisible(false);
    _hideAllHilDockWidgets();

void MainWindow::_showDockWidgetAction(bool show)
    QAction* action = dynamic_cast<QAction*>(QObject::sender());
    Q_ASSERT(action);
    _showDockWidget(action->data().toString(), show);
void MainWindow::handleMisconfiguration(UASInterface* uas)
{
    static QTime lastTime;
    // We have to debounce this signal
    if (!lastTime.isValid()) {
        lastTime.start();
    } else {
        if (lastTime.elapsed() < 10000) {
            lastTime.start();
            return;
        }
    }
dogmaphobic's avatar
dogmaphobic committed
    // Ask user if they want to handle this now
    QMessageBox::StandardButton button =
        QGCMessageBox::question(
            tr("Missing or Invalid Onboard Configuration"),
            tr("The onboard system configuration is missing or incomplete. Do you want to resolve this now?"),
            QMessageBox::Ok | QMessageBox::Cancel,
            QMessageBox::Ok);
Don Gagne's avatar
Don Gagne committed
    if (button == QMessageBox::Ok) {
dogmaphobic's avatar
dogmaphobic committed
        // They want to handle it, make sure this system is selected
        UASManager::instance()->setActiveUAS(uas);
        // Flick to config view
void MainWindow::loadEngineerView()
{
    if (_currentView != VIEW_ENGINEER)
        _storeCurrentViewState();
        _currentView = VIEW_ENGINEER;
dogmaphobic's avatar
dogmaphobic committed
        _ui.actionEngineersView->setChecked(true);
        _loadCurrentViewState();
    if (_currentView != VIEW_MISSION)
        _storeCurrentViewState();
        _currentView = VIEW_MISSION;
dogmaphobic's avatar
dogmaphobic committed
        _ui.actionMissionView->setChecked(true);
        _loadCurrentViewState();
void MainWindow::loadSetupView()
    if (_currentView != VIEW_SETUP)
        _storeCurrentViewState();
        _currentView = VIEW_SETUP;
dogmaphobic's avatar
dogmaphobic committed
        _ui.actionSetup->setChecked(true);
        _loadCurrentViewState();
void MainWindow::loadTerminalView()
{
    if (_currentView != VIEW_TERMINAL)
        _storeCurrentViewState();
        _currentView = VIEW_TERMINAL;
dogmaphobic's avatar
dogmaphobic committed
        _ui.actionTerminalView->setChecked(true);
        _loadCurrentViewState();
void MainWindow::loadGoogleEarthView()
{
    if (_currentView != VIEW_GOOGLEEARTH)
        _storeCurrentViewState();
        _currentView = VIEW_GOOGLEEARTH;
dogmaphobic's avatar
dogmaphobic committed
        _ui.actionGoogleEarthView->setChecked(true);
        _loadCurrentViewState();
    if (_currentView != VIEW_LOCAL3D)
        _storeCurrentViewState();
        _currentView = VIEW_LOCAL3D;
dogmaphobic's avatar
dogmaphobic committed
        _ui.actionLocal3DView->setChecked(true);
        _loadCurrentViewState();
void MainWindow::loadPilotView()
{
    if (_currentView != VIEW_FLIGHT)
        _storeCurrentViewState();
        _currentView = VIEW_FLIGHT;
dogmaphobic's avatar
dogmaphobic committed
        _ui.actionFlightView->setChecked(true);
        _loadCurrentViewState();
void MainWindow::loadSimulationView()
{
    if (_currentView != VIEW_SIMULATION)
        _storeCurrentViewState();
        _currentView = VIEW_SIMULATION;
dogmaphobic's avatar
dogmaphobic committed
        _ui.actionSimulationView->setChecked(true);
        _loadCurrentViewState();
Don Gagne's avatar
Don Gagne committed
/// @brief Hides the spash screen if it is currently being shown
void MainWindow::hideSplashScreen(void)
Don Gagne's avatar
Don Gagne committed
{
    if (_splashScreen) {
        _splashScreen->hide();
        _splashScreen = NULL;
    }
}

void MainWindow::manageLinks()
{
    SettingsDialog settings(joystick, this, SettingsDialog::ShowCommLinks);
    settings.exec();
}

/// @brief Saves the last used connection
void MainWindow::saveLastUsedConnection(const QString connection)
{
    QSettings settings;
    QString key(MAIN_SETTINGS_GROUP);
    key += "/LAST_CONNECTION";
    settings.setValue(key, connection);
}

/// @brief Restore (and connects) the last used connection (if any)
void MainWindow::restoreLastUsedConnection()
{
    // TODO This should check and see of the port/whatever is present
    // first. That is, if the last connection was to a PX4 on some serial
    // port, it should check and see if the port is present before making
    // the connection.
    QSettings settings;
    QString key(MAIN_SETTINGS_GROUP);
    key += "/LAST_CONNECTION";
    QString connection;
    if(settings.contains(key)) {
        connection = settings.value(connection).toString();
        // Create a link for it
        LinkInterface* link = LinkManager::instance()->createLink(connection);
        if(link) {
            // Connect it
            LinkManager::instance()->connectLink(link);
        }
    }
}
#ifdef QGC_MOUSE_ENABLED_LINUX
bool MainWindow::x11Event(XEvent *event)
{
    emit x11EventOccured(event);
#endif // QGC_MOUSE_ENABLED_LINUX

#ifdef UNITTEST_BUILD
void MainWindow::_showQmlTestWidget(void)
{
    new QmlTestWidget();
}
#endif

// There is a bug in Qt where a Canvas element inside a QQuickWidget does not
// receive update requests. We hook into this event and notify the tool bar
// to update its canvas elements. If other QQuickWidgets start using Canvas
// and this bug is not fixed, this should be turned into a signal emited by
// MainWindow and the various QQuickWidgets that need it should connect to it.
bool MainWindow::event(QEvent* e)
{
    bool result = true;
    switch (e->type()) {
        case QEvent::Paint:
            result = QMainWindow::event(e);
            _mainToolBar->updateCanvas();
            return result;
        default:
            break;
    }
    return QMainWindow::event(e);
}