Commit cd05ed73 authored by Volker Krause's avatar Volker Krause
Browse files

Allow to import and export product schemas

parent b3270758
......@@ -79,16 +79,27 @@ QByteArray Product::toJson() const
return doc.toJson();
}
static Product productFromJsonObject(const QJsonObject &obj)
{
Product product;
product.setName(obj.value(QStringLiteral("name")).toString());
product.setSchema(SchemaEntry::fromJson(obj.value(QStringLiteral("schema")).toArray()));
return product;
}
QVector<Product> Product::fromJson(const QByteArray &data)
{
QVector<Product> products;
const auto doc = QJsonDocument::fromJson(data);
foreach (const auto &value, doc.array()) {
const auto obj = value.toObject();
Product product;
product.setName(obj.value(QStringLiteral("name")).toString());
product.setSchema(SchemaEntry::fromJson(obj.value(QStringLiteral("schema")).toArray()));
products.push_back(product);
if (doc.isArray()) {
const auto array = doc.array();
products.reserve(array.size());
foreach (const auto &value, array) {
const auto obj = value.toObject();
products.push_back(productFromJsonObject(obj));
}
} else if (doc.isObject()) {
products.push_back(productFromJsonObject(doc.object()));
}
return products;
}
......@@ -25,6 +25,7 @@
#include <core/schemaentrytemplates.h>
#include <QDebug>
#include <QFileDialog>
#include <QMenu>
#include <QMessageBox>
#include <QNetworkReply>
......@@ -46,6 +47,8 @@ SchemaEditWidget::SchemaEditWidget(QWidget *parent) :
connect(ui->newEntryName, &QLineEdit::returnPressed, this, &SchemaEditWidget::addEntry);
connect(ui->actionDelete, &QAction::triggered, this, &SchemaEditWidget::deleteEntry);
connect(ui->actionSave, &QAction::triggered, this, &SchemaEditWidget::save);
connect(ui->actionImportSchema, &QAction::triggered, this, &SchemaEditWidget::importSchema);
connect(ui->actionExportSchema, &QAction::triggered, this, &SchemaEditWidget::exportSchema);
connect(ui->schemaView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &SchemaEditWidget::updateState);
connect(ui->newEntryName, &QLineEdit::textChanged, this, &SchemaEditWidget::updateState);
......@@ -61,7 +64,7 @@ SchemaEditWidget::SchemaEditWidget(QWidget *parent) :
m_createFromTemplateAction = templateMenu->menuAction();
m_createFromTemplateAction->setIcon(QIcon::fromTheme(QStringLiteral("document-new-from-template")));
addActions({ m_createFromTemplateAction, ui->actionDelete, ui->actionSave });
addActions({ m_createFromTemplateAction, ui->actionDelete, ui->actionSave, ui->actionImportSchema, ui->actionExportSchema });
updateState();
}
......@@ -120,3 +123,41 @@ void SchemaEditWidget::updateState()
m_createFromTemplateAction->setEnabled(m_schemaModel->product().isValid());
ui->actionSave->setEnabled(m_schemaModel->product().isValid());
}
void SchemaEditWidget::exportSchema()
{
const auto fileName = QFileDialog::getSaveFileName(this, tr("Export Schema"));
if (fileName.isEmpty())
return;
QFile f(fileName);
if (!f.open(QFile::WriteOnly)) {
QMessageBox::critical(this, tr("Export Failed"), tr("Could not open file: %1").arg(f.errorString()));
return;
}
f.write(m_schemaModel->product().toJson());
logMessage(tr("Schema of %1 exported to %2.").arg(m_schemaModel->product().name(), f.fileName()));
}
void SchemaEditWidget::importSchema()
{
const auto fileName = QFileDialog::getOpenFileName(this, tr("Import Schema"));
if (fileName.isEmpty())
return;
QFile f(fileName);
if (!f.open(QFile::ReadOnly)) {
QMessageBox::critical(this, tr("Import Failed"), tr("Could not open file: %1").arg(f.errorString()));
return;
}
const auto products = Product::fromJson(f.readAll());
if (products.size() != 1 || !products.at(0).isValid()) {
QMessageBox::critical(this, tr("Import Failed"), tr("Selected file contains no valid product schema."));
return;
}
auto p = products.at(0);
p.setName(m_schemaModel->product().name());
m_schemaModel->setProduct(p);
logMessage(tr("Schema of %1 imported from %2.").arg(m_schemaModel->product().name(), f.fileName()));
}
......@@ -52,6 +52,8 @@ private:
void addEntry();
void deleteEntry();
void save();
void exportSchema();
void importSchema();
void updateState();
......
......@@ -74,6 +74,28 @@
<string>Del</string>
</property>
</action>
<action name="actionExportSchema">
<property name="icon">
<iconset theme="document-export"/>
</property>
<property name="text">
<string>&amp;Export Schema</string>
</property>
<property name="toolTip">
<string>Export the schema of this product.</string>
</property>
</action>
<action name="actionImportSchema">
<property name="icon">
<iconset theme="document-import"/>
</property>
<property name="text">
<string>&amp;Import Schema</string>
</property>
<property name="toolTip">
<string>Import a schema for this product.</string>
</property>
</action>
</widget>
<resources/>
<connections/>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment