MainToolBarController.cc 6.29 KB
Newer Older
dogmaphobic's avatar
dogmaphobic committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2015 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief QGC Main Tool Bar
 *   @author Gus Grubba <mavlink@grubba.com>
 */

#include <QQmlContext>
#include <QQmlEngine>

33
#include "MainToolBarController.h"
Don Gagne's avatar
Don Gagne committed
34
#include "ScreenToolsController.h"
35
#include "MainWindow.h"
dogmaphobic's avatar
dogmaphobic committed
36
#include "UASMessageView.h"
37 38
#include "UASMessageHandler.h"
#include "QGCApplication.h"
39
#include "MultiVehicleManager.h"
40
#include "UAS.h"
dogmaphobic's avatar
dogmaphobic committed
41

42 43
MainToolBarController::MainToolBarController(QObject* parent)
    : QObject(parent)
44
    , _vehicle(NULL)
Don Gagne's avatar
Don Gagne committed
45
    , _mav(NULL)
46
    , _progressBarValue(0.0f)
47 48
    , _telemetryRRSSI(0)
    , _telemetryLRSSI(0)
dogmaphobic's avatar
dogmaphobic committed
49
    , _rollDownMessages(0)
50
    , _toolbarMessageVisible(false)
dogmaphobic's avatar
dogmaphobic committed
51
{
52
    _activeVehicleChanged(qgcApp()->toolbox()->multiVehicleManager()->activeVehicle());
53
    
54
    // RSSI (didn't like standard connection)
55
    connect(qgcApp()->toolbox()->mavlinkProtocol(),
56 57
        SIGNAL(radioStatusChanged(LinkInterface*, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned)), this,
        SLOT(_telemetryChanged(LinkInterface*, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned)));
58
    
59
    connect(qgcApp()->toolbox()->multiVehicleManager(), &MultiVehicleManager::activeVehicleChanged, this, &MainToolBarController::_activeVehicleChanged);
dogmaphobic's avatar
dogmaphobic committed
60 61
}

62
MainToolBarController::~MainToolBarController()
dogmaphobic's avatar
dogmaphobic committed
63 64 65 66
{

}

67
void MainToolBarController::onSetupView()
68
{
69
    MainWindow::instance()->showSetupView();
70 71
}

72
void MainToolBarController::onPlanView()
73
{
74
    MainWindow::instance()->showPlanView();
75 76
}

77
void MainToolBarController::onFlyView()
dogmaphobic's avatar
dogmaphobic committed
78
{
79
    MainWindow::instance()->showFlyView();
dogmaphobic's avatar
dogmaphobic committed
80 81
}

82
void MainToolBarController::onEnterMessageArea(int x, int y)
dogmaphobic's avatar
dogmaphobic committed
83
{
84 85 86
    Q_UNUSED(x);
    Q_UNUSED(y);

dogmaphobic's avatar
dogmaphobic committed
87
    // If not already there and messages are actually present
88 89 90
    if(!_rollDownMessages && qgcApp()->toolbox()->uasMessageHandler()->messages().count()) {
        if (qgcApp()->toolbox()->multiVehicleManager()->activeVehicle()) {
            qgcApp()->toolbox()->multiVehicleManager()->activeVehicle()->resetMessages();
91 92 93
        }

        // FIXME: Position of the message dropdown is hacked right now to speed up Qml conversion
dogmaphobic's avatar
dogmaphobic committed
94 95
        // Show messages
        int dialogWidth = 400;
96
#if 0
dogmaphobic's avatar
dogmaphobic committed
97 98 99
        x = x - (dialogWidth >> 1);
        if(x < 0) x = 0;
        y = height() / 3;
100 101
#endif

dogmaphobic's avatar
dogmaphobic committed
102
        // Put dialog on top of the message alert icon
103
        _rollDownMessages = new UASMessageViewRollDown(qgcApp()->toolbox()->uasMessageHandler(), MainWindow::instance());
dogmaphobic's avatar
dogmaphobic committed
104
        _rollDownMessages->setAttribute(Qt::WA_DeleteOnClose);
105
        _rollDownMessages->move(QPoint(100, 100));
dogmaphobic's avatar
dogmaphobic committed
106
        _rollDownMessages->setMinimumSize(dialogWidth,200);
107
        connect(_rollDownMessages, &UASMessageViewRollDown::closeWindow, this, &MainToolBarController::_leaveMessageView);
dogmaphobic's avatar
dogmaphobic committed
108 109 110 111
        _rollDownMessages->show();
    }
}

112
void MainToolBarController::_leaveMessageView()
dogmaphobic's avatar
dogmaphobic committed
113 114 115 116 117
{
    // Mouse has left the message window area (and it has closed itself)
    _rollDownMessages = NULL;
}

118
void MainToolBarController::_activeVehicleChanged(Vehicle* vehicle)
119
{
120 121
    // Disconnect the previous one (if any)
    if (_vehicle) {
122
        disconnect(_vehicle->autopilotPlugin(), &AutoPilotPlugin::parameterListProgress, this, &MainToolBarController::_setProgressBarValue);
123
        _mav = NULL;
124
        _vehicle = NULL;
125
    }
126
    
dogmaphobic's avatar
dogmaphobic committed
127
    // Connect new system
128
    if (vehicle)
dogmaphobic's avatar
dogmaphobic committed
129
    {
130 131
        _vehicle = vehicle;
        _mav = vehicle->uas();
132
        connect(_vehicle->autopilotPlugin(), &AutoPilotPlugin::parameterListProgress, this, &MainToolBarController::_setProgressBarValue);
dogmaphobic's avatar
dogmaphobic committed
133
    }
134 135
}

136
void MainToolBarController::_telemetryChanged(LinkInterface*, unsigned, unsigned, unsigned rssi, unsigned remrssi, unsigned, unsigned, unsigned)
137
{
Don Gagne's avatar
Don Gagne committed
138 139 140
    if((unsigned)_telemetryLRSSI != rssi) {
        // According to the Silabs data sheet, the RSSI value is 0.5db per bit
        _telemetryLRSSI = rssi >> 1;
141
        emit telemetryLRSSIChanged(_telemetryLRSSI);
142
    }
Don Gagne's avatar
Don Gagne committed
143 144 145 146
    if((unsigned)_telemetryRRSSI != remrssi) {
        // According to the Silabs data sheet, the RSSI value is 0.5db per bit
        _telemetryRRSSI = remrssi >> 1;
        emit telemetryRRSSIChanged(_telemetryRRSSI);
147
    }
dogmaphobic's avatar
dogmaphobic committed
148 149
}

150
void MainToolBarController::_setProgressBarValue(float value)
151 152 153 154
{
    _progressBarValue = value;
    emit progressBarValueChanged(value);
}
Don Gagne's avatar
Don Gagne committed
155

156
void MainToolBarController::showToolBarMessage(const QString& message)
157 158 159 160
{
    _toolbarMessageQueueMutex.lock();
    
    if (_toolbarMessageQueue.count() == 0 && !_toolbarMessageVisible) {
161
        QTimer::singleShot(500, this, &MainToolBarController::_delayedShowToolBarMessage);
162 163 164 165 166 167 168
    }
    
    _toolbarMessageQueue += message;
    
    _toolbarMessageQueueMutex.unlock();
}

169
void MainToolBarController::_delayedShowToolBarMessage(void)
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
{
    QString messages;
    
    if (!_toolbarMessageVisible) {
        _toolbarMessageQueueMutex.lock();
        
        foreach (QString message, _toolbarMessageQueue) {
            messages += message + "\n";
        }
        _toolbarMessageQueue.clear();
        
        _toolbarMessageQueueMutex.unlock();
        
        if (!messages.isEmpty()) {
            _toolbarMessageVisible = true;
            emit showMessage(messages);
        }
    }
}

190
void MainToolBarController::onToolBarMessageClosed(void)
191 192 193
{
    _toolbarMessageVisible = false;
    _delayedShowToolBarMessage();
Don Gagne's avatar
Don Gagne committed
194
}
Don Gagne's avatar
Don Gagne committed
195 196 197 198 199

void MainToolBarController::showSettings(void)
{
    MainWindow::instance()->showSettings();
}
Don Gagne's avatar
Don Gagne committed
200 201 202 203 204

void MainToolBarController::manageLinks(void)
{
    MainWindow::instance()->manageLinks();
}