-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcyberelems.cpp
More file actions
938 lines (803 loc) · 28.2 KB
/
cyberelems.cpp
File metadata and controls
938 lines (803 loc) · 28.2 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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
/*
* CyberElems — Cyberpunk HUD Element Viewer & Color Editor
*
* Single-file Qt application for browsing, previewing, and customizing
* 90 cyberpunk SVG HUD elements from the react-cyber-elements library.
*
* Color manipulation mirrors the React library approach:
* - fil0/fil1 CSS classes map to primary/secondary fill colors
* - Per-path color override via nth-of-type equivalent
* - Stroke color/width adjustment
* - Fill opacity control
*/
#include <QApplication>
#include <QMainWindow>
#include <QWidget>
#include <QListView>
#include <QAbstractListModel>
#include <QStyledItemDelegate>
#include <QSvgRenderer>
#include <QPainter>
#include <QPixmap>
#include <QCache>
#include <QColor>
#include <QColorDialog>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QSplitter>
#include <QLineEdit>
#include <QToolButton>
#include <QComboBox>
#include <QSlider>
#include <QLabel>
#include <QCheckBox>
#include <QGroupBox>
#include <QStatusBar>
#include <QMenuBar>
#include <QAction>
#include <QActionGroup>
#include <QFileDialog>
#include <QClipboard>
#include <QMimeData>
#include <QRegularExpression>
#include <QDir>
#include <QDebug>
#include <QScrollBar>
#include <vector>
// ============================================================================
// CyberElement Data
// ============================================================================
struct CyberElementData {
QString name;
QString originalSvg;
};
static std::vector<CyberElementData> g_elements;
static void loadElements()
{
QDir dir(":/cyberelems/content");
QStringList files = dir.entryList({"CyberEl*.svg"}, QDir::Files, QDir::Name);
// Sort numerically
std::sort(files.begin(), files.end(), [](const QString &a, const QString &b) {
static QRegularExpression rx("(\\d+)");
int na = rx.match(a).captured(1).toInt();
int nb = rx.match(b).captured(1).toInt();
return na < nb;
});
for (const QString &file : files) {
QFile f(":/cyberelems/content/" + file);
if (f.open(QIODevice::ReadOnly)) {
CyberElementData el;
el.name = file.section('.', 0, 0); // "CyberEl1"
el.originalSvg = QString::fromUtf8(f.readAll());
g_elements.push_back(std::move(el));
}
}
}
// ============================================================================
// SVG Color Manipulation (mirrors React CSS class approach)
// ============================================================================
struct ColorSettings {
QColor primaryFill = QColor("#252626"); // fil0 — dark
QColor secondaryFill = QColor("#767676"); // fil1 — light
QColor strokeColor = Qt::transparent;
double strokeWidth = 0.0;
double fillOpacity = 1.0;
QColor bgColor = Qt::transparent;
bool isDefault() const {
return primaryFill == QColor("#252626")
&& secondaryFill == QColor("#767676")
&& strokeColor == Qt::transparent
&& strokeWidth == 0.0
&& fillOpacity == 1.0;
}
};
static QString applyColorSettings(const QString &svg, const ColorSettings &cs)
{
QString result = svg;
// Replace primary fill color (fil0 class default: #252626)
if (cs.primaryFill != QColor("#252626")) {
result.replace(QLatin1String("fill:#252626"), "fill:" + cs.primaryFill.name());
result.replace(QLatin1String("fill: #252626"), "fill: " + cs.primaryFill.name());
}
// Replace secondary fill color (fil1 class default: #767676)
if (cs.secondaryFill != QColor("#767676")) {
result.replace(QLatin1String("fill:#767676"), "fill:" + cs.secondaryFill.name());
result.replace(QLatin1String("fill: #767676"), "fill: " + cs.secondaryFill.name());
}
// Some elements use #231f1f or other dark fills — map to primary
if (cs.primaryFill != QColor("#252626")) {
result.replace(QLatin1String("fill:#231f1f"), "fill:" + cs.primaryFill.name());
result.replace(QLatin1String("fill: #231f1f"), "fill: " + cs.primaryFill.name());
}
// Apply fill opacity to all paths
if (cs.fillOpacity < 1.0) {
QString opStr = QString::number(cs.fillOpacity, 'f', 2);
// Add fill-opacity to existing paths
static QRegularExpression rxPath("<path");
result.replace(rxPath, "<path fill-opacity=\"" + opStr + "\"");
}
// Apply stroke to paths
if (cs.strokeColor.alpha() > 0 && cs.strokeWidth > 0) {
QString strokeAttr = QString(" stroke=\"%1\" stroke-width=\"%2\"")
.arg(cs.strokeColor.name())
.arg(cs.strokeWidth, 0, 'f', 1);
static QRegularExpression rxPath2("<path");
result.replace(rxPath2, "<path" + strokeAttr);
}
return result;
}
// ============================================================================
// CyberElementModel — QAbstractListModel for the grid
// ============================================================================
enum CyberRoles {
NameRole = Qt::UserRole + 1,
SvgRole,
IndexRole
};
class CyberElementModel : public QAbstractListModel {
Q_OBJECT
public:
explicit CyberElementModel(QObject *parent = nullptr)
: QAbstractListModel(parent), m_cache(200) {}
int rowCount(const QModelIndex & = QModelIndex()) const override {
return static_cast<int>(m_filtered.size());
}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override {
if (!index.isValid() || index.row() >= rowCount())
return {};
int idx = m_filtered[index.row()];
switch (role) {
case Qt::DisplayRole:
case NameRole:
return g_elements[idx].name;
case Qt::DecorationRole:
return renderIcon(idx);
case SvgRole:
return modifiedSvg(idx);
case IndexRole:
return idx;
case Qt::ToolTipRole:
return g_elements[idx].name;
}
return {};
}
void setFilter(const QString &f) {
beginResetModel();
m_filter = f;
rebuildFiltered();
endResetModel();
}
void setIconSize(int s) {
if (m_iconSize != s) {
m_iconSize = s;
m_cache.clear();
emit dataChanged(index(0), index(rowCount()-1), {Qt::DecorationRole});
}
}
int iconSize() const { return m_iconSize; }
void setColorSettings(const ColorSettings &cs) {
m_colors = cs;
m_cache.clear();
emit dataChanged(index(0), index(rowCount()-1), {Qt::DecorationRole, SvgRole});
}
const ColorSettings &colorSettings() const { return m_colors; }
QString modifiedSvg(int idx) const {
return applyColorSettings(g_elements[idx].originalSvg, m_colors);
}
QPixmap renderIconAtSize(int idx, int size) const {
QString svg = modifiedSvg(idx);
QSvgRenderer renderer(svg.toUtf8());
if (!renderer.isValid())
return {};
QPixmap pm(size, size);
pm.fill(m_colors.bgColor);
QPainter p(&pm);
p.setRenderHint(QPainter::Antialiasing);
p.setRenderHint(QPainter::SmoothPixmapTransform);
renderer.render(&p);
return pm;
}
int actualIndex(int row) const {
if (row < 0 || row >= rowCount()) return -1;
return m_filtered[row];
}
int filteredCount() const { return rowCount(); }
void refresh() {
m_cache.clear();
emit dataChanged(index(0), index(rowCount()-1));
}
private:
QPixmap renderIcon(int idx) const {
if (QPixmap *pm = m_cache.object(idx))
return *pm;
QPixmap pm = renderIconAtSize(idx, m_iconSize);
m_cache.insert(idx, new QPixmap(pm));
return pm;
}
void rebuildFiltered() {
m_filtered.clear();
QRegularExpression rx(QRegularExpression::escape(m_filter),
QRegularExpression::CaseInsensitiveOption);
for (int i = 0; i < (int)g_elements.size(); ++i) {
if (m_filter.isEmpty() || g_elements[i].name.contains(rx))
m_filtered.push_back(i);
}
}
std::vector<int> m_filtered;
QString m_filter;
int m_iconSize = 48;
ColorSettings m_colors;
mutable QCache<int, QPixmap> m_cache;
};
// ============================================================================
// CyberDelegate — Custom rendering for grid cells
// ============================================================================
class CyberDelegate : public QStyledItemDelegate {
public:
explicit CyberDelegate(QObject *parent = nullptr)
: QStyledItemDelegate(parent) {}
void setIconSize(int s) { m_size = s; }
void paint(QPainter *painter, const QStyleOptionViewItem &opt,
const QModelIndex &idx) const override
{
painter->save();
QRect r = opt.rect;
// Selection/hover highlight
if (opt.state & QStyle::State_Selected) {
painter->fillRect(r, QColor(0, 229, 255, 40));
painter->setPen(QPen(QColor(0, 229, 255), 1));
painter->drawRect(r.adjusted(0, 0, -1, -1));
} else if (opt.state & QStyle::State_MouseOver) {
painter->fillRect(r, QColor(255, 255, 255, 10));
}
// Icon
QPixmap pm = idx.data(Qt::DecorationRole).value<QPixmap>();
if (!pm.isNull()) {
int x = r.x() + (r.width() - pm.width()) / 2;
int y = r.y() + m_padding;
painter->drawPixmap(x, y, pm);
}
// Name
painter->setPen(QColor(140, 140, 160));
QFont f = painter->font();
f.setPixelSize(10);
painter->setFont(f);
QRect textRect(r.x(), r.bottom() - m_nameHeight, r.width(), m_nameHeight);
QString name = idx.data(Qt::DisplayRole).toString();
painter->drawText(textRect, Qt::AlignHCenter | Qt::AlignTop, name);
painter->restore();
}
QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const override {
return QSize(m_size + m_padding * 2, m_size + m_padding * 2 + m_nameHeight);
}
private:
int m_size = 48;
int m_padding = 8;
int m_nameHeight = 18;
};
// ============================================================================
// ColorButton — Color picker button with swatch
// ============================================================================
class ColorButton : public QToolButton {
Q_OBJECT
public:
explicit ColorButton(const QColor &initial, QWidget *parent = nullptr)
: QToolButton(parent), m_color(initial)
{
setFixedSize(28, 28);
updateIcon();
connect(this, &QToolButton::clicked, this, &ColorButton::pickColor);
}
QColor color() const { return m_color; }
void setColor(const QColor &c) {
if (m_color != c) {
m_color = c;
updateIcon();
emit colorChanged(m_color);
}
}
signals:
void colorChanged(const QColor &color);
private slots:
void pickColor() {
QColor c = QColorDialog::getColor(m_color, this, "Pick Color",
QColorDialog::ShowAlphaChannel);
if (c.isValid())
setColor(c);
}
private:
void updateIcon() {
QPixmap pm(20, 20);
pm.fill(m_color);
QPainter p(&pm);
p.setPen(QColor(80, 80, 80));
p.drawRect(0, 0, 19, 19);
setIcon(QIcon(pm));
}
QColor m_color;
};
// ============================================================================
// PreviewPanel — Shows selected element with controls
// ============================================================================
class PreviewPanel : public QWidget {
Q_OBJECT
public:
explicit PreviewPanel(QWidget *parent = nullptr) : QWidget(parent)
{
auto *layout = new QVBoxLayout(this);
layout->setContentsMargins(8, 8, 8, 8);
// Preview area
m_previewLabel = new QLabel;
m_previewLabel->setAlignment(Qt::AlignCenter);
m_previewLabel->setMinimumSize(200, 200);
m_previewLabel->setStyleSheet("background: #0a0a0f; border: 1px solid #1e1e2e;");
layout->addWidget(m_previewLabel);
// Name
m_nameLabel = new QLabel;
m_nameLabel->setAlignment(Qt::AlignCenter);
m_nameLabel->setStyleSheet("color: #00e5ff; font-weight: bold; font-size: 13px;");
layout->addWidget(m_nameLabel);
// --- Color Controls ---
auto *colorGroup = new QGroupBox("Colors");
auto *cgl = new QVBoxLayout(colorGroup);
cgl->setSpacing(6);
// Primary fill
auto *r1 = new QHBoxLayout;
r1->addWidget(new QLabel("Primary (fil0):"));
m_primaryBtn = new ColorButton(QColor("#252626"));
r1->addWidget(m_primaryBtn);
r1->addStretch();
cgl->addLayout(r1);
// Secondary fill
auto *r2 = new QHBoxLayout;
r2->addWidget(new QLabel("Secondary (fil1):"));
m_secondaryBtn = new ColorButton(QColor("#767676"));
r2->addWidget(m_secondaryBtn);
r2->addStretch();
cgl->addLayout(r2);
// Stroke color
auto *r3 = new QHBoxLayout;
r3->addWidget(new QLabel("Stroke:"));
m_strokeColorBtn = new ColorButton(Qt::transparent);
r3->addWidget(m_strokeColorBtn);
r3->addStretch();
cgl->addLayout(r3);
// Stroke width
auto *r4 = new QHBoxLayout;
r4->addWidget(new QLabel("Stroke Width:"));
m_strokeWidthSlider = new QSlider(Qt::Horizontal);
m_strokeWidthSlider->setRange(0, 50);
m_strokeWidthSlider->setValue(0);
m_strokeWidthLabel = new QLabel("0.0");
m_strokeWidthLabel->setFixedWidth(30);
r4->addWidget(m_strokeWidthSlider);
r4->addWidget(m_strokeWidthLabel);
cgl->addLayout(r4);
// Fill opacity
auto *r5 = new QHBoxLayout;
r5->addWidget(new QLabel("Fill Opacity:"));
m_opacitySlider = new QSlider(Qt::Horizontal);
m_opacitySlider->setRange(0, 100);
m_opacitySlider->setValue(100);
m_opacityLabel = new QLabel("1.00");
m_opacityLabel->setFixedWidth(30);
r5->addWidget(m_opacitySlider);
r5->addWidget(m_opacityLabel);
cgl->addLayout(r5);
// Background color
auto *r6 = new QHBoxLayout;
r6->addWidget(new QLabel("Background:"));
m_bgColorBtn = new ColorButton(Qt::transparent);
r6->addWidget(m_bgColorBtn);
r6->addStretch();
cgl->addLayout(r6);
layout->addWidget(colorGroup);
// Presets
auto *presetGroup = new QGroupBox("Presets");
auto *pgl = new QGridLayout(presetGroup);
pgl->setSpacing(4);
struct Preset { const char *name; const char *primary; const char *secondary; const char *bg; };
static const Preset presets[] = {
// From react-cyber-elements demo (ss1.png)
{"Original Light", "#252626", "#767676", "#ffffff"},
{"Dark Gray", "#888888", "#555555", "#1a1a2e"},
{"Neon Magenta+Yellow","#ff00ff", "#ffff00", "#000000"},
{"Blue Neon", "#0066ff", "#0044cc", "#0a0a1a"},
{"Pink Pastel", "#ffb0c8", "#e890b0", "#ffffff"},
// Extra presets
{"Original Dark", "#252626", "#767676", "#0a0a0f"},
{"Neon Cyan", "#00e5ff", "#006a75", "#0a0a0f"},
{"Neon Green", "#39ff14", "#1a8a0a", "#000000"},
{"Gold", "#ffd700", "#b8860b", "#0a0a0f"},
{"Blueprint", "#4488ff", "#2255aa", "#0c1428"},
{"Terminal", "#00ff00", "#008800", "#000000"},
{"Red Alert", "#ff0040", "#880022", "#0f0008"},
};
int col = 0, row = 0;
for (auto &p : presets) {
auto *btn = new QToolButton;
btn->setFixedSize(28, 28);
btn->setToolTip(p.name);
QPixmap pm(24, 24);
QPainter painter(&pm);
painter.fillRect(0, 0, 24, 24, QColor(p.bg));
painter.fillRect(2, 2, 10, 20, QColor(p.primary));
painter.fillRect(12, 2, 10, 20, QColor(p.secondary));
painter.setPen(QColor(80, 80, 80));
painter.drawRect(0, 0, 23, 23);
painter.end();
btn->setIcon(QIcon(pm));
btn->setIconSize(QSize(24, 24));
QString pri = p.primary;
QString sec = p.secondary;
QString bg = p.bg;
connect(btn, &QToolButton::clicked, this, [this, pri, sec, bg]() {
m_primaryBtn->setColor(QColor(pri));
m_secondaryBtn->setColor(QColor(sec));
m_bgColorBtn->setColor(QColor(bg));
});
pgl->addWidget(btn, row, col);
if (++col >= 6) { col = 0; ++row; }
}
layout->addWidget(presetGroup);
// Buttons
auto *btnRow = new QHBoxLayout;
auto *resetBtn = new QToolButton;
resetBtn->setText("Reset");
auto *copySvgBtn = new QToolButton;
copySvgBtn->setText("Copy SVG");
auto *exportSvgBtn = new QToolButton;
exportSvgBtn->setText("Export SVG");
auto *exportPngBtn = new QToolButton;
exportPngBtn->setText("Export PNG");
btnRow->addWidget(resetBtn);
btnRow->addWidget(copySvgBtn);
btnRow->addWidget(exportSvgBtn);
btnRow->addWidget(exportPngBtn);
layout->addLayout(btnRow);
layout->addStretch();
// Connections
connect(m_primaryBtn, &ColorButton::colorChanged, this, &PreviewPanel::onColorChanged);
connect(m_secondaryBtn, &ColorButton::colorChanged, this, &PreviewPanel::onColorChanged);
connect(m_strokeColorBtn, &ColorButton::colorChanged, this, &PreviewPanel::onColorChanged);
connect(m_bgColorBtn, &ColorButton::colorChanged, this, &PreviewPanel::onColorChanged);
connect(m_strokeWidthSlider, &QSlider::valueChanged, this, [this](int v) {
m_strokeWidthLabel->setText(QString::number(v / 10.0, 'f', 1));
onColorChanged();
});
connect(m_opacitySlider, &QSlider::valueChanged, this, [this](int v) {
m_opacityLabel->setText(QString::number(v / 100.0, 'f', 2));
onColorChanged();
});
connect(resetBtn, &QToolButton::clicked, this, &PreviewPanel::resetColors);
connect(copySvgBtn, &QToolButton::clicked, this, &PreviewPanel::copySvg);
connect(exportSvgBtn, &QToolButton::clicked, this, &PreviewPanel::exportSvg);
connect(exportPngBtn, &QToolButton::clicked, this, &PreviewPanel::exportPng);
}
void setElement(int idx, const QPixmap &pm, const QString &name) {
m_currentIndex = idx;
m_nameLabel->setText(name);
updatePreview(pm);
}
void updatePreview(const QPixmap &pm) {
if (pm.isNull()) {
m_previewLabel->setText("No element selected");
return;
}
QSize targetSize = m_previewLabel->minimumSize();
QPixmap scaled = pm.scaled(targetSize, Qt::KeepAspectRatio,
Qt::SmoothTransformation);
m_previewLabel->setPixmap(scaled);
}
ColorSettings currentSettings() const {
ColorSettings cs;
cs.primaryFill = m_primaryBtn->color();
cs.secondaryFill = m_secondaryBtn->color();
cs.strokeColor = m_strokeColorBtn->color();
cs.strokeWidth = m_strokeWidthSlider->value() / 10.0;
cs.fillOpacity = m_opacitySlider->value() / 100.0;
cs.bgColor = m_bgColorBtn->color();
return cs;
}
int currentIndex() const { return m_currentIndex; }
signals:
void colorSettingsChanged(const ColorSettings &cs);
void copySvgRequested();
void exportSvgRequested();
void exportPngRequested();
private slots:
void onColorChanged() {
emit colorSettingsChanged(currentSettings());
}
void resetColors() {
m_primaryBtn->setColor(QColor("#252626"));
m_secondaryBtn->setColor(QColor("#767676"));
m_strokeColorBtn->setColor(Qt::transparent);
m_strokeWidthSlider->setValue(0);
m_opacitySlider->setValue(100);
m_bgColorBtn->setColor(Qt::transparent);
}
void copySvg() { emit copySvgRequested(); }
void exportSvg() { emit exportSvgRequested(); }
void exportPng() { emit exportPngRequested(); }
private:
QLabel *m_previewLabel;
QLabel *m_nameLabel;
ColorButton *m_primaryBtn;
ColorButton *m_secondaryBtn;
ColorButton *m_strokeColorBtn;
ColorButton *m_bgColorBtn;
QSlider *m_strokeWidthSlider;
QLabel *m_strokeWidthLabel;
QSlider *m_opacitySlider;
QLabel *m_opacityLabel;
int m_currentIndex = -1;
};
// ============================================================================
// MainWindow
// ============================================================================
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr)
: QMainWindow(parent)
{
setWindowTitle(QString("CyberElems (ver. %1, build %2)")
.arg(QStringLiteral(APP_VERSION))
.arg(APP_BUILD));
resize(1100, 750);
// Central widget
auto *central = new QWidget;
setCentralWidget(central);
auto *mainLayout = new QVBoxLayout(central);
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->setSpacing(0);
// Toolbar row
auto *toolbar = new QWidget;
toolbar->setFixedHeight(36);
toolbar->setStyleSheet("background: #12121a; border-bottom: 1px solid #1e1e2e;");
auto *tbLayout = new QHBoxLayout(toolbar);
tbLayout->setContentsMargins(8, 4, 8, 4);
tbLayout->setSpacing(8);
// Search
m_search = new QLineEdit;
m_search->setPlaceholderText("Filter elements...");
m_search->setFixedWidth(200);
m_search->setStyleSheet(
"QLineEdit { background: #0a0a0f; border: 1px solid #1e1e2e; "
"border-radius: 4px; color: #c0c0d0; padding: 4px 8px; }"
"QLineEdit:focus { border-color: #00e5ff; }");
tbLayout->addWidget(m_search);
// Cell size slider (snaps to predefined sizes)
tbLayout->addWidget(new QLabel("Cell:"));
m_cellSizeSlider = new QSlider(Qt::Horizontal);
m_cellSizeSlider->setRange(0, 5);
m_cellSizeSlider->setValue(2); // 48
m_cellSizeSlider->setFixedWidth(100);
m_cellSizeSlider->setTickPosition(QSlider::TicksBelow);
m_cellSizeSlider->setTickInterval(1);
tbLayout->addWidget(m_cellSizeSlider);
m_cellSizeLabel = new QLabel("48");
m_cellSizeLabel->setFixedWidth(28);
tbLayout->addWidget(m_cellSizeLabel);
tbLayout->addStretch();
// Count label
m_countLabel = new QLabel;
m_countLabel->setStyleSheet("color: #666;");
tbLayout->addWidget(m_countLabel);
mainLayout->addWidget(toolbar);
// Splitter: grid | preview
m_splitter = new QSplitter(Qt::Horizontal);
// Grid
m_listView = new QListView;
m_listView->setViewMode(QListView::IconMode);
m_listView->setResizeMode(QListView::Adjust);
m_listView->setUniformItemSizes(true);
m_listView->setSelectionMode(QAbstractItemView::SingleSelection);
m_listView->setMovement(QListView::Static);
m_listView->setWrapping(true);
m_listView->setSpacing(4);
m_listView->setStyleSheet(
"QListView { background: #0a0a0f; border: none; }"
"QListView::item:selected { background: transparent; }"
"QListView::item:hover { background: transparent; }");
m_model = new CyberElementModel(this);
m_delegate = new CyberDelegate(this);
m_listView->setModel(m_model);
m_listView->setItemDelegate(m_delegate);
m_splitter->addWidget(m_listView);
// Preview panel
m_preview = new PreviewPanel;
m_preview->setFixedWidth(300);
m_splitter->addWidget(m_preview);
mainLayout->addWidget(m_splitter);
// Status bar
statusBar()->showMessage("Ready");
// Menu
setupMenus();
setupConnections();
// Initial load (g_elements already populated in main())
m_model->setFilter("");
setCellSize(48);
updateCount();
}
private slots:
void onSelectionChanged(const QModelIndex ¤t, const QModelIndex &) {
if (!current.isValid()) return;
int idx = m_model->actualIndex(current.row());
if (idx < 0) return;
m_selectedIndex = idx;
QPixmap pm = m_model->renderIconAtSize(idx, 200);
m_preview->setElement(idx, pm, g_elements[idx].name);
statusBar()->showMessage(g_elements[idx].name);
}
void onColorSettingsChanged(const ColorSettings &cs) {
m_model->setColorSettings(cs);
// Update preview
if (m_selectedIndex >= 0) {
QPixmap pm = m_model->renderIconAtSize(m_selectedIndex, 200);
m_preview->updatePreview(pm);
}
}
void setCellSize(int size) {
m_model->setIconSize(size);
m_delegate->setIconSize(size);
m_listView->doItemsLayout();
}
void updateCount() {
m_countLabel->setText(QString("%1 elements").arg(m_model->filteredCount()));
}
void onCopySvg() {
if (m_selectedIndex < 0) return;
QString svg = m_model->modifiedSvg(m_selectedIndex);
QGuiApplication::clipboard()->setText(svg);
statusBar()->showMessage("SVG copied to clipboard", 2000);
}
void onExportSvg() {
if (m_selectedIndex < 0) return;
QString name = g_elements[m_selectedIndex].name;
QString path = QFileDialog::getSaveFileName(this, "Export SVG",
name + ".svg", "SVG Files (*.svg)");
if (path.isEmpty()) return;
QFile f(path);
if (f.open(QIODevice::WriteOnly)) {
f.write(m_model->modifiedSvg(m_selectedIndex).toUtf8());
statusBar()->showMessage("Exported: " + path, 3000);
}
}
void onExportPng() {
if (m_selectedIndex < 0) return;
QString name = g_elements[m_selectedIndex].name;
QString path = QFileDialog::getSaveFileName(this, "Export PNG",
name + ".png", "PNG Files (*.png)");
if (path.isEmpty()) return;
QPixmap pm = m_model->renderIconAtSize(m_selectedIndex, 1024);
pm.save(path, "PNG");
statusBar()->showMessage("Exported: " + path, 3000);
}
private:
void setupMenus() {
auto *fileMenu = menuBar()->addMenu("&File");
auto *exportSvg = fileMenu->addAction("Export SVG...", this, &MainWindow::onExportSvg);
exportSvg->setShortcut(QKeySequence("Ctrl+S"));
auto *exportPng = fileMenu->addAction("Export PNG...", this, &MainWindow::onExportPng);
exportPng->setShortcut(QKeySequence("Ctrl+Shift+S"));
fileMenu->addSeparator();
auto *quitAction = fileMenu->addAction("Quit");
quitAction->setShortcut(QKeySequence::Quit);
connect(quitAction, &QAction::triggered, qApp, &QApplication::quit);
auto *editMenu = menuBar()->addMenu("&Edit");
auto *copySvg = editMenu->addAction("Copy SVG");
copySvg->setShortcut(QKeySequence::Copy);
connect(copySvg, &QAction::triggered, this, &MainWindow::onCopySvg);
auto *viewMenu = menuBar()->addMenu("&View");
auto *sizeGroup = new QActionGroup(this);
struct SizeEntry { const char *label; int size; };
static const SizeEntry sizes[] = {
{"Small (32)", 32}, {"Medium (48)", 48}, {"Large (96)", 96}, {"XL (128)", 128}
};
for (const auto &entry : sizes) {
auto *act = viewMenu->addAction(entry.label);
act->setCheckable(true);
act->setChecked(entry.size == 48);
sizeGroup->addAction(act);
int sz = entry.size;
connect(act, &QAction::triggered, this, [this, sz]() {
setCellSize(sz);
syncCellSizeSlider(sz);
});
}
}
void setupConnections() {
connect(m_search, &QLineEdit::textChanged, this, [this](const QString &t) {
m_model->setFilter(t);
updateCount();
});
connect(m_cellSizeSlider, &QSlider::valueChanged, this, [this](int pos) {
int size = cellSizeFromSlider(pos);
m_cellSizeLabel->setText(QString::number(size));
setCellSize(size);
});
connect(m_listView->selectionModel(), &QItemSelectionModel::currentChanged,
this, &MainWindow::onSelectionChanged);
connect(m_preview, &PreviewPanel::colorSettingsChanged,
this, &MainWindow::onColorSettingsChanged);
connect(m_preview, &PreviewPanel::copySvgRequested, this, &MainWindow::onCopySvg);
connect(m_preview, &PreviewPanel::exportSvgRequested, this, &MainWindow::onExportSvg);
connect(m_preview, &PreviewPanel::exportPngRequested, this, &MainWindow::onExportPng);
}
static int cellSizeFromSlider(int pos) {
static const int sizes[] = {24, 32, 48, 64, 96, 128};
return sizes[qBound(0, pos, 5)];
}
void syncCellSizeSlider(int size) {
static const int sizes[] = {24, 32, 48, 64, 96, 128};
for (int i = 0; i < 6; ++i) {
if (sizes[i] == size) {
m_cellSizeSlider->blockSignals(true);
m_cellSizeSlider->setValue(i);
m_cellSizeLabel->setText(QString::number(size));
m_cellSizeSlider->blockSignals(false);
return;
}
}
}
QLineEdit *m_search;
QSlider *m_cellSizeSlider;
QLabel *m_cellSizeLabel;
QLabel *m_countLabel;
QSplitter *m_splitter;
QListView *m_listView;
CyberElementModel *m_model;
CyberDelegate *m_delegate;
PreviewPanel *m_preview;
int m_selectedIndex = -1;
};
// ============================================================================
// main
// ============================================================================
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setApplicationName("CyberElems");
app.setApplicationVersion("1.0");
// Dark palette
QPalette palette;
palette.setColor(QPalette::Window, QColor("#12121a"));
palette.setColor(QPalette::WindowText, QColor("#c0c0d0"));
palette.setColor(QPalette::Base, QColor("#0a0a0f"));
palette.setColor(QPalette::AlternateBase, QColor("#12121a"));
palette.setColor(QPalette::ToolTipBase, QColor("#1e1e2e"));
palette.setColor(QPalette::ToolTipText, QColor("#c0c0d0"));
palette.setColor(QPalette::Text, QColor("#c0c0d0"));
palette.setColor(QPalette::Button, QColor("#1e1e2e"));
palette.setColor(QPalette::ButtonText, QColor("#c0c0d0"));
palette.setColor(QPalette::BrightText, QColor("#00e5ff"));
palette.setColor(QPalette::Highlight, QColor(0, 229, 255, 60));
palette.setColor(QPalette::HighlightedText, QColor("#00e5ff"));
app.setPalette(palette);
app.setStyleSheet(
"QGroupBox { border: 1px solid #1e1e2e; border-radius: 4px; margin-top: 12px; "
"padding-top: 8px; color: #666; font-size: 11px; }"
"QGroupBox::title { subcontrol-origin: margin; left: 8px; padding: 0 4px; }"
"QToolButton { background: #1e1e2e; border: 1px solid #2e2e3e; border-radius: 3px; "
"padding: 3px 8px; color: #c0c0d0; }"
"QToolButton:hover { border-color: #00e5ff; }"
"QComboBox { background: #1e1e2e; border: 1px solid #2e2e3e; border-radius: 3px; "
"padding: 2px 6px; color: #c0c0d0; }"
"QSlider::groove:horizontal { background: #1e1e2e; height: 4px; border-radius: 2px; }"
"QSlider::handle:horizontal { background: #00e5ff; width: 12px; margin: -4px 0; "
"border-radius: 6px; }"
"QStatusBar { background: #12121a; color: #666; border-top: 1px solid #1e1e2e; }"
"QMenuBar { background: #12121a; color: #c0c0d0; }"
"QMenuBar::item:selected { background: #1e1e2e; }"
"QMenu { background: #12121a; color: #c0c0d0; border: 1px solid #1e1e2e; }"
"QMenu::item:selected { background: rgba(0, 229, 255, 0.2); }"
"QLabel { color: #888; font-size: 11px; }"
);
::loadElements();
MainWindow w;
w.show();
return app.exec();
}
#include "cyberelems.moc"