Allow changing log colors from config.json

This commit is contained in:
jagannatharjun
2020-05-05 12:17:23 +05:30
parent 2c100feb35
commit cce8f178e4
3 changed files with 27 additions and 26 deletions

View File

@@ -35,6 +35,7 @@
#include <QPalette>
#include "base/global.h"
#include "uithememanager.h"
namespace
{
@@ -72,6 +73,7 @@ QVariant BaseLogModel::Message::type() const
BaseLogModel::BaseLogModel(QObject *parent)
: QAbstractListModel(parent)
, m_messages(MAX_VISIBLE_MESSAGES)
, m_timeForeground(UIThemeManager::instance()->getColor(QLatin1String("Log.TimeStamp"), Qt::darkGray))
{
}
@@ -100,7 +102,9 @@ QVariant BaseLogModel::data(const QModelIndex &index, const int role) const
return message.time();
case MessageRole:
return message.message();
case ForegroundRole:
case TimeForegroundRole:
return m_timeForeground;
case MessageForegroundRole:
return message.foreground();
case TypeRole:
return message.type();
@@ -134,6 +138,12 @@ void BaseLogModel::reset()
LogMessageModel::LogMessageModel(QObject *parent)
: BaseLogModel(parent)
, m_foregroundForMessageTypes {
{Log::NORMAL, UIThemeManager::instance()->getColor(QLatin1String("Log.Normal"), QApplication::palette().color(QPalette::WindowText))},
{Log::INFO, UIThemeManager::instance()->getColor(QLatin1String("Log.Info"), Qt::blue)},
{Log::WARNING, UIThemeManager::instance()->getColor(QLatin1String("Log.Warning"), QColor {255, 165, 0})}, // orange
{Log::CRITICAL, UIThemeManager::instance()->getColor(QLatin1String("Log.Critical"), Qt::red)}
}
{
for (const Log::Msg &msg : asConst(Logger::instance()->getMessages()))
handleNewMessage(msg);
@@ -144,32 +154,14 @@ void LogMessageModel::handleNewMessage(const Log::Msg &message)
{
const QString time = QDateTime::fromMSecsSinceEpoch(message.timestamp).toString(Qt::SystemLocaleShortDate);
const QString messageText = message.message;
QColor foreground;
switch (message.type) {
// The RGB QColor constructor is used for performance
case Log::NORMAL:
foreground = QApplication::palette().color(QPalette::WindowText);
break;
case Log::INFO:
foreground = QColor(0, 0, 255); // blue
break;
case Log::WARNING:
foreground = QColor(255, 165, 0); // orange
break;
case Log::CRITICAL:
foreground = QColor(255, 0, 0); // red
break;
default:
Q_ASSERT(false);
break;
}
const QColor foreground = m_foregroundForMessageTypes[message.type];
addNewMessage({time, messageText, foreground, message.type});
}
LogPeerModel::LogPeerModel(QObject *parent)
: BaseLogModel(parent)
, m_bannedPeerForeground(UIThemeManager::instance()->getColor(QLatin1String("Log.BannedPeer"), Qt::red))
{
for (const Log::Peer &peer : asConst(Logger::instance()->getPeers()))
handleNewMessage(peer);
@@ -182,7 +174,6 @@ void LogPeerModel::handleNewMessage(const Log::Peer &peer)
const QString message = peer.blocked
? tr("%1 was blocked due to %2", "0.0.0.0 was blocked due to reason").arg(peer.ip, peer.reason)
: tr("%1 was banned", "0.0.0.0 was banned").arg(peer.ip);
const QColor foreground = Qt::red;
addNewMessage({time, message, foreground, Log::NORMAL});
addNewMessage({time, message, m_bannedPeerForeground, Log::NORMAL});
}