diff --git a/src/Parameter/ParameterWidget.cc b/src/Parameter/ParameterWidget.cc index deff0ac8..88714d66 100644 --- a/src/Parameter/ParameterWidget.cc +++ b/src/Parameter/ParameterWidget.cc @@ -81,6 +81,7 @@ void ParameterWidget::setFile(QString jsonFile){ getParameterSet(this->jsonFile); connect(addButton,SIGNAL(clicked()),this,SLOT(onSetAdd())); connect(deleteButton,SIGNAL(clicked()),this,SLOT(onSetDelete())); + this->comboBox->clear(); setComboBoxForSet(); } @@ -271,23 +272,26 @@ void ParameterWidget::applyParameterSet(string setName){ for(pt::ptree::value_type &v : root.get_child(path)){ entry_map_t::iterator entry =entries.find(v.first); if(entry!=entries.end()){ - if(entry->second->dvt == Value::NUMBER){ - entry->second->value=ValuePtr(v.second.get_value()); - }else if(entry->second->dvt== Value::BOOL){ - entry->second->value=ValuePtr(v.second.get_value()); - }else if(entry->second->dvt== Value::VECTOR){ - ModuleContext ctx; + if(entry->second->dvt== Value::STRING){ + entry->second->value=ValuePtr(v.second.data()); + }else{ + AssignmentList *assignmentList; assignmentList=parser(v.second.data().c_str()); + if(assignmentList==NULL){ + return ; + } + + ModuleContext ctx; Assignment *assignment; for(int i=0; isize(); i++) { - assignment=assignmentList[i].data(); + ValuePtr newValue=assignmentList[i].data()->expr.get()->evaluate(&ctx); + if(entry->second->dvt==newValue->type()){ + entry->second->value=newValue; + } } - entry->second->value=assignment->expr.get()->evaluate(&ctx); - }else if(entry->second->dvt== Value::STRING){ - entry->second->value=ValuePtr(v.second.data()); } - } + } } } diff --git a/src/Parameter/parameterset.cpp b/src/Parameter/parameterset.cpp index 9f4d1a0d..746b5c67 100644 --- a/src/Parameter/parameterset.cpp +++ b/src/Parameter/parameterset.cpp @@ -6,11 +6,12 @@ ParameterSet::ParameterSet() } ParameterSet::~ParameterSet() { - myfile.close(); + } void ParameterSet::getParameterSet(string filename){ + fstream myfile; myfile.open (filename); // send your JSON above to the parser below, but populate ss first if(myfile.is_open()){ @@ -22,6 +23,27 @@ void ParameterSet::getParameterSet(string filename){ std::cerr << e.what() << std::endl; } } + myfile.close(); +} + +void ParameterSet::writeParameterSet(string filename){ + if(root.empty()){ + return; + } + + fstream myfile; + myfile.open(filename,ios::out); + // send your JSON above to the parser below, but populate ss first + if(myfile.is_open()){ + try{ + pt::write_json(myfile, this->root); + } + catch (std::exception const& e){ + + std::cerr << e.what() << std::endl; + } + } + myfile.close(); } void ParameterSet::applyParameterSet(FileModule *fileModule,string setName) @@ -39,21 +61,23 @@ void ParameterSet::applyParameterSet(FileModule *fileModule,string setName) Assignment *assignment; assignment=&(*it); const ValuePtr defaultValue = assignment->expr.get()->evaluate(&ctx); - if(defaultValue->type()== Value::NUMBER){ - assignment->expr = shared_ptr(new Literal(ValuePtr(v.second.get_value()))) ; - }else if(defaultValue->type()== Value::BOOL){ - assignment->expr = shared_ptr(new Literal(ValuePtr(v.second.get_value()))); - }else if(defaultValue->type()== Value::VECTOR){ + if(defaultValue->type()== Value::STRING){ + assignment->expr = shared_ptr(new Literal(ValuePtr(v.second.data()))); + }else{ AssignmentList *assignmentList; assignmentList=parser(v.second.data().c_str()); - for(int i=0; isize(); i++) { - assignment=assignmentList[i].data(); + if(assignmentList==NULL){ + return ; + } + ModuleContext ctx; + for(int i=0; isize(); i++) { + if(defaultValue->type()== assignmentList[i].data()->expr.get()->evaluate(&ctx)->type()){ + assignment=assignmentList[i].data(); + } } - }else if(defaultValue->type()== Value::STRING){ - assignment->expr = shared_ptr(new Literal(ValuePtr(v.second.data()))); } - } + } } } } diff --git a/src/Parameter/parameterset.h b/src/Parameter/parameterset.h index 3702c337..157c9646 100644 --- a/src/Parameter/parameterset.h +++ b/src/Parameter/parameterset.h @@ -20,7 +20,6 @@ class ParameterSet protected: pt::ptree root; - fstream myfile; typedef map Parameterset; Parameterset parameterSet; @@ -28,6 +27,7 @@ public: ParameterSet(); ~ParameterSet(); void getParameterSet(string filename); + void writeParameterSet(string filename); void applyParameterSet(FileModule *fileModule,string setName); }; diff --git a/src/Parameter/parametertext.cpp b/src/Parameter/parametertext.cpp index ea031a6e..d4fed030 100644 --- a/src/Parameter/parametertext.cpp +++ b/src/Parameter/parametertext.cpp @@ -26,10 +26,15 @@ void ParameterText::on_Changed() AssignmentList *assignmentList; assignmentList=parser(lineEdit->text().toStdString().c_str()); Assignment *assignment; - for(int i=0; isize(); i++) { - assignment=assignmentList[i].data(); + if(assignmentList==NULL){ + return ; + } + for(int i=0; isize(); i++) { + ValuePtr newValue=assignmentList[i].data()->expr.get()->evaluate(&ctx); + if(object->dvt==newValue->type()){ + object->value=newValue; + } } - object->value=assignment->expr.get()->evaluate(&ctx); } emit changed(); } diff --git a/src/mainwin.cc b/src/mainwin.cc index 905d2664..072ff5c7 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -176,7 +176,7 @@ MainWindow::MainWindow(const QString &filename) { setupUi(this); - this->parameterWidget->setFile(filename); + editorDockTitleWidget = new QWidget(); consoleDockTitleWidget = new QWidget(); parameterDockTitleWidget = new QWidget(); @@ -783,7 +783,7 @@ void MainWindow::setFileName(const QString &filename) QFileInfo fileinfo(filename); this->fileName = fileinfo.absoluteFilePath(); setWindowFilePath(this->fileName); - + this->parameterWidget->setFile(this->fileName); QDir::setCurrent(fileinfo.dir().absolutePath()); this->top_ctx.setDocumentPath(fileinfo.dir().absolutePath().toLocal8Bit().constData()); } @@ -1433,6 +1433,8 @@ void MainWindow::actionSaveAs() } } } + QString jsonfile=new_filename; + this->parameterWidget->writeParameterSet(jsonfile.replace(".scad",".json").toStdString()); setFileName(new_filename); actionSave(); } diff --git a/testdata/scad/customizer/group.scad b/testdata/scad/customizer/group.scad new file mode 100644 index 00000000..521679b6 --- /dev/null +++ b/testdata/scad/customizer/group.scad @@ -0,0 +1,42 @@ +// combo box for nunber +Numbers=2; // [0, 1, 2, 3] + +// combo box for string +Strings="foo"; // [foo, bar, baz] + +//labeled combo box for numbers +Labeled_values=10; // [10:L, 20:M, 30:L] + +//labeled combo box for string +Labeled_value="S"; // [S:Small, M:Medium, L:Large] + +/*[ Global ]*/ +// slider widget for number +slider =34; // [10:100] + +//step slider for number +stepSlider=2; //[0:5:100] + +/* [Hidden] */ + +//description +Variable = true; //comment + +/*[Global] */ + +// spinbox with step size 23 +Spinbox = 5; //23 + +/* [Textbox] */ + +//Text box for vector with more than 4 elements +Vector=[12,34,44,43,23,23];//comment + +// Text box for string +String="hello"; //comment + +/* [Special vector] */ +//Text box for vector with less than or equal to 4 elements +Vector2=[12,34,45,23]; //any thing + +echo(String); diff --git a/testdata/scad/customizer/nativeParameter.scad b/testdata/scad/customizer/nativeParameter.scad new file mode 100644 index 00000000..744a1807 --- /dev/null +++ b/testdata/scad/customizer/nativeParameter.scad @@ -0,0 +1,58 @@ +@Parameter(12) +x1 = 1; +@Description("normal description") +@Parameter(12) +x2 = 1; +@Description("normal1 description") +@Description("normal2 description") +@Parameter(12) +x3 = 1; +@Parameter("ADAD") +@Parameter([1 : 12]) +@Parameter(12) +x4 = 1; +@Group("Global") +@Description("normal description") +@Parameter([1 : 12]) +x5 = 1; +@Description("normal description") +@Group("Global") +@Parameter([1 : 2 : 12]) +x6 = 1; +@Description("normal description") +x7 = 1; +@Group("Global") +x8 = 1; +x9 = 1; +x10 = 1; +x11 = 1; +@Group("Global") +@Parameter([12]) +x12 = 1; +@Parameter([[10, "Small"], [20, "Medium"], [30, "Large"]]) +x13 = 10; +@Parameter([[10, 100], [20, 101], [30, 102]]) +x14 = 10; +@Parameter("parameter") +x15 = 10; +@Parameter([0, 1, 2, 3]) +x16 = 10; +@Parameter("parameter") +x17 = "text"; +@Parameter(["foo", "bar", "baz"]) +x18 = "text"; +@Parameter([[0, "text"], [1, "foo"], [2, "bar"], [3, "hello"]]) +x19 = "text"; +@Parameter([["foo", 10], ["bar", 10], ["baz", 30]]) +x20 = "text"; +@Parameter([["foo", "yes"], ["bar", "no"], ["baz", "mgiht"]]) +x21 = "text"; +@Parameter([23, 4]) +x22 = [12, 34]; +@Parameter([23, 4, 23, 4, 45]) +x23 = [12, 34]; +@Parameter([23, 4, 2, 3, 4, 6]) +x24 = [12, 34, 2, 3, 41, 23]; +@Parameter("end parameter") +x27 = 12; + diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 063c315d..dae85763 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1531,7 +1531,8 @@ add_cmdline_test(moduledumptest EXE ${OPENSCAD_BINPATH} ARGS -o SUFFIX ast FILES ${CMAKE_SOURCE_DIR}/../testdata/scad/customizer/allmodulescomment.scad ${CMAKE_SOURCE_DIR}/../testdata/scad/customizer/allfunctionscomment.scad ${CMAKE_SOURCE_DIR}/../testdata/scad/customizer/allexpressionscomment.scad - + ${CMAKE_SOURCE_DIR}/../testdata/scad/customizer/nativeParameter.scad + ${CMAKE_SOURCE_DIR}/../testdata/scad/customizer/group.scad ) add_cmdline_test(csgtexttest SUFFIX txt FILES ${CMAKE_SOURCE_DIR}/../testdata/scad/misc/allexpressions.scad diff --git a/tests/regression/moduledumptest/group-expected.ast b/tests/regression/moduledumptest/group-expected.ast new file mode 100644 index 00000000..569e9b88 --- /dev/null +++ b/tests/regression/moduledumptest/group-expected.ast @@ -0,0 +1,42 @@ +@Description("combo box for nunber") +@Parameter([0, 1, 2, 3]) +Numbers = 2; +@Description("combo box for string") +@Parameter(["foo", "bar", "baz"]) +Strings = "foo"; +@Description("labeled combo box for numbers") +@Parameter([[10, "L"], [20, "M"], [30, "L"]]) +Labeled_values = 10; +@Description("labeled combo box for string") +@Parameter([["S", "Small"], ["M", "Medium"], ["L", "Large"]]) +Labeled_value = "S"; +@Group(" Global ") +@Description("slider widget for number") +@Parameter([10 : 100]) +slider = 34; +@Group(" Global ") +@Description("step slider for number") +@Parameter([0 : 5 : 100]) +stepSlider = 2; +@Group("Hidden") +@Description("description") +@Parameter("comment") +Variable = true; +@Group("Global") +@Description("spinbox with step size 23") +@Parameter(23) +Spinbox = 5; +@Group("Textbox") +@Description("Text box for vector with more than 4 elements") +@Parameter("comment") +Vector = [12, 34, 44, 43, 23, 23]; +@Group("Textbox") +@Description("Text box for string") +@Parameter("comment") +String = "hello"; +@Group("Special vector") +@Description("Text box for vector with less than or equal to 4 elements") +@Parameter("any thing") +Vector2 = [12, 34, 45, 23]; +echo(String); + diff --git a/tests/regression/moduledumptest/nativeParameter-expected.ast b/tests/regression/moduledumptest/nativeParameter-expected.ast new file mode 100644 index 00000000..96b94c53 --- /dev/null +++ b/tests/regression/moduledumptest/nativeParameter-expected.ast @@ -0,0 +1,55 @@ +@Parameter(12) +x1 = 1; +@Description("normal description") +@Parameter(12) +x2 = 1; +@Description("normal1 description") +@Parameter(12) +x3 = 1; +@Parameter("ADAD") +x4 = 1; +@Group("Global") +@Description("normal description") +@Parameter([1 : 12]) +x5 = 1; +@Group("Global") +@Description("normal description") +@Parameter([1 : 2 : 12]) +x6 = 1; +@Description("normal description") +x7 = 1; +@Group("Global") +x8 = 1; +x9 = 1; +x10 = 1; +x11 = 1; +@Group("Global") +@Parameter([12]) +x12 = 1; +@Parameter([[10, "Small"], [20, "Medium"], [30, "Large"]]) +x13 = 10; +@Parameter([[10, 100], [20, 101], [30, 102]]) +x14 = 10; +@Parameter("parameter") +x15 = 10; +@Parameter([0, 1, 2, 3]) +x16 = 10; +@Parameter("parameter") +x17 = "text"; +@Parameter(["foo", "bar", "baz"]) +x18 = "text"; +@Parameter([[0, "text"], [1, "foo"], [2, "bar"], [3, "hello"]]) +x19 = "text"; +@Parameter([["foo", 10], ["bar", 10], ["baz", 30]]) +x20 = "text"; +@Parameter([["foo", "yes"], ["bar", "no"], ["baz", "mgiht"]]) +x21 = "text"; +@Parameter([23, 4]) +x22 = [12, 34]; +@Parameter([23, 4, 23, 4, 45]) +x23 = [12, 34]; +@Parameter([23, 4, 2, 3, 4, 6]) +x24 = [12, 34, 2, 3, 41, 23]; +@Parameter("end parameter") +x27 = 12; +