forked from qt-creator/qt-creator
QmlDesigner: Connect collection save functionality to UI
Task-number: QDS-10922 Change-Id: I9d2d930b8203b76d40e0ce1c83351e490509020e 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:
@@ -392,6 +392,22 @@ void CollectionDetailsModel::loadCollection(const ModelNode &sourceNode, const Q
|
||||
}
|
||||
}
|
||||
|
||||
bool CollectionDetailsModel::exportCollection(const QString &path, const QString &collectionName, const QString &exportType)
|
||||
{
|
||||
QUrl url(path);
|
||||
QString fileAddress = url.isLocalFile() ? url.toLocalFile() : path;
|
||||
|
||||
if (exportType == "JSON") {
|
||||
QJsonArray content = m_currentCollection.getJsonCollection();
|
||||
return saveCollectionAsJson(fileAddress, content, collectionName);
|
||||
} else if (exportType == "CSV") {
|
||||
QString content = m_currentCollection.getCsvCollection();
|
||||
return saveCollectionAsCsv(fileAddress, content);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void CollectionDetailsModel::updateEmpty()
|
||||
{
|
||||
bool isEmptyNow = rowCount() == 0;
|
||||
@@ -527,24 +543,32 @@ void CollectionDetailsModel::setCollectionName(const QString &newCollectionName)
|
||||
}
|
||||
}
|
||||
|
||||
bool CollectionDetailsModel::saveCollectionAsJson(const QString &collection, const QJsonArray &content, const QString &source)
|
||||
bool CollectionDetailsModel::saveCollectionAsJson(const QString &path, const QJsonArray &content, const QString &collectionName)
|
||||
{
|
||||
QFile sourceFile(source);
|
||||
if (sourceFile.open(QFile::ReadWrite)) {
|
||||
QFile sourceFile(path);
|
||||
QJsonDocument document;
|
||||
|
||||
if (sourceFile.exists() && sourceFile.open(QFile::ReadWrite)) {
|
||||
QJsonParseError jpe;
|
||||
QJsonDocument document = QJsonDocument::fromJson(sourceFile.readAll(), &jpe);
|
||||
document = QJsonDocument::fromJson(sourceFile.readAll(), &jpe);
|
||||
|
||||
if (jpe.error == QJsonParseError::NoError) {
|
||||
QJsonObject collectionMap = document.object();
|
||||
collectionMap[collection] = content;
|
||||
collectionMap[collectionName] = content;
|
||||
document.setObject(collectionMap);
|
||||
}
|
||||
|
||||
sourceFile.resize(0);
|
||||
if (sourceFile.write(document.toJson()))
|
||||
return true;
|
||||
|
||||
} else if (sourceFile.open(QFile::WriteOnly)) {
|
||||
QJsonObject collection;
|
||||
collection[collectionName] = content;
|
||||
document.setObject(collection);
|
||||
}
|
||||
|
||||
if (sourceFile.write(document.toJson()))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -53,15 +53,15 @@ public:
|
||||
Q_INVOKABLE bool selectColumn(int section);
|
||||
Q_INVOKABLE bool renameColumn(int section, const QString &newValue);
|
||||
Q_INVOKABLE bool setPropertyType(int column, const QString &newValue, bool force = false);
|
||||
|
||||
Q_INVOKABLE bool selectRow(int row);
|
||||
|
||||
Q_INVOKABLE void deselectAll();
|
||||
|
||||
static Q_INVOKABLE QStringList typesList();
|
||||
|
||||
void loadCollection(const ModelNode &sourceNode, const QString &collection);
|
||||
|
||||
Q_INVOKABLE bool exportCollection(const QString &path, const QString &collectionName, const QString &exportType);
|
||||
|
||||
signals:
|
||||
void collectionNameChanged(const QString &collectionName);
|
||||
void selectedColumnChanged(int);
|
||||
@@ -78,7 +78,7 @@ private:
|
||||
void setCollectionName(const QString &newCollectionName);
|
||||
void loadJsonCollection(const QString &source, const QString &collection);
|
||||
void loadCsvCollection(const QString &source, const QString &collectionName);
|
||||
bool saveCollectionAsJson(const QString &collection, const QJsonArray &content, const QString &source);
|
||||
bool saveCollectionAsJson(const QString &path, const QJsonArray &content, const QString &collectionName);
|
||||
bool saveCollectionAsCsv(const QString &path, const QString &content);
|
||||
|
||||
QHash<CollectionReference, CollectionDetails> m_openedCollections;
|
||||
|
||||
@@ -50,7 +50,7 @@ public:
|
||||
void updateNodeSource(const ModelNode &node);
|
||||
void updateNodeId(const ModelNode &node);
|
||||
|
||||
QString selectedSourceAddress() const;
|
||||
Q_INVOKABLE QString selectedSourceAddress() const;
|
||||
|
||||
signals:
|
||||
void selectedIndexChanged(int idx);
|
||||
|
||||
@@ -167,4 +167,5 @@ void CollectionWidget::warn(const QString &title, const QString &body)
|
||||
Q_ARG(QVariant, title),
|
||||
Q_ARG(QVariant, body));
|
||||
}
|
||||
|
||||
} // namespace QmlDesigner
|
||||
|
||||
Reference in New Issue
Block a user