UASMessageView.cc 3.33 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
/*=====================================================================

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/>.

======================================================================*/

24
#include <QMenu>
25
#include <QScrollBar>
26

27
#include "UASMessageView.h"
28
#include "QGCUnconnectedInfoWidget.h"
29
#include "ui_UASMessageView.h"
30

31
/*-------------------------------------------------------------------------------------
32
  UASMessageView
33 34
-------------------------------------------------------------------------------------*/

35
UASMessageView::UASMessageView(QWidget *parent) :
36
    QWidget(parent),
37
    _ui(new Ui::UASMessageView)
38
{
39 40
    _ui->setupUi(this);
}
41

42
UASMessageView::~UASMessageView()
43 44 45
{
    delete _ui;
}
46

47
/*-------------------------------------------------------------------------------------
48
  UASMessageViewWidget
49
-------------------------------------------------------------------------------------*/
50

51
UASMessageViewWidget::UASMessageViewWidget(UASMessageHandler* uasMessageHandler, QWidget *parent)
52
    : UASMessageView(parent)
53
    , _unconnectedWidget(NULL)
54
    , _uasMessageHandler(uasMessageHandler)
55 56
{
    setStyleSheet("QPlainTextEdit { border: 0px }");
Don Gagne's avatar
Don Gagne committed
57

58
    // Enable the right-click menu for the text editor. This works because the plainTextEdit
59
    // widget has its context menu policy set to its actions list. So any actions we add
60 61
    // to this widget's action list will be automatically displayed.
    // We only have the clear action right now.
62 63
    QAction* clearAction = new QAction(tr("Clear Messages"), this);
    connect(clearAction, &QAction::triggered, this, &UASMessageViewWidget::clearMessages);
64 65
    ui()->plainTextEdit->addAction(clearAction);
    // Connect message handler
66
    connect(_uasMessageHandler, &UASMessageHandler::textMessageReceived, this, &UASMessageViewWidget::handleTextMessage);
67 68
}

69
UASMessageViewWidget::~UASMessageViewWidget()
70
{
71

72 73
}

74 75 76
void UASMessageViewWidget::clearMessages()
{
    ui()->plainTextEdit->clear();
77
    _uasMessageHandler->clearMessages();
78 79
}

80
void UASMessageViewWidget::handleTextMessage(UASMessage *message)
81
{
82 83 84 85 86 87 88 89 90 91 92 93
    // 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();
        msgWidget->appendHtml(message->getFormatedText());
        // Ensure text area scrolls correctly
        scroller->setValue(scroller->maximum());
        msgWidget->setUpdatesEnabled(true);
94
    }
95
}