Commit 897e0e62 authored by Volker Krause's avatar Volker Krause
Browse files

Make basic aggregation changes work in the schema editor

parent e07c56e8
......@@ -19,6 +19,7 @@
#include "ui_aggregationeditwidget.h"
#include "schemaentryitemeditorfactory.h"
#include <core/aggregation.h>
#include <model/aggregationeditormodel.h>
#include <QStyledItemDelegate>
......@@ -35,12 +36,51 @@ AggregationEditWidget::AggregationEditWidget(QWidget* parent) :
ui->aggregationView->setModel(m_model);
qobject_cast<QStyledItemDelegate*>(ui->aggregationView->itemDelegate())->setItemEditorFactory(m_editorFactory);
connect(ui->actionAddAggregation, &QAction::triggered, this, &AggregationEditWidget::addAggregation);
connect(ui->actionDeleteAggregation, &QAction::triggered, this, &AggregationEditWidget::deleteAggregation);
addActions({ ui->actionAddAggregation, ui->actionDeleteAggregation });
}
AggregationEditWidget::~AggregationEditWidget() = default;
Product AggregationEditWidget::product() const
{
return m_model->product();
}
void AggregationEditWidget::setProduct(const Product& product)
{
m_model->setProduct(product);
m_editorFactory->setProduct(product);
}
void AggregationEditWidget::addAggregation()
{
auto p = product();
auto aggrs = p.aggregations();
aggrs += Aggregation();
p.setAggregations(aggrs);
setProduct(p);
}
void AggregationEditWidget::deleteAggregation()
{
const auto rows = ui->aggregationView->selectionModel()->selectedRows();
if (rows.isEmpty())
return;
const auto idx = rows.at(0);
auto p = product();
auto aggrs = p.aggregations();
aggrs.remove(idx.row());
p.setAggregations(aggrs);
setProduct(p);
}
void AggregationEditWidget::updateState()
{
ui->actionAddAggregation->setEnabled(product().isValid());
ui->actionDeleteAggregation->setEnabled(ui->aggregationView->selectionModel()->hasSelection());
}
......@@ -41,9 +41,15 @@ public:
explicit AggregationEditWidget(QWidget *parent = nullptr);
~AggregationEditWidget();
Product product() const;
void setProduct(const Product &product);
private:
void addAggregation();
void deleteAggregation();
void updateState();
std::unique_ptr<Ui::AggregationEditWidget> ui;
AggregationEditorModel *m_model;
SchemaEntryItemEditorFactory *m_editorFactory;
......
......@@ -22,6 +22,28 @@
</widget>
</item>
</layout>
<action name="actionAddAggregation">
<property name="icon">
<iconset theme="list-add"/>
</property>
<property name="text">
<string>Add &amp;Aggregation</string>
</property>
<property name="toolTip">
<string>Add a new aggreagation.</string>
</property>
</action>
<action name="actionDeleteAggregation">
<property name="icon">
<iconset theme="edit-delete"/>
</property>
<property name="text">
<string>&amp;Delete Aggregation</string>
</property>
<property name="toolTip">
<string>Delete the currently selected aggregation.</string>
</property>
</action>
</widget>
<resources/>
<connections/>
......
......@@ -18,13 +18,19 @@
#include "schemaeditor.h"
#include "ui_schemaeditor.h"
#include <core/aggregation.h>
#include <core/product.h>
#include <core/schemaentrytemplates.h>
#include <rest/restapi.h>
#include <QFileDialog>
#include <QMenu>
#include <QMessageBox>
#include <QNetworkReply>
using namespace UserFeedback::Analyzer;
SchemaEditor::SchemaEditor(QWidget* parent) :
QWidget(parent),
ui(new Ui::SchemaEditor)
......@@ -34,13 +40,15 @@ SchemaEditor::SchemaEditor(QWidget* parent) :
connect(ui->schema, &SchemaEditWidget::logMessage, this, &SchemaEditor::logMessage);
connect(ui->schema, &SchemaEditWidget::productChanged, this, &SchemaEditor::productChanged);
auto templateMenu = new QMenu(tr("Schema entry templates"), this);
connect(ui->tabWidget, &QTabWidget::currentChanged, this, &SchemaEditor::updateState);
auto templateMenu = new QMenu(tr("Schema Entry Templates"), this);
for (const auto &t : SchemaEntryTemplates::availableTemplates()) {
auto a = templateMenu->addAction(t.name());
a->setData(QVariant::fromValue(t));
connect(a, &QAction::triggered, this, [this, a]() {
const auto t = a->data().value<Product>();
auto p = ui->schema->product();
auto p = product();
p.addTemplate(t);
setProduct(p);
});
......@@ -49,9 +57,18 @@ SchemaEditor::SchemaEditor(QWidget* parent) :
m_createFromTemplateAction = templateMenu->menuAction();
m_createFromTemplateAction->setIcon(QIcon::fromTheme(QStringLiteral("document-new-from-template")));
addActions({ m_createFromTemplateAction });
ui->actionSave->setShortcut(QKeySequence::Save);
connect(ui->actionSave, &QAction::triggered, this, &SchemaEditor::save);
connect(ui->actionImportSchema, &QAction::triggered, this, &SchemaEditor::importSchema);
connect(ui->actionExportSchema, &QAction::triggered, this, &SchemaEditor::exportSchema);
addActions({ m_createFromTemplateAction, ui->actionSave, ui->actionImportSchema, ui->actionExportSchema });
auto sep = new QAction(parent);
sep->setSeparator(true);
addAction(sep);
addActions(ui->schema->actions());
addActions(ui->aggregation->actions());
updateState();
}
......@@ -59,9 +76,17 @@ SchemaEditor::~SchemaEditor() = default;
void SchemaEditor::setRESTClient(RESTClient* client)
{
m_restClient = client;
ui->schema->setRESTClient(client);
}
Product SchemaEditor::product() const
{
auto p = ui->schema->product();
p.setAggregations(ui->aggregation->product().aggregations());
return p;
}
void SchemaEditor::setProduct(const Product& product)
{
ui->schema->setProduct(product);
......@@ -69,7 +94,68 @@ void SchemaEditor::setProduct(const Product& product)
updateState();
}
void SchemaEditor::save()
{
auto reply = RESTApi::updateProduct(m_restClient, product());
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
if (reply->error() != QNetworkReply::NoError)
return;
emit logMessage(QString::fromUtf8((reply->readAll())));
emit productChanged(product());
});
}
void SchemaEditor::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(product().toJson());
logMessage(tr("Schema of %1 exported to %2.").arg(product().name(), f.fileName()));
}
void SchemaEditor::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(product().name());
setProduct(p);
logMessage(tr("Schema of %1 imported from %2.").arg(product().name(), f.fileName()));
}
void SchemaEditor::updateState()
{
m_createFromTemplateAction->setEnabled(ui->schema->product().isValid());
const auto p = product();
m_createFromTemplateAction->setEnabled(p.isValid());
ui->actionSave->setEnabled(p.isValid());
ui->actionExportSchema->setEnabled(p.isValid());
ui->actionImportSchema->setEnabled(p.isValid());
const auto schemaEditActive = ui->tabWidget->currentWidget() == ui->schema;
const auto aggrEditActive = ui->tabWidget->currentWidget() == ui->aggregation;
for (auto action : ui->schema->actions())
action->setVisible(schemaEditActive);
for (auto action : ui->aggregation->actions())
action->setVisible(aggrEditActive);
}
......@@ -42,6 +42,8 @@ public:
~SchemaEditor();
void setRESTClient(RESTClient *client);
Product product() const;
void setProduct(const Product &product);
signals:
......@@ -49,9 +51,14 @@ signals:
void logMessage(const QString &msg);
private:
void save();
void exportSchema();
void importSchema();
void updateState();
std::unique_ptr<Ui::SchemaEditor> ui;
RESTClient *m_restClient = nullptr;
QAction *m_createFromTemplateAction;
};
......
......@@ -10,9 +10,6 @@
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>0</number>
......@@ -44,6 +41,42 @@
</widget>
</item>
</layout>
<action name="actionSave">
<property name="icon">
<iconset theme="document-save">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>&amp;Save Schema</string>
</property>
<property name="toolTip">
<string>Save changes to the product schema.</string>
</property>
</action>
<action name="actionImportSchema">
<property name="icon">
<iconset theme="document-import">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>&amp;Import Schema</string>
</property>
<property name="toolTip">
<string>Import a schema for this product.</string>
</property>
</action>
<action name="actionExportSchema">
<property name="icon">
<iconset theme="document-export">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>&amp;Export Schema</string>
</property>
<property name="toolTip">
<string>Export the schema of this product.</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
......
......@@ -25,7 +25,6 @@
#include <core/schemaentrytemplates.h>
#include <QDebug>
#include <QFileDialog>
#include <QMenu>
#include <QMessageBox>
#include <QNetworkReply>
......@@ -46,14 +45,11 @@ SchemaEditWidget::SchemaEditWidget(QWidget *parent) :
connect(ui->addEntryButton, &QPushButton::clicked, this, &SchemaEditWidget::addEntry);
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);
addActions({ ui->actionDelete, ui->actionSave, ui->actionImportSchema, ui->actionExportSchema });
addActions({ ui->actionDelete });
updateState();
}
......@@ -96,61 +92,10 @@ void SchemaEditWidget::deleteEntry()
m_schemaModel->deleteRow(idx);
}
void SchemaEditWidget::save()
{
auto reply = RESTApi::updateProduct(m_restClient, m_schemaModel->product());
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
if (reply->error() != QNetworkReply::NoError)
return;
emit logMessage(QString::fromUtf8((reply->readAll())));
emit productChanged(m_schemaModel->product());
});
}
void SchemaEditWidget::updateState()
{
const auto selection = ui->schemaView->selectionModel()->selection();
ui->actionDelete->setEnabled(!selection.isEmpty());
ui->addEntryButton->setEnabled(!ui->newEntryName->text().isEmpty());
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,9 +52,6 @@ signals:
private:
void addEntry();
void deleteEntry();
void save();
void exportSchema();
void importSchema();
void updateState();
......
......@@ -11,18 +11,6 @@
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QTreeView" name="schemaView">
<property name="selectionMode">
......@@ -49,20 +37,10 @@
</layout>
</item>
</layout>
<action name="actionSave">
<property name="icon">
<iconset theme="document-save"/>
</property>
<property name="text">
<string>&amp;Save Schema</string>
</property>
<property name="toolTip">
<string>Save changes to the product schema.</string>
</property>
</action>
<action name="actionDelete">
<property name="icon">
<iconset theme="edit-delete"/>
<iconset theme="edit-delete">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>&amp;Delete</string>
......@@ -74,28 +52,6 @@
<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