apmtoolbar.cpp 6.14 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
{
    // Configure our QML object
Don Gagne's avatar
Don Gagne committed
13 14 15 16 17 18 19 20
    
    // Hack to fix QTBUG 34300 on OSX where QDir::currentPath has changed behavior. This causes
    // relative paths to inside the .app package to fail.
#ifdef Q_OS_MAC
    QString qmlFile = QApplication::applicationDirPath();
    qmlFile.append("/qml/ApmToolBar.qml");
    setSource(QUrl::fromLocalFile(qmlFile));
#else
21
    setSource(QUrl::fromLocalFile("qml/ApmToolBar.qml"));
Don Gagne's avatar
Don Gagne committed
22
#endif
23
    setResizeMode(QDeclarativeView::SizeRootObjectToView);
24
    this->rootContext()->setContextProperty("globalObj", this);
25 26
    connect(LinkManager::instance(),SIGNAL(newLink(LinkInterface*)),
            this, SLOT(updateLinkDisplay(LinkInterface*)));
27 28 29 30

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

32
    setConnection(false);
33 34 35 36 37 38 39 40 41 42 43 44

    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)
    {
45 46 47 48
        disconnect(m_uas,SIGNAL(armingChanged(bool)),
                   this,SLOT(armingChanged(bool)));
        disconnect(uas,SIGNAL(armingChanged(int, QString)),
                this,SLOT(armingChanged(int, QString)));
49
    }
50 51 52 53 54
    connect(uas,SIGNAL(armingChanged(bool)),
            this,SLOT(armingChanged(bool)));
    connect(uas,SIGNAL(armingChanged(int, QString)),
            this,SLOT(armingChanged(int, QString)));

55 56 57 58
}
void APMToolBar::armingChanged(bool armed)
{
    this->rootObject()->setProperty("armed",armed);
59 60
}

61 62 63
void APMToolBar::armingChanged(int sysId, QString armingState)
{
    qDebug() << "APMToolBar: sysid " << sysId << " armState" << armingState;
64 65 66 67
}

void APMToolBar::setFlightViewAction(QAction *action)
{
68
    connect(this, SIGNAL(triggerFlightView()), action, SIGNAL(triggered()));
69 70 71 72
}

void APMToolBar::setFlightPlanViewAction(QAction *action)
{
73
    connect(this, SIGNAL(triggerFlightPlanView()), action, SIGNAL(triggered()));
74 75 76 77
}

void APMToolBar::setHardwareViewAction(QAction *action)
{
78
    connect(this, SIGNAL(triggerHardwareView()), action, SIGNAL(triggered()));
79 80 81 82
}

void APMToolBar::setSoftwareViewAction(QAction *action)
{
83
    connect(this, SIGNAL(triggerSoftwareView()), action, SIGNAL(triggered()));
84 85 86 87
}

void APMToolBar::setSimulationViewAction(QAction *action)
{
88
    connect(this, SIGNAL(triggerSimulationView()), action, SIGNAL(triggered()));
89 90 91 92
}

void APMToolBar::setTerminalViewAction(QAction *action)
{
93 94 95 96 97 98
    connect(this, SIGNAL(triggerTerminalView()), action, SIGNAL(triggered()));
}

void APMToolBar::setConnectMAVAction(QAction *action)
{
    connect(this, SIGNAL(connectMAV()), action, SIGNAL(triggered()));
99 100 101 102 103
}

void APMToolBar::selectFlightView()
{
    qDebug() << "APMToolBar: SelectFlightView";
104
    emit triggerFlightView();
105 106 107 108 109
}

void APMToolBar::selectFlightPlanView()
{
    qDebug() << "APMToolBar: SelectFlightPlanView";
110
    emit triggerFlightPlanView();
111 112 113 114 115
}

void APMToolBar::selectHardwareView()
{
    qDebug() << "APMToolBar: selectHardwareView";
116
    emit triggerHardwareView();
117 118 119 120 121
}

void APMToolBar::selectSoftwareView()
{
    qDebug() << "APMToolBar: selectSoftwareView";
122
    emit triggerSoftwareView();
123 124 125 126 127 128 129 130 131 132 133 134 135 136
}

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

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

void APMToolBar::connectMAV()
{
137 138
    qDebug() << "APMToolBar: connectMAV ";

139 140 141
    bool connected = false;
    if (LinkManager::instance()->getSerialLinks().count() > 0)
        connected = LinkManager::instance()->getSerialLinks().last()->isConnected();
142 143
    bool result;

144
    if (!connected && LinkManager::instance()->getSerialLinks().count() == 0)
145 146 147 148 149
    {
        // No Link so prompt to connect one
        MainWindow::instance()->addLink();
    } else if (!connected) {
        // Need to Connect Link
150
        result = LinkManager::instance()->getSerialLinks().last()->connect();
151

152
    } else if (connected && LinkManager::instance()->getSerialLinks().count() > 0) {
153
        // result need to be the opposite of success.
154
        result = !LinkManager::instance()->getSerialLinks().last()->disconnect();
155 156
    }
    qDebug() << "result = " << result;
157 158

    // Change the image to represent the state
159
    setConnection(result);
160

161 162 163
    emit MAVConnected(result);
}

164 165 166 167 168 169 170
void APMToolBar::setConnection(bool connection)
{
    // Change the image to represent the state
    QObject *object = rootObject();
    object->setProperty("connected", connection);
}

171 172 173 174 175 176 177 178 179 180 181 182 183
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;

184
    if (LinkManager::instance()->getSerialLinks().count() > 0)
185
    {
186
        SerialLink *link = LinkManager::instance()->getSerialLinks().last();
187
        // Serial Link so prompt to config it
188 189
        connect(link, SIGNAL(updateLink(LinkInterface*)),
                             this, SLOT(updateLinkDisplay(LinkInterface*)));
190 191 192 193 194 195 196 197 198
        result = MainWindow::instance()->configLink(link);

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

199 200
}

201 202 203 204 205
void APMToolBar::updateLinkDisplay(LinkInterface* newLink)
{
    qDebug() << "APMToolBar: updateLinkDisplay";
    QObject *object = rootObject();

206
    if (newLink && object){
207
        qint64 baudrate = newLink->getConnectionSpeed();
208 209 210 211
        object->setProperty("baudrateLabel", QString::number(baudrate));

        QString linkName = newLink->getName();
        object->setProperty("linkNameLabel", linkName);
212 213 214 215 216

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

        setConnection(newLink->isConnected());
217 218
    }
}