Commit 1f7f5f91 authored by Volker Krause's avatar Volker Krause
Browse files

Move schema editor to its own file, and implement entry deletion.

parent 6591ce66
......@@ -12,6 +12,7 @@ set(analyzer_srcs
restclient.cpp
sample.cpp
schemamodel.cpp
schemaeditwidget.cpp
schemaentryitemeditorfactory.cpp
schemaentrytypecombobox.cpp
serverinfo.cpp
......
......@@ -26,8 +26,6 @@
#include "datamodel.h"
#include "productmodel.h"
#include "restclient.h"
#include "schemamodel.h"
#include "schemaentryitemeditorfactory.h"
#include "serverinfo.h"
#include "surveydialog.h"
#include "surveymodel.h"
......@@ -46,7 +44,6 @@
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QSettings>
#include <QStyledItemDelegate>
#include <QTimer>
#include <QUrl>
......@@ -62,7 +59,6 @@ MainWindow::MainWindow() :
m_aggregatedDataModel(new AggregatedDataModel(this)),
m_surveyModel(new SurveyModel(this)),
m_chart(new Chart(this)),
m_schemaModel(new SchemaModel(this)),
m_feedbackProvider(new UserFeedback::Provider(this))
{
ui->setupUi(this);
......@@ -70,8 +66,6 @@ MainWindow::MainWindow() :
ui->dataView->setModel(m_dataModel);
ui->surveyView->setModel(m_surveyModel);
ui->aggregatedDataView->setModel(m_aggregatedDataModel);
ui->schemaView->setModel(m_schemaModel);
qobject_cast<QStyledItemDelegate*>(ui->schemaView->itemDelegate())->setItemEditorFactory(new SchemaEntryItemEditorFactory);
setWindowIcon(QIcon::fromTheme(QStringLiteral("search")));
connect(m_restClient, &RESTClient::errorMessage, this, &MainWindow::logError);
......@@ -139,12 +133,7 @@ MainWindow::MainWindow() :
m_chart->setModel(model);
});
connect(ui->addSchemaEntryButton, &QPushButton::clicked, this, [this]() {
m_schemaModel->addEntry(ui->newSchemaEntryName->text());
ui->newSchemaEntryName->clear();
});
connect(ui->saveSchemaButton, &QPushButton::clicked, this, [this]() {
const auto p = m_schemaModel->product();
connect(ui->schemaEdit, &SchemaEditWidget::productChanged, this, [this](const Product &p) {
auto reply = m_restClient->put(QStringLiteral("products/") + p.name(), p.toJson());
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
if (reply->error() != QNetworkReply::NoError)
......@@ -176,7 +165,7 @@ MainWindow::MainWindow() :
return;
m_dataModel->setProductId(product.name());
m_surveyModel->setProductId(product.name());
m_schemaModel->setProduct(product);
ui->schemaEdit->setProduct(product);
});
settings.beginGroup(QStringLiteral("MainWindow"));
......
......@@ -43,7 +43,6 @@ class Product;
class ProductModel;
class ServerInfo;
class RESTClient;
class SchemaModel;
class SurveyModel;
class TimeAggregationModel;
......@@ -77,7 +76,6 @@ private:
AggregatedDataModel *m_aggregatedDataModel;
SurveyModel *m_surveyModel;
Chart *m_chart;
SchemaModel *m_schemaModel;
UserFeedback::Provider *m_feedbackProvider;
};
......
......@@ -106,36 +106,7 @@
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_8">
<item>
<widget class="QTableView" name="schemaView">
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="newSchemaEntryName">
<property name="placeholderText">
<string>New entry</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="addSchemaEntryButton">
<property name="text">
<string>&amp;Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="saveSchemaButton">
<property name="text">
<string>&amp;Save</string>
</property>
</widget>
</item>
</layout>
<widget class="UserFeedback::Analyzer::SchemaEditWidget" name="schemaEdit" native="true"/>
</item>
</layout>
</widget>
......@@ -373,6 +344,12 @@
<header>provider/widgets/notificationwidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>UserFeedback::Analyzer::SchemaEditWidget</class>
<extends>QWidget</extends>
<header>schemaeditwidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
......
#include "schemaeditwidget.h"
#include "ui_schemaeditwidget.h"
#include "product.h"
#include "schemaentryitemeditorfactory.h"
#include "schemamodel.h"
#include <QDebug>
#include <QMessageBox>
#include <QStyledItemDelegate>
using namespace UserFeedback::Analyzer;
SchemaEditWidget::SchemaEditWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::SchemaEditWidget),
m_schemaModel(new SchemaModel(this))
{
ui->setupUi(this);
ui->schemaView->setModel(m_schemaModel);
qobject_cast<QStyledItemDelegate*>(ui->schemaView->itemDelegate())->setItemEditorFactory(new SchemaEntryItemEditorFactory);
connect(ui->addEntryButton, &QPushButton::clicked, this, &SchemaEditWidget::addEntry);
connect(ui->newEntryName, &QLineEdit::returnPressed, this, &SchemaEditWidget::addEntry);
connect(ui->deleteEntryButton, &QPushButton::clicked, this, &SchemaEditWidget::deleteEntry);
connect(ui->saveButton, &QPushButton::clicked, this, [this]() {
emit productChanged(m_schemaModel->product());
});
connect(ui->schemaView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &SchemaEditWidget::updateState);
connect(ui->newEntryName, &QLineEdit::textChanged, this, &SchemaEditWidget::updateState);
updateState();
}
SchemaEditWidget::~SchemaEditWidget() = default;
void SchemaEditWidget::setProduct(const Product& product)
{
m_schemaModel->setProduct(product);
updateState();
}
void SchemaEditWidget::addEntry()
{
m_schemaModel->addEntry(ui->newEntryName->text());
ui->newEntryName->clear();
}
void SchemaEditWidget::deleteEntry()
{
const auto selection = ui->schemaView->selectionModel()->selection();
if (selection.isEmpty())
return;
const auto r = QMessageBox::critical(this, tr("Delete Schema Entry"), tr("Do you really want to delete this entry, and all recorded data for it?"),
QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Cancel);
if (r != QMessageBox::Discard)
return;
const auto idx = selection.first().topLeft().row();
m_schemaModel->deleteEntry(idx);
}
void SchemaEditWidget::updateState()
{
const auto selection = ui->schemaView->selectionModel()->selection();
ui->deleteEntryButton->setEnabled(!selection.isEmpty());
ui->addEntryButton->setEnabled(!ui->newEntryName->text().isEmpty());
ui->saveButton->setEnabled(m_schemaModel->product().isValid());
}
\ No newline at end of file
#ifndef USERFEEDBACK_ANALYZER_SCHEMAEDITWIDGET_H
#define USERFEEDBACK_ANALYZER_SCHEMAEDITWIDGET_H
#include <QWidget>
#include <memory>
namespace UserFeedback {
namespace Analyzer {
namespace Ui
{
class SchemaEditWidget;
}
class Product;
class SchemaModel;
class SchemaEditWidget : public QWidget
{
Q_OBJECT
public:
explicit SchemaEditWidget(QWidget *parent = nullptr);
~SchemaEditWidget();
void setProduct(const Product &product);
signals:
void productChanged(const Product &product);
private:
void addEntry();
void deleteEntry();
void updateState();
std::unique_ptr<Ui::SchemaEditWidget> ui;
SchemaModel *m_schemaModel;
};
}
}
#endif // USERFEEDBACK_ANALYZER_SCHEMAEDITWIDGET_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>UserFeedback::Analyzer::SchemaEditWidget</class>
<widget class="QWidget" name="UserFeedback::Analyzer::SchemaEditWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>528</width>
<height>344</height>
</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="QTableView" name="schemaView">
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="newEntryName">
<property name="placeholderText">
<string>New entry</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="addEntryButton">
<property name="text">
<string>&amp;Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="deleteEntryButton">
<property name="text">
<string>Delete</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="saveButton">
<property name="text">
<string>&amp;Save</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
......@@ -49,6 +49,15 @@ void SchemaModel::addEntry(const QString &name)
endInsertRows();
}
void SchemaModel::deleteEntry(int row)
{
auto schema = m_product.schema();
beginRemoveRows(QModelIndex(), row, row);
schema.removeAt(row);
m_product.setSchema(schema);
endRemoveRows();
}
int SchemaModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
......
......@@ -38,6 +38,7 @@ public:
void setProduct(const Product &product);
void addEntry(const QString &name);
void deleteEntry(int row);
int rowCount(const QModelIndex &parent) const override;
int columnCount(const QModelIndex &parent) const override;
......
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