QmlDesigner: Export collection as CSV

Task-number: QDS-10618
Change-Id: Iaae95dd920b453e53c22fdf65c2f6d80652d5be3
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
Reviewed-by: Qt CI Patch Build Bot <ci_patchbuild_bot@qt.io>
Reviewed-by: Ali Kianian <ali.kianian@qt.io>
This commit is contained in:
Shrief Gabr
2023-10-09 13:16:59 +03:00
parent 100ea34060
commit 5671bd3cd0
4 changed files with 39 additions and 2 deletions

View File

@@ -5,6 +5,7 @@
#include <QJsonArray>
#include <QJsonObject>
#include <QTextStream>
#include <QVariant>
namespace QmlDesigner {
@@ -269,4 +270,29 @@ QJsonArray CollectionDetails::getJsonCollection() const
return collectionArray;
}
QString CollectionDetails::getCsvCollection() const
{
QString content;
if (d->headers.count() <= 0)
return "";
for (const QString &header : std::as_const(d->headers))
content += header + ',';
content.back() = '\n';
for (const QJsonObject &elementsRow : std::as_const(d->elements)) {
for (const QString &header : std::as_const(d->headers)) {
const QJsonValue &value = elementsRow.value(header);
if (value.isDouble())
content += QString::number(value.toDouble()) + ',';
else
content += value.toString() + ',';
}
content.back() = '\n';
}
return content;
}
} // namespace QmlDesigner

View File

@@ -69,6 +69,8 @@ public:
void swap(CollectionDetails &other);
QJsonArray getJsonCollection() const;
QString getCsvCollection() const;
CollectionDetails &operator=(const CollectionDetails &other);
private:

View File

@@ -361,7 +361,6 @@ void SingleCollectionModel::loadJsonCollection(const QString &source, const QStr
}
SourceFormat sourceFormat = jsonFileIsOk ? SourceFormat::Json : SourceFormat::Unknown;
beginResetModel();
m_currentCollection.resetDetails(getJsonHeaders(collectionNodes), elements, sourceFormat);
endResetModel();
@@ -401,7 +400,6 @@ void SingleCollectionModel::loadCsvCollection(const QString &source,
}
SourceFormat sourceFormat = csvFileIsOk ? SourceFormat::Csv : SourceFormat::Unknown;
beginResetModel();
m_currentCollection.resetDetails(headers, elements, sourceFormat);
endResetModel();
@@ -436,4 +434,14 @@ bool SingleCollectionModel::saveCollectionAsJson(const QString &collection, cons
return false;
}
bool SingleCollectionModel::saveCollectionAsCsv(const QString &path, const QString &content)
{
QFile file(path);
if (file.open(QFile::WriteOnly) && file.write(content.toUtf8()))
return true;
return false;
}
} // namespace QmlDesigner

View File

@@ -74,6 +74,7 @@ private:
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 saveCollectionAsCsv(const QString &path, const QString &content);
QHash<CollectionReference, CollectionDetails> m_openedCollections;
CollectionDetails m_currentCollection;