Commit cb91fc7a authored by CABEL Tristan's avatar CABEL Tristan

Merge branch 'feature/parameterReader' into 'develop'

Feature/parameter reader

See merge request !20
parents 3626d16a 25b118a0
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "dtkWidgetsMenuBar.h" #include "dtkWidgetsMenuBar.h"
#include <dtkCore/dtkCoreParameter> #include <dtkCore/dtkCoreParameter>
#include <dtkCore/dtkCoreParameterReader>
#include <dtkFonts/dtkFontAwesome> #include <dtkFonts/dtkFontAwesome>
...@@ -34,11 +35,10 @@ public: ...@@ -34,11 +35,10 @@ public:
void parseParameter(dtkWidgetsMenu *, const QJsonObject&); void parseParameter(dtkWidgetsMenu *, const QJsonObject&);
public: public:
QJsonArray nature; QJsonArray menu;
QJsonObject definition;
public: public:
QHash<QString, dtkCoreParameter *> parameters; dtkCoreParameters parameters;
}; };
// /////////////////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////////////////
...@@ -46,21 +46,10 @@ public: ...@@ -46,21 +46,10 @@ public:
void dtkWidgetsParameterMenuBarGeneratorPrivate::parseParameter(dtkWidgetsMenu *menu, const QJsonObject& parameter_object) void dtkWidgetsParameterMenuBarGeneratorPrivate::parseParameter(dtkWidgetsMenu *menu, const QJsonObject& parameter_object)
{ {
QString widget_string = parameter_object["widget"].toString(); 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(); 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); menu->addParameter(parameter->label(), parameter, widget_string);
parameters[parameter_uid] = parameter;
} }
template<class T> template<class T>
...@@ -97,47 +86,41 @@ void dtkWidgetsParameterMenuBarGeneratorPrivate::parseGroup(T* menu, const QJson ...@@ -97,47 +86,41 @@ void dtkWidgetsParameterMenuBarGeneratorPrivate::parseGroup(T* menu, const QJson
// dtkWidgetsParameterMenuBarGenerator implementation // 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 menu_file(menu_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;
}
QJsonObject nature_object = nature_document.object(); dtkCoreParameterReader param_reader;
if(!nature_object.contains("contents")) { param_reader.setJsonFile(definition_file_path);
qWarning() << Q_FUNC_INFO << "The nature parameters file should contain a first json object with key 'contents'. The file cannot be processed any further."; if (!param_reader.read()) {
return; qWarning() << Q_FUNC_INFO << "The file" << definition_file_path << "could not be read.";
} return;
}
d->parameters = param_reader.parameters();
QJsonParseError definition_error; if (menu_file.open(QFile::ReadOnly)) {
QJsonDocument definition_document = QJsonDocument::fromJson(definition_file.readAll(), &definition_error); QJsonParseError menu_error;
if(definition_error.error != QJsonParseError::NoError) { QJsonDocument menu_document = QJsonDocument::fromJson(menu_file.readAll(), &menu_error);
qWarning() << Q_FUNC_INFO << "Error :" << definition_error.errorString() << "parsing" << definition_file_path << "offset :" << definition_error.offset << "."; if(menu_error.error != QJsonParseError::NoError) {
qWarning() << Q_FUNC_INFO << "Error :" << menu_error.errorString() << "parsing" << menu_file_path << "offset :" << menu_error.offset << ".";
return; return;
} }
QJsonObject definition_object = definition_document.object(); QJsonObject menu_object = menu_document.object();
if(!definition_object.contains("contents")) { if(!menu_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."; 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; return;
} }
QJsonValue nature_contents = nature_object["contents"]; QJsonValue menu_contents = menu_object["contents"];
QJsonValue definition_contents = definition_object["contents"]; if (menu_contents.isArray()) {
if (nature_contents.isArray() && definition_contents.isObject()) { d->menu = menu_contents.toArray();
d->nature = nature_contents.toArray();
d->definition = definition_contents.toObject();
} else { } else {
qWarning() << Q_FUNC_INFO << "The first object with key 'contents' must be an array."; qWarning() << Q_FUNC_INFO << "The first object with key 'contents' must be an array.";
} }
} else { } 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) ...@@ -148,7 +131,7 @@ dtkWidgetsParameterMenuBarGenerator::~dtkWidgetsParameterMenuBarGenerator(void)
void dtkWidgetsParameterMenuBarGenerator::populate(dtkWidgetsMenuBar *menubar) 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()) { if(it->isObject()) {
QJsonObject content_object = it->toObject(); QJsonObject content_object = it->toObject();
if(content_object.contains("group")) { if(content_object.contains("group")) {
...@@ -163,7 +146,7 @@ void dtkWidgetsParameterMenuBarGenerator::populate(dtkWidgetsMenuBar *menubar) ...@@ -163,7 +146,7 @@ void dtkWidgetsParameterMenuBarGenerator::populate(dtkWidgetsMenuBar *menubar)
} }
} }
QHash<QString, dtkCoreParameter *> dtkWidgetsParameterMenuBarGenerator::parameters(void) const dtkCoreParameters dtkWidgetsParameterMenuBarGenerator::parameters(void) const
{ {
return d->parameters; return d->parameters;
} }
......
...@@ -16,11 +16,12 @@ ...@@ -16,11 +16,12 @@
#include <dtkWidgetsExport> #include <dtkWidgetsExport>
#include <dtkCore/dtkCoreParameters>
#include <QtCore> #include <QtCore>
class dtkWidgetsMenuBar; class dtkWidgetsMenuBar;
class dtkCoreParameter; class dtkCoreParameter;
class dtkWidgetsParameter;
// /////////////////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////////////////
// dtkWidgetsParameterMenuBarGenerator declaration // dtkWidgetsParameterMenuBarGenerator declaration
...@@ -29,14 +30,14 @@ class dtkWidgetsParameter; ...@@ -29,14 +30,14 @@ class dtkWidgetsParameter;
class DTKWIDGETS_EXPORT dtkWidgetsParameterMenuBarGenerator class DTKWIDGETS_EXPORT dtkWidgetsParameterMenuBarGenerator
{ {
public: public:
dtkWidgetsParameterMenuBarGenerator(const QString&, const QString&); dtkWidgetsParameterMenuBarGenerator(const QString& menu_file, const QString& definition_file);
~dtkWidgetsParameterMenuBarGenerator(void); ~dtkWidgetsParameterMenuBarGenerator(void);
public: public:
void populate(dtkWidgetsMenuBar *menubar); void populate(dtkWidgetsMenuBar *menubar);
public: public:
QHash<QString, dtkCoreParameter *> parameters(void) const; dtkCoreParameters parameters(void) const;
protected: protected:
class dtkWidgetsParameterMenuBarGeneratorPrivate *d; class dtkWidgetsParameterMenuBarGeneratorPrivate *d;
......
...@@ -58,6 +58,7 @@ add_executable(${PROJECT_NAME} ...@@ -58,6 +58,7 @@ add_executable(${PROJECT_NAME}
target_link_libraries(${PROJECT_NAME} dtkWidgets) target_link_libraries(${PROJECT_NAME} dtkWidgets)
target_link_libraries(${PROJECT_NAME} dtkCore) 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::Widgets)
target_link_libraries(${PROJECT_NAME} Qt5::Test) target_link_libraries(${PROJECT_NAME} Qt5::Test)
......
...@@ -17,8 +17,9 @@ ...@@ -17,8 +17,9 @@
#include <dtkWidgetsTest> #include <dtkWidgetsTest>
#include <dtkWidgets/dtkWidgets> #include <dtkThemes>
#include <dtkWidgets/dtkWidgets>
#include <dtkWidgets/dtkWidgetsParameterMenuBarGenerator> #include <dtkWidgets/dtkWidgetsParameterMenuBarGenerator>
// /////////////////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////////////////
...@@ -36,20 +37,11 @@ class dtkWidgetsParameterMenuBarGeneratorTestCasePrivate ...@@ -36,20 +37,11 @@ class dtkWidgetsParameterMenuBarGeneratorTestCasePrivate
dtkWidgetsParameterMenuBarGeneratorTestCase::dtkWidgetsParameterMenuBarGeneratorTestCase(void) : d(new dtkWidgetsParameterMenuBarGeneratorTestCasePrivate) dtkWidgetsParameterMenuBarGeneratorTestCase::dtkWidgetsParameterMenuBarGeneratorTestCase(void) : d(new dtkWidgetsParameterMenuBarGeneratorTestCasePrivate)
{ {
qRegisterMetaTypeStreamOperators<dtk::d_bool>("dtk::d_bool"); dtk::core::registerParameters();
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::widgets::setVerboseLoading(true); dtk::widgets::setVerboseLoading(true);
dtk::widgets::initialize(""); dtk::widgets::initialize("");
dtkThemesEngine::instance()->apply();
} }
dtkWidgetsParameterMenuBarGeneratorTestCase::~dtkWidgetsParameterMenuBarGeneratorTestCase(void) dtkWidgetsParameterMenuBarGeneratorTestCase::~dtkWidgetsParameterMenuBarGeneratorTestCase(void)
...@@ -69,11 +61,19 @@ void dtkWidgetsParameterMenuBarGeneratorTestCase::init(void) ...@@ -69,11 +61,19 @@ void dtkWidgetsParameterMenuBarGeneratorTestCase::init(void)
void dtkWidgetsParameterMenuBarGeneratorTestCase::testBasic(void) void dtkWidgetsParameterMenuBarGeneratorTestCase::testBasic(void)
{ {
QString nature_file_path = QFINDTESTDATA("../resources/parameters_nature.json"); QString nature_file_path = QFINDTESTDATA("../resources/new_menu.json");
QString definition_file_path = QFINDTESTDATA("../resources/parameters_definition.json"); QString definition_file_path = QFINDTESTDATA("../resources/new_definition.json");
dtkWidgetsParameterMenuBarGenerator menu_bar_generator(nature_file_path, definition_file_path); 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) 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