forked from qt-creator/qt-creator
QmlDesigner: Fix data loss after saving collections
Task-number: QDS-11582 Change-Id: I69fbfbcebd880a4df1597bde25aac8372950cdfb Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io> Reviewed-by: Qt CI Patch Build Bot <ci_patchbuild_bot@qt.io> Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
This commit is contained in:
@@ -238,6 +238,7 @@ bool CollectionDetails::setPropertyValue(int row, int column, const QVariant &va
|
||||
return false;
|
||||
|
||||
element.insert(d->properties.at(column).name, QJsonValue::fromVariant(value));
|
||||
markChanged();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -458,16 +459,19 @@ void CollectionDetails::resetPropertyTypes()
|
||||
resetPropertyType(property);
|
||||
}
|
||||
|
||||
QString CollectionDetails::getCollectionAsJsonString() const
|
||||
QJsonArray CollectionDetails::getCollectionAsJsonArray() const
|
||||
{
|
||||
QJsonArray collectionArray;
|
||||
|
||||
for (const QJsonObject &element : std::as_const(d->elements))
|
||||
collectionArray.push_back(element);
|
||||
|
||||
QString collectionString = QString::fromUtf8(QJsonDocument(collectionArray).toJson());
|
||||
return collectionArray;
|
||||
}
|
||||
|
||||
return collectionString;
|
||||
QString CollectionDetails::getCollectionAsJsonString() const
|
||||
{
|
||||
return QString::fromUtf8(QJsonDocument(getCollectionAsJsonArray()).toJson());
|
||||
}
|
||||
|
||||
QString CollectionDetails::getCollectionAsCsvString() const
|
||||
|
@@ -106,6 +106,8 @@ public:
|
||||
QString getCollectionAsJsonString() const;
|
||||
QString getCollectionAsCsvString() const;
|
||||
|
||||
QJsonArray getCollectionAsJsonArray() const;
|
||||
|
||||
static void registerDeclarativeType();
|
||||
|
||||
CollectionDetails &operator=(const CollectionDetails &other);
|
||||
|
@@ -12,6 +12,7 @@
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonParseError>
|
||||
|
||||
@@ -413,7 +414,7 @@ void CollectionDetailsModel::loadCollection(const ModelNode &sourceNode, const Q
|
||||
|
||||
bool CollectionDetailsModel::saveCurrentCollection()
|
||||
{
|
||||
return saveCollection({}, &m_currentCollection);
|
||||
return saveCollection({});
|
||||
}
|
||||
|
||||
bool CollectionDetailsModel::exportCollection(const QString &filePath)
|
||||
@@ -599,45 +600,44 @@ bool CollectionDetailsModel::saveCollection(const QString &filePath, CollectionD
|
||||
{
|
||||
bool saved = false;
|
||||
|
||||
auto saveSingleCollection = [&](CollectionDetails &singleCollection) {
|
||||
|
||||
const ModelNode node = singleCollection.reference().node;
|
||||
const ModelNode node = m_currentCollection.reference().node;
|
||||
QString path = CollectionEditor::getSourceCollectionPath(node);
|
||||
QString saveFormat = CollectionEditor::getSourceCollectionType(node);
|
||||
|
||||
QFile sourceFile(path);
|
||||
|
||||
if (!filePath.isEmpty()) {
|
||||
QUrl url(filePath);
|
||||
path = url.isLocalFile() ? QFileInfo(url.toLocalFile()).absoluteFilePath() : url.toString();
|
||||
saveFormat = url.isLocalFile() ? QFileInfo(url.toLocalFile()).suffix().toLower() : saveFormat;
|
||||
QString content = saveFormat == "json" ? collection->getCollectionAsJsonString()
|
||||
: saveFormat == "csv" ? collection->getCollectionAsCsvString() : QString();
|
||||
|
||||
sourceFile.setFileName(path);
|
||||
|
||||
if (sourceFile.open(QFile::WriteOnly))
|
||||
saved = sourceFile.write(content.toUtf8());
|
||||
} else if (filePath.isEmpty() && sourceFile.open(QFile::ReadWrite)) {
|
||||
QJsonParseError jpe;
|
||||
QJsonDocument document = QJsonDocument::fromJson(sourceFile.readAll(), &jpe);
|
||||
|
||||
if (jpe.error == QJsonParseError::NoError) {
|
||||
QJsonObject obj = document.object();
|
||||
|
||||
for (const CollectionDetails &openedCollection : std::as_const(m_openedCollections))
|
||||
obj[openedCollection.reference().name] = openedCollection.getCollectionAsJsonArray();
|
||||
|
||||
document.setObject(obj);
|
||||
saved = sourceFile.write(document.toJson());
|
||||
|
||||
if (saved)
|
||||
collection->markSaved();
|
||||
}
|
||||
|
||||
saved = saveCollectionFromString(path, (saveFormat == "json") ? singleCollection.getCollectionAsJsonString() :
|
||||
(saveFormat == "csv") ? singleCollection.getCollectionAsCsvString() : QString());
|
||||
|
||||
if (saved && filePath.isEmpty())
|
||||
singleCollection.markSaved();
|
||||
};
|
||||
|
||||
if (!collection) {
|
||||
for (CollectionDetails &openedCollection : m_openedCollections)
|
||||
saveSingleCollection(openedCollection);
|
||||
} else {
|
||||
saveSingleCollection(*collection);
|
||||
}
|
||||
|
||||
return saved;
|
||||
}
|
||||
|
||||
bool CollectionDetailsModel::saveCollectionFromString(const QString &path, const QString &content)
|
||||
{
|
||||
QFile file(path);
|
||||
|
||||
if (file.open(QFile::WriteOnly) && file.write(content.toUtf8()))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
QString CollectionDetailsModel::warningToString(DataTypeWarning::Warning warning) const
|
||||
{
|
||||
return DataTypeWarning::getDataTypeWarningString(warning);
|
||||
|
@@ -82,7 +82,6 @@ private:
|
||||
void loadJsonCollection(const QString &source, const QString &collection);
|
||||
void loadCsvCollection(const QString &source, const QString &collectionName);
|
||||
bool saveCollection(const QString &filePath = {}, CollectionDetails *collection = nullptr);
|
||||
bool saveCollectionFromString(const QString &path, const QString &content);
|
||||
QVariant variantFromString(const QString &value);
|
||||
|
||||
QHash<CollectionReference, CollectionDetails> m_openedCollections;
|
||||
|
Reference in New Issue
Block a user