UASMessageView.cc 5.71 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

dogmaphobic's avatar
dogmaphobic committed
27
#include "MainToolBar.h"
28
#include "UASMessageView.h"
29
#include "QGCUnconnectedInfoWidget.h"
30 31
#include "UASMessageHandler.h"
#include "ui_UASMessageView.h"
32

33
/*-------------------------------------------------------------------------------------
34
  UASMessageView
35 36
-------------------------------------------------------------------------------------*/

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

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

49
/*-------------------------------------------------------------------------------------
50
  UASMessageViewWidget
51
-------------------------------------------------------------------------------------*/
52

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

73
UASMessageViewWidget::~UASMessageViewWidget()
74
{
75

76 77
}

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

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

110
/*-------------------------------------------------------------------------------------
111
  UASMessageViewRollDown
112 113
-------------------------------------------------------------------------------------*/

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

134
UASMessageViewRollDown::~UASMessageViewRollDown()
135
{
136

137
}
138

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

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