Commit fe8cb974 authored by SZPYRKA Jean-Luc's avatar SZPYRKA Jean-Luc
Browse files

Merge branch 'feature/import_export_parameters' into 'develop'

add export/export parameters methods in dtkWidgetsMenu

See merge request !78
parents 448d6234 97217594
Pipeline #255256 passed with stage
in 0 seconds
......@@ -243,6 +243,40 @@ dtkWidgetsMenuItem *dtkWidgetsMenu::addParameter(const QString& title, dtkCorePa
return it;
}
void dtkWidgetsMenu::exportParameters(QJsonObject &json, bool recursive) const
{
for (auto o: d->object_hash) {
dtkWidgetsMenuItemParameter *item = dynamic_cast<dtkWidgetsMenuItemParameter*>(o);
if (item){
json.insert(item->parameter()->uid(), QJsonObject::fromVariantHash(item->parameter()->toVariantHash()));
}
}
if (recursive) {
for (auto menu: d->menu_list) {
menu->exportParameters(json, true);
}
}
}
// Import parameter value from json to embedded parameters in menu and submenus
void dtkWidgetsMenu::importParameters(const QJsonObject &json, bool recursive) const
{
for (auto o: d->object_hash) {
dtkWidgetsMenuItemParameter *item = dynamic_cast<dtkWidgetsMenuItemParameter*>(o);
if (item){
if (json.contains(item->parameter()->uid())) {
QVariant v = json[item->parameter()->uid()].toVariant();
item->parameter()->shareValue(v);
}
}
}
if (recursive) {
for (auto menu: d->menu_list) {
menu->importParameters(json, true);
}
}
}
void dtkWidgetsMenu::removeItem(dtkWidgetsMenuItem *item)
{
if (!(item && d->object_hash.contains(item->title())))
......
......@@ -65,6 +65,9 @@ public:
dtkWidgetsMenuItem *addParameter(dtkWidgetsMenuItem *item);
dtkWidgetsMenuItem *addParameter(const QString&, dtkCoreParameter *, const QString& representation, bool persistent = false);
void exportParameters(QJsonObject &json, bool recursive = true) const;
void importParameters(const QJsonObject& json, bool recursive = true) const;
void removeItem(dtkWidgetsMenuItem *item);
void removeMenu(dtkWidgetsMenu *menu);
......
......@@ -71,7 +71,24 @@ void dtkWidgetsParameterMenuBarGeneratorTestCase::testBasic(void)
menu_bar_generator.populate(&bar);
auto menus = bar.menus();
QCOMPARE(menus.count() , 4);
QJsonObject json;
for (auto menu: menus) {
menu->exportParameters(json);
}
QJsonObject json2 = json["framerate"].toObject();
int fps = json2["value"].toInt();
QCOMPARE(fps, 10);
json2["value"] = "42";
json["framerate"] = json2;
for (auto menu: menus) {
menu->importParameters(json);
}
QJsonObject json3;
for (auto menu: menus) {
menu->exportParameters(json3);
}
fps = json3["framerate"].toObject()["value"].toInt();
QCOMPARE(fps, 42);
}
void dtkWidgetsParameterMenuBarGeneratorTestCase::cleanup(void)
......
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