@@ -431,36 +431,15 @@ bool Generator::writeHeaderStyleNamespace() {
431
431
432
432
bool Generator::writePaletteDefinition () {
433
433
header_->stream () << " \
434
- class palette {\n \
434
+ class palette;\n \
435
+ class palette_data {\n \
435
436
public:\n \
436
- palette() = default;\n \
437
- palette(const palette &other) = delete;\n \
438
- \n \
439
- QByteArray save() const;\n \
440
- bool load(const QByteArray &cache);\n \
441
- \n \
442
- enum class SetResult {\n \
443
- Ok,\n \
444
- KeyNotFound,\n \
445
- ValueNotFound,\n \
446
- Duplicate,\n \
447
- };\n \
448
- SetResult setColor(QLatin1String name, uchar r, uchar g, uchar b, uchar a);\n \
449
- SetResult setColor(QLatin1String name, QLatin1String from);\n \
450
- void reset() {\n \
451
- clear();\n \
452
- finalize();\n \
453
- }\n \
454
- \n \
455
- // Created not inited, should be finalized before usage.\n \
456
- void finalize();\n \
457
- \n \
458
- int indexOfColor(color c) const;\n \
459
- color colorAtIndex(int index) const;\n \
437
+ static constexpr auto kCount = " << (1 + module_.variablesCount ()) << " ;\n \
438
+ static int32 Checksum();\n \
460
439
\n \
461
440
inline const color &transparent() const { return _colors[0]; }; // special color\n " ;
462
441
463
- int indexInPalette = 1 ;
442
+ auto indexInPalette = 1 ;
464
443
if (!module_.enumVariables ([&](const Variable &variable) -> bool {
465
444
auto name = variable.name .back ();
466
445
if (variable.value .type ().tag != structure::TypeTag::Color) {
@@ -471,43 +450,12 @@ public:\n\
471
450
header_->stream () << " \t inline const color &" << name << " () const { return _colors[" << index << " ]; };\n " ;
472
451
return true ;
473
452
})) return false ;
453
+ const auto count = indexInPalette;
474
454
475
- auto count = indexInPalette;
476
455
header_->stream () << " \
477
456
\n \
478
- palette &operator=(const palette &other);\n \
479
- \n \
480
- static int32 Checksum();\n \
481
- \n \
482
- ~palette() {\n \
483
- clear();\n \
484
- }\n \
485
- \n \
486
- private:\n \
487
- static constexpr auto kCount = " << count << " ;\n \
488
- \n \
489
- void clear() {\n \
490
- for (int i = 0; i != kCount; ++i) {\n \
491
- if (_status[i] != Status::Initial) {\n \
492
- data(i)->~ColorData();\n \
493
- _status[i] = Status::Initial;\n \
494
- _ready = false;\n \
495
- }\n \
496
- }\n \
497
- }\n \
498
- \n \
499
- struct TempColorData { uchar r, g, b, a; };\n \
500
- void compute(int index, int fallbackIndex, TempColorData value) {\n \
501
- if (_status[index] == Status::Initial) {\n \
502
- if (fallbackIndex >= 0 && _status[fallbackIndex] == Status::Loaded) {\n \
503
- _status[index] = Status::Loaded;\n \
504
- new (data(index)) internal::ColorData(*data(fallbackIndex));\n \
505
- } else {\n \
506
- _status[index] = Status::Created;\n \
507
- new (data(index)) internal::ColorData(value.r, value.g, value.b, value.a);\n \
508
- }\n \
509
- }\n \
510
- }\n \
457
+ protected:\n \
458
+ void finalize(palette &that);\n \
511
459
\n \
512
460
internal::ColorData *data(int index) {\n \
513
461
return reinterpret_cast<internal::ColorData*>(_data) + index;\n \
@@ -516,15 +464,6 @@ private:\n\
516
464
const internal::ColorData *data(int index) const {\n \
517
465
return reinterpret_cast<const internal::ColorData*>(_data) + index;\n \
518
466
}\n \
519
- \n \
520
- void setData(int index, const internal::ColorData &value) {\n \
521
- if (_status[index] == Status::Initial) {\n \
522
- new (data(index)) internal::ColorData(value);\n \
523
- } else {\n \
524
- *data(index) = value;\n \
525
- }\n \
526
- _status[index] = Status::Loaded;\n \
527
- }\n \
528
467
\n \
529
468
enum class Status {\n \
530
469
Initial,\n \
@@ -548,23 +487,22 @@ private:\n\
548
487
namespace main_palette {\n \
549
488
\n \
550
489
not_null<const palette*> get();\n \
551
- QByteArray save();\n \
552
- bool load(const QByteArray &cache);\n \
553
- palette::SetResult setColor(QLatin1String name, uchar r, uchar g, uchar b, uchar a);\n \
554
- palette::SetResult setColor(QLatin1String name, QLatin1String from);\n \
555
- void apply(const palette &other);\n \
556
- void reset();\n \
557
- int indexOfColor(color c);\n \
558
490
\n \
559
491
struct row {\n \
560
- \t QLatin1String name;\n \
561
- \t QLatin1String value;\n \
562
- \t QLatin1String fallback;\n \
563
- \t QLatin1String description;\n \
492
+ QLatin1String name;\n \
493
+ QLatin1String value;\n \
494
+ QLatin1String fallback;\n \
495
+ QLatin1String description;\n \
564
496
};\n \
565
497
QList<row> data();\n \
566
498
\n \
567
- } // namespace main_palette\n " ;
499
+ } // namespace main_palette\n \
500
+ \n \
501
+ namespace internal {\n \
502
+ \n \
503
+ int GetPaletteIndex(QLatin1String name);\n \
504
+ \n \
505
+ } // namespace internal\n " ;
568
506
569
507
return true ;
570
508
}
@@ -649,6 +587,10 @@ bool Generator::writeRefsDeclarations() {
649
587
}
650
588
651
589
bool Generator::writeIncludesInSource () {
590
+ if (isPalette_) {
591
+ source_->include (" ui/style/style_core_palette.h" );
592
+ source_->newline ();
593
+ }
652
594
if (!module_.hasIncludes ()) {
653
595
return true ;
654
596
}
@@ -716,25 +658,8 @@ bool Generator::writeRefsDefinition() {
716
658
717
659
bool Generator::writeSetPaletteColor () {
718
660
source_->stream () << " \n \
719
- int palette::indexOfColor(style::color c) const {\n \
720
- auto start = data(0);\n \
721
- if (c._data >= start && c._data < start + kCount) {\n \
722
- return static_cast<int>(c._data - start);\n \
723
- }\n \
724
- return -1;\n \
725
- }\n \
726
- \n \
727
- color palette::colorAtIndex(int index) const {\n \
728
- Assert(_ready);\n \
729
- Assert(index >= 0 && index < kCount);\n \
730
- return _colors[index];\n \
731
- }\n \
732
- \n \
733
- void palette::finalize() {\n \
734
- if (_ready) return;\n \
735
- _ready = true;\n \
736
- \n \
737
- compute(0, -1, { 255, 255, 255, 0}); // special color\n " ;
661
+ void palette_data::finalize(palette &that) {\n \
662
+ that.compute(0, -1, { 255, 255, 255, 0}); // special color\n " ;
738
663
739
664
QList<structure::FullName> names;
740
665
module_.enumVariables ([&](const Variable &variable) -> bool {
@@ -757,7 +682,7 @@ void palette::finalize() {\n\
757
682
auto fallbackIterator = paletteIndices_.find (colorFallbackName (variable.value ));
758
683
auto fallbackIndex = (fallbackIterator == paletteIndices_.end ()) ? -1 : fallbackIterator->second ;
759
684
auto assignment = QString (" { %1, %2, %3, %4 }" ).arg (color.red ).arg (color.green ).arg (color.blue ).arg (color.alpha );
760
- source_->stream () << " \t compute (" << index << " , " << fallbackIndex << " , " << assignment << " );\n " ;
685
+ source_->stream () << " \t that.compute (" << index << " , " << fallbackIndex << " , " << assignment << " );\n " ;
761
686
checksumString.append ((' &' + name + ' :' + assignment).toUtf8 ());
762
687
763
688
auto isCopy = !variable.value .copyOf ().isEmpty ();
@@ -774,7 +699,7 @@ void palette::finalize() {\n\
774
699
return false ;
775
700
}
776
701
777
- dataRows.append (" \t result.push_back({ qstr(\" " + name + " \" ), qstr(\" " + value + " \" ), qstr(\" " + (isCopy ? QString () : fallbackName) + " \" ), qstr(" + stringToEncodedString (variable.description . toStdString () ) + " ) });\n " );
702
+ dataRows.append (" \t result.push_back({ qstr(\" " + name + " \" ), qstr(\" " + value + " \" ), qstr(\" " + (isCopy ? QString () : fallbackName) + " \" ), qstr(" + stringToEncodedString (variable.description ) + " ) });\n " );
778
703
return true ;
779
704
});
780
705
if (!result) {
@@ -794,35 +719,14 @@ void palette::finalize() {\n\
794
719
}
795
720
source_->stream () << " \
796
721
}\n \
797
- palette &palette::operator=(const palette &other) {\n \
798
- auto wasReady = _ready;\n \
799
- for (int i = 0; i != kCount; ++i) {\n \
800
- if (other._status[i] == Status::Loaded) {\n \
801
- if (_status[i] == Status::Initial) {\n \
802
- new (data(i)) internal::ColorData(*other.data(i));\n \
803
- } else {\n \
804
- *data(i) = *other.data(i);\n \
805
- }\n \
806
- _status[i] = Status::Loaded;\n \
807
- } else if (_status[i] != Status::Initial) {\n \
808
- data(i)->~ColorData();\n \
809
- _status[i] = Status::Initial;\n \
810
- _ready = false;\n \
811
- }\n \
812
- }\n \
813
- if (wasReady && !_ready) {\n \
814
- finalize();\n \
815
- }\n \
816
- return *this;\n \
817
- }\n \
818
722
\n \
819
- int32 palette ::Checksum() {\n \
723
+ int32 palette_data ::Checksum() {\n \
820
724
return " << checksum << " ;\n \
821
725
}\n " ;
822
726
823
- source_->newline ().pushNamespace ().newline ();
727
+ source_->newline ().pushNamespace (" internal " ).newline ();
824
728
source_->stream () << " \
825
- int getPaletteIndex (QLatin1String name) {\n \
729
+ int GetPaletteIndex (QLatin1String name) {\n \
826
730
auto size = name.size();\n \
827
731
auto data = name.data();\n " ;
828
732
@@ -959,90 +863,12 @@ int getPaletteIndex(QLatin1String name) {\n\
959
863
960
864
source_->newline ().popNamespace ().newline ();
961
865
source_->stream () << " \
962
- QByteArray palette::save() const {\n \
963
- if (!_ready) const_cast<palette*>(this)->finalize();\n \
964
- \n \
965
- auto result = QByteArray(" << (count * 4 ) << " , Qt::Uninitialized);\n \
966
- for (auto i = 0, index = 0; i != " << count << " ; ++i) {\n \
967
- result[index++] = static_cast<uchar>(data(i)->c.red());\n \
968
- result[index++] = static_cast<uchar>(data(i)->c.green());\n \
969
- result[index++] = static_cast<uchar>(data(i)->c.blue());\n \
970
- result[index++] = static_cast<uchar>(data(i)->c.alpha());\n \
971
- }\n \
972
- return result;\n \
973
- }\n \
974
- \n \
975
- bool palette::load(const QByteArray &cache) {\n \
976
- if (cache.size() != " << (count * 4 ) << " ) return false;\n \
977
- \n \
978
- auto p = reinterpret_cast<const uchar*>(cache.constData());\n \
979
- for (auto i = 0; i != " << count << " ; ++i) {\n \
980
- setData(i, { p[i * 4 + 0], p[i * 4 + 1], p[i * 4 + 2], p[i * 4 + 3] });\n \
981
- }\n \
982
- return true;\n \
983
- }\n \
984
- \n \
985
- palette::SetResult palette::setColor(QLatin1String name, uchar r, uchar g, uchar b, uchar a) {\n \
986
- auto nameIndex = getPaletteIndex(name);\n \
987
- if (nameIndex < 0) return SetResult::KeyNotFound;\n \
988
- auto duplicate = (_status[nameIndex] != Status::Initial);\n \
989
- \n \
990
- setData(nameIndex, { r, g, b, a });\n \
991
- return duplicate ? SetResult::Duplicate : SetResult::Ok;\n \
992
- }\n \
993
- \n \
994
- palette::SetResult palette::setColor(QLatin1String name, QLatin1String from) {\n \
995
- auto nameIndex = getPaletteIndex(name);\n \
996
- if (nameIndex < 0) return SetResult::KeyNotFound;\n \
997
- auto duplicate = (_status[nameIndex] != Status::Initial);\n \
998
- \n \
999
- auto fromIndex = getPaletteIndex(from);\n \
1000
- if (fromIndex < 0 || _status[fromIndex] != Status::Loaded) return SetResult::ValueNotFound;\n \
1001
- \n \
1002
- setData(nameIndex, *data(fromIndex));\n \
1003
- return duplicate ? SetResult::Duplicate : SetResult::Ok;\n \
1004
- }\n \
1005
- \n \
1006
866
namespace main_palette {\n \
1007
867
\n \
1008
868
not_null<const palette*> get() {\n \
1009
869
return &_palette;\n \
1010
870
}\n \
1011
871
\n \
1012
- QByteArray save() {\n \
1013
- return _palette.save();\n \
1014
- }\n \
1015
- \n \
1016
- bool load(const QByteArray &cache) {\n \
1017
- if (_palette.load(cache)) {\n \
1018
- style::internal::resetIcons();\n \
1019
- return true;\n \
1020
- }\n \
1021
- return false;\n \
1022
- }\n \
1023
- \n \
1024
- palette::SetResult setColor(QLatin1String name, uchar r, uchar g, uchar b, uchar a) {\n \
1025
- return _palette.setColor(name, r, g, b, a);\n \
1026
- }\n \
1027
- \n \
1028
- palette::SetResult setColor(QLatin1String name, QLatin1String from) {\n \
1029
- return _palette.setColor(name, from);\n \
1030
- }\n \
1031
- \n \
1032
- void apply(const palette &other) {\n \
1033
- _palette = other;\n \
1034
- style::internal::resetIcons();\n \
1035
- }\n \
1036
- \n \
1037
- void reset() {\n \
1038
- _palette.reset();\n \
1039
- style::internal::resetIcons();\n \
1040
- }\n \
1041
- \n \
1042
- int indexOfColor(color c) {\n \
1043
- return _palette.indexOfColor(c);\n \
1044
- }\n \
1045
- \n \
1046
872
QList<row> data() {\n \
1047
873
auto result = QList<row>();\n \
1048
874
result.reserve(" << count << " );\n \
0 commit comments