80 lines
2.5 KiB
C++
80 lines
2.5 KiB
C++
#include "logpage.h"
|
|
#include "ui_logpage.h"
|
|
#include "uihelpers.h"
|
|
|
|
#include "../models/logrecord.h"
|
|
#include "../repositories/logrepository.h"
|
|
|
|
#include <QAbstractItemView>
|
|
#include <QDateTime>
|
|
#include <QDebug>
|
|
#include <QTableWidget>
|
|
#include <QTableWidgetItem>
|
|
|
|
LogPage::LogPage(QWidget *parent)
|
|
: QWidget(parent)
|
|
, ui(new Ui::LogPage)
|
|
{
|
|
ui->setupUi(this);
|
|
|
|
ui->logTable->setColumnCount(6);
|
|
ui->logTable->setHorizontalHeaderLabels(QStringList() << "时间" << "类型" << "模块" << "动作" << "内容" << "状态");
|
|
const int dateTimeWidth = UiHelpers::textColumnWidth(
|
|
ui->logTable, "2026-10-30 23:59:59");
|
|
UiHelpers::configureTableColumns(
|
|
ui->logTable,
|
|
QList<int>() << dateTimeWidth << 100 << 100 << 110 << 360 << 80,
|
|
4, 60);
|
|
ui->logTable->verticalHeader()->setVisible(false);
|
|
ui->logTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
ui->logTable->setAlternatingRowColors(true);
|
|
|
|
loadLogs();
|
|
}
|
|
|
|
LogPage::~LogPage()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void LogPage::addLog(const QString &type, const QString &module, const QString &action,
|
|
const QString &content, const QString &status)
|
|
{
|
|
addLogRow(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"),
|
|
type, module, action, content, status);
|
|
}
|
|
|
|
void LogPage::loadLogs()
|
|
{
|
|
QString error;
|
|
LogRepository repository;
|
|
const QList<LogRecord> records = repository.listLogs(&error);
|
|
if (!error.isEmpty()) {
|
|
qWarning() << "Failed to load operation logs:" << error;
|
|
return;
|
|
}
|
|
|
|
ui->logTable->setRowCount(0);
|
|
for (int index = records.size() - 1; index >= 0; --index) {
|
|
const LogRecord &record = records.at(index);
|
|
const QString time = record.createdAt.isValid()
|
|
? record.createdAt.toString("yyyy-MM-dd HH:mm:ss")
|
|
: "-";
|
|
addLogRow(time, record.type, record.module, record.action,
|
|
record.content, record.status);
|
|
}
|
|
}
|
|
|
|
void LogPage::addLogRow(const QString &time, const QString &type, const QString &module,
|
|
const QString &action, const QString &content, const QString &status)
|
|
{
|
|
ui->logTable->insertRow(0);
|
|
const QStringList values = QStringList()
|
|
<< time << type << module << action << content << status;
|
|
for (int col = 0; col < values.size(); ++col) {
|
|
QTableWidgetItem *item = new QTableWidgetItem(values.at(col));
|
|
if (col == 4) item->setTextAlignment(Qt::AlignCenter);
|
|
ui->logTable->setItem(0, col, item);
|
|
}
|
|
}
|