apmtoolbar.cpp 5.97 KB
Newer Older
1
#include <QDebug>
2
#include <QQmlContext>
3
#include <QGraphicsObject>
Bryant Mairs's avatar
Bryant Mairs committed
4
#include <QQuickItem>
5 6
#include "LinkManager.h"
#include "MainWindow.h"
7 8 9

#include "apmtoolbar.h"

10
APMToolBar::APMToolBar():
11
    QQuickView(), m_uas(0)
12 13
{
    // Configure our QML object
Don Gagne's avatar
Don Gagne committed
14 15 16 17 18 19 20 21
    
    // 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
22
    setSource(QUrl::fromLocalFile("qml/ApmToolBar.qml"));
Don Gagne's avatar
Don Gagne committed
23
#endif
24
    setResizeMode(QQuickView::SizeRootObjectToView);
Bryant Mairs's avatar
Bryant Mairs committed
25
    rootContext()->setContextProperty("globalObj", this);
26 27
    connect(LinkManager::instance(),SIGNAL(newLink(LinkInterface*)),
            this, SLOT(updateLinkDisplay(LinkInterface*)));
28 29 30 31

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

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

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

56 57 58
}
void APMToolBar::armingChanged(bool armed)
{
Bryant Mairs's avatar
Bryant Mairs committed
59
    rootObject()->setProperty("armed", armed);
60 61
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    if (LinkManager::instance()->getSerialLinks().count() > 0) {
        bool result;
        bool connected = LinkManager::instance()->getSerialLinks().last()->isConnected();
        if (connected) {
            // result need to be the opposite of success.
            result = !LinkManager::instance()->getSerialLinks().last()->disconnect();
        } else {
            // Need to Connect Link
            result = LinkManager::instance()->getSerialLinks().last()->connect();
        }
        qDebug() << "result = " << result;

        // Change the image to represent the state
        setConnection(result);
        
        emit MAVConnected(result);
    } else {
157 158 159 160 161
        // No Link so prompt to connect one
        MainWindow::instance()->addLink();
    }
}

162 163 164
void APMToolBar::setConnection(bool connection)
{
    // Change the image to represent the state
Bryant Mairs's avatar
Bryant Mairs committed
165
    rootObject()->setProperty("connected", connection);
166 167
}

168 169 170 171 172 173 174 175 176 177 178 179 180
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;

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

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

196 197
}

198 199 200
void APMToolBar::updateLinkDisplay(LinkInterface* newLink)
{
    qDebug() << "APMToolBar: updateLinkDisplay";
Bryant Mairs's avatar
Bryant Mairs committed
201
    QObject *object = rootObject();
202

203
    if (newLink && rootObject()){
204
        qint64 baudrate = newLink->getConnectionSpeed();
Bryant Mairs's avatar
Bryant Mairs committed
205
        object->setProperty("baudrateLabel", QString::number(baudrate));
206 207

        QString linkName = newLink->getName();
Bryant Mairs's avatar
Bryant Mairs committed
208
        object->setProperty("linkNameLabel", linkName);
209 210 211 212 213

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

        setConnection(newLink->isConnected());
214 215
    }
}