73 lines
2.2 KiB
C++
73 lines
2.2 KiB
C++
#include "logpage.h"
|
|||
|
|
#include "ui_logpage.h"
|
||
|
|
|
||
|
|
#include "../models/logrecord.h"
|
||
|
|
#include "../repositories/logrepository.h"
|
||
|
|
|
||
|
|
#include <QAbstractItemView>
|
||
|
|
#include <QDateTime>
|
||
|
|
#include <QDebug>
|
||
|
|
#include <QHeaderView>
|
||
|
|
#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() << "时间" << "类型" << "模块" << "动作" << "内容" << "状态");
|
||
|
|
ui->logTable->horizontalHeader()->setStretchLastSection(true);
|
||
|
|
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) {
|
||
|
|
ui->logTable->setItem(0, col, new QTableWidgetItem(values.at(col)));
|
||
|
|
}
|
||
|
|
}
|