UASMessageView.cc 3.88 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 57 58 59 60
{
    setStyleSheet("QPlainTextEdit { border: 0px }");
    // Construct initial widget
    _unconnectedWidget = new QGCUnconnectedInfoWidget(this);
    ui()->horizontalLayout->addWidget(_unconnectedWidget);
    ui()->plainTextEdit->hide();
61
    // Enable the right-click menu for the text editor. This works because the plainTextEdit
62
    // widget has its context menu policy set to its actions list. So any actions we add
63 64
    // to this widget's action list will be automatically displayed.
    // We only have the clear action right now.
65 66
    QAction* clearAction = new QAction(tr("Clear Messages"), this);
    connect(clearAction, &QAction::triggered, this, &UASMessageViewWidget::clearMessages);
67 68
    ui()->plainTextEdit->addAction(clearAction);
    // Connect message handler
69
    connect(_uasMessageHandler, &UASMessageHandler::textMessageReceived, this, &UASMessageViewWidget::handleTextMessage);
70 71
}

72
UASMessageViewWidget::~UASMessageViewWidget()
73
{
74

75 76
}

77 78 79
void UASMessageViewWidget::clearMessages()
{
    ui()->plainTextEdit->clear();
80
    _uasMessageHandler->clearMessages();
81 82
}

83
void UASMessageViewWidget::handleTextMessage(UASMessage *message)
84
{
85 86 87 88 89 90
    // Reset
    if(!message) {
        ui()->plainTextEdit->clear();
        _unconnectedWidget->show();
        ui()->plainTextEdit->hide();
    } else {
91
        // Make sure the UI is configured for showing messages.
92 93
        // Note that this call is NOT equivalent to `_unconnectedWidget->isVisible()`.
        if (!_unconnectedWidget->isHidden())
94
        {
95 96
            _unconnectedWidget->hide();
            ui()->plainTextEdit->show();
97
        }
98 99 100 101 102 103 104 105
        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);
106
    }
107
}