115 lines
3.4 KiB
C++
115 lines
3.4 KiB
C++
#include "mainwindow.h"
|
||
|
||
#include <QFileDialog>
|
||
#include <QFont>
|
||
#include <QHBoxLayout>
|
||
#include <QMessageBox>
|
||
#include <QPushButton>
|
||
#include <QTextEdit>
|
||
#include <QVBoxLayout>
|
||
#include <QWidget>
|
||
#include <ctime>
|
||
|
||
#include "utils/logger.h"
|
||
|
||
void MainWindow::setupLogsTab() {
|
||
logsTab_ = new QWidget(this);
|
||
auto* layout = new QVBoxLayout(logsTab_);
|
||
|
||
// 创建工具栏
|
||
auto* toolBar = new QHBoxLayout();
|
||
logRefreshButton_ = new QPushButton(tr("刷新"), logsTab_);
|
||
logClearButton_ = new QPushButton(tr("清空"), logsTab_);
|
||
logExportButton_ = new QPushButton(tr("导出"), logsTab_);
|
||
|
||
toolBar->addWidget(logRefreshButton_);
|
||
toolBar->addWidget(logClearButton_);
|
||
toolBar->addWidget(logExportButton_);
|
||
toolBar->addStretch(1);
|
||
layout->addLayout(toolBar);
|
||
|
||
// 创建日志显示区域
|
||
logDisplay_ = new QTextEdit(logsTab_);
|
||
logDisplay_->setReadOnly(true);
|
||
logDisplay_->setFont(QFont("Courier", 9));
|
||
layout->addWidget(logDisplay_);
|
||
|
||
// 连接按钮信号
|
||
connect(logRefreshButton_, &QPushButton::clicked, this, &MainWindow::refreshLogDisplay);
|
||
connect(logClearButton_, &QPushButton::clicked, this, &MainWindow::clearLogs);
|
||
connect(logExportButton_, &QPushButton::clicked, this, &MainWindow::exportLogs);
|
||
|
||
// 初始化日志显示
|
||
refreshLogDisplay();
|
||
}
|
||
|
||
void MainWindow::refreshLogDisplay() {
|
||
logDisplay_->clear();
|
||
const auto& entries = logger_.getEntries();
|
||
|
||
QString content;
|
||
for (const auto& entry : entries) {
|
||
content += QString::fromStdString(entry.format()) + "\n";
|
||
}
|
||
|
||
if (content.isEmpty()) {
|
||
logDisplay_->setText(tr("暂无日志记录"));
|
||
} else {
|
||
logDisplay_->setText(content);
|
||
// 滚动到最后
|
||
QTextCursor cursor = logDisplay_->textCursor();
|
||
cursor.movePosition(QTextCursor::End);
|
||
logDisplay_->setTextCursor(cursor);
|
||
}
|
||
}
|
||
|
||
void MainWindow::clearLogs() {
|
||
QMessageBox::StandardButton reply = QMessageBox::question(
|
||
this,
|
||
tr("清空日志"),
|
||
tr("确定要清空所有日志记录吗?此操作无法撤销。"),
|
||
QMessageBox::Yes | QMessageBox::No
|
||
);
|
||
|
||
if (reply == QMessageBox::Yes) {
|
||
logger_.clear();
|
||
refreshLogDisplay();
|
||
QMessageBox::information(this, tr("清空完成"), tr("所有日志已清空"));
|
||
}
|
||
}
|
||
|
||
void MainWindow::exportLogs() {
|
||
QString fileName = QFileDialog::getSaveFileName(
|
||
this,
|
||
tr("导出日志"),
|
||
dataFolder_ + "/logs_" + QString::number(std::time(nullptr)) + ".txt",
|
||
tr("文本文件 (*.txt);;所有文件 (*)")
|
||
);
|
||
|
||
if (!fileName.isEmpty()) {
|
||
bool success = logger_.exportToFile(fileName.toStdString());
|
||
if (success) {
|
||
QMessageBox::information(
|
||
this,
|
||
tr("导出成功"),
|
||
tr("日志已成功导出到:\n") + fileName
|
||
);
|
||
} else {
|
||
QMessageBox::warning(
|
||
this,
|
||
tr("导出失败"),
|
||
tr("无法写入文件")
|
||
);
|
||
}
|
||
}
|
||
}
|
||
|
||
void MainWindow::logOperation(LogEntryType type, const std::string& operation,
|
||
const std::string& details, const std::string& objectId) {
|
||
logger_.log(type, operation, details, objectId);
|
||
// 如果当前在日志页面,自动刷新显示
|
||
if (navList_->currentRow() == 6) { // 日志页是第7个(0-based索引为6)
|
||
refreshLogDisplay();
|
||
}
|
||
}
|