forked from qt-creator/qt-creator
Annotation: toJsonValue / fromJsonValue
Change-Id: I743176a90cf617a6bcdb988f3c34d0fc232a001d Reviewed-by: Aleksei German <aleksei.german@qt.io> Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
@@ -25,12 +25,13 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QObject>
|
|
||||||
#include <QDebug>
|
|
||||||
#include <QDataStream>
|
#include <QDataStream>
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
#include "qmldesignercorelib_global.h"
|
|
||||||
#include "nodeinstanceglobal.h"
|
#include "nodeinstanceglobal.h"
|
||||||
|
#include "qmldesignercorelib_global.h"
|
||||||
|
|
||||||
namespace QmlDesigner {
|
namespace QmlDesigner {
|
||||||
|
|
||||||
@@ -97,6 +98,8 @@ public:
|
|||||||
bool isEmpty() const;
|
bool isEmpty() const;
|
||||||
|
|
||||||
QString toQString() const;
|
QString toQString() const;
|
||||||
|
QJsonValue toJsonValue() const;
|
||||||
|
bool fromJsonValue(QJsonValue const &);
|
||||||
|
|
||||||
friend QDebug &operator<<(QDebug &stream, const Comment &comment);
|
friend QDebug &operator<<(QDebug &stream, const Comment &comment);
|
||||||
|
|
||||||
@@ -130,6 +133,8 @@ public:
|
|||||||
|
|
||||||
QString toQString() const;
|
QString toQString() const;
|
||||||
void fromQString(const QString &str);
|
void fromQString(const QString &str);
|
||||||
|
QJsonValue toJsonValue() const;
|
||||||
|
bool fromJsonValue(QJsonValue const &);
|
||||||
|
|
||||||
friend QDebug &operator<<(QDebug &stream, const Annotation &annotation);
|
friend QDebug &operator<<(QDebug &stream, const Annotation &annotation);
|
||||||
|
|
||||||
|
@@ -26,6 +26,7 @@
|
|||||||
#include "annotation.h"
|
#include "annotation.h"
|
||||||
|
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
#include <QJsonArray>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
namespace QmlDesigner {
|
namespace QmlDesigner {
|
||||||
@@ -150,6 +151,27 @@ QString Comment::toQString() const
|
|||||||
return result.join(s_sep);
|
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)
|
QDebug &operator<<(QDebug &stream, const Comment &comment)
|
||||||
{
|
{
|
||||||
stream << "\"title: " << comment.m_title << "\" ";
|
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)
|
QDebug &operator<<(QDebug &stream, const Annotation &annotation)
|
||||||
{
|
{
|
||||||
stream << "\"Annotation: " << annotation.m_comments << "\" ";
|
stream << "\"Annotation: " << annotation.m_comments << "\" ";
|
||||||
|
Reference in New Issue
Block a user