UASMessageHandler.cc 5.85 KB
Newer Older
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
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009 - 2011 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 Message Handler
 *   @author Gus Grubba <mavlink@grubba.com>
 */

#include "QGCApplication.h"
31
#include "UASMessageHandler.h"
32 33
#include "UASManager.h"

34
UASMessage::UASMessage(int componentid, int severity, QString text)
35 36 37 38 39 40
{
    _compId   = componentid;
    _severity = severity;
    _text     = text;
}

41
IMPLEMENT_QGC_SINGLETON(UASMessageHandler, UASMessageHandler)
42

43
UASMessageHandler::UASMessageHandler(QObject *parent)
44 45
    : QGCSingleton(parent)
    , _activeUAS(NULL)
dogmaphobic's avatar
dogmaphobic committed
46 47 48
    , _errorCount(0)
    , _warningCount(0)
    , _normalCount(0)
49 50 51
{
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setActiveUAS(UASInterface*)));
    emit textMessageReceived(NULL);
dogmaphobic's avatar
dogmaphobic committed
52
    emit textMessageCountChanged(0);
53 54
}

55
UASMessageHandler::~UASMessageHandler()
56
{
57
    clearMessages();
58 59
}

60
void UASMessageHandler::clearMessages()
61 62 63 64 65 66
{
    _mutex.lock();
    while(_messages.count()) {
        delete _messages.last();
        _messages.pop_back();
    }
dogmaphobic's avatar
dogmaphobic committed
67 68 69
    _errorCount   = 0;
    _warningCount = 0;
    _normalCount  = 0;
70
    _mutex.unlock();
dogmaphobic's avatar
dogmaphobic committed
71
    emit textMessageCountChanged(0);
72 73
}

74
void UASMessageHandler::setActiveUAS(UASInterface* uas)
75 76 77 78 79 80
{
    // If we were already attached to an autopilot, disconnect it.
    if (_activeUAS && _activeUAS != uas)
    {
        disconnect(_activeUAS, SIGNAL(textMessageReceived(int,int,int,QString)), this, SLOT(handleTextMessage(int,int,int,QString)));
        _activeUAS = NULL;
81
        clearMessages();
82 83 84 85 86 87
        emit textMessageReceived(NULL);
    }
    // And now if there's an autopilot to follow, set up the UI.
    if (uas)
    {
        // Connect to the new UAS.
88
        clearMessages();
89 90 91 92 93
        _activeUAS = uas;
        connect(uas, SIGNAL(textMessageReceived(int,int,int,QString)), this, SLOT(handleTextMessage(int,int,int,QString)));
    }
}

dogmaphobic's avatar
dogmaphobic committed
94
void UASMessageHandler::handleTextMessage(int, int compId, int severity, QString text)
95 96 97 98 99 100 101
{

    // Color the output depending on the message severity. We have 3 distinct cases:
    // 1: If we have an ERROR or worse, make it bigger, bolder, and highlight it red.
    // 2: If we have a warning or notice, just make it bold and color it orange.
    // 3: Otherwise color it the standard color, white.

dogmaphobic's avatar
dogmaphobic committed
102 103
    _mutex.lock();

104 105 106 107 108 109 110 111 112 113
    // So first determine the styling based on the severity.
    QString style;
    switch (severity)
    {
    case MAV_SEVERITY_EMERGENCY:
    case MAV_SEVERITY_ALERT:
    case MAV_SEVERITY_CRITICAL:
    case MAV_SEVERITY_ERROR:
        //Use set RGB values from given color from QGC
        style = QString("color: rgb(%1, %2, %3); font-weight:bold").arg(QGC::colorRed.red()).arg(QGC::colorRed.green()).arg(QGC::colorRed.blue());
dogmaphobic's avatar
dogmaphobic committed
114
        _errorCount++;
115 116 117 118
        break;
    case MAV_SEVERITY_NOTICE:
    case MAV_SEVERITY_WARNING:
        style = QString("color: rgb(%1, %2, %3); font-weight:bold").arg(QGC::colorOrange.red()).arg(QGC::colorOrange.green()).arg(QGC::colorOrange.blue());
dogmaphobic's avatar
dogmaphobic committed
119
        _warningCount++;
120 121 122
        break;
    default:
        style = QString("color:white; font-weight:bold");
dogmaphobic's avatar
dogmaphobic committed
123
        _normalCount++;
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 151 152 153 154 155 156 157 158 159 160 161
        break;
    }

    // And determine the text for the severitie
    QString severityText("");
    switch (severity)
    {
    case MAV_SEVERITY_EMERGENCY:
        severityText = QString(tr(" EMERGENCY:"));
        break;
    case MAV_SEVERITY_ALERT:
        severityText = QString(tr(" ALERT:"));
        break;
    case MAV_SEVERITY_CRITICAL:
        severityText = QString(tr(" Critical:"));
        break;
    case MAV_SEVERITY_ERROR:
        severityText = QString(tr(" Error:"));
        break;
    case MAV_SEVERITY_WARNING:
        severityText = QString(tr(" Warning:"));
        break;
    case MAV_SEVERITY_NOTICE:
        severityText = QString(tr(" Notice:"));
        break;
    case MAV_SEVERITY_INFO:
        severityText = QString(tr(" Info:"));
        break;
    case MAV_SEVERITY_DEBUG:
        severityText = QString(tr(" Debug:"));
        break;
    default:
        severityText = QString(tr(""));
        break;
    }

    // Finally preppend the properly-styled text with a timestamp.
    QString dateString = QDateTime::currentDateTime().toString("hh:mm:ss.zzz");
162
    UASMessage* message = new UASMessage(compId, severity, text);
163 164
    message->_setFormatedText(QString("<p style=\"color:#CCCCCC\">[%2 - COMP:%3]<font style=\"%1\">%4 %5</font></p>").arg(style).arg(dateString).arg(compId).arg(severityText).arg(text));
    _messages.append(message);
dogmaphobic's avatar
dogmaphobic committed
165
    int count = _messages.count();
166 167
    _mutex.unlock();
    emit textMessageReceived(message);
dogmaphobic's avatar
dogmaphobic committed
168
    emit textMessageCountChanged(count);
169
}
dogmaphobic's avatar
dogmaphobic committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

int UASMessageHandler::getErrorCount() {
    _mutex.lock();
    int c = _errorCount;
    _errorCount = 0;
    _mutex.unlock();
    return c;
}

int UASMessageHandler::getWarningCount() {
    _mutex.lock();
    int c = _warningCount;
    _warningCount = 0;
    _mutex.unlock();
    return c;
}

int UASMessageHandler::getNormalCount() {
    _mutex.lock();
    int c = _normalCount;
    _normalCount = 0;
    _mutex.unlock();
    return c;
}