QGCMessageView.cc 5.37 KB
Newer Older
1
#include <QMenu>
2
#include <QScrollBar>
3

4
#include "QGCMessageView.h"
5 6 7 8 9
#include "GAudioOutput.h"
#include "QGCUnconnectedInfoWidget.h"
#include "UASManager.h"
#include "ui_QGCMessageView.h"

10 11 12 13 14
QGCMessageView::QGCMessageView(QWidget *parent) :
    QWidget(parent),
    activeUAS(NULL),
    ui(new Ui::QGCMessageView)
{
15
    setObjectName("QUICKVIEW_MESSAGE_CONSOLE")  ;
16 17

    ui->setupUi(this);
18
    setStyleSheet("QPlainTextEdit { border: 0px }");
19 20 21 22 23 24

    // Construct initial widget
    connectWidget = new QGCUnconnectedInfoWidget(this);
    ui->horizontalLayout->addWidget(connectWidget);
    ui->plainTextEdit->hide();

25
    // Enable the right-click menu for the text editor. This works because the plainTextEdit
26
    // widget has its context menu policy set to its actions list. So any actions we add
27 28 29 30 31 32 33
    // to this widget's action list will be automatically displayed.
    // We only have the clear action right now.
    QAction* clearAction = new QAction(tr("Clear Text"), this);
    connect(clearAction, SIGNAL(triggered()), ui->plainTextEdit, SLOT(clear()));
    ui->plainTextEdit->addAction(clearAction);

    // Connect to the currently active UAS.
34
    setActiveUAS(UASManager::instance()->getActiveUAS());
35 36 37 38 39
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setActiveUAS(UASInterface*)));
}

QGCMessageView::~QGCMessageView()
{
40 41
    // The only thing we need to delete is the ui because it's the only thing not cleaned up automatically
    // by the deletion of its parent.
42 43 44 45 46
    delete ui;
}

void QGCMessageView::setActiveUAS(UASInterface* uas)
{
47 48 49 50 51
    // If we were already attached to an autopilot, disconnect it, restoring
    // the widget to its initial state as needed.
    if (activeUAS)
    {
        disconnect(activeUAS, SIGNAL(textMessageReceived(int,int,int,QString)), this, SLOT(handleTextMessage(int,int,int,QString)));
52
        ui->plainTextEdit->clear();
53
        activeUAS = NULL;
54 55
    }

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    // And now if there's an autopilot to follow, set up the UI.
    if (uas)
    {
        // Make sure the UI is configured for showing messages.
        // Note that this call is NOT equivalent to `connectWidget->isVisible()`.
        if (!connectWidget->isHidden())
        {
            connectWidget->hide();
            ui->plainTextEdit->show();
        }

        // And connect to the new UAS.
        connect(uas, SIGNAL(textMessageReceived(int,int,int,QString)), this, SLOT(handleTextMessage(int,int,int,QString)));
        activeUAS = uas;
    }
    // But if there's no new autopilot, restore the connect button.
    else
    {
        connectWidget->show();
        ui->plainTextEdit->hide();
    }
77 78
}

79
void QGCMessageView::handleTextMessage(int uasid, int compId, int severity, QString text)
80
{
81
    QPlainTextEdit *msgWidget = ui->plainTextEdit;
82
    (void)uasid; // Unused variable voided.
83
    // Turn off updates while we're appending content to avoid breaking the autoscroll behavior
84 85 86
    msgWidget->setUpdatesEnabled(false);
    QScrollBar *scroller = msgWidget->verticalScrollBar();

87
    // Color the output depending on the message severity. We have 3 distinct cases:
88 89 90
    // 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.
91 92 93 94 95 96 97 98 99

    // So first deteremine 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:
100 101
        //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());
102 103
        break;
    case MAV_SEVERITY_NOTICE:
104
    case MAV_SEVERITY_WARNING:
105
        style = QString("color: rgb(%1, %2, %3); font-weight:bold").arg(QGC::colorOrange.red()).arg(QGC::colorOrange.green()).arg(QGC::colorOrange.blue());
106 107
        break;
    default:
108
        style = QString("color:white; font-weight:bold");
109
        break;
110
    }
111

112
    // And determine the text for the severitie
113 114 115 116
    QString severityText("");
    switch (severity)
    {
    case MAV_SEVERITY_EMERGENCY:
Lorenz Meier's avatar
Lorenz Meier committed
117
        severityText = QString(tr(" EMERGENCY:"));
118 119
        break;
    case MAV_SEVERITY_ALERT:
Lorenz Meier's avatar
Lorenz Meier committed
120
        severityText = QString(tr(" ALERT:"));
121 122
        break;
    case MAV_SEVERITY_CRITICAL:
Lorenz Meier's avatar
Lorenz Meier committed
123
        severityText = QString(tr(" Critical:"));
124 125
        break;
    case MAV_SEVERITY_ERROR:
Lorenz Meier's avatar
Lorenz Meier committed
126
        severityText = QString(tr(" Error:"));
127 128
        break;
    case MAV_SEVERITY_WARNING:
Lorenz Meier's avatar
Lorenz Meier committed
129
        severityText = QString(tr(" Warning:"));
130 131
        break;
    case MAV_SEVERITY_NOTICE:
Lorenz Meier's avatar
Lorenz Meier committed
132
        severityText = QString(tr(" Notice:"));
133 134
        break;
    case MAV_SEVERITY_INFO:
Lorenz Meier's avatar
Lorenz Meier committed
135
        severityText = QString(tr(" Info:"));
136 137
        break;
    case MAV_SEVERITY_DEBUG:
Lorenz Meier's avatar
Lorenz Meier committed
138
        severityText = QString(tr(" Debug:"));
139 140
        break;
    default:
Lorenz Meier's avatar
Lorenz Meier committed
141
        severityText = QString(tr(""));
142
        break;
143 144
    }

145
    // Finally append the properly-styled text with a timestamp.
146
    QString dateString = QDateTime::currentDateTime().toString("hh:mm:ss.zzz");
147
    msgWidget->appendHtml(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));
148

149
    // Ensure text area scrolls correctly
150 151
    scroller->setValue(scroller->maximum());
    msgWidget->setUpdatesEnabled(true);
Lorenz Meier's avatar
Lorenz Meier committed
152
}