From a26169d4bcff370b7ac1d7c119c4f43a70540ccf Mon Sep 17 00:00:00 2001 From: Bryant Date: Thu, 20 Feb 2014 13:54:26 -0800 Subject: [PATCH] QGCMessageView: Extensive code cleanup and bug fixing. Right-click menu works cleaner. Coloring of output text is correct. Output text now handles all severities of messages. --- src/ui/uas/QGCMessageView.cc | 145 +++++++++++++++++++++++++---------- src/ui/uas/QGCMessageView.h | 12 +-- src/ui/uas/QGCMessageView.ui | 47 +++++------- 3 files changed, 127 insertions(+), 77 deletions(-) diff --git a/src/ui/uas/QGCMessageView.cc b/src/ui/uas/QGCMessageView.cc index 5c583e0e2f..2bf8963d3c 100644 --- a/src/ui/uas/QGCMessageView.cc +++ b/src/ui/uas/QGCMessageView.cc @@ -1,95 +1,158 @@ -#include "QGCMessageView.h" - #include #include - +#include "QGCMessageView.h" #include "GAudioOutput.h" #include "QGCUnconnectedInfoWidget.h" #include "UASManager.h" #include "ui_QGCMessageView.h" - QGCMessageView::QGCMessageView(QWidget *parent) : QWidget(parent), activeUAS(NULL), - clearAction(new QAction(tr("Clear Text"), this)), ui(new Ui::QGCMessageView) { - setObjectName("QUICKVIEW_MESSAGE_CONSOLE"); + setObjectName("QUICKVIEW_MESSAGE_CONSOLE") ; ui->setupUi(this); - setStyleSheet("QScrollArea { border: 0px; } QPlainTextEdit { border: 0px }"); + setStyleSheet("QPlainTextEdit { border: 0px }"); // Construct initial widget connectWidget = new QGCUnconnectedInfoWidget(this); ui->horizontalLayout->addWidget(connectWidget); ui->plainTextEdit->hide(); + // Enable the right-click menu for the text editor. This works because the plainTextEdit + // widget has its context menu policy set to its actions list. So we any actions we add + // 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. setActiveUAS(UASManager::instance()->getActiveUAS()); connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setActiveUAS(UASInterface*))); } QGCMessageView::~QGCMessageView() { + // 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. 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))); + // 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))); 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())); + activeUAS = NULL; } - connect(uas, SIGNAL(textMessageReceived(int,int,int,QString)), this, SLOT(handleTextMessage(int,int,int,QString))); - activeUAS = uas; + // 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(); + } } void QGCMessageView::handleTextMessage(int uasid, int compId, int severity, QString text) { - // XXX color messages according to severity - QPlainTextEdit *msgWidget = ui->plainTextEdit; - //turn off updates while we're appending content to avoid breaking the autoscroll behavior + // Turn off updates while we're appending content to avoid breaking the autoscroll behavior msgWidget->setUpdatesEnabled(false); QScrollBar *scroller = msgWidget->verticalScrollBar(); + // Get all the UAS info. UASInterface *uas = UASManager::instance()->getUASForId(uasid); 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? + + // 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. + // 2: If we have a warning or notice, just make it bold. + // 3: Otherwise color it the standard color. + + // 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: + // TODO: Move this audio output to UAS.cc, as it doesn't make sense to put audio output in a message logger widget. GAudioOutput::instance()->say(text.toLower()); - msgWidget->appendHtml(QString("

[%1:%2] %3

").arg(uasName).arg(compId).arg(text)); + style = QString("color:#DC143C;font-size:large;font-weight:bold"); + break; + case MAV_SEVERITY_WARNING: + case MAV_SEVERITY_NOTICE: + style = QString("color:%1;font-weight:bold").arg(colorName); + break; + default: + style = QString("color:%1;").arg(colorName); + break; } - else { - msgWidget->appendHtml(QString("

[%2:%3] %4

").arg(colorName).arg(uasName).arg(compId).arg(text)); + + // And determine the text for the severities. + 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("Unknown")); + break; } + // Finally append the properly-styled text. + msgWidget->appendHtml(QString("

[%2:%3] %4 - %5

").arg(style).arg(uasName).arg(compId).arg(severityText).arg(text)); + qDebug() << msgWidget->document()->toHtml(); + // Ensure text area scrolls correctly scroller->setValue(scroller->maximum()); msgWidget->setUpdatesEnabled(true); - -} - -void QGCMessageView::contextMenuEvent(QContextMenuEvent* event) -{ - if(activeUAS) { - QMenu menu(this); - menu.addAction(clearAction); - menu.exec(event->globalPos()); - } -} +} \ No newline at end of file diff --git a/src/ui/uas/QGCMessageView.h b/src/ui/uas/QGCMessageView.h index 67c69ce74c..6504939060 100644 --- a/src/ui/uas/QGCMessageView.h +++ b/src/ui/uas/QGCMessageView.h @@ -34,17 +34,11 @@ public slots: */ void handleTextMessage(int uasid, int componentid, int severity, QString text); - /** - * @brief Hand context menu event - * @param event - */ - virtual void contextMenuEvent(QContextMenuEvent* event); - protected: + // Stores the UAS that we're currently receiving messages from. UASInterface* activeUAS; - QVBoxLayout* initialLayout; - QGCUnconnectedInfoWidget *connectWidget; - QAction* clearAction; + // Stores the connect widget that is displayed when no UAS is active. + QGCUnconnectedInfoWidget* connectWidget; private: Ui::QGCMessageView *ui; diff --git a/src/ui/uas/QGCMessageView.ui b/src/ui/uas/QGCMessageView.ui index 069ee2f02e..2c3b60defd 100644 --- a/src/ui/uas/QGCMessageView.ui +++ b/src/ui/uas/QGCMessageView.ui @@ -6,14 +6,17 @@ 0 0 - 305 - 283 + 248 + 249 + + Qt::NoContextMenu + Form - + 0 @@ -27,32 +30,22 @@ 0 - - + + + Qt::ActionsContextMenu + + + false + + + false + + true - - - - 0 - 0 - 303 - 273 - - - - - 0 - - - - - true - - - - - + + + -- GitLab