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

Factor out the survey editor into its own view

parent e9dc431b
......@@ -29,12 +29,14 @@ set(analyzer_srcs
ratioset.cpp
ratiosetaggregationmodel.cpp
sample.cpp
surveydialog.cpp
timeaggregationmodel.cpp
schemaeditor/schemaeditwidget.cpp
schemaeditor/schemaentryitemeditorfactory.cpp
schemaeditor/schemaentrytypecombobox.cpp
surveyeditor/surveydialog.cpp
surveyeditor/surveyeditor.cpp
)
add_executable(UserFeedbackAnalyzerApplication ${analyzer_srcs})
......
......@@ -26,11 +26,9 @@
#include "datamodel.h"
#include "numericaggregationmodel.h"
#include "ratiosetaggregationmodel.h"
#include "surveydialog.h"
#include "timeaggregationmodel.h"
#include <model/productmodel.h>
#include <model/surveymodel.h>
#include <rest/restapi.h>
#include <rest/restclient.h>
......@@ -62,21 +60,21 @@ MainWindow::MainWindow() :
m_dataModel(new DataModel(this)),
m_timeAggregationModel(new TimeAggregationModel(this)),
m_aggregatedDataModel(new AggregatedDataModel(this)),
m_surveyModel(new SurveyModel(this)),
m_chart(new Chart(this)),
m_feedbackProvider(new UserFeedback::Provider(this))
{
ui->setupUi(this);
setWindowIcon(QIcon::fromTheme(QStringLiteral("search")));
addView(ui->surveyEditor, ui->menuSurvery);
ui->productListView->setModel(m_productModel);
ui->dataView->setModel(m_dataModel);
ui->surveyView->setModel(m_surveyModel);
ui->aggregatedDataView->setModel(m_aggregatedDataModel);
setWindowIcon(QIcon::fromTheme(QStringLiteral("search")));
connect(m_restClient, &RESTClient::errorMessage, this, &MainWindow::logError);
m_productModel->setRESTClient(m_restClient);
m_dataModel->setRESTClient(m_restClient);
m_surveyModel->setRESTClient(m_restClient);
ui->surveyEditor->setRESTClient(m_restClient);
m_timeAggregationModel->setSourceModel(m_dataModel);
m_chart->setModel(m_timeAggregationModel);
......@@ -100,11 +98,6 @@ MainWindow::MainWindow() :
ui->actionDeleteProduct->setIcon(QIcon::fromTheme(QStringLiteral("edit-delete")));
connect(ui->actionDeleteProduct, &QAction::triggered, this, &MainWindow::deleteProduct);
ui->actionAddSurvey->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
connect(ui->actionAddSurvey, &QAction::triggered, this, &MainWindow::createSurvey);
ui->actionDeleteSurvey->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
connect(ui->actionDeleteSurvey, &QAction::triggered, this, &MainWindow::deleteSurvey);
ui->actionAggregateYear->setData(TimeAggregationModel::AggregateYear);
ui->actionAggregateMonth->setData(TimeAggregationModel::AggregateMonth);
ui->actionAggregateWeek->setData(TimeAggregationModel::AggregateWeek);
......@@ -191,6 +184,15 @@ MainWindow::~MainWindow()
settings.setValue(QStringLiteral("Geometry"), saveGeometry());
}
template <typename T>
void MainWindow::addView(T *view, QMenu *menu)
{
for (auto action : view->actions())
menu->addAction(action);
connect(view, &T::logMessage, this, &MainWindow::logMessage);
}
void MainWindow::connectToServer(const ServerInfo& info)
{
m_restClient->connectToServer(info);
......@@ -260,7 +262,7 @@ void MainWindow::productSelected()
if (!product.isValid())
return;
m_dataModel->setProduct(product);
m_surveyModel->setProduct(product);
ui->surveyEditor->setProduct(product);
ui->schemaEdit->setProduct(product);
m_chart->setModel(nullptr);
......@@ -311,44 +313,6 @@ void MainWindow::productSelected()
}
}
void MainWindow::createSurvey()
{
const auto product = selectedProduct();
if (!product.isValid())
return;
SurveyDialog dlg(this);
if (!dlg.exec())
return;
auto reply = RESTApi::createSurvey(m_restClient, product, dlg.survey());
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
if (reply->error() == QNetworkReply::NoError) {
logMessage(QString::fromUtf8(reply->readAll()));
}
m_surveyModel->reload();
});
}
void MainWindow::deleteSurvey()
{
const auto product = selectedProduct();
if (!product.isValid())
return;
// TODO safety check
const auto selection = ui->surveyView->selectionModel()->selectedRows();
if (selection.isEmpty())
return;
const auto survey = selection.first().data(SurveyModel::SurveyRole).value<Survey>();
if (survey.id() < 0)
return;
auto reply = RESTApi::deleteSurvey(m_restClient, survey);
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
if (reply->error() != QNetworkReply::NoError)
return;
logMessage(QString::fromUtf8(reply->readAll()));
m_surveyModel->reload();
});
}
void MainWindow::logMessage(const QString& msg)
{
ui->logWidget->appendPlainText(msg);
......
......@@ -44,7 +44,6 @@ class Product;
class ProductModel;
class ServerInfo;
class RESTClient;
class SurveyModel;
class TimeAggregationModel;
class MainWindow : public QMainWindow
......@@ -61,14 +60,13 @@ private:
void deleteProduct();
void productSelected();
void createSurvey();
void deleteSurvey();
void logMessage(const QString &msg);
void logError(const QString &msg);
Product selectedProduct() const;
template <typename T> void addView(T *view, QMenu *menu);
std::unique_ptr<Ui::MainWindow> ui;
RESTClient *m_restClient;
ProductModel *m_productModel;
......@@ -76,7 +74,6 @@ private:
TimeAggregationModel *m_timeAggregationModel;
QVector<QAbstractItemModel*> m_aggregationModels;
AggregatedDataModel *m_aggregatedDataModel;
SurveyModel *m_surveyModel;
Chart *m_chart;
UserFeedback::Provider *m_feedbackProvider;
......
......@@ -89,14 +89,7 @@
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<widget class="QTreeView" name="surveyView">
<property name="rootIsDecorated">
<bool>false</bool>
</property>
<property name="uniformRowHeights">
<bool>true</bool>
</property>
</widget>
<widget class="UserFeedback::Analyzer::SurveyEditor" name="surveyEditor" native="true"/>
</item>
</layout>
</widget>
......@@ -141,14 +134,12 @@
<addaction name="actionAddProduct"/>
<addaction name="actionDeleteProduct"/>
</widget>
<widget class="QMenu" name="menu_Survery">
<widget class="QMenu" name="menuSurvery">
<property name="title">
<string>&amp;Survery</string>
</property>
<addaction name="actionAddSurvey"/>
<addaction name="actionDeleteSurvey"/>
</widget>
<widget class="QMenu" name="menu_Analytics">
<widget class="QMenu" name="menuAnalytics">
<property name="title">
<string>Anal&amp;ytics</string>
</property>
......@@ -172,8 +163,8 @@
</widget>
<addaction name="menu_File"/>
<addaction name="menu_Products"/>
<addaction name="menu_Survery"/>
<addaction name="menu_Analytics"/>
<addaction name="menuSurvery"/>
<addaction name="menuAnalytics"/>
<addaction name="menu_Help"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
......@@ -256,22 +247,6 @@
<string>Delete product and all associated data.</string>
</property>
</action>
<action name="actionAddSurvey">
<property name="text">
<string>&amp;Add Survey...</string>
</property>
<property name="toolTip">
<string>Add new survery.</string>
</property>
</action>
<action name="actionDeleteSurvey">
<property name="text">
<string>&amp;Delete Survey</string>
</property>
<property name="toolTip">
<string>Delete a survey.</string>
</property>
</action>
<action name="actionAggregateYear">
<property name="checkable">
<bool>true</bool>
......@@ -354,6 +329,12 @@
<header>schemaeditor/schemaeditwidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>UserFeedback::Analyzer::SurveyEditor</class>
<extends>QWidget</extends>
<header location="global">surveyeditor/surveyeditor.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
......
/*
Copyright (C) 2016 Volker Krause <vkrause@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "surveyeditor.h"
#include "ui_surveyeditor.h"
#include "surveydialog.h"
#include <model/surveymodel.h>
#include <rest/restapi.h>
#include <QNetworkReply>
using namespace UserFeedback::Analyzer;
SurveyEditor::SurveyEditor(QWidget* parent) :
QWidget(parent),
ui(new Ui::SurveyEditor),
m_surveyModel(new SurveyModel(this))
{
ui->setupUi(this);
ui->surveyView->setModel(m_surveyModel);
ui->actionAddSurvey->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
connect(ui->actionAddSurvey, &QAction::triggered, this, &SurveyEditor::createSurvey);
ui->actionDeleteSurvey->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
connect(ui->actionDeleteSurvey, &QAction::triggered, this, &SurveyEditor::deleteSurvey);
addActions({ ui->actionAddSurvey, ui->actionDeleteSurvey });
}
SurveyEditor::~SurveyEditor() = default;
void SurveyEditor::setRESTClient(RESTClient* client)
{
m_restClient = client;
m_surveyModel->setRESTClient(client);
}
void SurveyEditor::setProduct(const Product& product)
{
m_product = product;
m_surveyModel->setProduct(product);
}
void SurveyEditor::createSurvey()
{
if (!m_product.isValid())
return;
SurveyDialog dlg(this);
if (!dlg.exec())
return;
auto reply = RESTApi::createSurvey(m_restClient, m_product, dlg.survey());
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
if (reply->error() == QNetworkReply::NoError) {
emit logMessage(QString::fromUtf8(reply->readAll()));
}
m_surveyModel->reload();
});
}
void SurveyEditor::deleteSurvey()
{
if (!m_product.isValid())
return;
// TODO safety check
const auto selection = ui->surveyView->selectionModel()->selectedRows();
if (selection.isEmpty())
return;
const auto survey = selection.first().data(SurveyModel::SurveyRole).value<Survey>();
if (survey.id() < 0)
return;
auto reply = RESTApi::deleteSurvey(m_restClient, survey);
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
if (reply->error() != QNetworkReply::NoError)
return;
emit logMessage(QString::fromUtf8(reply->readAll()));
m_surveyModel->reload();
});
}
/*
Copyright (C) 2016 Volker Krause <vkrause@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef USERFEEDBACK_ANALYZER_SURVEYEDITOR_H
#define USERFEEDBACK_ANALYZER_SURVEYEDITOR_H
#include <core/product.h>
#include <QWidget>
#include <memory>
namespace UserFeedback {
namespace Analyzer {
class RESTClient;
class SurveyModel;
namespace Ui
{
class SurveyEditor;
}
class SurveyEditor : public QWidget
{
Q_OBJECT
public:
explicit SurveyEditor(QWidget *parent = nullptr);
~SurveyEditor();
void setRESTClient(RESTClient *client);
void setProduct(const Product &product);
signals:
void logMessage(const QString &msg);
private:
void createSurvey();
void deleteSurvey();
private:
std::unique_ptr<Ui::SurveyEditor> ui;
SurveyModel *m_surveyModel;
RESTClient *m_restClient = nullptr;
Product m_product;
};
}
}
#endif // USERFEEDBACK_ANALYZER_SURVEYEDITOR_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>UserFeedback::Analyzer::SurveyEditor</class>
<widget class="QWidget" name="UserFeedback::Analyzer::SurveyEditor">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</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="QTreeView" name="surveyView">
<property name="rootIsDecorated">
<bool>false</bool>
</property>
<property name="uniformRowHeights">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
<action name="actionAddSurvey">
<property name="text">
<string>&amp;Add Survey...</string>
</property>
<property name="toolTip">
<string>Add new survery.</string>
</property>
</action>
<action name="actionDeleteSurvey">
<property name="text">
<string>&amp;Delete Survey</string>
</property>
<property name="toolTip">
<string>Delete a survey.</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
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