Skip to content

Commit aaa64e6

Browse files
committed
Move most of the palette code to lib_ui.
1 parent 4ff2c7f commit aaa64e6

File tree

2 files changed

+33
-204
lines changed

2 files changed

+33
-204
lines changed

codegen/style/generator.cpp

Lines changed: 30 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -431,36 +431,15 @@ bool Generator::writeHeaderStyleNamespace() {
431431

432432
bool Generator::writePaletteDefinition() {
433433
header_->stream() << "\
434-
class palette {\n\
434+
class palette;\n\
435+
class palette_data {\n\
435436
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\
460439
\n\
461440
inline const color &transparent() const { return _colors[0]; }; // special color\n";
462441

463-
int indexInPalette = 1;
442+
auto indexInPalette = 1;
464443
if (!module_.enumVariables([&](const Variable &variable) -> bool {
465444
auto name = variable.name.back();
466445
if (variable.value.type().tag != structure::TypeTag::Color) {
@@ -471,43 +450,12 @@ public:\n\
471450
header_->stream() << "\tinline const color &" << name << "() const { return _colors[" << index << "]; };\n";
472451
return true;
473452
})) return false;
453+
const auto count = indexInPalette;
474454

475-
auto count = indexInPalette;
476455
header_->stream() << "\
477456
\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\
511459
\n\
512460
internal::ColorData *data(int index) {\n\
513461
return reinterpret_cast<internal::ColorData*>(_data) + index;\n\
@@ -516,15 +464,6 @@ private:\n\
516464
const internal::ColorData *data(int index) const {\n\
517465
return reinterpret_cast<const internal::ColorData*>(_data) + index;\n\
518466
}\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\
528467
\n\
529468
enum class Status {\n\
530469
Initial,\n\
@@ -548,23 +487,22 @@ private:\n\
548487
namespace main_palette {\n\
549488
\n\
550489
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\
558490
\n\
559491
struct row {\n\
560-
\tQLatin1String name;\n\
561-
\tQLatin1String value;\n\
562-
\tQLatin1String fallback;\n\
563-
\tQLatin1String description;\n\
492+
QLatin1String name;\n\
493+
QLatin1String value;\n\
494+
QLatin1String fallback;\n\
495+
QLatin1String description;\n\
564496
};\n\
565497
QList<row> data();\n\
566498
\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";
568506

569507
return true;
570508
}
@@ -649,6 +587,10 @@ bool Generator::writeRefsDeclarations() {
649587
}
650588

651589
bool Generator::writeIncludesInSource() {
590+
if (isPalette_) {
591+
source_->include("ui/style/style_core_palette.h");
592+
source_->newline();
593+
}
652594
if (!module_.hasIncludes()) {
653595
return true;
654596
}
@@ -716,25 +658,8 @@ bool Generator::writeRefsDefinition() {
716658

717659
bool Generator::writeSetPaletteColor() {
718660
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";
738663

739664
QList<structure::FullName> names;
740665
module_.enumVariables([&](const Variable &variable) -> bool {
@@ -757,7 +682,7 @@ void palette::finalize() {\n\
757682
auto fallbackIterator = paletteIndices_.find(colorFallbackName(variable.value));
758683
auto fallbackIndex = (fallbackIterator == paletteIndices_.end()) ? -1 : fallbackIterator->second;
759684
auto assignment = QString("{ %1, %2, %3, %4 }").arg(color.red).arg(color.green).arg(color.blue).arg(color.alpha);
760-
source_->stream() << "\tcompute(" << index << ", " << fallbackIndex << ", " << assignment << ");\n";
685+
source_->stream() << "\tthat.compute(" << index << ", " << fallbackIndex << ", " << assignment << ");\n";
761686
checksumString.append(('&' + name + ':' + assignment).toUtf8());
762687

763688
auto isCopy = !variable.value.copyOf().isEmpty();
@@ -774,7 +699,7 @@ void palette::finalize() {\n\
774699
return false;
775700
}
776701

777-
dataRows.append("\tresult.push_back({ qstr(\"" + name + "\"), qstr(\"" + value + "\"), qstr(\"" + (isCopy ? QString() : fallbackName) + "\"), qstr(" + stringToEncodedString(variable.description.toStdString()) + ") });\n");
702+
dataRows.append("\tresult.push_back({ qstr(\"" + name + "\"), qstr(\"" + value + "\"), qstr(\"" + (isCopy ? QString() : fallbackName) + "\"), qstr(" + stringToEncodedString(variable.description) + ") });\n");
778703
return true;
779704
});
780705
if (!result) {
@@ -794,35 +719,14 @@ void palette::finalize() {\n\
794719
}
795720
source_->stream() << "\
796721
}\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\
818722
\n\
819-
int32 palette::Checksum() {\n\
723+
int32 palette_data::Checksum() {\n\
820724
return " << checksum << ";\n\
821725
}\n";
822726

823-
source_->newline().pushNamespace().newline();
727+
source_->newline().pushNamespace("internal").newline();
824728
source_->stream() << "\
825-
int getPaletteIndex(QLatin1String name) {\n\
729+
int GetPaletteIndex(QLatin1String name) {\n\
826730
auto size = name.size();\n\
827731
auto data = name.data();\n";
828732

@@ -959,90 +863,12 @@ int getPaletteIndex(QLatin1String name) {\n\
959863

960864
source_->newline().popNamespace().newline();
961865
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\
1006866
namespace main_palette {\n\
1007867
\n\
1008868
not_null<const palette*> get() {\n\
1009869
return &_palette;\n\
1010870
}\n\
1011871
\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\
1046872
QList<row> data() {\n\
1047873
auto result = QList<row>();\n\
1048874
result.reserve(" << count << ");\n\

codegen/style/module.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ class Module {
6565
bool hasVariables() const {
6666
return !variables_.isEmpty();
6767
}
68+
int variablesCount() const {
69+
return variables_.size();
70+
}
6871

6972
template <typename F>
7073
bool enumVariables(F functor) const {

0 commit comments

Comments
 (0)