Simple UserOperationLog table.

This commit is contained in:
Krad
2021-11-23 13:22:00 +08:00
parent b31bcbcf63
commit 6247aae95e
10 changed files with 253 additions and 22 deletions

View File

@@ -11,6 +11,7 @@
#include <QListWidgetItem>
#include "ui_tabformwidget.h"
#include <QLabel>
#include "UserOperationLogForm.h"
AdminSettingForm::AdminSettingForm(QWidget *parent, Qt::WindowFlags f) : TabFormWidget(parent) {
@@ -19,7 +20,7 @@ AdminSettingForm::AdminSettingForm(QWidget *parent, Qt::WindowFlags f) : TabForm
QListWidget* widget = new QListWidget(this);
widget->setFixedWidth(250);
QStringList menus;
menus<<tr("Account Manage")<<tr("System Setting")<<"System Information"<<tr("About");
menus<<tr("Account Manage")<<tr("System Setting")<<tr("System Information")<<tr("Operation Log")<<tr("About");
widget->addItems(menus);
widget->setSpacing(3);
for (int i = 0; i < menus.count(); ++i) {
@@ -46,6 +47,10 @@ AdminSettingForm::AdminSettingForm(QWidget *parent, Qt::WindowFlags f) : TabForm
Info->setText("info");
stackedWidget->addWidget(Info);
UserOperationLogForm* operationLogForm = new UserOperationLogForm(this);
stackedWidget->addWidget(operationLogForm);
QLabel* about = new QLabel(this);
about->setText("About");
stackedWidget->addWidget(about);

View File

@@ -25,22 +25,22 @@
SelectFormWidget::SelectFormWidget(QWidget *parent) :
TabFormWidget(parent)
{
const char* style="QHeaderView::section{background-color:#595959;"
" min-height:50px;max-height:50px;"
"font-weight:Bold; font-size:16px; border:1px solid #323232;}"
"QHeaderView::section:horizontal{border-bottom: 1px solid rgb(0,170,255);}"
"QHeaderView::section:vertical{min-height:36px;max-height:36px;}"
"QWidget#edit_patient{min-width:300px;max-width:300px;}"
"QTableView{border:none}"
"QTableView{alternate-background-color: #595959;selection-color:white;selection-background-color:#0078d8}"
"QToolButton#sexBtn{min-width:120px;max-width:120px;font-size:20px;padding:2px;}"
"QToolButton#sexBtn:disabled{color:silver}"
"QWidget#sexpanelwidget{border:1px solid silver;}"
"QWidget#sexpanelwidget:enabled{background-color: #515151;}"
"QToolButton#sexBtn:checked{border:2px solid darkorange;padding:0px;}"
;
this->setStyleSheet(this->styleSheet().append(style));
// const char* style="QHeaderView::section{background-color:#595959;"
// " min-height:50px;max-height:50px;"
// "font-weight:Bold; font-size:16px; border:1px solid #323232;}"
// "QHeaderView::section:horizontal{border-bottom: 1px solid rgb(0,170,255);}"
// "QHeaderView::section:vertical{min-height:36px;max-height:36px;}"
// "QWidget#edit_patient{min-width:300px;max-width:300px;}"
// "QTableView{border:none}"
// "QTableView{alternate-background-color: #595959;selection-color:white;selection-background-color:#0078d8}"
// "QToolButton#sexBtn{min-width:120px;max-width:120px;font-size:20px;padding:2px;}"
// "QToolButton#sexBtn:disabled{color:silver}"
// "QWidget#sexpanelwidget{border:1px solid silver;}"
// "QWidget#sexpanelwidget:enabled{background-color: #515151;}"
// "QToolButton#sexBtn:checked{border:2px solid darkorange;padding:0px;}"
// ;
//
// this->setStyleSheet(this->styleSheet().append(style));
//init command bar
QHBoxLayout* layout =new QHBoxLayout();
ui->commandWidget->setLayout(layout);

View File

@@ -0,0 +1,43 @@
//
// Created by Krad on 2021/11/23.
//
#include "UserOperationLogForm.h"
#include <QVBoxLayout>
#include <QAbstractListModel>
#include "log/UserOperationLog.h"
#include "log/LogFileTableModel.h"
#include <QHeaderView>
UserOperationLogForm::UserOperationLogForm(QWidget *parent) {
QVBoxLayout* layout = new QVBoxLayout(this);
QWidget* header = new QWidget(this);
table = new QTableView(this);
layout->addWidget(header);
layout->addWidget(table);
//暂时先放构造函数,之后需要移除,等需要时再调用
loadUserOperationLog();
}
UserOperationLogForm::~UserOperationLogForm() {
}
void UserOperationLogForm::loadUserOperationLog() {
LogFileTableModel* model = new LogFileTableModel(this);
QString filePath = UserOperationLog::Default()->currentLogFile();
model->setFileName(filePath);
QStringList header;
header<<"Operation Date"<<"Operation Time"<<"User"<<"Operation";
model->setHeader(header);
// UserOperationLog::getHistoryLogFiles();
table->setModel(model);
table->setAlternatingRowColors(true);
table->setSelectionMode(QAbstractItemView::SingleSelection);
table->setEditTriggers(QAbstractItemView::NoEditTriggers);
table->setSelectionBehavior(QAbstractItemView::SelectRows);
table->verticalHeader()->setDefaultSectionSize(38);
table->horizontalHeader()->setStretchLastSection(true);
table->setColumnWidth(0,250);
table->setColumnWidth(1,250);
}

View File

@@ -0,0 +1,23 @@
//
// Created by Krad on 2021/11/23.
//
#ifndef GUI_USEROPERATIONLOGFORM_H
#define GUI_USEROPERATIONLOGFORM_H
#include <QWidget>
#include <QtWidgets/QTableView>
class UserOperationLogForm:public QWidget {
Q_OBJECT
public:
explicit UserOperationLogForm(QWidget *parent = nullptr);
~UserOperationLogForm();
void loadUserOperationLog();
private:
QTableView * table = nullptr;
};
#endif //GUI_USEROPERATIONLOGFORM_H

View File

@@ -65,6 +65,7 @@ void GUIMessageDialog::startLoading() {
}
accept();
EventCenter::Default()->triggerEvent(GUIEvents::RequestStop, nullptr, nullptr);
LOG_USER_OPERATION(Stop);
});
timerID = startTimer(100);
ui->btn_main->setText("Stop");

View File

@@ -0,0 +1,56 @@
//
// Created by Krad on 2021/11/23.
//
#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include "LogFileTableModel.h"
LogFileTableModel::LogFileTableModel(QObject *parent) : QAbstractTableModel(parent) {
}
void LogFileTableModel::setFileName(QString fileName) {
logdata.clear();
QFile f;
f.setFileName(fileName);
if (!f.exists()) return;
if(f.open(QFile::OpenModeFlag::ReadOnly | QFile::OpenModeFlag::Text))
{
QTextStream in(&f);
while(!in.atEnd()){
logdata.push_back(in.readLine().split("\t"));
}
}
}
QVariant LogFileTableModel::data(const QModelIndex &index, int role) const {
if (!index.isValid()) return QVariant();
if (role == Qt::TextAlignmentRole) {
return Qt::AlignCenter;
}
if (role == Qt::DisplayRole || role == Qt::EditRole )
{
return logdata[index.row()][index.column()];
}
return QVariant();
}
int LogFileTableModel::rowCount(const QModelIndex &parent) const {
if (!logdata.isEmpty()) return logdata.count();
return 0;
}
int LogFileTableModel::columnCount(const QModelIndex &parent) const {
if (!logdata.isEmpty()) return logdata[0].count();
return 0;
}
QVariant LogFileTableModel::headerData(int section, Qt::Orientation orientation, int role) const {
if(role != Qt::DisplayRole)
return QVariant();
if(orientation == Qt::Horizontal)
return headerStrings.at(section);
return QVariant();
}

View File

@@ -0,0 +1,34 @@
//
// Created by Krad on 2021/11/23.
//
#ifndef GUI_LOGFILETABLEMODEL_H
#define GUI_LOGFILETABLEMODEL_H
#include <QAbstractTableModel>
#include <QList>
#include <QStringList>
class LogFileTableModel : public QAbstractTableModel{
public:
LogFileTableModel(QObject *parent = nullptr);
~LogFileTableModel(){}
void setFileName(QString filename);
void setHeader(QStringList header){
headerStrings = header;
}
protected:
//数据展示用
QVariant data(const QModelIndex &index, int role) const;
//行数,重新实现
int rowCount(const QModelIndex &parent) const;
//列数,重新实现
int columnCount(const QModelIndex &parent) const;
//标头
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
private:
QList<QStringList> logdata;
QStringList headerStrings;
};
#endif //GUI_LOGFILETABLEMODEL_H

View File

@@ -7,12 +7,16 @@
#include <QDir>
#include "appvals/AppGlobalValues.h"
#include "models/User.h"
#include <QDirIterator>
#include <qdebug.h>
const char * logDir = "./log";
void UserOperationLog::init() {
QDir log_dir("./");
if (!log_dir.exists("./log")) log_dir.mkdir("log");
currentFileName = "./log/" + QDate::currentDate().toString("yyyy-MM-dd")+QString("-op.log");
if (!log_dir.exists(logDir)) log_dir.mkdir("log");
currentFileName = logDir + QDate::currentDate().toString("/yyyy-MM-dd")+QString("-op.log");
logFile.setFileName(currentFileName);
if (logFile.exists())
{
@@ -23,11 +27,29 @@ void UserOperationLog::init() {
out.setDevice(&logFile);
}
const char space = ' ';
QString addSpace(char* str)
{
QString s;
for (int i = 0; str[i]!='\0'; ++i) {
if (str[i]>='A' && str[i]<='Z' && i>0)
{
s.append(space);
s.append(str[i]);
} else{
s.append(str[i]);
}
}
return s;
}
QString getOperationName(UserOperation operation)
{
switch (operation) {
#define ADD_OPERATION(name)\
case name: return #name;
case name: return addSpace(#name);
USER_OPERATIONS()
#undef ADD_OPERATION
default:
@@ -44,11 +66,11 @@ void UserOperationLog::log(UserOperation operation, bool processing) {
QString operationName = getOperationName(operation);
AppGlobalValues::setInProcessing(processing);
QString UserName = User::Current()->getUserCode().isEmpty()?"anonymous":User::Current()->getUserCode();
out << now.toString(Qt::DateFormat::ISODateWithMs)<<"\t"<<UserName<<"\t"<<operationName<<endl;
out << now.toString(Qt::DateFormat::ISODateWithMs).replace("T","\t")<<"\t"<<UserName<<"\t"<<operationName<<endl;
}
void UserOperationLog::reloadFile() {
QString newFileName = "./log/" + QDate::currentDate().toString("yyyy-MM-dd")+QString("-op.log");
QString newFileName = logDir + QDate::currentDate().toString("/yyyy-MM-dd")+QString("-op.log");
//inprocessing 暂时没有使用
if (newFileName == currentFileName && !AppGlobalValues::InProcessing().toBool()) return;
logFile.close();
@@ -61,3 +83,29 @@ void UserOperationLog::reloadFile() {
}
out.setDevice(&logFile);
}
QStringList UserOperationLog::getHistoryLogFiles() {
QDirIterator iter(logDir, QDir::Files | QDir::NoSymLinks);
QStringList list;
while(iter.hasNext())
{
iter.next();
list << iter.fileInfo().absoluteFilePath();
qDebug()<<iter.fileInfo().absoluteFilePath();
}
return list;
}
void UserOperationLog::loadLogFromFile(QString path, QStringList& result) {
QFile f;
f.setFileName(path);
if (!f.exists()) return;
if(f.open(QFile::OpenModeFlag::ReadOnly | QFile::OpenModeFlag::Text))
{
QTextStream in(&f);
while(!in.atEnd()){
result << in.readLine();
}
}
}

View File

@@ -50,8 +50,16 @@ public:
static UserOperationLog d;
return &d;
}
static void cleanHistoryLog();
static QStringList getHistoryLogFiles();
void log(UserOperation operation, bool processing = false);
void reloadFile();
QString currentLogFile(){
return currentFileName;
}
static void loadLogFromFile(QString filePath, QStringList& result);
private:
QString currentFileName;
QFile logFile;

View File

@@ -106,6 +106,19 @@ MainWindow::MainWindow(QWidget* parent) :
"QListView{border:none;margin:5px; font-weight:bold}"
"QListView::item{background:#4c5052;min-height:30px; border:1px solid silver; border-radius:5px}"
"QListView::item:selected{background:#365880; font-weight:bold;color:white}"
"QHeaderView::section{background-color:#595959;"
" min-height:50px;max-height:50px;"
"font-weight:Bold; font-size:16px; border:1px solid #323232;}"
"QHeaderView::section:horizontal{border-bottom: 1px solid rgb(0,170,255);}"
"QHeaderView::section:vertical{min-height:36px;max-height:36px;}"
"QWidget#edit_patient{min-width:300px;max-width:300px;}"
"QTableView{border:none}"
"QTableView{alternate-background-color: #595959;selection-color:white;selection-background-color:#0078d8}"
"QToolButton#sexBtn{min-width:120px;max-width:120px;font-size:20px;padding:2px;}"
"QToolButton#sexBtn:disabled{color:silver}"
"QWidget#sexpanelwidget{border:1px solid silver;}"
"QWidget#sexpanelwidget:enabled{background-color: #515151;}"
"QToolButton#sexBtn:checked{border:2px solid darkorange;padding:0px;}"
;
this->setStyleSheet(style);
ui->setupUi(this);