-
Notifications
You must be signed in to change notification settings - Fork 142
/
LVGLCore.cpp
818 lines (720 loc) · 23.9 KB
/
LVGLCore.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
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
#include "LVGLCore.h"
#include <ft2build.h>
#include <QDebug>
#include <QJsonObject>
#include <QPainter>
#include FT_FREETYPE_H
#include "LVGLFontData.h"
#include "LVGLObject.h"
#include "widgets/LVGLWidgets.h"
LVGLCore lvgl(nullptr);
const char *LVGLCore::DEFAULT_DAYS[7] = {"Su", "Mo", "Tu", "We",
"Th", "Fr", "Sa"};
const char *LVGLCore::DEFAULT_MONTHS[12] = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
LVGLCore::LVGLCore(QObject *parent) : QObject(parent), m_defaultFont(nullptr) {
FT_Init_FreeType(&m_ft);
}
LVGLCore::~LVGLCore() {
FT_Done_FreeType(m_ft);
qDeleteAll(m_images);
qDeleteAll(m_widgets);
qDeleteAll(m_fonts);
}
void LVGLCore::init(int width, int height) {
lv_init();
const uint32_t n = static_cast<uint32_t>(width * height);
m_dispFrameBuf.resize(n);
m_buf1.resize(n);
m_buf2.resize(n);
lv_disp_buf_init(&m_dispBuf, m_buf1.data(), m_buf2.data(), n);
lv_disp_drv_init(&m_dispDrv);
m_dispDrv.hor_res = static_cast<lv_coord_t>(width);
m_dispDrv.ver_res = static_cast<lv_coord_t>(height);
m_dispDrv.user_data = this;
m_dispDrv.flush_cb = flushCb;
m_dispDrv.buffer = &m_dispBuf;
lv_disp_drv_register(&m_dispDrv);
// to be sure that there is no button press at the start
m_inputData.state = LV_INDEV_STATE_REL;
lv_indev_drv_t indev_drv;
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = inputCb;
indev_drv.user_data = this;
lv_indev_drv_register(&indev_drv);
QImage pix(":/images/littlevgl_logo.png");
m_default = lvgl.addImage(pix, "default");
lv_style_copy(&m_screenStyle, &lv_style_scr);
#if LV_FONT_ROBOTO_12
m_fonts << new LVGLFontData("Roboto 12", "lv_font_roboto_12", 12,
&lv_font_roboto_12);
#endif
#if LV_FONT_ROBOTO_16
m_fonts << new LVGLFontData("Roboto 16", "lv_font_roboto_16", 16,
&lv_font_roboto_16);
#endif
#if LV_FONT_ROBOTO_22
m_fonts << new LVGLFontData("Roboto 22", "lv_font_roboto_22", 22,
&lv_font_roboto_22);
#endif
#if LV_FONT_ROBOTO_28
m_fonts << new LVGLFontData("Roboto 28", "lv_font_roboto_28", 28,
&lv_font_roboto_28);
#endif
#if LV_FONT_UNSCII_8
m_fonts << new LVGLFontData("UNSCII 8", "lv_font_unscii_8", 8,
&lv_font_unscii_8);
#endif
// search for default font name
for (const LVGLFontData *f : m_fonts) {
if (f->font() == LV_FONT_DEFAULT) {
m_defaultFont = f;
break;
}
}
Q_ASSERT(m_defaultFont != nullptr);
addWidget(new LVGLArc);
addWidget(new LVGLBar);
addWidget(new LVGLButton);
addWidget(new LVGLButtonMatrix);
addWidget(new LVGLCalendar);
addWidget(new LVGLCanvas);
addWidget(new LVGLChart);
addWidget(new LVGLCheckBox);
addWidget(new LVGLColorPicker);
addWidget(new LVGLContainer);
addWidget(new LVGLDropDownList);
addWidget(new LVGLGauge);
addWidget(new LVGLImage);
addWidget(new LVGLImageButton);
addWidget(new LVGLImageSlider);
addWidget(new LVGLKeyboard);
addWidget(new LVGLLabel);
addWidget(new LVGLLED);
addWidget(new LVGLLine);
addWidget(new LVGLLineMeter);
addWidget(new LVGLMessageBox);
addWidget(new LVGLList);
addWidget(new LVGLPage);
addWidget(new LVGLPreloader);
addWidget(new LVGLSlider);
addWidget(new LVGLSwitch);
addWidget(new LVGLTabview);
addWidget(new LVGLTextArea);
setScreenColor(Qt::white);
changeResolution({width, height});
// lv_log_register_print_cb(logCb);
}
bool LVGLCore::changeResolution(QSize size) {
const uint32_t n = static_cast<uint32_t>(size.width() * size.height());
if (n != m_dispBuf.size) {
m_dispFrameBuf.resize(n);
m_buf1.resize(n);
m_buf2.resize(n);
lv_disp_buf_init(&m_dispBuf, m_buf1.data(), m_buf2.data(), n);
}
m_dispDrv.hor_res = static_cast<lv_coord_t>(size.width());
m_dispDrv.ver_res = static_cast<lv_coord_t>(size.height());
lv_disp_drv_update(lv_disp_get_default(), &m_dispDrv);
return false;
}
QPixmap LVGLCore::framebuffer() const {
auto disp = lv_disp_get_default();
auto width = lv_disp_get_hor_res(disp);
auto height = lv_disp_get_ver_res(disp);
QImage img(width, height, QImage::Format_ARGB32);
memcpy(img.bits(), m_dispFrameBuf.data(),
static_cast<size_t>(width * height) * 4);
return QPixmap::fromImage(img);
}
QPixmap LVGLCore::grab(const QRect ®ion) const {
const auto stride = lv_disp_get_hor_res(lv_disp_get_default());
QImage img(region.width(), region.height(), QImage::Format_ARGB32);
for (auto y = 0; y < region.height(); ++y)
memcpy(img.scanLine(y + region.y()),
&m_dispFrameBuf[static_cast<size_t>(y * stride + region.x())],
static_cast<size_t>(stride) * 4);
QPixmap pix;
try {
pix = QPixmap::fromImage(img);
} catch (std::exception const &ex) {
qDebug() << ex.what();
}
return pix;
}
LVGLObject *LVGLCore::findByName(const QString &name) {
if (!m_objects.isEmpty()) {
for (auto o : m_objects)
if (o->name() == name) return o;
}
return nullptr;
}
int LVGLCore::width() const {
return lv_disp_get_hor_res(lv_disp_get_default());
}
int LVGLCore::height() const {
return lv_disp_get_ver_res(lv_disp_get_default());
}
QSize LVGLCore::size() const {
auto disp = lv_disp_get_default();
return QSize(lv_disp_get_hor_res(disp), lv_disp_get_ver_res(disp));
}
LVGLImageData *LVGLCore::addImage(QImage image, QString name) {
if (image.isNull()) return nullptr;
LVGLImageData *img = new LVGLImageData(image, "", name);
m_images.insert(name, img);
return img;
}
LVGLImageData *LVGLCore::addImage(QString fileName, QString name) {
QImage image(fileName);
if (image.isNull()) return nullptr;
if (name.isEmpty()) {
// create sorted list by type
QList<int> numbers;
for (LVGLImageData *o : m_images) {
if (o->fileName().isEmpty()) continue;
const int index = o->name().lastIndexOf('_');
const int num = o->name().mid(index + 1).toInt();
auto ind = std::lower_bound(numbers.begin(), numbers.end(), num);
numbers.insert(ind, num);
}
// find next free id
int id = 1;
for (int num : numbers) {
if (num == id)
id++;
else
break;
}
name = QString("img_%1").arg(id);
}
LVGLImageData *img = new LVGLImageData(image, fileName, name);
m_images.insert(name, img);
return img;
}
void LVGLCore::addImage(LVGLImageData *image) {
m_images.insert(image->name(), image);
}
QStringList LVGLCore::imageNames() const { return m_images.keys(); }
QList<LVGLImageData *> LVGLCore::images() const { return m_images.values(); }
lv_img_dsc_t *LVGLCore::image(QString name) {
LVGLImageData *img = m_images.value(name, nullptr);
if (img) return img->img_des();
return nullptr;
}
lv_img_dsc_t *LVGLCore::defaultImage() const { return m_default->img_des(); }
QString LVGLCore::nameByImage(const lv_img_dsc_t *img_dsc) const {
for (LVGLImageData *img : m_images) {
if (img->img_des() == img_dsc) return img->name();
}
return "";
}
LVGLImageData *LVGLCore::imageByDesc(const lv_img_dsc_t *img_dsc) const {
for (LVGLImageData *img : m_images) {
if (img->img_des() == img_dsc) return img;
}
return nullptr;
}
bool LVGLCore::removeImage(LVGLImageData *img) {
for (auto it = m_images.begin(); it != m_images.end(); ++it) {
if (it.value() == img) {
m_images.remove(it.key());
delete img;
return true;
}
}
return false;
}
void LVGLCore::removeAllImages() {
qDeleteAll(m_images);
m_images.clear();
}
QStringList LVGLCore::symbolNames() const {
return QStringList() << "LV_SYMBOL_AUDIO"
<< "LV_SYMBOL_VIDEO"
<< "LV_SYMBOL_LIST"
<< "LV_SYMBOL_OK"
<< "LV_SYMBOL_CLOSE"
<< "LV_SYMBOL_POWER"
<< "LV_SYMBOL_SETTINGS"
<< "LV_SYMBOL_HOME"
<< "LV_SYMBOL_DOWNLOAD"
<< "LV_SYMBOL_DRIVE"
<< "LV_SYMBOL_REFRESH"
<< "LV_SYMBOL_MUTE"
<< "LV_SYMBOL_VOLUME_MID"
<< "LV_SYMBOL_VOLUME_MAX"
<< "LV_SYMBOL_IMAGE"
<< "LV_SYMBOL_EDIT"
<< "LV_SYMBOL_PREV"
<< "LV_SYMBOL_PLAY"
<< "LV_SYMBOL_PAUSE"
<< "LV_SYMBOL_STOP"
<< "LV_SYMBOL_NEXT"
<< "LV_SYMBOL_EJECT"
<< "LV_SYMBOL_LEFT"
<< "LV_SYMBOL_RIGHT"
<< "LV_SYMBOL_PLUS"
<< "LV_SYMBOL_MINUS"
<< "LV_SYMBOL_EYE_OPEN"
<< "LV_SYMBOL_EYE_CLOSE"
<< "LV_SYMBOL_WARNING"
<< "LV_SYMBOL_SHUFFLE"
<< "LV_SYMBOL_UP"
<< "LV_SYMBOL_DOWN"
<< "LV_SYMBOL_LOOP"
<< "LV_SYMBOL_DIRECTORY"
<< "LV_SYMBOL_UPLOAD"
<< "LV_SYMBOL_CALL"
<< "LV_SYMBOL_CUT"
<< "LV_SYMBOL_COPY"
<< "LV_SYMBOL_SAVE"
<< "LV_SYMBOL_CHARGE"
<< "LV_SYMBOL_PASTE"
<< "LV_SYMBOL_BELL"
<< "LV_SYMBOL_KEYBOARD"
<< "LV_SYMBOL_GPS"
<< "LV_SYMBOL_FILE"
<< "LV_SYMBOL_WIFI"
<< "LV_SYMBOL_BATTERY_FULL"
<< "LV_SYMBOL_BATTERY_3"
<< "LV_SYMBOL_BATTERY_2"
<< "LV_SYMBOL_BATTERY_1"
<< "LV_SYMBOL_BATTERY_EMPTY"
<< "LV_SYMBOL_USB"
<< "LV_SYMBOL_BLUETOOTH"
<< "LV_SYMBOL_TRASH"
<< "LV_SYMBOL_BACKSPACE"
<< "LV_SYMBOL_SD_CARD"
<< "LV_SYMBOL_NEW_LINE";
}
const char *LVGLCore::symbol(const QString &name) const {
if (name == "LV_SYMBOL_AUDIO")
return LV_SYMBOL_AUDIO;
else if (name == "LV_SYMBOL_VIDEO")
return LV_SYMBOL_VIDEO;
else if (name == "LV_SYMBOL_LIST")
return LV_SYMBOL_LIST;
else if (name == "LV_SYMBOL_OK")
return LV_SYMBOL_OK;
else if (name == "LV_SYMBOL_CLOSE")
return LV_SYMBOL_CLOSE;
else if (name == "LV_SYMBOL_POWER")
return LV_SYMBOL_POWER;
else if (name == "LV_SYMBOL_SETTINGS")
return LV_SYMBOL_SETTINGS;
else if (name == "LV_SYMBOL_HOME")
return LV_SYMBOL_HOME;
else if (name == "LV_SYMBOL_DOWNLOAD")
return LV_SYMBOL_DOWNLOAD;
else if (name == "LV_SYMBOL_DRIVE")
return LV_SYMBOL_DRIVE;
else if (name == "LV_SYMBOL_REFRESH")
return LV_SYMBOL_REFRESH;
else if (name == "LV_SYMBOL_MUTE")
return LV_SYMBOL_MUTE;
else if (name == "LV_SYMBOL_VOLUME_MID")
return LV_SYMBOL_VOLUME_MID;
else if (name == "LV_SYMBOL_VOLUME_MAX")
return LV_SYMBOL_VOLUME_MAX;
else if (name == "LV_SYMBOL_IMAGE")
return LV_SYMBOL_IMAGE;
else if (name == "LV_SYMBOL_EDIT")
return LV_SYMBOL_EDIT;
else if (name == "LV_SYMBOL_PREV")
return LV_SYMBOL_PREV;
else if (name == "LV_SYMBOL_PLAY")
return LV_SYMBOL_PLAY;
else if (name == "LV_SYMBOL_PAUSE")
return LV_SYMBOL_PAUSE;
else if (name == "LV_SYMBOL_STOP")
return LV_SYMBOL_STOP;
else if (name == "LV_SYMBOL_NEXT")
return LV_SYMBOL_NEXT;
else if (name == "LV_SYMBOL_EJECT")
return LV_SYMBOL_EJECT;
else if (name == "LV_SYMBOL_LEFT")
return LV_SYMBOL_LEFT;
else if (name == "LV_SYMBOL_RIGHT")
return LV_SYMBOL_RIGHT;
else if (name == "LV_SYMBOL_PLUS")
return LV_SYMBOL_PLUS;
else if (name == "LV_SYMBOL_MINUS")
return LV_SYMBOL_MINUS;
else if (name == "LV_SYMBOL_EYE_OPEN")
return LV_SYMBOL_EYE_OPEN;
else if (name == "LV_SYMBOL_EYE_CLOSE")
return LV_SYMBOL_EYE_CLOSE;
else if (name == "LV_SYMBOL_WARNING")
return LV_SYMBOL_WARNING;
else if (name == "LV_SYMBOL_SHUFFLE")
return LV_SYMBOL_SHUFFLE;
else if (name == "LV_SYMBOL_UP")
return LV_SYMBOL_UP;
else if (name == "LV_SYMBOL_DOWN")
return LV_SYMBOL_DOWN;
else if (name == "LV_SYMBOL_LOOP")
return LV_SYMBOL_LOOP;
else if (name == "LV_SYMBOL_DIRECTORY")
return LV_SYMBOL_DIRECTORY;
else if (name == "LV_SYMBOL_UPLOAD")
return LV_SYMBOL_UPLOAD;
else if (name == "LV_SYMBOL_CALL")
return LV_SYMBOL_CALL;
else if (name == "LV_SYMBOL_CUT")
return LV_SYMBOL_CUT;
else if (name == "LV_SYMBOL_COPY")
return LV_SYMBOL_COPY;
else if (name == "LV_SYMBOL_SAVE")
return LV_SYMBOL_SAVE;
else if (name == "LV_SYMBOL_CHARGE")
return LV_SYMBOL_CHARGE;
else if (name == "LV_SYMBOL_PASTE")
return LV_SYMBOL_PASTE;
else if (name == "LV_SYMBOL_BELL")
return LV_SYMBOL_BELL;
else if (name == "LV_SYMBOL_KEYBOARD")
return LV_SYMBOL_KEYBOARD;
else if (name == "LV_SYMBOL_GPS")
return LV_SYMBOL_GPS;
else if (name == "LV_SYMBOL_FILE")
return LV_SYMBOL_FILE;
else if (name == "LV_SYMBOL_WIFI")
return LV_SYMBOL_WIFI;
else if (name == "LV_SYMBOL_BATTERY_FULL")
return LV_SYMBOL_BATTERY_FULL;
else if (name == "LV_SYMBOL_BATTERY_3")
return LV_SYMBOL_BATTERY_3;
else if (name == "LV_SYMBOL_BATTERY_2")
return LV_SYMBOL_BATTERY_2;
else if (name == "LV_SYMBOL_BATTERY_1")
return LV_SYMBOL_BATTERY_1;
else if (name == "LV_SYMBOL_BATTERY_EMPTY")
return LV_SYMBOL_BATTERY_EMPTY;
else if (name == "LV_SYMBOL_USB")
return LV_SYMBOL_USB;
else if (name == "LV_SYMBOL_BLUETOOTH")
return LV_SYMBOL_BLUETOOTH;
else if (name == "LV_SYMBOL_TRASH")
return LV_SYMBOL_TRASH;
else if (name == "LV_SYMBOL_BACKSPACE")
return LV_SYMBOL_BACKSPACE;
else if (name == "LV_SYMBOL_SD_CARD")
return LV_SYMBOL_SD_CARD;
else if (name == "LV_SYMBOL_NEW_LINE")
return LV_SYMBOL_NEW_LINE;
else
return nullptr;
}
void LVGLCore::poll() {
lv_task_handler();
lv_tick_inc(static_cast<uint32_t>(m_time.elapsed()));
m_time.restart();
}
void LVGLCore::objsclear() { m_objects.clear(); }
void LVGLCore::sendMouseEvent(int x, int y, bool pressed) {
m_inputData.point.x = static_cast<lv_coord_t>(x);
m_inputData.point.y = static_cast<lv_coord_t>(y);
m_inputData.state = pressed ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
}
QPoint LVGLCore::get_absolute_position(const lv_obj_t *obj) const {
if (obj == lv_scr_act()) return QPoint(0, 0);
int x = lv_obj_get_x(obj);
int y = lv_obj_get_y(obj);
lv_obj_t *parent = lv_obj_get_parent(obj);
while (parent) {
if (parent == lv_scr_act()) break;
x += lv_obj_get_x(parent);
y += lv_obj_get_y(parent);
parent = lv_obj_get_parent(parent);
}
return QPoint(x, y);
}
QSize LVGLCore::get_object_size(const lv_obj_t *lv_obj) const {
return QSize(lv_obj_get_width(lv_obj), lv_obj_get_height(lv_obj));
}
QRect LVGLCore::get_object_rect(const lv_obj_t *lv_obj) const {
return QRect(get_absolute_position(lv_obj), get_object_size(lv_obj));
}
void LVGLCore::addObject(LVGLObject *object) { m_objects << object; }
void LVGLCore::removeObject(LVGLObject *object) {
auto childs = object->childs();
while (!childs.isEmpty()) {
removeObject(childs.at(0));
childs = object->childs();
}
if (object->parent()) object->parent()->removeChild(object);
m_objects.removeOne(object);
delete object;
object = nullptr;
}
void LVGLCore::removeAllObjects() {
auto objs = m_objects;
while (!objs.isEmpty()) {
if (objs.at(0) && !objs.at(0)->parent()) removeObject(m_objects.at(0));
objs = m_objects;
}
}
QList<LVGLObject *> LVGLCore::allObjects() const { return m_objects; }
QList<LVGLObject *> LVGLCore::topLevelObjects() const {
QList<LVGLObject *> ret;
for (LVGLObject *c : m_objects) {
if (c->parent() == nullptr) ret << c;
}
return ret;
}
QList<LVGLObject *> LVGLCore::objectsByType(QString className) const {
QList<LVGLObject *> ret;
for (LVGLObject *c : m_objects) {
if (c->widgetClass()->className() == className) ret << c;
}
return ret;
}
LVGLObject *LVGLCore::object(QString name) const {
if (name.isEmpty()) return nullptr;
for (LVGLObject *c : m_objects) {
if (c->name() == name) return c;
}
return nullptr;
}
LVGLObject *LVGLCore::object(lv_obj_t *obj) const {
if (obj == nullptr) return nullptr;
for (LVGLObject *c : m_objects) {
if (c->obj() == obj) return c;
}
return nullptr;
}
QColor LVGLCore::toColor(lv_color_t c) const {
#if LV_COLOR_DEPTH == 24
return QColor(c.ch.red, c.ch.green, c.ch.blue);
#elif LV_COLOR_DEPTH == 32
return QColor(c.ch.red, c.ch.green, c.ch.blue, c.ch.alpha);
#endif
}
lv_color_t LVGLCore::fromColor(QColor c) const {
#if LV_COLOR_DEPTH == 24
lv_color_t ret;
ret.ch.red = c.red() & 0xff;
ret.ch.green = c.green() & 0xff;
ret.ch.blue = c.blue() & 0xff;
return ret;
#elif LV_COLOR_DEPTH == 32
lv_color_t ret;
ret.ch.red = c.red() & 0xff;
ret.ch.green = c.green() & 0xff;
ret.ch.blue = c.blue() & 0xff;
ret.ch.alpha = c.alpha() & 0xff;
return ret;
#endif
}
lv_color_t LVGLCore::fromColor(QVariant v) const {
if (v.type() == QVariant::Map) {
QVariantMap map = v.toMap();
#if LV_COLOR_DEPTH == 32
lv_color_t c;
c.ch.red = map["red"].toInt() & 0xff;
c.ch.green = map["green"].toInt() & 0xff;
c.ch.blue = map["blue"].toInt() & 0xff;
c.ch.alpha = map["alpha"].toInt() & 0xff;
return c;
#endif
}
return fromColor(v.value<QColor>());
}
QJsonObject LVGLCore::colorToJson(lv_color_t c) const {
QJsonObject color(
{{"red", c.ch.red}, {"green", c.ch.green}, {"blue", c.ch.blue}});
#if LV_COLOR_DEPTH == 32
color.insert("alpha", c.ch.alpha);
#endif
return color;
}
lv_color_t LVGLCore::colorFromJson(QJsonObject obj) const {
#if LV_COLOR_DEPTH == 32
lv_color_t c;
c.ch.red = obj["red"].toInt() & 0xff;
c.ch.green = obj["green"].toInt() & 0xff;
c.ch.blue = obj["blue"].toInt() & 0xff;
c.ch.alpha = obj["alpha"].toInt() & 0xff;
return c;
#else
return lv_color_hex(0x000000);
#endif
}
LVGLFontData *LVGLCore::addFont(const QString &fileName, uint8_t size) {
LVGLFontData *font = LVGLFontData::parse(fileName, size, 4, 0x0020, 0x007f);
if (font) m_fonts << font;
return font;
}
void LVGLCore::addFont(LVGLFontData *font) {
if (font) m_fonts << font;
}
bool LVGLCore::removeFont(LVGLFontData *font) {
return m_fonts.removeOne(font);
}
QStringList LVGLCore::fontNames() const {
QStringList ret;
for (const LVGLFontData *f : m_fonts) ret << f->name();
return ret;
}
QStringList LVGLCore::fontCodeNames() const {
QStringList ret;
for (const LVGLFontData *f : m_fonts) ret << f->codeName();
return ret;
}
const lv_font_t *LVGLCore::font(int index) const {
if ((index > 0) && (index < m_fonts.size())) return m_fonts.at(index)->font();
return m_defaultFont->font();
}
const lv_font_t *LVGLCore::font(const QString &name,
Qt::CaseSensitivity cs) const {
for (const LVGLFontData *font : m_fonts) {
if (name.compare(font->name(), cs) == 0) return font->font();
}
return m_defaultFont->font();
}
int LVGLCore::indexOfFont(const lv_font_t *font) const {
int index = 0;
for (auto it = m_fonts.begin(); it != m_fonts.end(); ++it, ++index) {
if ((*it)->font() == font) return index;
}
return -1;
}
QString LVGLCore::fontName(const lv_font_t *font) const {
for (const LVGLFontData *f : m_fonts) {
if (f->font() == font) return f->name();
}
return m_defaultFont->name();
}
QString LVGLCore::fontCodeName(const lv_font_t *font) const {
for (const LVGLFontData *f : m_fonts) {
if (f->font() == font) return f->codeName();
}
return m_defaultFont->codeName();
}
QList<const LVGLFontData *> LVGLCore::customFonts() const {
QList<const LVGLFontData *> ret;
for (const LVGLFontData *font : m_fonts) {
if (font->isCustomFont()) ret << font;
}
return ret;
}
void LVGLCore::removeCustomFonts() {
for (auto it = m_fonts.begin(); it != m_fonts.end();) {
if ((*it)->isCustomFont()) {
delete *it;
it = m_fonts.erase(it);
} else {
++it;
}
}
}
QString LVGLCore::baseStyleName(const lv_style_t *style) const {
if (style == &lv_style_scr)
return "lv_style_scr";
else if (style == &lv_style_transp)
return "lv_style_transp";
else if (style == &lv_style_transp_fit)
return "lv_style_transp_fit";
else if (style == &lv_style_transp_tight)
return "lv_style_transp_tight";
else if (style == &lv_style_plain)
return "lv_style_plain";
else if (style == &lv_style_plain_color)
return "lv_style_plain_color";
else if (style == &lv_style_pretty)
return "lv_style_pretty";
else if (style == &lv_style_pretty_color)
return "lv_style_pretty_color";
else if (style == &lv_style_btn_rel)
return "lv_style_btn_rel";
else if (style == &lv_style_btn_pr)
return "lv_style_btn_pr";
else if (style == &lv_style_btn_tgl_rel)
return "lv_style_btn_tgl_rel";
else if (style == &lv_style_btn_tgl_pr)
return "lv_style_btn_tgl_pr";
else if (style == &lv_style_btn_ina)
return "lv_style_btn_ina";
return "";
}
void LVGLCore::setScreenColor(QColor color) {
m_screenStyle.body.main_color = fromColor(color);
m_screenStyle.body.grad_color = m_screenStyle.body.main_color;
lv_obj_set_style(lv_scr_act(), &m_screenStyle);
}
QColor LVGLCore::screenColor() const {
return toColor(m_screenStyle.body.main_color);
}
bool LVGLCore::screenColorChanged() const {
return (
m_screenStyle.body.main_color.full != lv_style_scr.body.main_color.full &&
m_screenStyle.body.grad_color.full != lv_style_scr.body.grad_color.full);
}
QList<const LVGLWidget *> LVGLCore::widgets() const {
return m_widgets.values();
}
const LVGLWidget *LVGLCore::widget(const QString &name) const {
if (m_widgets.contains(name)) return m_widgets[name];
return nullptr;
}
void LVGLCore::tick() {
lv_task_handler();
lv_tick_inc(20);
}
void LVGLCore::addWidget(LVGLWidget *w) {
auto size = w->minimumSize();
if (size.width() > width() || size.height() > height())
changeResolution(
{std::max(size.width(), width()), std::max(size.height(), height())});
setScreenColor(Qt::transparent);
lv_obj_t *o = w->newObject(lv_scr_act());
lv_obj_set_pos(o, 0, 0);
lv_obj_set_size(o, w->minimumSize().width(), w->minimumSize().height());
lv_scr_load(lv_scr_act());
lv_tick_inc(LV_DISP_DEF_REFR_PERIOD);
lv_task_handler();
w->setPreview(
/*grab(QRect(QPoint(0, 0), size))*/ framebuffer().copy({{0, 0}, size}));
w->preview().save(w->name() + ".png");
lv_obj_del(o);
lv_scr_load(lv_scr_act());
lv_tick_inc(LV_DISP_DEF_REFR_PERIOD);
lv_task_handler();
m_widgets.insert(w->className(), w);
}
void LVGLCore::flushHandler(lv_disp_drv_t *disp, const lv_area_t *area,
lv_color_t *color_p) {
const auto stride = disp->hor_res;
for (auto y = area->y1; y <= area->y2; ++y) {
for (auto x = area->x1; x <= area->x2; ++x) {
m_dispFrameBuf[x + y * stride].full = color_p->full;
color_p++;
}
}
lv_disp_flush_ready(disp);
}
bool LVGLCore::inputHandler(lv_indev_drv_t *indev_driver,
lv_indev_data_t *data) {
(void)indev_driver;
data->state = m_inputData.state;
data->point.x = m_inputData.point.x;
data->point.y = m_inputData.point.y;
return false; /*Return `false` because we are not buffering and no more data
to read*/
}
void LVGLCore::flushCb(lv_disp_drv_t *disp, const lv_area_t *area,
lv_color_t *color_p) {
LVGLCore *self = reinterpret_cast<LVGLCore *>(disp->user_data);
self->flushHandler(disp, area, color_p);
}
bool LVGLCore::inputCb(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) {
LVGLCore *self = reinterpret_cast<LVGLCore *>(indev_driver->user_data);
return self->inputHandler(indev_driver, data);
}
void LVGLCore::logCb(lv_log_level_t level, const char *file, uint32_t line,
const char *dsc) {
qDebug().nospace() << file << " (" << line << "," << level << "): " << dsc;
}