XNSim/XNMonitor/DebugWidget/DebugWidget.cpp

169 lines
4.3 KiB
C++
Raw Normal View History

2025-04-28 12:25:20 +08:00
/**
* @file DebugWidget.cpp
* @author jinchao
* @brief
* @version 1.0
* @date 2025-03-10
*
* @copyright Copyright (c) 2025 COMAC
*
*/
#include "DebugWidget.h"
#include <QDateTime>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
DebugWidget::DebugWidget(QWidget *parent)
: QWidget(parent), m_textEdit(nullptr), m_clearButton(nullptr), m_saveButton(nullptr),
m_layout(nullptr), m_showInfoCheck(nullptr), m_showErrorCheck(nullptr),
m_showWarningCheck(nullptr)
{
//设置窗口标题
setWindowTitle(tr("调试信息"));
//设置UI
setupUI();
}
DebugWidget::~DebugWidget()
{
}
void DebugWidget::setupUI()
{
//创建布局
m_layout = new QVBoxLayout(this);
//创建过滤选项
QHBoxLayout *filterLayout = new QHBoxLayout();
//创建信息复选框
m_showInfoCheck = new QCheckBox(tr("信息"), this);
m_showErrorCheck = new QCheckBox(tr("错误"), this);
m_showWarningCheck = new QCheckBox(tr("警告"), this);
// 默认全部选中
m_showInfoCheck->setChecked(true);
m_showErrorCheck->setChecked(true);
m_showWarningCheck->setChecked(true);
//添加过滤选项
filterLayout->addWidget(m_showInfoCheck);
filterLayout->addWidget(m_showErrorCheck);
filterLayout->addWidget(m_showWarningCheck);
filterLayout->addStretch();
//添加过滤选项到布局
m_layout->addLayout(filterLayout);
//创建文本显示区域
m_textEdit = new QTextEdit(this);
//设置文本显示区域为只读
m_textEdit->setReadOnly(true);
//添加文本显示区域到布局
m_layout->addWidget(m_textEdit);
// 创建按钮
m_clearButton = new QPushButton(tr("清除日志"), this);
m_saveButton = new QPushButton(tr("保存日志"), this);
//创建按钮布局
QHBoxLayout *buttonLayout = new QHBoxLayout();
//添加清除按钮到按钮布局
buttonLayout->addWidget(m_clearButton);
//添加保存按钮到按钮布局
buttonLayout->addWidget(m_saveButton);
//添加伸缩项到按钮布局
buttonLayout->addStretch();
//添加按钮布局到布局
m_layout->addLayout(buttonLayout);
//连接信号和槽
connect(m_clearButton, &QPushButton::clicked, this, &DebugWidget::clearLog);
connect(m_saveButton, &QPushButton::clicked, this, &DebugWidget::saveLog);
}
bool DebugWidget::shouldShowMessageType(int type) const
{
//根据类型判断是否显示
switch (type) {
case 0:
return m_showInfoCheck->isChecked();
case 1:
return m_showErrorCheck->isChecked();
case 2:
return m_showWarningCheck->isChecked();
default:
return false;
}
}
void DebugWidget::appendMessage(const QString &message)
{
//获取当前时间
QString timeStamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
//添加消息到文本显示区域
m_textEdit->append(QString("[%1] %2").arg(timeStamp).arg(message));
}
void DebugWidget::appendError(const QString &error)
{
//获取当前时间
QString timeStamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
//添加错误消息到文本显示区域
m_textEdit->append(
QString("<font color='red'>[%1] ERROR: %2</font>").arg(timeStamp).arg(error));
}
void DebugWidget::appendWarning(const QString &warning)
{
//获取当前时间
QString timeStamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
//添加警告消息到文本显示区域
m_textEdit->append(
QString("<font color='orange'>[%1] WARNING: %2</font>").arg(timeStamp).arg(warning));
}
void DebugWidget::clearLog()
{
//清除文本显示区域
m_textEdit->clear();
}
void DebugWidget::saveLog()
{
//获取保存文件名
QString fileName = QFileDialog::getSaveFileName(
this, tr("保存日志"), QString(), tr("日志文件 (*.log);;文本文件 (*.txt);;所有文件 (*.*)"));
//如果文件名不为空
if (fileName.isEmpty()) {
return;
}
//创建文件
QFile file(fileName);
//如果文件打开失败
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
return;
}
//创建文本流
QTextStream out(&file);
//写入文本
out << m_textEdit->toPlainText();
//关闭文件
file.close();
}
void DebugWidget::onSendDebugMessage(int type, const QString &message)
{
if (!shouldShowMessageType(type)) {
return;
}
//根据类型添加消息
switch (type) {
case 0:
appendMessage(message);
break;
case 1:
appendError(message);
break;
case 2:
appendWarning(message);
break;
default:
break;
}
}