/*===================================================================== QGroundControl Open Source Ground Control Station (c) 2009 - 2011 QGROUNDCONTROL PROJECT 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 . ======================================================================*/ #include #include #include "UASMessageView.h" #include "QGCUnconnectedInfoWidget.h" #include "ui_UASMessageView.h" /*------------------------------------------------------------------------------------- UASMessageView -------------------------------------------------------------------------------------*/ UASMessageView::UASMessageView(QWidget *parent) : QWidget(parent), _ui(new Ui::UASMessageView) { _ui->setupUi(this); } UASMessageView::~UASMessageView() { delete _ui; } /*------------------------------------------------------------------------------------- UASMessageViewWidget -------------------------------------------------------------------------------------*/ UASMessageViewWidget::UASMessageViewWidget(UASMessageHandler* uasMessageHandler, QWidget *parent) : UASMessageView(parent) , _unconnectedWidget(NULL) , _uasMessageHandler(uasMessageHandler) { setStyleSheet("QPlainTextEdit { border: 0px }"); // Construct initial widget _unconnectedWidget = new QGCUnconnectedInfoWidget(this); ui()->horizontalLayout->addWidget(_unconnectedWidget); ui()->plainTextEdit->hide(); // Enable the right-click menu for the text editor. This works because the plainTextEdit // widget has its context menu policy set to its actions list. So any actions we add // to this widget's action list will be automatically displayed. // We only have the clear action right now. QAction* clearAction = new QAction(tr("Clear Messages"), this); connect(clearAction, &QAction::triggered, this, &UASMessageViewWidget::clearMessages); ui()->plainTextEdit->addAction(clearAction); // Connect message handler connect(_uasMessageHandler, &UASMessageHandler::textMessageReceived, this, &UASMessageViewWidget::handleTextMessage); } UASMessageViewWidget::~UASMessageViewWidget() { } void UASMessageViewWidget::clearMessages() { ui()->plainTextEdit->clear(); _uasMessageHandler->clearMessages(); } void UASMessageViewWidget::handleTextMessage(UASMessage *message) { // Reset if(!message) { ui()->plainTextEdit->clear(); _unconnectedWidget->show(); ui()->plainTextEdit->hide(); } else { // Make sure the UI is configured for showing messages. // Note that this call is NOT equivalent to `_unconnectedWidget->isVisible()`. if (!_unconnectedWidget->isHidden()) { _unconnectedWidget->hide(); ui()->plainTextEdit->show(); } 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); } }