UASMessageView.cc 5.69 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 30
#include "UASMessageHandler.h"
#include "ui_UASMessageView.h"
31

32
/*-------------------------------------------------------------------------------------
33
  UASMessageView
34 35
-------------------------------------------------------------------------------------*/

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

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

48
/*-------------------------------------------------------------------------------------
49
  UASMessageViewWidget
50
-------------------------------------------------------------------------------------*/
51

52 53
UASMessageViewWidget::UASMessageViewWidget(QWidget *parent)
    : UASMessageView(parent)
54 55 56 57 58 59 60
    , _unconnectedWidget(NULL)
{
    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::instance(), &UASMessageHandler::textMessageReceived, this, &UASMessageViewWidget::handleTextMessage);
70 71
}

72
UASMessageViewWidget::~UASMessageViewWidget()
73
{
74

75 76
}

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

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 108
}

109
/*-------------------------------------------------------------------------------------
110
  UASMessageViewRollDown
111 112
-------------------------------------------------------------------------------------*/

dogmaphobic's avatar
dogmaphobic committed
113
UASMessageViewRollDown::UASMessageViewRollDown(QWidget *parent)
114
    : UASMessageView(parent)
115
{
116 117
    setAttribute(Qt::WA_TranslucentBackground);
    setStyleSheet("background-color: rgba(0%,0%,0%,80%); border: 2px;");
118 119
    QPlainTextEdit *msgWidget = ui()->plainTextEdit;
    // Init Messages
120
    UASMessageHandler::instance()->lockAccess();
121
    msgWidget->setUpdatesEnabled(false);
122
    QVector<UASMessage*> messages = UASMessageHandler::instance()->messages();
123 124 125
    for(int i = 0; i < messages.count(); i++) {
        msgWidget->appendHtml(messages.at(i)->getFormatedText());
    }
126
    QScrollBar *scroller = msgWidget->verticalScrollBar();
127 128
    scroller->setValue(scroller->maximum());
    msgWidget->setUpdatesEnabled(true);
129
    connect(UASMessageHandler::instance(), &UASMessageHandler::textMessageReceived, this, &UASMessageViewRollDown::handleTextMessage);
130
    UASMessageHandler::instance()->unlockAccess();
131
}
132

133
UASMessageViewRollDown::~UASMessageViewRollDown()
134
{
135

136
}
137

138
void UASMessageViewRollDown::handleTextMessage(UASMessage *message)
139 140
{
    // Reset
141
    if(!message) {
142 143 144 145 146 147 148 149 150 151 152 153
        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);
    }
}
154

dogmaphobic's avatar
dogmaphobic committed
155
void UASMessageViewRollDown::leaveEvent(QEvent*)
156
{
dogmaphobic's avatar
dogmaphobic committed
157
    emit closeWindow();
158
    close();
Lorenz Meier's avatar
Lorenz Meier committed
159
}