apmtoolbar.cpp 5.81 KB
Newer Older
1 2 3
#include <QDebug>
#include <QDeclarativeContext>
#include <QGraphicsObject>
4 5
#include "LinkManager.h"
#include "MainWindow.h"
6 7 8

#include "apmtoolbar.h"

9
APMToolBar::APMToolBar(QWidget *parent):
10
    QDeclarativeView(parent), m_uas(0)
11 12 13 14
{
    // Configure our QML object
    setSource(QUrl::fromLocalFile("qml/ApmToolBar.qml"));
    setResizeMode(QDeclarativeView::SizeRootObjectToView);
15
    this->rootContext()->setContextProperty("globalObj", this);
16 17
    connect(LinkManager::instance(),SIGNAL(newLink(LinkInterface*)),
            this, SLOT(updateLinkDisplay(LinkInterface*)));
18 19 20 21

    if (LinkManager::instance()->getLinks().count()>=3) {
        updateLinkDisplay(LinkManager::instance()->getLinks().last());
    }
22

23
    setConnection(false);
24 25 26 27 28 29 30 31 32 33 34 35

    connect(UASManager::instance(),SIGNAL(activeUASSet(UASInterface*)),this,SLOT(activeUasSet(UASInterface*)));
    activeUasSet(UASManager::instance()->getActiveUAS());
}
void APMToolBar::activeUasSet(UASInterface *uas)
{
    if (!uas)
    {
        return;
    }
    if (m_uas)
    {
36 37 38 39
        disconnect(m_uas,SIGNAL(armingChanged(bool)),
                   this,SLOT(armingChanged(bool)));
        disconnect(uas,SIGNAL(armingChanged(int, QString)),
                this,SLOT(armingChanged(int, QString)));
40
    }
41 42 43 44 45
    connect(uas,SIGNAL(armingChanged(bool)),
            this,SLOT(armingChanged(bool)));
    connect(uas,SIGNAL(armingChanged(int, QString)),
            this,SLOT(armingChanged(int, QString)));

46 47 48 49
}
void APMToolBar::armingChanged(bool armed)
{
    this->rootObject()->setProperty("armed",armed);
50 51
}

52 53 54
void APMToolBar::armingChanged(int sysId, QString armingState)
{
    qDebug() << "APMToolBar: sysid " << sysId << " armState" << armingState;
55 56 57 58
}

void APMToolBar::setFlightViewAction(QAction *action)
{
59
    connect(this, SIGNAL(triggerFlightView()), action, SIGNAL(triggered()));
60 61 62 63
}

void APMToolBar::setFlightPlanViewAction(QAction *action)
{
64
    connect(this, SIGNAL(triggerFlightPlanView()), action, SIGNAL(triggered()));
65 66 67 68
}

void APMToolBar::setHardwareViewAction(QAction *action)
{
69
    connect(this, SIGNAL(triggerHardwareView()), action, SIGNAL(triggered()));
70 71 72 73
}

void APMToolBar::setSoftwareViewAction(QAction *action)
{
74
    connect(this, SIGNAL(triggerSoftwareView()), action, SIGNAL(triggered()));
75 76 77 78
}

void APMToolBar::setSimulationViewAction(QAction *action)
{
79
    connect(this, SIGNAL(triggerSimulationView()), action, SIGNAL(triggered()));
80 81 82 83
}

void APMToolBar::setTerminalViewAction(QAction *action)
{
84 85 86 87 88 89
    connect(this, SIGNAL(triggerTerminalView()), action, SIGNAL(triggered()));
}

void APMToolBar::setConnectMAVAction(QAction *action)
{
    connect(this, SIGNAL(connectMAV()), action, SIGNAL(triggered()));
90 91 92 93 94
}

void APMToolBar::selectFlightView()
{
    qDebug() << "APMToolBar: SelectFlightView";
95
    emit triggerFlightView();
96 97 98 99 100
}

void APMToolBar::selectFlightPlanView()
{
    qDebug() << "APMToolBar: SelectFlightPlanView";
101
    emit triggerFlightPlanView();
102 103 104 105 106
}

void APMToolBar::selectHardwareView()
{
    qDebug() << "APMToolBar: selectHardwareView";
107
    emit triggerHardwareView();
108 109 110 111 112
}

void APMToolBar::selectSoftwareView()
{
    qDebug() << "APMToolBar: selectSoftwareView";
113
    emit triggerSoftwareView();
114 115 116 117 118 119 120 121 122 123 124 125 126 127
}

void APMToolBar::selectSimulationView()
{
    qDebug() << "APMToolBar: selectSimulationView";
}

void APMToolBar::selectTerminalView()
{
    qDebug() << "APMToolBar: selectTerminalView";
}

void APMToolBar::connectMAV()
{
128 129
    qDebug() << "APMToolBar: connectMAV ";

130 131 132
    bool connected = false;
    if (LinkManager::instance()->getSerialLinks().count() > 0)
        connected = LinkManager::instance()->getSerialLinks().last()->isConnected();
133 134
    bool result;

135
    if (!connected && LinkManager::instance()->getSerialLinks().count() == 0)
136 137 138 139 140
    {
        // No Link so prompt to connect one
        MainWindow::instance()->addLink();
    } else if (!connected) {
        // Need to Connect Link
141
        result = LinkManager::instance()->getSerialLinks().last()->connect();
142

143
    } else if (connected && LinkManager::instance()->getSerialLinks().count() > 0) {
144
        // result need to be the opposite of success.
145
        result = !LinkManager::instance()->getSerialLinks().last()->disconnect();
146 147
    }
    qDebug() << "result = " << result;
148 149

    // Change the image to represent the state
150
    setConnection(result);
151

152 153 154
    emit MAVConnected(result);
}

155 156 157 158 159 160 161
void APMToolBar::setConnection(bool connection)
{
    // Change the image to represent the state
    QObject *object = rootObject();
    object->setProperty("connected", connection);
}

162 163 164 165 166 167 168 169 170 171 172 173 174
APMToolBar::~APMToolBar()
{
    qDebug() << "Destory APM Toolbar";
}

void APMToolBar::showConnectionDialog()
{
    // Displays a UI where the user can select a MAV Link.
    qDebug() << "APMToolBar: showConnectionDialog link count ="
             << LinkManager::instance()->getLinks().count();

    bool result;

175
    if (LinkManager::instance()->getSerialLinks().count() > 0)
176
    {
177
        SerialLink *link = LinkManager::instance()->getSerialLinks().last();
178
        // Serial Link so prompt to config it
179 180
        connect(link, SIGNAL(updateLink(LinkInterface*)),
                             this, SLOT(updateLinkDisplay(LinkInterface*)));
181 182 183 184 185 186 187 188 189
        result = MainWindow::instance()->configLink(link);

        if (!result)
            qDebug() << "Link Config Failed!";
    } else {
        // No Link so prompt to create one
        MainWindow::instance()->addLink();
    }

190 191
}

192 193 194 195 196
void APMToolBar::updateLinkDisplay(LinkInterface* newLink)
{
    qDebug() << "APMToolBar: updateLinkDisplay";
    QObject *object = rootObject();

197
    if (newLink && object){
198 199 200 201 202
        qint64 baudrate = newLink->getNominalDataRate();
        object->setProperty("baudrateLabel", QString::number(baudrate));

        QString linkName = newLink->getName();
        object->setProperty("linkNameLabel", linkName);
203 204 205 206 207

        connect(newLink, SIGNAL(connected(bool)),
                this, SLOT(setConnection(bool)));

        setConnection(newLink->isConnected());
208 209
    }
}