Files
handleaiuinew/HanddleAI/HandleAiUi-new/front/pages/logPageWidget.h
T
2024-05-20 01:02:24 +08:00

229 lines
6.8 KiB
C++

#include <qwidget.h>
#include <qlayout.h>
#include <qlabel.h>
#include <QTextEdit>
#include <QDebug>
#include <qpushbutton.h>
#include <unordered_map>
#include <uiModule/components/fancyWidget.h>
#include <qtextstream.h>
#include <streambuf>
#include <iostream>
#include <qstackedwidget.h>
#include <mutex>
#include <qfile.h>
#include <qcombobox.h>
static QTextEdit* globalQtTextEdit=nullptr;
static QFile* qtLogFile;
static QFile* stdLogFile;
static QTextStream* globalQtlogFile = nullptr;
static QTextStream* globalStdlogFile = nullptr;
static void MessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
QString logStr = (QDateTime::currentDateTime().toString(u8"(yyyy-MM-dd hh:mm:ss dddd)")).toUtf8();
switch (type) {
case QtDebugMsg:
logStr.append("Debug: ");
break;
case QtInfoMsg:
logStr.append("Info: ");
break;
case QtWarningMsg:
logStr.append("Warning: ");
break;
case QtCriticalMsg:
logStr.append("Critical: ");
break;
case QtFatalMsg:
logStr.append("Fatal: ");
break;
}
logStr.append(QString("%1%2:\n-->%3\n").arg(context.line).arg(context.function).arg(msg));
*globalQtlogFile << logStr << endl;
//QFile logFile(QString("./Log/%1QtLog.log").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd")));
//if (logFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)) {
// globalQtlogFile.setCodec("UTF-8");
// globalQtlogFile.setDevice(&logFile);
// globalQtlogFile << logStr << endl;
//}
//if (globalQtTextEdit != nullptr) {
// globalQtTextEdit->moveCursor(QTextCursor::End);
// globalQtTextEdit->insertPlainText(logStr + "\n");
//}
}
class stdConsoleRedirector : public std::basic_streambuf<char> {
public:
stdConsoleRedirector(std::ostream& stream, QTextEdit* textEdit) : m_stream(stream), m_textEdit(textEdit) {
m_oldBuf = stream.rdbuf(this); // Redirect stream to this buffer
}
~stdConsoleRedirector() {
m_stream.rdbuf(m_oldBuf); // Restore old buffer when destroyed
}
protected:
virtual int_type overflow(int_type v) {
m_textEdit->moveCursor(QTextCursor::End);
m_textEdit->insertPlainText(QString(v));
return v;
}
virtual std::streamsize xsputn(const char* p, std::streamsize n) {
QString text = QString::fromUtf8(p, static_cast<int>(n));
m_textEdit->moveCursor(QTextCursor::End);
m_textEdit->insertPlainText(text);
QString logStr = (QDateTime::currentDateTime().toString(u8"(yyyy-MM-dd hh:mm:ss dddd)")).toUtf8();
logStr.append(QString("-->%1\n").arg(text));
*globalStdlogFile << logStr << endl;
return n;
}
private:
std::ostream& m_stream;
std::streambuf* m_oldBuf;
QTextEdit* m_textEdit;
};
class logWidget :public QWidget
{
public:
enum logType
{
stdConsle,
qtConsle
};
QVBoxLayout* Layout;
QHBoxLayout* btnLayout;
std::vector<gradientButton*>btns;
QLabel* icon, * Title;
QStackedWidget* widgets;
QTextEdit stdConsoleOutput;
QTextEdit qtConsoleOutput;
logWidget()
{
QPalette palette;
palette.setColor(QPalette::WindowText, Qt::white);
QFont font("微软雅黑", 24, QFont::Bold);
widgets = new QStackedWidget();
widgets->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
widgets->setMaximumSize(9999, 9999);
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
this->setMaximumSize(9999, 9999);
this->setObjectName("configSettingWidget");
//this->setStyleSheet(QString("QWidget {background-color: rgb(%1)}").arg(sColor_Gold));
//this->showMaximized();
Layout = new QVBoxLayout(this);
Layout->setContentsMargins(0, 0, 0, 0);
Layout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
Title = new QLabel(u8"日志");
Title->setFont(font);
Title->setPalette(palette);
Title->setAlignment(Qt::AlignLeft);
Layout->addWidget(Title);
btnLayout = new QHBoxLayout();
btnLayout->setContentsMargins(0, 0, 0, 0);
btnLayout->setAlignment(Qt::AlignLeft);
Layout->addLayout(btnLayout);
stdConsoleOutput.setReadOnly(true);
qtConsoleOutput.setReadOnly(true);
addConfigWidget(u8"控制台", &stdConsoleOutput, logType::stdConsle);
addConfigWidget(u8"QT日志", &qtConsoleOutput, logType::qtConsle);
btns[0]->pressed();
Layout->addWidget(widgets);
}
void addConfigWidget(QString btnText,QTextEdit* textWidget, logType type)
{
QPalette palette;
palette.setColor(QPalette::WindowText, Qt::white);
QFont font("微软雅黑", 13, QFont::Bold);
QFont font2("微软雅黑", 8);
QFont font3("微软雅黑", 16, QFont::Bold);
//切换配置页面
int index = btns.size();
gradientButton* btn = new gradientButton(160, 50, btnText, "");
btnLayout->addWidget(btn);
btns.push_back(btn);
connect(btn, &QPushButton::clicked, [=]()
{
for (auto btnPtr : btns)
btnPtr->toDefault();
btn->pressed();
widgets->setCurrentIndex(index);
});
//页面widget
QWidget* widget = new QWidget();
widgets->addWidget(widget);
widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QVBoxLayout* widgetLayout = new QVBoxLayout(widget);
widgetLayout->setContentsMargins(0, 0, 0, 0);
widgetLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
QHBoxLayout* toolHLayout = new QHBoxLayout();
widgetLayout->addLayout(toolHLayout);
QLabel* toolName = new QLabel(u8"过滤器");
toolName->setFont(font2);
toolName->setPalette(palette);
toolHLayout->addWidget(toolName);
QStringList options = { "Debug","Info","Warning" };
QComboBox* filterSelector = new QComboBox();
filterSelector->addItems(options);
toolHLayout->addWidget(filterSelector);
QLabel* toolName2 = new QLabel(u8" 正则匹配");
toolName2->setFont(font2);
toolName2->setPalette(palette);
toolHLayout->addWidget(toolName2);
QLineEdit* lineedit = new QLineEdit();
toolHLayout->addWidget(lineedit);
widgetLayout->addWidget(textWidget);
if(type==stdConsle)
{
stdConsoleRedirector _stdConsoleRedirector(std::cout, textWidget);
stdLogFile = new QFile(QString("./Log/%1StdLog.log").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd")));
if (stdLogFile->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)) {
globalStdlogFile = new QTextStream();
globalStdlogFile->setCodec("UTF-8");
globalStdlogFile->setDevice(stdLogFile);
}
}
else if (type == qtConsle)
{
qtLogFile =new QFile(QString("./Log/%1QtLog.log").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd")));
if (qtLogFile->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)) {
globalQtlogFile = new QTextStream();
globalQtlogFile->setCodec("UTF-8");
globalQtlogFile->setDevice(qtLogFile);
}
globalQtTextEdit = textWidget;
//globalQtTextEdit->setUpdatesEnabled(false);
qInstallMessageHandler(MessageHandler);
qInfo() << "This is a Qt Info message.";
qWarning() << "This is a Qt Warning message.";
qDebug() << "This is a Qt Debug message.";
qCritical() << "This is a Qt Critical message.";
//qFatal("This is a Qt Fatal message.");
}
}
};
#pragma once