UASMessageView.cc 3.01 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
9 10


11
#include <QMenu>
12
#include <QScrollBar>
13

14
#include "UASMessageView.h"
15
#include "QGCUnconnectedInfoWidget.h"
16
#include "ui_UASMessageView.h"
17

18
/*-------------------------------------------------------------------------------------
19
  UASMessageView
20 21
-------------------------------------------------------------------------------------*/

22
UASMessageView::UASMessageView(QWidget *parent) :
23
    QWidget(parent),
24
    _ui(new Ui::UASMessageView)
25
{
26 27
    _ui->setupUi(this);
}
28

29
UASMessageView::~UASMessageView()
30 31 32
{
    delete _ui;
}
33

34
/*-------------------------------------------------------------------------------------
35
  UASMessageViewWidget
36
-------------------------------------------------------------------------------------*/
37

38
UASMessageViewWidget::UASMessageViewWidget(UASMessageHandler* uasMessageHandler, QWidget *parent)
39
    : UASMessageView(parent)
40
    , _unconnectedWidget(NULL)
41
    , _uasMessageHandler(uasMessageHandler)
42 43
{
    setStyleSheet("QPlainTextEdit { border: 0px }");
Don Gagne's avatar
Don Gagne committed
44

45
    // Enable the right-click menu for the text editor. This works because the plainTextEdit
46
    // widget has its context menu policy set to its actions list. So any actions we add
47 48
    // to this widget's action list will be automatically displayed.
    // We only have the clear action right now.
49 50
    QAction* clearAction = new QAction(tr("Clear Messages"), this);
    connect(clearAction, &QAction::triggered, this, &UASMessageViewWidget::clearMessages);
51 52
    ui()->plainTextEdit->addAction(clearAction);
    // Connect message handler
53
    connect(_uasMessageHandler, &UASMessageHandler::textMessageReceived, this, &UASMessageViewWidget::handleTextMessage);
54 55
}

56
UASMessageViewWidget::~UASMessageViewWidget()
57
{
58

59 60
}

61 62 63
void UASMessageViewWidget::clearMessages()
{
    ui()->plainTextEdit->clear();
64
    _uasMessageHandler->clearMessages();
65 66
}

67
void UASMessageViewWidget::handleTextMessage(UASMessage *message)
68
{
69 70 71 72 73 74 75 76
    // Reset
    if(!message) {
        ui()->plainTextEdit->clear();
    } else {
        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();
dogmaphobic's avatar
dogmaphobic committed
77 78 79 80 81
        QString messages = message->getFormatedText();
        messages = messages.replace("<#E>", "color: #f95e5e; font: monospace;");
        messages = messages.replace("<#I>", "color: #f9b55e; font: monospace;");
        messages = messages.replace("<#N>", "color: #ffffff; font: monospace;");
        msgWidget->appendHtml(messages);
82 83 84
        // Ensure text area scrolls correctly
        scroller->setValue(scroller->maximum());
        msgWidget->setUpdatesEnabled(true);
85
    }
86
}