QGCMessageView.cc 2.91 KB
Newer Older
1 2 3
#include "QGCMessageView.h"

#include <QMenu>
4
#include <QScrollBar>
5

6 7 8 9 10 11 12

#include "GAudioOutput.h"
#include "QGCUnconnectedInfoWidget.h"
#include "UASManager.h"
#include "ui_QGCMessageView.h"


13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
QGCMessageView::QGCMessageView(QWidget *parent) :
    QWidget(parent),
    activeUAS(NULL),
    clearAction(new QAction(tr("Clear Text"), this)),
    ui(new Ui::QGCMessageView)
{
    setObjectName("QUICKVIEW_MESSAGE_CONSOLE");

    ui->setupUi(this);
    setStyleSheet("QScrollArea { border: 0px; } QPlainTextEdit { border: 0px }");

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

29
    setActiveUAS(UASManager::instance()->getActiveUAS());
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setActiveUAS(UASInterface*)));
}

QGCMessageView::~QGCMessageView()
{
    delete ui;
}

void QGCMessageView::setActiveUAS(UASInterface* uas)
{
    if (!uas)
        return;

    if (activeUAS) {
        disconnect(uas, SIGNAL(textMessageReceived(int,int,int,QString)), this, SLOT(handleTextMessage(int,int,int,QString)));
        ui->plainTextEdit->clear();
    } else {

        // First time UI setup, clear layout
        ui->horizontalLayout->removeWidget(connectWidget);
        connectWidget->deleteLater();
        ui->plainTextEdit->show();

        connect(clearAction, SIGNAL(triggered()), ui->plainTextEdit, SLOT(clear()));
    }

    connect(uas, SIGNAL(textMessageReceived(int,int,int,QString)), this, SLOT(handleTextMessage(int,int,int,QString)));
    activeUAS = uas;
}

60
void QGCMessageView::handleTextMessage(int uasid, int compId, int severity, QString text)
61 62 63
{
    // XXX color messages according to severity

64 65 66 67 68 69 70
    QPlainTextEdit *msgWidget = ui->plainTextEdit;

    //turn off updates while we're appending content to avoid breaking the autoscroll behavior
    msgWidget->setUpdatesEnabled(false);
    QScrollBar *scroller = msgWidget->verticalScrollBar();

    UASInterface *uas = UASManager::instance()->getUASForId(uasid);
71 72 73 74
    QString uasName(uas->getUASName());
    QString colorName(uas->getColor().name());
    //change styling based on severity
    if (160 == severity ) { //TODO where is the constant for "critical" severity?
75 76
        GAudioOutput::instance()->say(text.toLower());
        msgWidget->appendHtml(QString("<p style=\"color:#DC143C;background-color:#FFFACD;font-size:large;font-weight:bold\">[%1:%2] %3</p>").arg(uasName).arg(compId).arg(text));
77 78 79 80 81
    }
    else {
        msgWidget->appendHtml(QString("<p style=\"color:%1;font-size:smaller\">[%2:%3] %4</p>").arg(colorName).arg(uasName).arg(compId).arg(text));
    }

82
    // Ensure text area scrolls correctly
83 84 85
    scroller->setValue(scroller->maximum());
    msgWidget->setUpdatesEnabled(true);

86 87 88 89 90 91 92 93
}

void QGCMessageView::contextMenuEvent(QContextMenuEvent* event)
{
    QMenu menu(this);
    menu.addAction(clearAction);
    menu.exec(event->globalPos());
}