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:
Shrief Gabr
2023-10-24 09:44:31 +03:00
parent 3564e645a2
commit d10f760a55
7 changed files with 100 additions and 11 deletions

View File

@@ -2,15 +2,18 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
import QtQuick
import Qt.labs.platform as PlatformWidgets
import HelperWidgets 2.0 as HelperWidgets
import StudioControls 1.0 as StudioControls
import StudioTheme 1.0 as StudioTheme
import CollectionEditorBackend
Item {
id: root
property real iconHeight: 20
required property var model
required property var backend
property int selectedRow: -1
implicitHeight: 30
@@ -96,6 +99,64 @@ Item {
top: parent.top
bottom: parent.bottom
}
IconButton {
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")
}
}
IconButton {
icon: StudioTheme.Constants.export_medium
tooltip: qsTr("Export collection to a new file")
enabled: root.model.collectionName !== ""
onClicked: exportMenu.popup()
}
StudioControls.Menu {
id: exportMenu
StudioControls.MenuItem {
text: qsTr("Export as JSON")
onTriggered:
{
fileDialog.defaultSuffix = "json"
fileDialog.open()
}
}
StudioControls.MenuItem {
text: qsTr("Export as CSV")
onTriggered:
{
fileDialog.defaultSuffix = "csv"
fileDialog.open()
}
}
}
}
PlatformWidgets.FileDialog {
id: fileDialog
fileMode: PlatformWidgets.FileDialog.SaveFile
onAccepted:
{
var fileAddress = file.toString()
if (fileAddress.indexOf("json") !== -1)
root.model.exportCollection(fileAddress, root.model.collectionName, "JSON")
else if (fileAddress.indexOf("csv") !== -1)
root.model.exportCollection(fileAddress, root.model.collectionName, "CSV")
fileDialog.reject()
}
}
Rectangle {

View File

@@ -12,6 +12,7 @@ Rectangle {
id: root
required property var model
required property var backend
implicitWidth: 600
implicitHeight: 400
@@ -60,6 +61,7 @@ Rectangle {
CollectionDetailsToolbar {
id: toolbar
model: root.model
backend: root.backend
Layout.fillWidth: true
}

View File

@@ -144,6 +144,7 @@ Item {
CollectionDetailsView {
model: root.collectionDetailsModel
backend: root.model
anchors {
left: collectionsRect.right
right: parent.right

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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);

View File

@@ -167,4 +167,5 @@ void CollectionWidget::warn(const QString &title, const QString &body)
Q_ARG(QVariant, title),
Q_ARG(QVariant, body));
}
} // namespace QmlDesigner