-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiconmodel.cpp
More file actions
609 lines (504 loc) · 16.4 KB
/
iconmodel.cpp
File metadata and controls
609 lines (504 loc) · 16.4 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
#include "iconmodel.h"
#include <QDebug>
#include <QSvgRenderer>
#include <QPainter>
#include <QRegularExpression>
// Helper to adjust stroke-width in SVG source
// For fill-based icons: sliderPos maps to absolute values 0, 0.25, 0.5, 1, 1.25, 1.5
// For stroke-based icons: sliderPos maps to relative scales 0.5x, 0.75x, 1x, 1.25x, 1.5x
static QString adjustStrokeWidth(const QString &svgSource, int sliderPos, bool fillBased) {
static QRegularExpression rx(QStringLiteral("stroke-width=\"([0-9.]+)\""));
if (fillBased) {
// Fill-based: replace stroke-width with absolute value
static const double absValues[] = { 0.0, 0.25, 0.5, 1.0, 1.25, 1.5 };
double newWidth = absValues[qBound(0, sliderPos, 5)];
QString result = svgSource;
result.replace(rx, QString("stroke-width=\"%1\"").arg(newWidth));
return result;
} else {
// Stroke-based: scale existing stroke-width values
if (sliderPos == 2) // 1x scale, no change needed
return svgSource;
static const double scales[] = { 0.5, 0.75, 1.0, 1.25, 1.5 };
double scale = scales[qBound(0, sliderPos, 4)];
QString result = svgSource;
QRegularExpressionMatchIterator it = rx.globalMatch(svgSource);
// Process matches in reverse order to preserve positions
QList<QRegularExpressionMatch> matches;
while (it.hasNext())
matches.prepend(it.next());
for (const auto &match : matches) {
bool ok;
double width = match.captured(1).toDouble(&ok);
if (ok) {
double newWidth = width * scale;
if (newWidth < 0.25) newWidth = 0.25; // Minimum stroke width
QString replacement = QString("stroke-width=\"%1\"").arg(newWidth);
result.replace(match.capturedStart(), match.capturedLength(), replacement);
}
}
return result;
}
}
// ============================================================================
// IconModel
// ============================================================================
IconModel::IconModel(QObject *parent)
: QAbstractListModel(parent)
, m_pixmapCache(500) // Cache up to 500 rendered icons
{
}
IconModel::~IconModel() = default;
int IconModel::rowCount(const QModelIndex &parent) const {
if (parent.isValid())
return 0;
return static_cast<int>(m_filteredIndices.size());
}
QVariant IconModel::data(const QModelIndex &index, int role) const {
if (!index.isValid() || index.row() >= static_cast<int>(m_filteredIndices.size()))
return QVariant();
int actualIndex = m_filteredIndices[index.row()];
const IconEntry &entry = m_allIcons[actualIndex];
switch (role) {
case Qt::DisplayRole:
case IconNameRole:
return entry.name;
case Qt::DecorationRole:
return renderIcon(actualIndex);
case IconSvgRole:
return getIconSvg(actualIndex);
case IconIndexRole:
return entry.index;
case IconLibraryRole:
return entry.libraryName;
case Qt::ToolTipRole:
return entry.name;
default:
return QVariant();
}
}
QHash<int, QByteArray> IconModel::roleNames() const {
QHash<int, QByteArray> roles = QAbstractListModel::roleNames();
roles[IconNameRole] = "iconName";
roles[IconSvgRole] = "iconSvg";
roles[IconIndexRole] = "iconIndex";
roles[IconLibraryRole] = "iconLibrary";
return roles;
}
void IconModel::setIconList(IconList *list) {
beginResetModel();
m_iconList = list;
m_allIcons.clear();
m_filteredIndices.clear();
m_pixmapCache.clear();
if (m_iconList) {
// Apply current colors to the new list
if (auto *svg = dynamic_cast<SVGIconList*>(m_iconList)) {
svg->setFillColor(m_fillColor);
}
if (auto *twoTone = dynamic_cast<SVGTwoToneIconList*>(m_iconList)) {
twoTone->setToneColor(m_toneColor);
}
int count = m_iconList->getCount();
m_allIcons.reserve(count);
QString libraryName = m_iconList->getLibraryName();
for (int i = 0; i < count; ++i) {
IconEntry entry;
entry.name = m_iconList->getName(i);
entry.index = i;
entry.libraryName = libraryName;
m_allIcons.push_back(entry);
}
rebuildFilteredList();
}
endResetModel();
emit iconListChanged();
}
IconList *IconModel::iconList() const {
return m_iconList;
}
SVGIconList *IconModel::svgIconList() const {
if (m_iconList && m_iconList->isSVG())
return static_cast<SVGIconList*>(m_iconList);
return nullptr;
}
BitmapIconList *IconModel::bitmapIconList() const {
if (m_iconList && m_iconList->isBitmap())
return static_cast<BitmapIconList*>(m_iconList);
return nullptr;
}
bool IconModel::isBitmapMode() const {
return m_iconList && m_iconList->isBitmap();
}
void IconModel::setIconSize(int size) {
if (m_iconSize != size) {
m_iconSize = size;
m_pixmapCache.clear();
emit dataChanged(index(0), index(rowCount() - 1), {Qt::DecorationRole});
}
}
int IconModel::iconSize() const {
return m_iconSize;
}
void IconModel::setFillColor(const QColor &color) {
if (m_fillColor != color) {
m_fillColor = color;
if (auto *svg = svgIconList()) {
svg->setFillColor(color);
}
m_pixmapCache.clear();
emit dataChanged(index(0), index(rowCount() - 1), {Qt::DecorationRole});
}
}
QColor IconModel::fillColor() const {
return m_fillColor;
}
void IconModel::setToneColor(const QColor &color) {
if (m_toneColor != color) {
m_toneColor = color;
if (auto *twoTone = dynamic_cast<SVGTwoToneIconList*>(m_iconList)) {
twoTone->setToneColor(color);
}
m_pixmapCache.clear();
emit dataChanged(index(0), index(rowCount() - 1), {Qt::DecorationRole});
}
}
QColor IconModel::toneColor() const {
return m_toneColor;
}
bool IconModel::isTwoTone() const {
return dynamic_cast<SVGTwoToneIconList*>(m_iconList) != nullptr;
}
void IconModel::setGrayscale(bool enabled) {
if (m_grayscale != enabled) {
m_grayscale = enabled;
if (auto *bitmap = bitmapIconList()) {
bitmap->setGrayscale(enabled);
}
m_pixmapCache.clear();
emit dataChanged(index(0), index(rowCount() - 1), {Qt::DecorationRole});
}
}
bool IconModel::isGrayscale() const {
return m_grayscale;
}
void IconModel::setBackgroundColor(const QColor &color) {
if (m_backgroundColor != color) {
m_backgroundColor = color;
m_pixmapCache.clear();
emit dataChanged(index(0), index(rowCount() - 1), {Qt::DecorationRole});
}
}
QColor IconModel::backgroundColor() const {
return m_backgroundColor;
}
void IconModel::setStrokeWidth(int width) {
int maxValue = m_fillBasedStroke ? 5 : 4;
width = qBound(0, width, maxValue);
if (m_strokeWidth != width) {
m_strokeWidth = width;
m_pixmapCache.clear();
emit dataChanged(index(0), index(rowCount() - 1), {Qt::DecorationRole});
}
}
void IconModel::setStrokeMode(bool fillBased) {
if (m_fillBasedStroke != fillBased) {
m_fillBasedStroke = fillBased;
m_pixmapCache.clear();
emit dataChanged(index(0), index(rowCount() - 1), {Qt::DecorationRole});
}
}
int IconModel::strokeWidth() const {
return m_strokeWidth;
}
void IconModel::setFilter(const QString &filter) {
if (m_filter != filter) {
beginResetModel();
m_filter = filter;
rebuildFilteredList();
endResetModel();
emit filterChanged();
}
}
QString IconModel::filter() const {
return m_filter;
}
QPixmap IconModel::getIconPixmap(int index) const {
return renderIcon(index);
}
QPixmap IconModel::getIconPixmapAtSize(int index, int size) const {
if (!m_iconList || index < 0 || index >= static_cast<int>(m_allIcons.size()))
return QPixmap();
int actualIndex = m_allIcons[index].index;
if (auto *bitmap = bitmapIconList()) {
// Bitmap icon - get and scale
QPixmap pixmap = bitmap->getPixmap(actualIndex);
if (!pixmap.isNull() && (pixmap.width() != size || pixmap.height() != size)) {
pixmap = pixmap.scaled(size, size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
if (m_backgroundColor.alpha() > 0) {
QPixmap withBg(pixmap.size());
withBg.fill(m_backgroundColor);
QPainter painter(&withBg);
painter.drawPixmap(0, 0, pixmap);
painter.end();
return withBg;
}
return pixmap;
}
if (auto *svg = svgIconList()) {
// SVG icon - render at requested size
QString svgSource = svg->getSource(actualIndex);
if (svgSource.isEmpty())
return QPixmap();
// Resolve entities if present
EntityMap entities = currentEntities(index);
if (!entities.isEmpty()) {
svgSource = SVGIconList::resolveEntities(svgSource, entities);
}
// Apply stroke width adjustment
svgSource = adjustStrokeWidth(svgSource, m_strokeWidth, m_fillBasedStroke);
QSvgRenderer renderer(svgSource.toUtf8());
if (!renderer.isValid())
return QPixmap();
QPixmap pixmap(size, size);
pixmap.fill(m_backgroundColor);
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
renderer.render(&painter);
painter.end();
return pixmap;
}
return QPixmap();
}
QString IconModel::getIconSvg(int index) const {
if (!m_iconList || index < 0 || index >= static_cast<int>(m_allIcons.size()))
return QString();
if (auto *svg = svgIconList()) {
QString source = svg->getSource(index);
// Resolve entities if present
EntityMap entities = currentEntities(index);
if (!entities.isEmpty()) {
source = SVGIconList::resolveEntities(source, entities);
}
// Apply stroke width adjustment
source = adjustStrokeWidth(source, m_strokeWidth, m_fillBasedStroke);
// Replace "currentColor" with actual fill color
if (m_fillColor.isValid() && m_fillColor.alpha() > 0) {
source.replace("currentColor", m_fillColor.name());
}
return source;
}
return QString(); // No SVG for bitmap icons
}
QString IconModel::getIconName(int index) const {
if (index < 0 || index >= static_cast<int>(m_allIcons.size()))
return QString();
return m_allIcons[index].name;
}
QStringList IconModel::getIconAliases(int index) const {
if (!m_iconList || index < 0 || index >= static_cast<int>(m_allIcons.size()))
return QStringList();
if (auto *bitmap = bitmapIconList())
return bitmap->getAliases(index);
return QStringList(); // No aliases for SVG icons
}
QStringList IconModel::getIconTags(int index) const {
if (!m_iconList || index < 0 || index >= static_cast<int>(m_allIcons.size()))
return QStringList();
if (auto *svg = svgIconList())
return svg->getTags(index);
return QStringList();
}
QString IconModel::getIconCategory(int index) const {
if (!m_iconList || index < 0 || index >= static_cast<int>(m_allIcons.size()))
return QString();
if (auto *svg = svgIconList())
return svg->getCategory(index);
return QString();
}
EntityMap IconModel::getIconEntities(int index) const {
if (!m_iconList || index < 0 || index >= static_cast<int>(m_allIcons.size()))
return EntityMap();
if (auto *svg = svgIconList())
return svg->getEntities(index);
return EntityMap();
}
bool IconModel::iconHasEntities(int index) const {
return !getIconEntities(index).isEmpty();
}
void IconModel::setIconEntities(int index, const EntityMap &entities) {
if (index < 0 || index >= static_cast<int>(m_allIcons.size()))
return;
m_customEntities[index] = entities;
m_pixmapCache.remove(index); // Force re-render
}
EntityMap IconModel::currentEntities(int index) const {
if (m_customEntities.contains(index))
return m_customEntities[index];
return getIconEntities(index);
}
void IconModel::refresh() {
m_pixmapCache.clear();
emit dataChanged(index(0), index(rowCount() - 1));
}
void IconModel::clearCache() {
m_pixmapCache.clear();
}
QPixmap IconModel::renderIcon(int index) const {
if (!m_iconList || index < 0 || index >= static_cast<int>(m_allIcons.size()))
return QPixmap();
// Check cache first
if (QPixmap *cached = m_pixmapCache.object(index))
return *cached;
int actualIndex = m_allIcons[index].index;
QPixmap pixmap;
if (auto *bitmap = bitmapIconList()) {
// Bitmap icon - get directly from resource
pixmap = bitmap->getPixmap(actualIndex);
if (!pixmap.isNull() && (pixmap.width() != m_iconSize || pixmap.height() != m_iconSize)) {
pixmap = pixmap.scaled(m_iconSize, m_iconSize,
Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
} else if (auto *svg = svgIconList()) {
// SVG icon - render from source
QString svgSource = svg->getSource(actualIndex);
if (svgSource.isEmpty())
return QPixmap();
// Resolve entities if present
EntityMap entities = currentEntities(index);
if (!entities.isEmpty()) {
svgSource = SVGIconList::resolveEntities(svgSource, entities);
}
// Apply stroke width adjustment
svgSource = adjustStrokeWidth(svgSource, m_strokeWidth, m_fillBasedStroke);
QSvgRenderer renderer(svgSource.toUtf8());
if (!renderer.isValid())
return QPixmap();
pixmap = QPixmap(m_iconSize, m_iconSize);
pixmap.fill(m_backgroundColor);
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
renderer.render(&painter);
painter.end();
}
if (pixmap.isNull())
return pixmap;
// Apply background for bitmap icons if needed
if (isBitmapMode() && m_backgroundColor.alpha() > 0) {
QPixmap withBg(pixmap.size());
withBg.fill(m_backgroundColor);
QPainter painter(&withBg);
painter.drawPixmap(0, 0, pixmap);
painter.end();
pixmap = withBg;
}
// Cache the result
m_pixmapCache.insert(index, new QPixmap(pixmap));
return pixmap;
}
void IconModel::rebuildFilteredList() {
m_filteredIndices.clear();
if (m_filter.isEmpty()) {
// No filter - show all icons
m_filteredIndices.reserve(m_allIcons.size());
for (size_t i = 0; i < m_allIcons.size(); ++i) {
m_filteredIndices.push_back(static_cast<int>(i));
}
} else {
// Filter by name (case-insensitive)
QRegularExpression regex(
QRegularExpression::escape(m_filter),
QRegularExpression::CaseInsensitiveOption
);
for (size_t i = 0; i < m_allIcons.size(); ++i) {
if (m_allIcons[i].name.contains(regex)) {
m_filteredIndices.push_back(static_cast<int>(i));
}
}
}
}
// ============================================================================
// IconCollectionRegistry
// ============================================================================
IconCollectionRegistry &IconCollectionRegistry::instance() {
static IconCollectionRegistry registry;
return registry;
}
void IconCollectionRegistry::registerCollection(const IconCollection &collection) {
m_collections.append(collection);
emit collectionsChanged();
}
QList<IconCollection> IconCollectionRegistry::collections() const {
return m_collections;
}
const IconCollection *IconCollectionRegistry::findCollection(const QString &id) const {
for (const auto &coll : m_collections) {
if (coll.id == id)
return &coll;
}
return nullptr;
}
SVGIconList *IconCollectionRegistry::createIconList(const QString &collectionId, IconStyle style, int size) const {
const IconCollection *coll = findCollection(collectionId);
if (!coll)
return nullptr;
// Handle TwoTone - requires both Outline and Filled
if (style == IconStyle::TwoTone) {
if (coll->hasStyle(IconStyle::Filled) && coll->hasStyle(IconStyle::Outline)) {
auto filledFactory = coll->styles.at(IconStyle::Filled);
auto outlineFactory = coll->styles.at(IconStyle::Outline);
return new TwoToneIconList(filledFactory(size), outlineFactory(size));
}
// Fallback to Outline if TwoTone not possible
style = IconStyle::Outline;
}
auto it = coll->styles.find(style);
if (it != coll->styles.end())
return it->second(size);
// Try fallback styles
if (coll->hasStyle(IconStyle::Outline))
return coll->styles.at(IconStyle::Outline)(size);
if (coll->hasStyle(IconStyle::Filled))
return coll->styles.at(IconStyle::Filled)(size);
return nullptr;
}
void IconCollectionRegistry::registerBitmapCollection(const BitmapCollection &collection) {
m_bitmapCollections.append(collection);
emit collectionsChanged();
}
QList<BitmapCollection> IconCollectionRegistry::bitmapCollections() const {
return m_bitmapCollections;
}
const BitmapCollection *IconCollectionRegistry::findBitmapCollection(const QString &id) const {
for (const auto &coll : m_bitmapCollections) {
if (coll.id == id)
return &coll;
}
return nullptr;
}
BitmapIconList *IconCollectionRegistry::createBitmapList(const QString &collectionId, int size) const {
const BitmapCollection *coll = findBitmapCollection(collectionId);
if (!coll || !coll->factory)
return nullptr;
// Use requested size or default
int actualSize = coll->availableSizes.contains(size) ? size : coll->defaultSize();
return coll->factory(actualSize);
}
QStringList IconCollectionRegistry::allCollectionNames() const {
QStringList names;
for (const auto &coll : m_collections)
names.append(coll.displayName);
for (const auto &coll : m_bitmapCollections)
names.append(coll.displayName);
return names;
}
bool IconCollectionRegistry::isBitmapCollection(const QString &displayName) const {
for (const auto &coll : m_bitmapCollections) {
if (coll.displayName == displayName)
return true;
}
return false;
}