apmtoolbar.cpp 4.41 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 11 12 13 14
    QDeclarativeView(parent)
{
    // 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

    QObject *object = rootObject();
Bill Bonney's avatar
Bill Bonney committed
24 25
    if (object)
        object->setProperty("connected", false);
26 27 28 29
}

void APMToolBar::setFlightViewAction(QAction *action)
{
30
    connect(this, SIGNAL(triggerFlightView()), action, SIGNAL(triggered()));
31 32 33 34
}

void APMToolBar::setFlightPlanViewAction(QAction *action)
{
35
    connect(this, SIGNAL(triggerFlightPlanView()), action, SIGNAL(triggered()));
36 37 38 39
}

void APMToolBar::setHardwareViewAction(QAction *action)
{
40
    connect(this, SIGNAL(triggerHardwareView()), action, SIGNAL(triggered()));
41 42 43 44
}

void APMToolBar::setSoftwareViewAction(QAction *action)
{
45
    connect(this, SIGNAL(triggerSoftwareView()), action, SIGNAL(triggered()));
46 47 48 49
}

void APMToolBar::setSimulationViewAction(QAction *action)
{
50
    connect(this, SIGNAL(triggerSimulationView()), action, SIGNAL(triggered()));
51 52 53 54
}

void APMToolBar::setTerminalViewAction(QAction *action)
{
55 56 57 58 59 60
    connect(this, SIGNAL(triggerTerminalView()), action, SIGNAL(triggered()));
}

void APMToolBar::setConnectMAVAction(QAction *action)
{
    connect(this, SIGNAL(connectMAV()), action, SIGNAL(triggered()));
61 62 63 64 65
}

void APMToolBar::selectFlightView()
{
    qDebug() << "APMToolBar: SelectFlightView";
66
    emit triggerFlightView();
67 68 69 70 71
}

void APMToolBar::selectFlightPlanView()
{
    qDebug() << "APMToolBar: SelectFlightPlanView";
72
    emit triggerFlightPlanView();
73 74 75 76 77
}

void APMToolBar::selectHardwareView()
{
    qDebug() << "APMToolBar: selectHardwareView";
78
    emit triggerHardwareView();
79 80 81 82 83
}

void APMToolBar::selectSoftwareView()
{
    qDebug() << "APMToolBar: selectSoftwareView";
84
    emit triggerSoftwareView();
85 86 87 88 89 90 91 92 93 94 95 96 97 98
}

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

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

void APMToolBar::connectMAV()
{
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    qDebug() << "APMToolBar: connectMAV ";

    bool connected = LinkManager::instance()->getLinks().last()->isConnected();
    bool result;

    if (!connected && LinkManager::instance()->getLinks().count() < 3)
    {
        // No Link so prompt to connect one
        MainWindow::instance()->addLink();
    } else if (!connected) {
        // Need to Connect Link
        result = LinkManager::instance()->getLinks().last()->connect();

    } else if (connected && LinkManager::instance()->getLinks().count() > 2) {
        // result need to be the opposite of success.
        result = !LinkManager::instance()->getLinks().last()->disconnect();
    }
    qDebug() << "result = " << result;
117 118 119 120 121

    // Change the image to represent the state
    QObject *object = rootObject();
    object->setProperty("connected", result);

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    emit MAVConnected(result);
}

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();

    LinkInterface *link = LinkManager::instance()->getLinks().last();
    bool result;

    if (link && LinkManager::instance()->getLinks().count() >= 3)
    {
        // Serial Link so prompt to config it
        result = MainWindow::instance()->configLink(link);

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

151 152
}

153 154 155 156 157
void APMToolBar::updateLinkDisplay(LinkInterface* newLink)
{
    qDebug() << "APMToolBar: updateLinkDisplay";
    QObject *object = rootObject();

158
    if (newLink && object){
159 160 161 162 163 164 165
        qint64 baudrate = newLink->getNominalDataRate();
        object->setProperty("baudrateLabel", QString::number(baudrate));

        QString linkName = newLink->getName();
        object->setProperty("linkNameLabel", linkName);
    }
}