Branch data Line data Source code
1 : : #include "MdiIconProvider.h"
2 : :
3 : : #include "ui/ThemeManager.h"
4 : :
5 : : #include <QFile>
6 : : #include <QFontDatabase>
7 : : #include <QIODevice>
8 : : #include <QLoggingCategory>
9 : : #include <QPainter>
10 : : #include <QPixmap>
11 : : #include <QTextStream>
12 : :
13 [ + + + - : 88 : Q_LOGGING_CATEGORY(lcMdi, "mailjd.mdi")
+ - - - ]
14 : :
15 : 296 : MdiIconProvider &MdiIconProvider::instance() {
16 [ + + + - : 296 : static MdiIconProvider s_instance;
+ - - - ]
17 : 296 : return s_instance;
18 : : }
19 : :
20 : 75 : void MdiIconProvider::init() {
21 [ - + ]: 75 : if (m_ready)
22 : 62 : return;
23 : :
24 : : // 1. Register font
25 : : int fontId =
26 [ + - ]: 75 : QFontDatabase::addApplicationFont(
27 : 150 : QStringLiteral(":/fonts/materialdesignicons-webfont.ttf"));
28 [ + + ]: 75 : if (fontId < 0) {
29 [ + - + - : 124 : qCWarning(lcMdi) << "Failed to load MDI webfont from resources";
+ - + + ]
30 : 62 : return;
31 : : }
32 [ + - ]: 13 : QStringList families = QFontDatabase::applicationFontFamilies(fontId);
33 [ - + ]: 13 : if (families.isEmpty()) {
34 [ # # # # : 0 : qCWarning(lcMdi) << "MDI font has no families";
# # # # ]
35 : 0 : return;
36 : : }
37 [ + - + - ]: 13 : m_font = QFont(families.first());
38 [ + - + - : 26 : qCInfo(lcMdi) << "MDI font loaded:" << families.first();
+ - + - +
- + + ]
39 : :
40 : : // 2. Parse codemap CSV
41 [ + - ]: 13 : QFile f(QStringLiteral(":/mdi-codemap.csv"));
42 [ + - - + ]: 13 : if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
43 [ # # # # : 0 : qCWarning(lcMdi) << "Failed to open mdi-codemap.csv from resources";
# # # # ]
44 : 0 : return;
45 : : }
46 [ + - ]: 13 : QTextStream ts(&f);
47 [ + - ]: 13 : ts.readLine(); // skip header
48 [ + - ]: 13 : m_icons.clear();
49 [ + - ]: 13 : m_lookup.clear();
50 [ + - + + ]: 96837 : while (!ts.atEnd()) {
51 [ + - + - ]: 96824 : QString line = ts.readLine().trimmed();
52 [ - + ]: 96824 : if (line.isEmpty())
53 : 0 : continue;
54 : 96824 : int comma = line.indexOf(',');
55 [ - + ]: 96824 : if (comma < 0)
56 : 0 : continue;
57 [ + - ]: 96824 : QString name = line.left(comma);
58 : : bool ok;
59 [ + - + - ]: 96824 : uint cp = line.mid(comma + 1).toUInt(&ok, 16);
60 [ - + ]: 96824 : if (!ok)
61 : 0 : continue;
62 : 96824 : m_icons.append({name, cp});
63 [ + - ]: 96824 : m_lookup.insert(name, cp);
64 [ + - + - ]: 96824 : }
65 [ + - + - : 26 : qCInfo(lcMdi) << "MDI codemap loaded:" << m_icons.size() << "icons";
+ - + - +
- + + ]
66 : 13 : m_ready = true;
67 [ + - + - : 96837 : }
+ - - - ]
68 : :
69 : 74759 : QIcon MdiIconProvider::icon(const QString &name, int size,
70 : : const QColor &colorIn) {
71 [ + - ]: 74759 : ensureInit();
72 [ + + ]: 74759 : if (!m_ready)
73 : 62 : return {};
74 : :
75 : : // 67.B3: default (invalid) color → theme secondary text color
76 : : const QColor color =
77 : 74697 : colorIn.isValid()
78 : 74697 : ? colorIn
79 [ + - + - : 223657 : : QColor(ThemeManager::instance().color(
+ + - - ]
80 [ + + + + : 298354 : QStringLiteral("@text_secondary")));
+ + - - -
- ]
81 : :
82 [ + - ]: 74697 : auto it = m_lookup.constFind(name);
83 [ + - - + ]: 74697 : if (it == m_lookup.constEnd())
84 : 0 : return {};
85 : :
86 : 149394 : QString cacheKey = QStringLiteral("%1@%2@%3")
87 [ + - ]: 149394 : .arg(name)
88 [ + - ]: 149394 : .arg(size)
89 [ + - ]: 74697 : .arg(color.rgba(), 0, 16);
90 [ + + ]: 74697 : if (QIcon *cached = m_cache[cacheKey])
91 [ + - ]: 167 : return *cached;
92 : :
93 : : // Render glyph via QPainter
94 [ + - ]: 74530 : QPixmap pix(size, size);
95 [ + - ]: 74530 : pix.fill(Qt::transparent);
96 [ + - ]: 74530 : QPainter p(&pix);
97 [ + - ]: 74530 : QFont f = m_font;
98 [ + - ]: 74530 : f.setPixelSize(size - 2);
99 [ + - ]: 74530 : p.setFont(f);
100 [ + - ]: 74530 : p.setPen(color);
101 : 74530 : uint cp = it.value();
102 : 74530 : QString glyphStr;
103 [ + + ]: 74530 : if (cp <= 0xFFFF) {
104 [ + - ]: 10 : glyphStr = QChar(static_cast<ushort>(cp));
105 : : } else {
106 : 74520 : char32_t cp32 = static_cast<char32_t>(cp);
107 [ + - ]: 74520 : glyphStr = QString::fromUcs4(&cp32, 1);
108 : : }
109 [ + - ]: 74530 : p.drawText(QRect(0, 0, size, size), Qt::AlignCenter, glyphStr);
110 [ + - ]: 74530 : p.end();
111 : :
112 [ + - + - : 74530 : auto *icon = new QIcon(pix);
- + - - ]
113 [ + - ]: 74530 : m_cache.insert(cacheKey, icon);
114 [ + - ]: 74530 : return *icon;
115 : 74697 : }
116 : :
117 : 74769 : void MdiIconProvider::ensureInit() {
118 [ + + ]: 74769 : if (!m_ready)
119 : 75 : init();
120 : 74769 : }
|