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

Add schema entry templates

parent 3d03a9ab
......@@ -8,6 +8,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/ ${CMAKE_MODULE_PATH})
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
if(NOT DEFINED CMAKE_INSTALL_RPATH_USE_LINK_PATH)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
......
......@@ -35,8 +35,11 @@ set(analyzer_srcs
schemaeditor/schemaeditwidget.cpp
schemaeditor/schemaentryitemeditorfactory.cpp
schemaeditor/schemaentrytemplates.cpp
schemaeditor/schemaentrytypecombobox.cpp
schemaentrytemplates/schemaentrytemplates.qrc
surveyeditor/surveydialog.cpp
surveyeditor/surveyeditor.cpp
)
......
......@@ -46,10 +46,15 @@ void SchemaModel::setProduct(const Product &product)
void SchemaModel::addEntry(const QString &name)
{
auto schema = m_product.schema();
beginInsertRows(QModelIndex(), schema.size(), schema.size());
SchemaEntry entry;
entry.setName(name);
addEntry(entry);
}
void SchemaModel::addEntry(const SchemaEntry &entry)
{
auto schema = m_product.schema();
beginInsertRows(QModelIndex(), schema.size(), schema.size());
schema.push_back(entry);
m_product.setSchema(schema);
endInsertRows();
......
......@@ -38,6 +38,7 @@ public:
void setProduct(const Product &product);
void addEntry(const QString &name);
void addEntry(const SchemaEntry &entry);
void deleteEntry(int row);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
......
......@@ -18,12 +18,14 @@
#include "schemaeditwidget.h"
#include "ui_schemaeditwidget.h"
#include "schemaentryitemeditorfactory.h"
#include "schemaentrytemplates.h"
#include <model/schemamodel.h>
#include <rest/restapi.h>
#include <core/product.h>
#include <QDebug>
#include <QMenu>
#include <QMessageBox>
#include <QNetworkReply>
#include <QStyledItemDelegate>
......@@ -47,9 +49,20 @@ SchemaEditWidget::SchemaEditWidget(QWidget *parent) :
connect(ui->schemaView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &SchemaEditWidget::updateState);
connect(ui->newEntryName, &QLineEdit::textChanged, this, &SchemaEditWidget::updateState);
updateState();
addActions({ ui->actionSave });
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]() {
m_schemaModel->addEntry(a->data().value<SchemaEntry>());
});
}
m_createFromTemplateAction = templateMenu->menuAction();
m_createFromTemplateAction->setIcon(QIcon::fromTheme(QStringLiteral("document-new-from-template")));
addActions({ m_createFromTemplateAction, ui->actionSave });
updateState();
}
SchemaEditWidget::~SchemaEditWidget() = default;
......@@ -104,5 +117,6 @@ void SchemaEditWidget::updateState()
ui->addEntryButton->setEnabled(!ui->newEntryName->text().isEmpty());
m_createFromTemplateAction->setEnabled(m_schemaModel->product().isValid());
ui->actionSave->setEnabled(m_schemaModel->product().isValid());
}
......@@ -58,6 +58,7 @@ private:
std::unique_ptr<Ui::SchemaEditWidget> ui;
RESTClient *m_restClient;
SchemaModel *m_schemaModel;
QAction *m_createFromTemplateAction;
};
}
......
/*
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 "schemaentrytemplates.h"
#include <core/schemaentry.h>
#include <QDebug>
#include <QDirIterator>
#include <QJsonArray>
#include <QJsonDocument>
#include <QStandardPaths>
#include <QVector>
#include <algorithm>
using namespace UserFeedback::Analyzer;
QVector<SchemaEntry> SchemaEntryTemplates::availableTemplates()
{
QVector<SchemaEntry> templates;
auto dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("org.kde.user-feedback/schemaentrytemplates"), QStandardPaths::LocateDirectory);
dirs += QStringLiteral(":/org.kde.user-feedback/schemaentrytemplates");
foreach (const auto &dir, dirs) {
QDirIterator it(dir, {QStringLiteral("*.json")}, QDir::Files | QDir::Readable);
while (it.hasNext()) {
const auto fileName = it.next();
qDebug() << fileName;
QFile f(fileName);
if (!f.open(QFile::ReadOnly))
continue;
const auto doc = QJsonDocument::fromJson(f.readAll());
templates += SchemaEntry::fromJson(doc.array());
}
}
std::sort(templates.begin(), templates.end(), [](const SchemaEntry &lhs, const SchemaEntry &rhs) {
return lhs.name() < rhs.name();
});
return templates;
}
/*
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_SCHEMAENTRYTEMPLATES_H
#define USERFEEDBACK_ANALYZER_SCHEMAENTRYTEMPLATES_H
template <typename T> class QVector;
namespace UserFeedback {
namespace Analyzer {
class SchemaEntry;
namespace SchemaEntryTemplates
{
QVector<SchemaEntry> availableTemplates();
}
}
}
#endif // USERFEEDBACK_ANALYZER_SCHEMAENTRYTEMPLATES_H
[
{
"name": "platform",
"type": "scalar",
"aggregation": "category",
"elements": [
{
"name": "platform.os",
"type": "string"
},
{
"name": "platform.version",
"type": "string"
}
]
}
]
[
{
"name": "productVersion",
"type": "scalar",
"aggregation": "category",
"elements": [
{
"name": "productVersion",
"type": "string"
}
]
}
]
[
{
"name": "qtVersion",
"type": "scalar",
"aggregation": "category",
"elements": [
{
"name": "qtVersion",
"type": "string"
}
]
}
]
<!DOCTYPE RCC>
<RCC version="1.0">
<qresource prefix="/org.kde.user-feedback/schemaentrytemplates">
<file>platform.json</file>
<file>productVersion.json</file>
<file>qtVersion.json</file>
<file>screens.json</file>
<file>usageCount.json</file>
<file>usageTime.json</file>
</qresource>
</RCC>
[
{
"name": "screens",
"type": "list",
"aggregation": "numeric",
"elements": [
{
"name": "screen.width",
"type": "int"
},
{
"name": "screen.height",
"type": "int"
}
]
}
]
[
{
"name": "startCount",
"type": "scalar",
"aggregation": "numeric",
"elements": [
{
"name": "startCount",
"type": "int"
}
]
}
]
[
{
"name": "usageTime",
"type": "scalar",
"aggregation": "numeric",
"elements": [
{
"name": "usageTime",
"type": "int"
}
]
}
]
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