QmlDesigner: Fix the bug for saving collection

- The node resolved path was wrong.
- Also the saving method is cleaned up.

Change-Id: Ic905c20e5899fcc23c97a8d42a1fdf7c0c8e1089
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
This commit is contained in:
Ali Kianian
2023-11-17 11:34:50 +02:00
parent a5a1152a76
commit 42405ffa74
5 changed files with 38 additions and 18 deletions

View File

@@ -102,13 +102,7 @@ Item {
icon: StudioTheme.Constants.updateContent_medium
tooltip: qsTr("Update existing file with changes")
enabled: root.model.collectionName !== ""
onClicked:
{
if (root.backend.selectedSourceAddress().indexOf("json") !== -1)
root.model.exportCollection(root.backend.selectedSourceAddress(), root.model.collectionName, "JSON")
else
root.model.exportCollection(root.backend.selectedSourceAddress(), root.model.collectionName, "CSV")
}
onClicked: root.model.saveCurrentCollection()
}
IconButton {

View File

@@ -408,9 +408,10 @@ void CollectionDetailsModel::loadCollection(const ModelNode &sourceNode, const Q
}
}
bool CollectionDetailsModel::exportCollection(const QString &path, const QString &collectionName, const QString &exportType)
bool CollectionDetailsModel::exportCollection(const QUrl &url,
const QString &collectionName,
const QString &exportType)
{
QUrl url(path);
QString filePath;
if (url.isLocalFile()) {
QFileInfo fileInfo(url.toLocalFile());
@@ -418,7 +419,7 @@ bool CollectionDetailsModel::exportCollection(const QString &path, const QString
fileInfo.setFile(QString("%1.%2").arg(url.toLocalFile(), exportType.toLower()));
filePath = fileInfo.absoluteFilePath();
} else {
filePath = path;
filePath = url.toString();
}
if (exportType == "JSON") {
@@ -432,6 +433,11 @@ bool CollectionDetailsModel::exportCollection(const QString &path, const QString
return false;
}
bool CollectionDetailsModel::saveCurrentCollection()
{
return saveCollection(m_currentCollection);
}
void CollectionDetailsModel::updateEmpty()
{
bool isEmptyNow = rowCount() == 0;
@@ -606,6 +612,28 @@ void CollectionDetailsModel::setCollectionName(const QString &newCollectionName)
}
}
bool CollectionDetailsModel::saveCollection(CollectionDetails &collection)
{
if (!collection.isValid() || !m_openedCollections.contains(collection.reference()))
return false;
const ModelNode node = collection.reference().node;
bool saved = false;
if (CollectionEditor::getSourceCollectionType(node) == "json") {
saved = saveCollectionAsJson(CollectionEditor::getSourceCollectionPath(node),
collection.getJsonCollection(),
collection.reference().name);
} else if (CollectionEditor::getSourceCollectionType(node) == "csv") {
saved = saveCollectionAsCsv(CollectionEditor::getSourceCollectionPath(node),
collection.getCsvCollection());
}
if (saved)
collection.markSaved();
return saved;
}
bool CollectionDetailsModel::saveCollectionAsJson(const QString &path, const QJsonArray &content, const QString &collectionName)
{
QFile sourceFile(path);

View File

@@ -63,7 +63,11 @@ public:
void loadCollection(const ModelNode &sourceNode, const QString &collection);
Q_INVOKABLE bool exportCollection(const QString &path, const QString &collectionName, const QString &exportType);
Q_INVOKABLE bool exportCollection(const QUrl &url,
const QString &collectionName,
const QString &exportType);
Q_INVOKABLE bool saveCurrentCollection();
signals:
void collectionNameChanged(const QString &collectionName);
@@ -81,6 +85,7 @@ private:
void setCollectionName(const QString &newCollectionName);
void loadJsonCollection(const QString &source, const QString &collection);
void loadCsvCollection(const QString &source, const QString &collectionName);
bool saveCollection(CollectionDetails &collection);
bool saveCollectionAsJson(const QString &path, const QJsonArray &content, const QString &collectionName);
bool saveCollectionAsCsv(const QString &path, const QString &content);
QVariant variantFromString(const QString &value);

View File

@@ -393,11 +393,6 @@ void CollectionSourceModel::updateNodeSource(const ModelNode &node)
updateCollectionList(index);
}
QString CollectionSourceModel::selectedSourceAddress() const
{
return index(m_selectedIndex).data(SourceRole).toString();
}
void CollectionSourceModel::onSelectedCollectionChanged(int collectionIndex)
{
CollectionListModel *collectionList = qobject_cast<CollectionListModel *>(sender());

View File

@@ -67,8 +67,6 @@ public:
void updateNodeName(const ModelNode &node);
void updateNodeSource(const ModelNode &node);
Q_INVOKABLE QString selectedSourceAddress() const;
signals:
void selectedIndexChanged(int idx);
void collectionSelected(const ModelNode &sourceNode, const QString &collectionName);