2015-08-20 00:49:03 +01:00
|
|
|
#pragma once
|
|
|
|
#include <QtGui/QColor>
|
|
|
|
#include <rainbow.h>
|
2016-11-03 00:10:16 +00:00
|
|
|
#include <MessageLevel.h>
|
2015-08-20 00:49:03 +01:00
|
|
|
#include <QMap>
|
|
|
|
|
|
|
|
class ColorCache
|
|
|
|
{
|
|
|
|
public:
|
2018-07-15 13:51:05 +01:00
|
|
|
ColorCache(QColor front, QColor back, qreal bias)
|
|
|
|
{
|
|
|
|
m_front = front;
|
|
|
|
m_back = back;
|
|
|
|
m_bias = bias;
|
|
|
|
};
|
2015-08-20 00:49:03 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
void addColor(int key, QColor color)
|
|
|
|
{
|
|
|
|
m_colors[key] = {color, blend(color), blendBackground(color)};
|
|
|
|
}
|
2015-08-20 00:49:03 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
void setForeground(QColor front)
|
|
|
|
{
|
|
|
|
if(m_front != front)
|
|
|
|
{
|
|
|
|
m_front = front;
|
|
|
|
recolorAll();
|
|
|
|
}
|
|
|
|
}
|
2015-08-20 00:49:03 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
void setBackground(QColor back)
|
|
|
|
{
|
|
|
|
if(m_back != back)
|
|
|
|
{
|
|
|
|
m_back = back;
|
|
|
|
recolorAll();
|
|
|
|
}
|
|
|
|
}
|
2015-08-20 00:49:03 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
QColor getFront(int key)
|
|
|
|
{
|
|
|
|
auto iter = m_colors.find(key);
|
|
|
|
if(iter == m_colors.end())
|
|
|
|
{
|
|
|
|
return QColor();
|
|
|
|
}
|
|
|
|
return (*iter).front;
|
|
|
|
}
|
2015-08-20 00:49:03 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
QColor getBack(int key)
|
|
|
|
{
|
|
|
|
auto iter = m_colors.find(key);
|
|
|
|
if(iter == m_colors.end())
|
|
|
|
{
|
|
|
|
return QColor();
|
|
|
|
}
|
|
|
|
return (*iter).back;
|
|
|
|
}
|
2015-08-20 00:49:03 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
/**
|
|
|
|
* Blend the color with the front color, adapting to the back color
|
|
|
|
*/
|
|
|
|
QColor blend(QColor color);
|
2015-08-20 00:49:03 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
/**
|
|
|
|
* Blend the color with the back color
|
|
|
|
*/
|
|
|
|
QColor blendBackground(QColor color);
|
2015-08-20 00:49:03 +01:00
|
|
|
|
|
|
|
protected:
|
2018-07-15 13:51:05 +01:00
|
|
|
void recolorAll();
|
2015-08-20 00:49:03 +01:00
|
|
|
|
|
|
|
protected:
|
2018-07-15 13:51:05 +01:00
|
|
|
struct ColorEntry
|
|
|
|
{
|
|
|
|
QColor original;
|
|
|
|
QColor front;
|
|
|
|
QColor back;
|
|
|
|
};
|
2015-08-20 00:49:03 +01:00
|
|
|
|
|
|
|
protected:
|
2018-07-15 13:51:05 +01:00
|
|
|
qreal m_bias;
|
|
|
|
QColor m_front;
|
|
|
|
QColor m_back;
|
|
|
|
QMap<int, ColorEntry> m_colors;
|
2015-08-20 00:49:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class LogColorCache : public ColorCache
|
|
|
|
{
|
|
|
|
public:
|
2018-07-15 13:51:05 +01:00
|
|
|
LogColorCache(QColor front, QColor back)
|
|
|
|
: ColorCache(front, back, 1.0)
|
|
|
|
{
|
2021-10-17 23:47:02 +01:00
|
|
|
addColor((int)MessageLevel::Launcher, QColor("purple"));
|
2018-07-15 13:51:05 +01:00
|
|
|
addColor((int)MessageLevel::Debug, QColor("green"));
|
|
|
|
addColor((int)MessageLevel::Warning, QColor("orange"));
|
|
|
|
addColor((int)MessageLevel::Error, QColor("red"));
|
|
|
|
addColor((int)MessageLevel::Fatal, QColor("red"));
|
|
|
|
addColor((int)MessageLevel::Message, front);
|
|
|
|
}
|
2015-08-20 00:49:03 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
QColor getFront(MessageLevel::Enum level)
|
|
|
|
{
|
|
|
|
if(!m_colors.contains((int) level))
|
|
|
|
{
|
|
|
|
return ColorCache::getFront((int)MessageLevel::Message);
|
|
|
|
}
|
|
|
|
return ColorCache::getFront((int)level);
|
|
|
|
}
|
2015-08-20 00:49:03 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
QColor getBack(MessageLevel::Enum level)
|
|
|
|
{
|
|
|
|
if(level == MessageLevel::Fatal)
|
|
|
|
{
|
|
|
|
return QColor(Qt::black);
|
|
|
|
}
|
|
|
|
return QColor(Qt::transparent);
|
|
|
|
}
|
2015-08-20 00:49:03 +01:00
|
|
|
};
|