/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ #include #include #include #include AZ_PUSH_DISABLE_WARNING(4251 4800 4244, "-Wunknown-warning-option") #include #include #include #include #include AZ_POP_DISABLE_WARNING #include #include #include #include namespace AZ { AZ_TYPE_INFO_SPECIALIZE(QVariant, "{EFF68052-50FF-4072-8047-31243EF95FEE}"); } namespace { static bool StyleVersionConverter([[maybe_unused]] AZ::SerializeContext& serializeContext, AZ::SerializeContext::DataElementNode& classElement) { if (classElement.GetVersion() <= 3) { auto selectorArrayElementIndex = classElement.FindElement(AZ_CRC("Selectors", 0x0e4270a0)); if (selectorArrayElementIndex != -1) { classElement.RemoveElement(selectorArrayElementIndex); } } return true; } namespace Styling = GraphCanvas::Styling; QString AttributeName(Styling::Attribute attribute) { switch (attribute) { case Styling::Attribute::BackgroundColor: return Styling::Attributes::BackgroundColor; case Styling::Attribute::BackgroundImage: return Styling::Attributes::BackgroundImage; case Styling::Attribute::CapStyle: return Styling::Attributes::CapStyle; case Styling::Attribute::GridMajorWidth: return Styling::Attributes::GridMajorWidth; case Styling::Attribute::GridMajorStyle: return Styling::Attributes::GridMajorStyle; case Styling::Attribute::GridMajorColor: return Styling::Attributes::GridMajorColor; case Styling::Attribute::GridMinorWidth: return Styling::Attributes::GridMinorWidth; case Styling::Attribute::GridMinorStyle: return Styling::Attributes::GridMinorStyle; case Styling::Attribute::GridMinorColor: return Styling::Attributes::GridMinorColor; case Styling::Attribute::FontFamily: return Styling::Attributes::FontFamily; case Styling::Attribute::FontSize: return Styling::Attributes::FontSize; case Styling::Attribute::FontWeight: return Styling::Attributes::FontWeight; case Styling::Attribute::FontStyle: return Styling::Attributes::FontStyle; case Styling::Attribute::FontVariant: return Styling::Attributes::FontVariant; case Styling::Attribute::Color: return Styling::Attributes::Color; case Styling::Attribute::BorderWidth: return Styling::Attributes::BorderWidth; case Styling::Attribute::BorderStyle: return Styling::Attributes::BorderStyle; case Styling::Attribute::BorderColor: return Styling::Attributes::BorderColor; case Styling::Attribute::BorderRadius: return Styling::Attributes::BorderRadius; case Styling::Attribute::LineWidth: return Styling::Attributes::LineWidth; case Styling::Attribute::LineStyle: return Styling::Attributes::LineStyle; case Styling::Attribute::LineColor: return Styling::Attributes::LineColor; case Styling::Attribute::LineCurve: return Styling::Attributes::LineCurve; case Styling::Attribute::LineSelectionPadding: return Styling::Attributes::LineSelectionPadding; case Styling::Attribute::Margin: return Styling::Attributes::Margin; case Styling::Attribute::Padding: return Styling::Attributes::Padding; case Styling::Attribute::Width: return Styling::Attributes::Width; case Styling::Attribute::Height: return Styling::Attributes::Height; case Styling::Attribute::MinWidth: return Styling::Attributes::MinWidth; case Styling::Attribute::MaxWidth: return Styling::Attributes::MaxWidth; case Styling::Attribute::MinHeight: return Styling::Attributes::MinHeight; case Styling::Attribute::MaxHeight: return Styling::Attributes::MaxHeight; case Styling::Attribute::Selectors: return Styling::Attributes::Selectors; case Styling::Attribute::TextAlignment: return Styling::Attributes::TextAlignment; case Styling::Attribute::Invalid: default: return "Invalid Attribute"; } } class QVariantSerializer : public AZ::SerializeContext::IDataSerializer { /// Store the class data into a binary buffer virtual size_t Save(const void* classPtr, AZ::IO::GenericStream& stream, bool isDataBigEndian /*= false*/) { auto variant = reinterpret_cast(classPtr); QByteArray buffer; QDataStream qtStream(&buffer, QIODevice::WriteOnly); qtStream.setByteOrder(isDataBigEndian ? QDataStream::BigEndian : QDataStream::LittleEndian); qtStream << *variant; return static_cast(stream.Write(buffer.length(), reinterpret_cast(buffer.data()))); } /// Convert binary data to text virtual size_t DataToText(AZ::IO::GenericStream& in, AZ::IO::GenericStream& out, bool isDataBigEndian /*= false*/) { (void)isDataBigEndian; QByteArray buffer = ReadAll(in); QByteArray&& base64 = buffer.toBase64(); return static_cast(out.Write(base64.size(), base64.data())); } /// Convert text data to binary, to support loading old version formats. We must respect text version if the text->binary format has changed! virtual size_t TextToData(const char* text, unsigned int textVersion, AZ::IO::GenericStream& stream, bool isDataBigEndian = false) { (void)textVersion; (void)isDataBigEndian; AZ_Assert(textVersion == 0, "Unknown char, short, int version!"); QByteArray decoder = QByteArray::fromBase64(text); stream.Seek(0, AZ::IO::GenericStream::ST_SEEK_BEGIN); return static_cast(stream.Write(decoder.size(), decoder.constData())); } /// Load the class data from a stream. virtual bool Load(void* classPtr, AZ::IO::GenericStream& in, unsigned int, bool isDataBigEndian = false) { QByteArray buffer = ReadAll(in); QDataStream qtStream(&buffer, QIODevice::ReadOnly); qtStream.setByteOrder(isDataBigEndian ? QDataStream::BigEndian : QDataStream::LittleEndian); QVariant* variant = reinterpret_cast(classPtr); qtStream >> *variant; return true; } bool CompareValueData(const void* left, const void* right) override { auto leftVariant = reinterpret_cast(left); auto rightVariant = reinterpret_cast(right); return *leftVariant == *rightVariant; } private: QByteArray ReadAll(AZ::IO::GenericStream& in) { const size_t length = in.GetLength(); QByteArray buffer; buffer.reserve(static_cast(length)); size_t processed = 0; size_t read = 0; char* data = buffer.data(); while (processed < length && (read = in.Read(length - processed, data + processed))) { processed += read; if (read == 0) { break; } } AZ_Assert(processed == length, "Incorrect amount of data read from stream"); buffer.resize(static_cast(processed)); return buffer; } }; } // namespace QDataStream& operator<<(QDataStream& out, const Qt::PenStyle& pen) { out << static_cast(pen); return out; } QDataStream& operator>>(QDataStream& in, Qt::PenStyle& pen) { int holder; in >> holder; pen = static_cast(holder); return in; } QDataStream& operator<<(QDataStream& out, const Qt::PenCapStyle& cap) { out << static_cast(cap); return out; } QDataStream& operator>>(QDataStream& in, Qt::PenCapStyle& cap) { int holder; in >> holder; cap = static_cast(holder); return in; } QDataStream& operator<<(QDataStream& out, const Qt::AlignmentFlag& alignment) { out << static_cast(alignment); return out; } QDataStream& operator>>(QDataStream& in, Qt::AlignmentFlag& alignment) { int holder; in >> holder; alignment = static_cast(holder); return in; } QDataStream& operator<<(QDataStream& out, const GraphCanvas::Styling::ConnectionCurveType& curve) { out << static_cast(curve); return out; } QDataStream& operator>>(QDataStream& in, GraphCanvas::Styling::ConnectionCurveType& curve) { int holder; in >> holder; curve = static_cast(holder); return in; } QDataStream& operator<<(QDataStream& out, const GraphCanvas::Styling::PaletteStyle& curve) { out << static_cast(curve); return out; } QDataStream& operator >> (QDataStream& in, GraphCanvas::Styling::PaletteStyle& curve) { int holder; in >> holder; curve = static_cast(holder); return in; } namespace GraphCanvas { namespace Styling { ////////// // Style ////////// void Style::Reflect(AZ::ReflectContext* context) { static bool reflected = false; AZ::SerializeContext* serializeContext = azrtti_cast(context); if (!serializeContext || (reflected && !serializeContext->IsRemovingReflection())) { return; } qRegisterMetaTypeStreamOperators(); qRegisterMetaTypeStreamOperators(); qRegisterMetaTypeStreamOperators(); qRegisterMetaTypeStreamOperators(); qRegisterMetaTypeStreamOperators(); // Allow QVectors to be serialized serializeContext->Class() ->Serializer(); //TODO port collection class away from QHash so it can be serialized serializeContext->Class