apmtoolbar.cpp 6.03 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    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 {
156 157 158 159 160
        // No Link so prompt to connect one
        MainWindow::instance()->addLink();
    }
}

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

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 201 202
void APMToolBar::updateLinkDisplay(LinkInterface* newLink)
{
    qDebug() << "APMToolBar: updateLinkDisplay";
    QObject *object = rootObject();

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

        QString linkName = newLink->getName();
        object->setProperty("linkNameLabel", linkName);
209 210 211 212 213

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

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