-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
redasmfonts.cpp
88 lines (73 loc) · 2.87 KB
/
redasmfonts.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "redasmfonts.h"
#include <QFontDatabase>
#include <QApplication>
#include <QPalette>
#include <QPainter>
#include <QIcon>
REDasmFontIconEngine::REDasmFontIconEngine(): QIconEngine() { }
void REDasmFontIconEngine::setFont(const QFont& font) { m_font = font; }
void REDasmFontIconEngine::setLetter(const QString& letter) { m_letter = letter; }
void REDasmFontIconEngine::setColor(const QColor& color) { m_color = color; }
void REDasmFontIconEngine::paint(QPainter* painter, const QRect& rect, QIcon::Mode mode, QIcon::State)
{
QFont font = m_font;
int drawsize = qRound(rect.height() * 0.8);
font.setPixelSize(drawsize);
QColor pc = m_color;
if(mode == QIcon::Disabled) pc = qApp->palette().color(QPalette::Disabled, QPalette::ButtonText);
if(mode == QIcon::Selected) pc = qApp->palette().color(QPalette::Active, QPalette::ButtonText);
painter->save();
painter->setPen(QPen(pc));
painter->setFont(font);
painter->drawText(rect, Qt::AlignCenter | Qt::AlignVCenter, m_letter);
painter->restore();
}
QPixmap REDasmFontIconEngine::pixmap(const QSize& size, QIcon::Mode mode, QIcon::State state)
{
QPixmap pixmap(size);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
this->paint(&painter, QRect(QPoint(0, 0), size), mode, state);
return pixmap;
}
QIconEngine* REDasmFontIconEngine::clone() const
{
auto* engine = new REDasmFontIconEngine();
engine->setFont(m_font);
return engine;
}
REDasmFonts::REDasmFonts()
{
int id = QFontDatabase::addApplicationFont(":/res/fonts/FontAwesomeBrands.otf");
if(id != -1) m_fabfamilies = QFontDatabase::applicationFontFamilies(id);
id = QFontDatabase::addApplicationFont(":/res/fonts/FontAwesome.otf");
if(id != -1) m_fafamilies = QFontDatabase::applicationFontFamilies(id);
}
QFont REDasmFonts::faFont() const { QFont f(m_fafamilies.front()); f.setStyleStrategy(QFont::NoFontMerging); return f; }
QFont REDasmFonts::faBrandsFont() const { QFont f(m_fabfamilies.front()); f.setStyleStrategy(QFont::NoFontMerging); return f; }
QIcon REDasmFonts::icon(const QChar& code, const QColor& color)
{
auto* engine = new REDasmFontIconEngine();
engine->setFont(this->faFont());
engine->setLetter(code);
engine->setColor(color);
return QIcon(engine);
}
QIcon REDasmFonts::icon(const QChar& code) { return this->icon(code, qApp->palette().color(QPalette::Normal, QPalette::ButtonText)); }
QIcon REDasmFonts::brand(const QChar& code, const QColor& color)
{
auto* engine = new REDasmFontIconEngine();
engine->setFont(this->faBrandsFont());
engine->setLetter(code);
engine->setColor(color);
return QIcon(engine);
}
QIcon REDasmFonts::brand(const QChar& code)
{
return this->brand(code, qApp->palette().color(QPalette::Normal, QPalette::ButtonText));
}
REDasmFonts* REDasmFonts::instance()
{
static REDasmFonts instance;
return &instance;
}