Browse Source

Merge pull request #12756 from jagannatharjun/log-colors

Allow changing log colors from config.json
adaptive-webui-19844
Vladimir Golovnev 5 years ago committed by GitHub
parent
commit
fb7774705d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      src/gui/log/loglistview.cpp
  2. 37
      src/gui/log/logmodel.cpp
  3. 12
      src/gui/log/logmodel.h

4
src/gui/log/loglistview.cpp

@ -81,7 +81,7 @@ namespace
const QPen originalPen = painter->pen(); const QPen originalPen = painter->pen();
QPen coloredPen = originalPen; QPen coloredPen = originalPen;
coloredPen.setColor(Qt::darkGray); coloredPen.setColor(index.data(BaseLogModel::TimeForegroundRole).value<QColor>());
painter->setPen(coloredPen); painter->setPen(coloredPen);
const QString time = index.data(BaseLogModel::TimeRole).toString(); const QString time = index.data(BaseLogModel::TimeRole).toString();
style->drawItemText(painter, textRect, option.displayAlignment, option.palette, (option.state & QStyle::State_Enabled), time); style->drawItemText(painter, textRect, option.displayAlignment, option.palette, (option.state & QStyle::State_Enabled), time);
@ -92,7 +92,7 @@ namespace
style->drawItemText(painter, textRect.adjusted(separatorCoordinateX, 0, 0, 0), option.displayAlignment, option.palette style->drawItemText(painter, textRect.adjusted(separatorCoordinateX, 0, 0, 0), option.displayAlignment, option.palette
, (option.state & QStyle::State_Enabled), SEPARATOR); , (option.state & QStyle::State_Enabled), SEPARATOR);
coloredPen.setColor(index.data(BaseLogModel::ForegroundRole).value<QColor>()); coloredPen.setColor(index.data(BaseLogModel::MessageForegroundRole).value<QColor>());
painter->setPen(coloredPen); painter->setPen(coloredPen);
const int messageCoordinateX = separatorCoordinateX + horizontalAdvance(fontMetrics, SEPARATOR); const int messageCoordinateX = separatorCoordinateX + horizontalAdvance(fontMetrics, SEPARATOR);
style->drawItemText(painter, textRect.adjusted(messageCoordinateX, 0, 0, 0), option.displayAlignment, option.palette style->drawItemText(painter, textRect.adjusted(messageCoordinateX, 0, 0, 0), option.displayAlignment, option.palette

37
src/gui/log/logmodel.cpp

@ -35,6 +35,7 @@
#include <QPalette> #include <QPalette>
#include "base/global.h" #include "base/global.h"
#include "uithememanager.h"
namespace namespace
{ {
@ -72,6 +73,7 @@ QVariant BaseLogModel::Message::type() const
BaseLogModel::BaseLogModel(QObject *parent) BaseLogModel::BaseLogModel(QObject *parent)
: QAbstractListModel(parent) : QAbstractListModel(parent)
, m_messages(MAX_VISIBLE_MESSAGES) , 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(); return message.time();
case MessageRole: case MessageRole:
return message.message(); return message.message();
case ForegroundRole: case TimeForegroundRole:
return m_timeForeground;
case MessageForegroundRole:
return message.foreground(); return message.foreground();
case TypeRole: case TypeRole:
return message.type(); return message.type();
@ -134,6 +138,12 @@ void BaseLogModel::reset()
LogMessageModel::LogMessageModel(QObject *parent) LogMessageModel::LogMessageModel(QObject *parent)
: BaseLogModel(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())) for (const Log::Msg &msg : asConst(Logger::instance()->getMessages()))
handleNewMessage(msg); 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 time = QDateTime::fromMSecsSinceEpoch(message.timestamp).toString(Qt::SystemLocaleShortDate);
const QString messageText = message.message; const QString messageText = message.message;
const QColor foreground = m_foregroundForMessageTypes[message.type];
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;
}
addNewMessage({time, messageText, foreground, message.type}); addNewMessage({time, messageText, foreground, message.type});
} }
LogPeerModel::LogPeerModel(QObject *parent) LogPeerModel::LogPeerModel(QObject *parent)
: BaseLogModel(parent) : BaseLogModel(parent)
, m_bannedPeerForeground(UIThemeManager::instance()->getColor(QLatin1String("Log.BannedPeer"), Qt::red))
{ {
for (const Log::Peer &peer : asConst(Logger::instance()->getPeers())) for (const Log::Peer &peer : asConst(Logger::instance()->getPeers()))
handleNewMessage(peer); handleNewMessage(peer);
@ -182,7 +174,6 @@ void LogPeerModel::handleNewMessage(const Log::Peer &peer)
const QString message = peer.blocked 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 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); : 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});
} }

12
src/gui/log/logmodel.h

@ -32,6 +32,8 @@
#include <boost/circular_buffer.hpp> #include <boost/circular_buffer.hpp>
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QColor>
#include <QHash>
#include "base/logger.h" #include "base/logger.h"
@ -44,7 +46,8 @@ public:
{ {
TimeRole = Qt::UserRole, TimeRole = Qt::UserRole,
MessageRole, MessageRole,
ForegroundRole, TimeForegroundRole,
MessageForegroundRole,
TypeRole TypeRole
}; };
@ -77,6 +80,7 @@ protected:
private: private:
boost::circular_buffer_space_optimized<Message> m_messages; boost::circular_buffer_space_optimized<Message> m_messages;
const QColor m_timeForeground;
}; };
class LogMessageModel : public BaseLogModel class LogMessageModel : public BaseLogModel
@ -89,6 +93,9 @@ public:
private slots: private slots:
void handleNewMessage(const Log::Msg &message); void handleNewMessage(const Log::Msg &message);
private:
const QHash<int, QColor> m_foregroundForMessageTypes;
}; };
class LogPeerModel : public BaseLogModel class LogPeerModel : public BaseLogModel
@ -101,4 +108,7 @@ public:
private slots: private slots:
void handleNewMessage(const Log::Peer &peer); void handleNewMessage(const Log::Peer &peer);
private:
const QColor m_bannedPeerForeground;
}; };

Loading…
Cancel
Save