Commit cb91fc7a authored by CABEL Tristan's avatar CABEL Tristan

Merge branch 'feature/parameterReader' into 'develop'

Feature/parameter reader

See merge request dtk/dtk-widgets!20
parents 3626d16a 25b118a0
......@@ -20,6 +20,7 @@
#include "dtkWidgetsMenuBar.h"
#include <dtkCore/dtkCoreParameter>
#include <dtkCore/dtkCoreParameterReader>
#include <dtkFonts/dtkFontAwesome>
......@@ -34,11 +35,10 @@ public:
void parseParameter(dtkWidgetsMenu *, const QJsonObject&);
public:
QJsonArray nature;
QJsonObject definition;
QJsonArray menu;
public:
QHash<QString, dtkCoreParameter *> parameters;
dtkCoreParameters parameters;
};
// ///////////////////////////////////////////////////////////////////
......@@ -46,21 +46,10 @@ public:
void dtkWidgetsParameterMenuBarGeneratorPrivate::parseParameter(dtkWidgetsMenu *menu, const QJsonObject& parameter_object)
{
QString widget_string = parameter_object["widget"].toString();
QString type_string = parameter_object["type"].toString();
QVariantHash map;
map.insert("type", type_string);
dtkCoreParameter *parameter = dtkCoreParameter::create(map);
if (!parameter)
return;
QString parameter_uid = parameter_object["uid"].toString();
parameter->setValue(this->definition[parameter_uid].toObject().toVariantHash());
dtkCoreParameter *parameter = this->parameters[parameter_uid];
menu->addParameter(parameter->label(), parameter, widget_string);
parameters[parameter_uid] = parameter;
}
template<class T>
......@@ -97,47 +86,41 @@ void dtkWidgetsParameterMenuBarGeneratorPrivate::parseGroup(T* menu, const QJson
// dtkWidgetsParameterMenuBarGenerator implementation
// ///////////////////////////////////////////////////////////////////
dtkWidgetsParameterMenuBarGenerator::dtkWidgetsParameterMenuBarGenerator(const QString& nature_file_path, const QString& definition_file_path) : d(new dtkWidgetsParameterMenuBarGeneratorPrivate)
dtkWidgetsParameterMenuBarGenerator::dtkWidgetsParameterMenuBarGenerator(const QString& menu_file_path, const QString& definition_file_path) : d(new dtkWidgetsParameterMenuBarGeneratorPrivate)
{
QFile nature_file(nature_file_path);
QFile definition_file(definition_file_path);
if (nature_file.open(QFile::ReadOnly) && definition_file.open(QFile::ReadOnly)) {
QJsonParseError nature_error;
QJsonDocument nature_document = QJsonDocument::fromJson(nature_file.readAll(), &nature_error);
if(nature_error.error != QJsonParseError::NoError) {
qWarning() << Q_FUNC_INFO << "Error :" << nature_error.errorString() << "parsing" << nature_file_path << "offset :" << nature_error.offset << ".";
return;
}
QFile menu_file(menu_file_path);
QJsonObject nature_object = nature_document.object();
if(!nature_object.contains("contents")) {
qWarning() << Q_FUNC_INFO << "The nature parameters file should contain a first json object with key 'contents'. The file cannot be processed any further.";
return;
}
dtkCoreParameterReader param_reader;
param_reader.setJsonFile(definition_file_path);
if (!param_reader.read()) {
qWarning() << Q_FUNC_INFO << "The file" << definition_file_path << "could not be read.";
return;
}
d->parameters = param_reader.parameters();
QJsonParseError definition_error;
QJsonDocument definition_document = QJsonDocument::fromJson(definition_file.readAll(), &definition_error);
if(definition_error.error != QJsonParseError::NoError) {
qWarning() << Q_FUNC_INFO << "Error :" << definition_error.errorString() << "parsing" << definition_file_path << "offset :" << definition_error.offset << ".";
if (menu_file.open(QFile::ReadOnly)) {
QJsonParseError menu_error;
QJsonDocument menu_document = QJsonDocument::fromJson(menu_file.readAll(), &menu_error);
if(menu_error.error != QJsonParseError::NoError) {
qWarning() << Q_FUNC_INFO << "Error :" << menu_error.errorString() << "parsing" << menu_file_path << "offset :" << menu_error.offset << ".";
return;
}
QJsonObject definition_object = definition_document.object();
if(!definition_object.contains("contents")) {
qWarning() << Q_FUNC_INFO << "The nature parameters file should contain a first json object with key 'contents'. The file cannot be processed any further.";
QJsonObject menu_object = menu_document.object();
if(!menu_object.contains("contents")) {
qWarning() << Q_FUNC_INFO << "The menu parameters file should contain a first json object with key 'contents'. The file cannot be processed any further.";
return;
}
QJsonValue nature_contents = nature_object["contents"];
QJsonValue definition_contents = definition_object["contents"];
if (nature_contents.isArray() && definition_contents.isObject()) {
d->nature = nature_contents.toArray();
d->definition = definition_contents.toObject();
QJsonValue menu_contents = menu_object["contents"];
if (menu_contents.isArray()) {
d->menu = menu_contents.toArray();
} else {
qWarning() << Q_FUNC_INFO << "The first object with key 'contents' must be an array.";
}
} else {
qWarning() << Q_FUNC_INFO << "The file" << nature_file_path << "could not be opened.";
qWarning() << Q_FUNC_INFO << "The file" << menu_file_path << "could not be opened.";
}
}
......@@ -148,7 +131,7 @@ dtkWidgetsParameterMenuBarGenerator::~dtkWidgetsParameterMenuBarGenerator(void)
void dtkWidgetsParameterMenuBarGenerator::populate(dtkWidgetsMenuBar *menubar)
{
for(auto it = d->nature.begin(); it != d->nature.end(); ++it) {
for(auto it = d->menu.begin(); it != d->menu.end(); ++it) {
if(it->isObject()) {
QJsonObject content_object = it->toObject();
if(content_object.contains("group")) {
......@@ -163,7 +146,7 @@ void dtkWidgetsParameterMenuBarGenerator::populate(dtkWidgetsMenuBar *menubar)
}
}
QHash<QString, dtkCoreParameter *> dtkWidgetsParameterMenuBarGenerator::parameters(void) const
dtkCoreParameters dtkWidgetsParameterMenuBarGenerator::parameters(void) const
{
return d->parameters;
}
......
......@@ -16,11 +16,12 @@
#include <dtkWidgetsExport>
#include <dtkCore/dtkCoreParameters>
#include <QtCore>
class dtkWidgetsMenuBar;
class dtkCoreParameter;
class dtkWidgetsParameter;
// ///////////////////////////////////////////////////////////////////
// dtkWidgetsParameterMenuBarGenerator declaration
......@@ -29,14 +30,14 @@ class dtkWidgetsParameter;
class DTKWIDGETS_EXPORT dtkWidgetsParameterMenuBarGenerator
{
public:
dtkWidgetsParameterMenuBarGenerator(const QString&, const QString&);
dtkWidgetsParameterMenuBarGenerator(const QString& menu_file, const QString& definition_file);
~dtkWidgetsParameterMenuBarGenerator(void);
public:
void populate(dtkWidgetsMenuBar *menubar);
public:
QHash<QString, dtkCoreParameter *> parameters(void) const;
dtkCoreParameters parameters(void) const;
protected:
class dtkWidgetsParameterMenuBarGeneratorPrivate *d;
......
......@@ -58,6 +58,7 @@ add_executable(${PROJECT_NAME}
target_link_libraries(${PROJECT_NAME} dtkWidgets)
target_link_libraries(${PROJECT_NAME} dtkCore)
target_link_libraries(${PROJECT_NAME} dtkThemes)
target_link_libraries(${PROJECT_NAME} Qt5::Widgets)
target_link_libraries(${PROJECT_NAME} Qt5::Test)
......
......@@ -17,8 +17,9 @@
#include <dtkWidgetsTest>
#include <dtkWidgets/dtkWidgets>
#include <dtkThemes>
#include <dtkWidgets/dtkWidgets>
#include <dtkWidgets/dtkWidgetsParameterMenuBarGenerator>
// ///////////////////////////////////////////////////////////////////
......@@ -36,20 +37,11 @@ class dtkWidgetsParameterMenuBarGeneratorTestCasePrivate
dtkWidgetsParameterMenuBarGeneratorTestCase::dtkWidgetsParameterMenuBarGeneratorTestCase(void) : d(new dtkWidgetsParameterMenuBarGeneratorTestCasePrivate)
{
qRegisterMetaTypeStreamOperators<dtk::d_bool>("dtk::d_bool");
qRegisterMetaTypeStreamOperators<dtk::d_real>("dtk::d_real");
qRegisterMetaTypeStreamOperators<dtk::d_int>("dtk::d_int");
qRegisterMetaTypeStreamOperators<dtk::d_string>("dtk::d_string");
qRegisterMetaType<dtk::d_real *>();
qRegisterMetaType<dtk::d_bool *>();
qRegisterMetaType<dtk::d_int *>();
qRegisterMetaType<dtk::d_string *>();
dtk::core::registerParameters();
dtk::widgets::setVerboseLoading(true);
dtk::widgets::initialize("");
dtkThemesEngine::instance()->apply();
}
dtkWidgetsParameterMenuBarGeneratorTestCase::~dtkWidgetsParameterMenuBarGeneratorTestCase(void)
......@@ -69,11 +61,19 @@ void dtkWidgetsParameterMenuBarGeneratorTestCase::init(void)
void dtkWidgetsParameterMenuBarGeneratorTestCase::testBasic(void)
{
QString nature_file_path = QFINDTESTDATA("../resources/parameters_nature.json");
QString definition_file_path = QFINDTESTDATA("../resources/parameters_definition.json");
QString nature_file_path = QFINDTESTDATA("../resources/new_menu.json");
QString definition_file_path = QFINDTESTDATA("../resources/new_definition.json");
dtkWidgetsParameterMenuBarGenerator menu_bar_generator(nature_file_path, definition_file_path);
dtkCoreParameters params = menu_bar_generator.parameters();
QCOMPARE(params.count() , 78);
dtkWidgetsMenuBar bar;
menu_bar_generator.populate(&bar);
auto menus = bar.menus();
QCOMPARE(menus.count() , 4);
}
void dtkWidgetsParameterMenuBarGeneratorTestCase::cleanup(void)
......
This diff is collapsed.
This diff is collapsed.
{
"contents": [
{"uid" : "hyp", "label": "hyp", "doc": "documentation", "default_value": 2, "bound_min": 0, "bound_max": 5},
{"uid" : "dif", "label": "dif", "doc": "documentation", "default_value": false},
{"uid" : "res", "label": "res", "doc": "documentation", "default_value": false},
{"uid" : "prec", "label": "prec", "doc": "documentation", "default_value": false},
{"uid" : "inf", "label": "inf", "doc": "documentation", "default_value": false},
{"uid" : "wind", "label": "wind", "doc": "documentation", "default_value": false},
]
}
{
"contents": [
{ "group": {"uid" : "Model parameters", "icon": "fa::cogs", "advanced": false,
"contents": [
{"parameter": {"uid": "hyp", "type": "dtk::d_int", "widget": "dtkWidgetsParameterIntSpinBox", "advanced": false}},
{"parameter": {"uid": "dif", "type": "dtk::d_bool", "widget": "dtkWidgetsParameterBoolCheckBox", "advanced": false}},
{"parameter": {"uid": "res", "type": "dtk::d_bool", "widget": "dtkWidgetsParameterBoolCheckBox", "advanced": false}},
{"parameter": {"uid": "prec", "type": "dtk::d_bool", "widget": "dtkWidgetsParameterBoolCheckBox", "advanced": false}},
{"parameter": {"uid": "inf", "type": "dtk::d_bool", "widget": "dtkWidgetsParameterBoolCheckBox", "advanced": false}},
{"parameter": {"uid": "wind", "type": "dtk::d_bool", "widget": "dtkWidgetsParameterBoolCheckBox", "advanced": false}}
]
} }
]
}
Markdown is supported
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