log: color printing adaptive system for dark/light themes.
This commit is contained in:
@@ -162,7 +162,7 @@ void Connecter::InitControl()
|
|||||||
}
|
}
|
||||||
auto name = item->text();
|
auto name = item->text();
|
||||||
ui->elbClient->setText(name);
|
ui->elbClient->setText(name);
|
||||||
ui->elbClient->setStyleSheet("color: blue;");
|
ui->elbClient->setStyleSheet("color: green;");
|
||||||
remoteCall_(name);
|
remoteCall_(name);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -12,10 +12,14 @@ LogPrint::LogPrint(QWidget* parent) : QWidget(parent), ui(new Ui::LogPrint)
|
|||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
InitControl();
|
InitControl();
|
||||||
|
InitColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LogPrint::InitControl()
|
void LogPrint::InitControl()
|
||||||
{
|
{
|
||||||
|
isLightMode_ = QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Light;
|
||||||
|
styleHints_ = QGuiApplication::styleHints();
|
||||||
|
connect(styleHints_, &QStyleHints::colorSchemeChanged, this, &LogPrint::ColorChange);
|
||||||
ui->pedText->setReadOnly(true);
|
ui->pedText->setReadOnly(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,33 +36,80 @@ std::string LogPrint::now_str()
|
|||||||
return timestamp.str();
|
return timestamp.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LogPrint::ColorChange(Qt::ColorScheme scheme)
|
||||||
|
{
|
||||||
|
if (scheme == Qt::ColorScheme::Dark) {
|
||||||
|
isLightMode_ = false;
|
||||||
|
} else {
|
||||||
|
isLightMode_ = true;
|
||||||
|
}
|
||||||
|
RePrintLog();
|
||||||
|
}
|
||||||
|
|
||||||
LogPrint::~LogPrint()
|
LogPrint::~LogPrint()
|
||||||
{
|
{
|
||||||
delete ui;
|
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)
|
void LogPrint::Info(const QString& message)
|
||||||
{
|
{
|
||||||
Print(message, Qt::black);
|
Print(message, PT_INFO);
|
||||||
}
|
}
|
||||||
void LogPrint::Warn(const QString& message)
|
void LogPrint::Warn(const QString& message)
|
||||||
{
|
{
|
||||||
Print(message, Qt::gray);
|
Print(message, PT_WARN);
|
||||||
}
|
}
|
||||||
void LogPrint::Error(const QString& message)
|
void LogPrint::Error(const QString& message)
|
||||||
{
|
{
|
||||||
Print(message, Qt::red);
|
Print(message, PT_ERROR);
|
||||||
}
|
}
|
||||||
void LogPrint::Debug(const QString& message)
|
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>")
|
void LogPrint::Print(const QString& message, PrintType type, bool recordHis)
|
||||||
.arg(color.color().name())
|
{
|
||||||
.arg(timeStr.toHtmlEscaped());
|
QMutexLocker locker(&mutex_);
|
||||||
ui->pedText->appendHtml(coloredLog);
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,25 @@
|
|||||||
#ifndef LOGCONTROL_H
|
#ifndef LOGCONTROL_H
|
||||||
#define LOGCONTROL_H
|
#define LOGCONTROL_H
|
||||||
|
|
||||||
#include <QBrush>
|
#include <QBrush>
|
||||||
|
#include <QGuiApplication>
|
||||||
|
#include <QMap>
|
||||||
|
#include <QMutex>
|
||||||
#include <QStandardItemModel>
|
#include <QStandardItemModel>
|
||||||
|
#include <QStyleHints>
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
class LogPrint;
|
class LogPrint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum PrintType { PT_INFO, PT_WARN, PT_ERROR, PT_DEBUG };
|
||||||
|
|
||||||
|
struct LogEntry {
|
||||||
|
QString message;
|
||||||
|
PrintType level;
|
||||||
|
};
|
||||||
|
|
||||||
class LogPrint : public QWidget
|
class LogPrint : public QWidget
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@@ -25,13 +36,22 @@ public:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
std::string now_str();
|
std::string now_str();
|
||||||
|
void ColorChange(Qt::ColorScheme scheme);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void InitControl();
|
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:
|
private:
|
||||||
|
QMutex mutex_;
|
||||||
|
bool isLightMode_{true};
|
||||||
Ui::LogPrint* ui;
|
Ui::LogPrint* ui;
|
||||||
|
QMap<PrintType, QBrush> lightMap_;
|
||||||
|
QMap<PrintType, QBrush> darkMap_;
|
||||||
|
QVector<LogEntry> logEntries_;
|
||||||
|
QStyleHints* styleHints_{};
|
||||||
QStandardItemModel* model_;
|
QStandardItemModel* model_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ bool Server::startServer(quint16 port)
|
|||||||
}
|
}
|
||||||
|
|
||||||
qDebug() << "Server started on port" << serverPort();
|
qDebug() << "Server started on port" << serverPort();
|
||||||
monitorTimer_->start(30000);
|
monitorTimer_->start(300000);
|
||||||
id_ = QString("0.0.0.0:%1").arg(serverPort());
|
id_ = QString("0.0.0.0:%1").arg(serverPort());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user