log: color printing adaptive system for dark/light themes.

This commit is contained in:
2025-06-21 02:50:05 +08:00
parent 61aee68800
commit d0a3496356
4 changed files with 86 additions and 15 deletions

View File

@@ -162,7 +162,7 @@ void Connecter::InitControl()
}
auto name = item->text();
ui->elbClient->setText(name);
ui->elbClient->setStyleSheet("color: blue;");
ui->elbClient->setStyleSheet("color: green;");
remoteCall_(name);
});

View File

@@ -12,10 +12,14 @@ LogPrint::LogPrint(QWidget* parent) : QWidget(parent), ui(new Ui::LogPrint)
{
ui->setupUi(this);
InitControl();
InitColor();
}
void LogPrint::InitControl()
{
isLightMode_ = QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Light;
styleHints_ = QGuiApplication::styleHints();
connect(styleHints_, &QStyleHints::colorSchemeChanged, this, &LogPrint::ColorChange);
ui->pedText->setReadOnly(true);
}
@@ -32,33 +36,80 @@ std::string LogPrint::now_str()
return timestamp.str();
}
void LogPrint::ColorChange(Qt::ColorScheme scheme)
{
if (scheme == Qt::ColorScheme::Dark) {
isLightMode_ = false;
} else {
isLightMode_ = true;
}
RePrintLog();
}
LogPrint::~LogPrint()
{
delete ui;
}
static const QColor DARK_INFO_COLOR = QColor(255, 255, 0);
static const QColor DARK_WARN_COLOR = QColor(255, 102, 0);
static const QColor DARK_ERROR_COLOR = QColor(255, 0, 255);
static const QColor DARK_DEBUG_COLOR = QColor(0, 255, 255);
void LogPrint::InitColor()
{
lightMap_[PT_INFO] = Qt::black;
lightMap_[PT_WARN] = Qt::blue;
lightMap_[PT_ERROR] = Qt::red;
lightMap_[PT_DEBUG] = Qt::gray;
darkMap_[PT_INFO] = DARK_INFO_COLOR;
darkMap_[PT_WARN] = DARK_WARN_COLOR;
darkMap_[PT_ERROR] = DARK_ERROR_COLOR;
darkMap_[PT_DEBUG] = DARK_DEBUG_COLOR;
}
void LogPrint::RePrintLog()
{
{
QMutexLocker locker(&mutex_);
ui->pedText->clear();
}
for (auto& entry : logEntries_) {
Print(entry.message, entry.level, false);
}
}
void LogPrint::Info(const QString& message)
{
Print(message, Qt::black);
Print(message, PT_INFO);
}
void LogPrint::Warn(const QString& message)
{
Print(message, Qt::gray);
Print(message, PT_WARN);
}
void LogPrint::Error(const QString& message)
{
Print(message, Qt::red);
Print(message, PT_ERROR);
}
void LogPrint::Debug(const QString& message)
{
Print(message, Qt::blue);
Print(message, PT_DEBUG);
}
void LogPrint::Print(const QString& message, const QBrush& color)
{
QString timeStr = QString("%1%2").arg(QString::fromStdString(now_str())).arg(message);
QString coloredLog = QString("<span style='color:%1;'>%2</span>")
.arg(color.color().name())
.arg(timeStr.toHtmlEscaped());
ui->pedText->appendHtml(coloredLog);
void LogPrint::Print(const QString& message, PrintType type, bool recordHis)
{
QMutexLocker locker(&mutex_);
QString timeStr = QString("%1%2").arg(QString::fromStdString(now_str())).arg(message);
QString r;
if (isLightMode_) {
r = QString("<span style='color:%1;'>%2</span>").arg(lightMap_[type].color().name()).arg(timeStr.toHtmlEscaped());
} else {
r = QString("<span style='color:%1;'>%2</span>").arg(darkMap_[type].color().name()).arg(timeStr.toHtmlEscaped());
}
if (recordHis) {
logEntries_.append({message, type});
}
ui->pedText->appendHtml(r);
}

View File

@@ -1,14 +1,25 @@
#ifndef LOGCONTROL_H
#ifndef LOGCONTROL_H
#define LOGCONTROL_H
#include <QBrush>
#include <QGuiApplication>
#include <QMap>
#include <QMutex>
#include <QStandardItemModel>
#include <QStyleHints>
#include <QWidget>
namespace Ui {
class LogPrint;
}
enum PrintType { PT_INFO, PT_WARN, PT_ERROR, PT_DEBUG };
struct LogEntry {
QString message;
PrintType level;
};
class LogPrint : public QWidget
{
Q_OBJECT
@@ -25,13 +36,22 @@ public:
public:
std::string now_str();
void ColorChange(Qt::ColorScheme scheme);
private:
void InitControl();
void Print(const QString& message, const QBrush& color);
void InitColor();
void RePrintLog();
void Print(const QString& message, PrintType type, bool recordHis = true);
private:
QMutex mutex_;
bool isLightMode_{true};
Ui::LogPrint* ui;
QMap<PrintType, QBrush> lightMap_;
QMap<PrintType, QBrush> darkMap_;
QVector<LogEntry> logEntries_;
QStyleHints* styleHints_{};
QStandardItemModel* model_;
};