forked from qt-creator/qt-creator
QmlDesigner: Export a collection as Json
Task-number: QDS-10617 Change-Id: I08be4cc77bf11e93a395a82aef1522656807a8c1 Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io> Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io> Reviewed-by: Qt CI Patch Build Bot <ci_patchbuild_bot@qt.io>
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "collectiondetails.h"
|
#include "collectiondetails.h"
|
||||||
|
|
||||||
|
#include <QJsonArray>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
|
|
||||||
@@ -196,4 +197,13 @@ void CollectionDetails::markChanged()
|
|||||||
d->isChanged = true;
|
d->isChanged = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QJsonArray CollectionDetails::getJsonCollection() const
|
||||||
|
{
|
||||||
|
QJsonArray collectionArray;
|
||||||
|
for (const QJsonObject &element : std::as_const(d->elements))
|
||||||
|
collectionArray.push_back(element);
|
||||||
|
|
||||||
|
return collectionArray;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace QmlDesigner
|
} // namespace QmlDesigner
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ public:
|
|||||||
bool markSaved();
|
bool markSaved();
|
||||||
|
|
||||||
void swap(CollectionDetails &other);
|
void swap(CollectionDetails &other);
|
||||||
|
QJsonArray getJsonCollection() const;
|
||||||
CollectionDetails &operator=(const CollectionDetails &other);
|
CollectionDetails &operator=(const CollectionDetails &other);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -228,4 +228,25 @@ void SingleCollectionModel::setCollectionName(const QString &newCollectionName)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool SingleCollectionModel::saveCollectionAsJson(const QString &collection, const QJsonArray &content, const QString &source)
|
||||||
|
{
|
||||||
|
QFile sourceFile(source);
|
||||||
|
if (sourceFile.open(QFile::ReadWrite)) {
|
||||||
|
QJsonParseError jpe;
|
||||||
|
QJsonDocument document = QJsonDocument::fromJson(sourceFile.readAll(), &jpe);
|
||||||
|
|
||||||
|
if (jpe.error == QJsonParseError::NoError) {
|
||||||
|
QJsonObject collectionMap = document.object();
|
||||||
|
collectionMap[collection] = content;
|
||||||
|
document.setObject(collectionMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceFile.resize(0);
|
||||||
|
if (sourceFile.write(document.toJson()))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace QmlDesigner
|
} // namespace QmlDesigner
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ private:
|
|||||||
void setCollectionName(const QString &newCollectionName);
|
void setCollectionName(const QString &newCollectionName);
|
||||||
void loadJsonCollection(const QString &source, const QString &collection);
|
void loadJsonCollection(const QString &source, const QString &collection);
|
||||||
void loadCsvCollection(const QString &source, const QString &collectionName);
|
void loadCsvCollection(const QString &source, const QString &collectionName);
|
||||||
|
bool saveCollectionAsJson(const QString &collection, const QJsonArray &content, const QString &source);
|
||||||
|
|
||||||
QHash<CollectionReference, CollectionDetails> m_openedCollections;
|
QHash<CollectionReference, CollectionDetails> m_openedCollections;
|
||||||
CollectionDetails m_currentCollection;
|
CollectionDetails m_currentCollection;
|
||||||
|
|||||||
Reference in New Issue
Block a user