From 93e4794114e837d1815b25d62f2a2acdbc9b81d0 Mon Sep 17 00:00:00 2001 From: Michael Winkelmann Date: Mon, 3 May 2021 22:17:20 +0200 Subject: [PATCH] Annotation: toJsonValue / fromJsonValue Change-Id: I743176a90cf617a6bcdb988f3c34d0fc232a001d Reviewed-by: Aleksei German Reviewed-by: Thomas Hartmann --- .../designercore/include/annotation.h | 11 +++-- .../designercore/model/annotation.cpp | 49 +++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/plugins/qmldesigner/designercore/include/annotation.h b/src/plugins/qmldesigner/designercore/include/annotation.h index 752c817211a..548a3c52aa7 100644 --- a/src/plugins/qmldesigner/designercore/include/annotation.h +++ b/src/plugins/qmldesigner/designercore/include/annotation.h @@ -25,12 +25,13 @@ #pragma once -#include -#include #include +#include +#include +#include -#include "qmldesignercorelib_global.h" #include "nodeinstanceglobal.h" +#include "qmldesignercorelib_global.h" namespace QmlDesigner { @@ -97,6 +98,8 @@ public: bool isEmpty() const; QString toQString() const; + QJsonValue toJsonValue() const; + bool fromJsonValue(QJsonValue const &); friend QDebug &operator<<(QDebug &stream, const Comment &comment); @@ -130,6 +133,8 @@ public: QString toQString() const; void fromQString(const QString &str); + QJsonValue toJsonValue() const; + bool fromJsonValue(QJsonValue const &); friend QDebug &operator<<(QDebug &stream, const Annotation &annotation); diff --git a/src/plugins/qmldesigner/designercore/model/annotation.cpp b/src/plugins/qmldesigner/designercore/model/annotation.cpp index f8f2edc0d8a..0ffac210164 100644 --- a/src/plugins/qmldesigner/designercore/model/annotation.cpp +++ b/src/plugins/qmldesigner/designercore/model/annotation.cpp @@ -26,6 +26,7 @@ #include "annotation.h" #include +#include #include namespace QmlDesigner { @@ -150,6 +151,27 @@ QString Comment::toQString() const return result.join(s_sep); } +QJsonValue Comment::toJsonValue() const +{ + return QJsonObject{ + {{"title", m_title}, {"author", m_author}, {"text", m_text}, {"timestamp", m_timestamp}}}; +}; + +bool Comment::fromJsonValue(QJsonValue const &v) +{ + if (!v.isObject()) + return false; + + auto obj = v.toObject(); + Comment comment; + comment.m_title = obj["title"].toString(); + comment.m_author = obj["author"].toString(); + comment.m_text = obj["text"].toString(); + comment.m_timestamp = obj["timestamp"].toInt(); + *this = comment; + return true; +} + QDebug &operator<<(QDebug &stream, const Comment &comment) { stream << "\"title: " << comment.m_title << "\" "; @@ -299,6 +321,33 @@ void Annotation::fromQString(const QString &str) } } +QJsonValue Annotation::toJsonValue() const +{ + QJsonObject obj; + QJsonArray comments; + for (auto &comment : m_comments) + comments.push_back(comment.toJsonValue()); + + obj["comments"] = comments; + return obj; +} + +bool Annotation::fromJsonValue(const QJsonValue &v) +{ + if (!v.isObject()) + return false; + + auto obj = v.toObject(); + auto jsonComments = obj["comments"].toArray(); + m_comments.clear(); + for (auto json : jsonComments) { + Comment comment; + if (comment.fromJsonValue(json)) + m_comments.push_back(comment); + } + return true; +} + QDebug &operator<<(QDebug &stream, const Annotation &annotation) { stream << "\"Annotation: " << annotation.m_comments << "\" ";