apmtoolbar.cpp 4.87 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
    setConnection(false);
24 25 26 27
}

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

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

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

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

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

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

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

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

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

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

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

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

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

void APMToolBar::connectMAV()
{
97 98
    qDebug() << "APMToolBar: connectMAV ";

99 100 101
    bool connected = false;
    if (LinkManager::instance()->getSerialLinks().count() > 0)
        connected = LinkManager::instance()->getSerialLinks().last()->isConnected();
102 103
    bool result;

104
    if (!connected && LinkManager::instance()->getSerialLinks().count() == 0)
105 106 107 108 109
    {
        // No Link so prompt to connect one
        MainWindow::instance()->addLink();
    } else if (!connected) {
        // Need to Connect Link
110
        result = LinkManager::instance()->getSerialLinks().last()->connect();
111

112
    } else if (connected && LinkManager::instance()->getSerialLinks().count() > 0) {
113
        // result need to be the opposite of success.
114
        result = !LinkManager::instance()->getSerialLinks().last()->disconnect();
115 116
    }
    qDebug() << "result = " << result;
117 118

    // Change the image to represent the state
119
    setConnection(result);
120

121 122 123
    emit MAVConnected(result);
}

124 125 126 127 128 129 130
void APMToolBar::setConnection(bool connection)
{
    // Change the image to represent the state
    QObject *object = rootObject();
    object->setProperty("connected", connection);
}

131 132 133 134 135 136 137 138 139 140 141 142 143
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;

144
    if (LinkManager::instance()->getSerialLinks().count() > 0)
145
    {
146
        SerialLink *link = LinkManager::instance()->getSerialLinks().last();
147
        // Serial Link so prompt to config it
148 149
        connect(link, SIGNAL(updateLink(LinkInterface*)),
                             this, SLOT(updateLinkDisplay(LinkInterface*)));
150 151 152 153 154 155 156 157 158
        result = MainWindow::instance()->configLink(link);

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

159 160
}

161 162 163 164 165
void APMToolBar::updateLinkDisplay(LinkInterface* newLink)
{
    qDebug() << "APMToolBar: updateLinkDisplay";
    QObject *object = rootObject();

166
    if (newLink && object){
167 168 169 170 171
        qint64 baudrate = newLink->getNominalDataRate();
        object->setProperty("baudrateLabel", QString::number(baudrate));

        QString linkName = newLink->getName();
        object->setProperty("linkNameLabel", linkName);
172 173 174 175 176

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

        setConnection(newLink->isConnected());
177 178
    }
}