Skip to content
Snippets Groups Projects
Commit fc495311 authored by Christophe Geuzaine's avatar Christophe Geuzaine
Browse files

added valueLabels to onelab (used to implement "enumeration-type" parameters)

parent a7ddeaf8
No related branches found
No related tags found
No related merge requests found
...@@ -677,7 +677,10 @@ void Msg::ExchangeOnelabParameter(const std::string &key, ...@@ -677,7 +677,10 @@ void Msg::ExchangeOnelabParameter(const std::string &key,
ps[0].setMax(fopt["Max"][0]); ps[0].setMin(-onelab::parameter::maxNumber()); ps[0].setMax(fopt["Max"][0]); ps[0].setMin(-onelab::parameter::maxNumber());
} }
if(noRange && fopt.count("Step")) ps[0].setStep(fopt["Step"][0]); if(noRange && fopt.count("Step")) ps[0].setStep(fopt["Step"][0]);
if(noChoices && fopt.count("Choices")) ps[0].setChoices(fopt["Choices"]); if(noChoices && fopt.count("Choices")){
ps[0].setChoices(fopt["Choices"]);
if(copt.count("Choices")) ps[0].setChoiceLabels(copt["Choices"]);
}
if(fopt.count("Visible")) ps[0].setVisible(fopt["Visible"][0] ? true : false); if(fopt.count("Visible")) ps[0].setVisible(fopt["Visible"][0] ? true : false);
if(copt.count("Help")) ps[0].setHelp(copt["Help"][0]); if(copt.count("Help")) ps[0].setHelp(copt["Help"][0]);
if(copt.count("Label")) ps[0].setLabel(copt["Label"][0]); if(copt.count("Label")) ps[0].setLabel(copt["Label"][0]);
......
...@@ -124,7 +124,7 @@ namespace onelab{ ...@@ -124,7 +124,7 @@ namespace onelab{
const std::set<std::string> &getClients() const { return _clients; } const std::set<std::string> &getClients() const { return _clients; }
static char charSep() { return '\0'; } static char charSep() { return '\0'; }
static double maxNumber() { return 1e200; } static double maxNumber() { return 1e200; }
static std::string version() { return "1.01"; } static std::string version() { return "1.02"; }
static std::string getNextToken(const std::string &msg, static std::string getNextToken(const std::string &msg,
std::string::size_type &first, std::string::size_type &first,
char separator=charSep()) char separator=charSep())
...@@ -218,6 +218,7 @@ namespace onelab{ ...@@ -218,6 +218,7 @@ namespace onelab{
private: private:
double _value, _min, _max, _step; double _value, _min, _max, _step;
std::vector<double> _choices; std::vector<double> _choices;
std::map<double, std::string> _valueLabels;
public: public:
number(const std::string &name="", double value=0., number(const std::string &name="", double value=0.,
const std::string &label="", const std::string &help="") const std::string &label="", const std::string &help="")
...@@ -228,12 +229,36 @@ namespace onelab{ ...@@ -228,12 +229,36 @@ namespace onelab{
void setMax(double max){ _max = max; } void setMax(double max){ _max = max; }
void setStep(double step){ _step = step; } void setStep(double step){ _step = step; }
void setChoices(const std::vector<double> &choices){ _choices = choices; } void setChoices(const std::vector<double> &choices){ _choices = choices; }
void setChoiceLabels(const std::vector<std::string> &labels)
{
if(labels.size() != _choices.size()) return;
for(unsigned int i = 0; i < _choices.size(); i++)
_valueLabels[_choices[i]] = labels[i];
}
void setValueLabels(const std::map<double, std::string> &valueLabels)
{
_valueLabels = valueLabels;
}
void setValueLabel(double value, const std::string &label)
{
_valueLabels[value] = label;
}
std::string getType() const { return "number"; } std::string getType() const { return "number"; }
double getValue() const { return _value; } double getValue() const { return _value; }
double getMin() const { return _min; } double getMin() const { return _min; }
double getMax() const { return _max; } double getMax() const { return _max; }
double getStep() const { return _step; } double getStep() const { return _step; }
const std::vector<double> &getChoices() const { return _choices; } const std::vector<double> &getChoices() const { return _choices; }
const std::map<double, std::string> &getValueLabels() const
{
return _valueLabels;
}
std::string getValueLabel(double value) const
{
std::map<double, std::string>::const_iterator it = _valueLabels.find(value);
if(it != _valueLabels.end()) return it->second;
return "";
}
void update(const number &p) void update(const number &p)
{ {
addClients(p.getClients()); // complete the list addClients(p.getClients()); // complete the list
...@@ -259,6 +284,12 @@ namespace onelab{ ...@@ -259,6 +284,12 @@ namespace onelab{
<< _choices.size() << charSep(); << _choices.size() << charSep();
for(unsigned int i = 0; i < _choices.size(); i++) for(unsigned int i = 0; i < _choices.size(); i++)
sstream << _choices[i] << charSep(); sstream << _choices[i] << charSep();
sstream << _valueLabels.size() << charSep();
for(std::map<double, std::string>::const_iterator it = _valueLabels.begin();
it != _valueLabels.end(); it++){
sstream << it->first << charSep();
sstream << sanitize(it->second) << charSep();
}
return sstream.str(); return sstream.str();
} }
std::string::size_type fromChar(const std::string &msg) std::string::size_type fromChar(const std::string &msg)
...@@ -272,6 +303,11 @@ namespace onelab{ ...@@ -272,6 +303,11 @@ namespace onelab{
_choices.resize(atoi(getNextToken(msg, pos).c_str())); _choices.resize(atoi(getNextToken(msg, pos).c_str()));
for(unsigned int i = 0; i < _choices.size(); i++) for(unsigned int i = 0; i < _choices.size(); i++)
_choices[i] = atof(getNextToken(msg, pos).c_str()); _choices[i] = atof(getNextToken(msg, pos).c_str());
int numValueLabels = atoi(getNextToken(msg, pos).c_str());
for(int i = 0; i < numValueLabels; i++){
double value = atof(getNextToken(msg, pos).c_str());
_valueLabels[value] = getNextToken(msg, pos);
}
return pos; return pos;
} }
}; };
......
...@@ -848,6 +848,20 @@ static void onelab_check_button_cb(Fl_Widget *w, void *data) ...@@ -848,6 +848,20 @@ static void onelab_check_button_cb(Fl_Widget *w, void *data)
} }
} }
static void onelab_choice_cb(Fl_Widget *w, void *data)
{
if(!data) return;
std::string name = FlGui::instance()->onelab->getPath((Fl_Tree_Item*)data);
std::vector<onelab::number> numbers;
onelab::server::instance()->get(numbers, name);
if(numbers.size()){
Fl_Choice *o = (Fl_Choice*)w;
std::vector<double> choices = numbers[0].getChoices();
if(o->value() < (int)choices.size()) numbers[0].setValue(choices[o->value()]);
onelab::server::instance()->set(numbers[0]);
}
}
static void onelab_input_range_cb(Fl_Widget *w, void *data) static void onelab_input_range_cb(Fl_Widget *w, void *data)
{ {
if(!data) return; if(!data) return;
...@@ -1020,7 +1034,9 @@ void onelabWindow::_addParameter(onelab::number &p) ...@@ -1020,7 +1034,9 @@ void onelabWindow::_addParameter(onelab::number &p)
n->labelsize(FL_NORMAL_SIZE + 5); n->labelsize(FL_NORMAL_SIZE + 5);
std::string label = p.getShortName(); std::string label = p.getShortName();
_tree->begin(); _tree->begin();
if(p.getChoices().size() == 2 && p.getChoices()[0] == 0 && p.getChoices()[1] == 1){ unsigned int numChoices = p.getChoices().size();
if(numChoices == 2 && p.getChoices()[0] == 0 && p.getChoices()[1] == 1){
// check box (boolean choice)
Fl_Check_Button *but = new Fl_Check_Button(1, 1, _itemWidth, 1); Fl_Check_Button *but = new Fl_Check_Button(1, 1, _itemWidth, 1);
_treeWidgets.push_back(but); _treeWidgets.push_back(but);
but->copy_label(label.c_str()); but->copy_label(label.c_str());
...@@ -1031,7 +1047,40 @@ void onelabWindow::_addParameter(onelab::number &p) ...@@ -1031,7 +1047,40 @@ void onelabWindow::_addParameter(onelab::number &p)
if(getFlColor(p.getAttribute("Highlight"), c)) if(getFlColor(p.getAttribute("Highlight"), c))
n->labelbgcolor(c); n->labelbgcolor(c);
} }
else if(numChoices && numChoices == p.getValueLabels().size()){
// enumeration (display choices as value labels, not numbers)
Fl_Choice *but = new Fl_Choice(1, 1, _itemWidth, 1);
_treeWidgets.push_back(but);
but->copy_label(label.c_str());
std::vector<Fl_Menu_Item> menu;
std::map<double, std::string> labels(p.getValueLabels());
for(std::map<double, std::string>::iterator it = labels.begin();
it != labels.end(); it++){
// need to manually manage the label strings
char *str = strdup(it->second.c_str());
_treeStrings.push_back(str);
Fl_Menu_Item it = {str, 0, 0, 0, 0};
menu.push_back(it);
}
Fl_Menu_Item it = {0};
menu.push_back(it);
but->copy(&menu[0]);
std::vector<double> choices = p.getChoices();
for(unsigned int i = 0; i < choices.size(); i++){
if(p.getValue() == choices[i]){
but->value(i);
break;
}
}
but->align(FL_ALIGN_RIGHT);
but->callback(onelab_choice_cb, (void*)n);
n->widget(but);
Fl_Color c;
if(getFlColor(p.getAttribute("Highlight"), c))
n->labelbgcolor(c);
}
else{ else{
// general number input
inputRange *but = new inputRange inputRange *but = new inputRange
(1, 1, _itemWidth, 1, onelab::parameter::maxNumber()); (1, 1, _itemWidth, 1, onelab::parameter::maxNumber());
_treeWidgets.push_back(but); _treeWidgets.push_back(but);
......
This diff is collapsed.
...@@ -184,7 +184,7 @@ typedef union YYSTYPE ...@@ -184,7 +184,7 @@ typedef union YYSTYPE
{ {
/* Line 1685 of yacc.c */ /* Line 1685 of yacc.c */
#line 86 "Gmsh.y" #line 92 "Gmsh.y"
char *c; char *c;
int i; int i;
......
...@@ -81,6 +81,12 @@ void yymsg(int level, const char *fmt, ...); ...@@ -81,6 +81,12 @@ void yymsg(int level, const char *fmt, ...);
void skip_until(const char *skip, const char *until); void skip_until(const char *skip, const char *until);
int PrintListOfDouble(char *format, List_T *list, char *buffer); int PrintListOfDouble(char *format, List_T *list, char *buffer);
fullMatrix<double> ListOfListOfDouble2Matrix(List_T *list); fullMatrix<double> ListOfListOfDouble2Matrix(List_T *list);
struct doubleXstring{
double d;
char *s;
};
%} %}
%union { %union {
...@@ -125,7 +131,7 @@ fullMatrix<double> ListOfListOfDouble2Matrix(List_T *list); ...@@ -125,7 +131,7 @@ fullMatrix<double> ListOfListOfDouble2Matrix(List_T *list);
%type <u> ColorExpr %type <u> ColorExpr
%type <c> StringExpr StringExprVar SendToFile HomologyCommand %type <c> StringExpr StringExprVar SendToFile HomologyCommand
%type <l> FExpr_Multi ListOfDouble ListOfDoubleOrAll RecursiveListOfDouble %type <l> FExpr_Multi ListOfDouble ListOfDoubleOrAll RecursiveListOfDouble
%type <l> RecursiveListOfListOfDouble %type <l> RecursiveListOfListOfDouble Enumeration
%type <l> ListOfColor RecursiveListOfColor %type <l> ListOfColor RecursiveListOfColor
%type <l> ListOfShapes Transform Extrude MultipleShape %type <l> ListOfShapes Transform Extrude MultipleShape
%type <l> TransfiniteCorners InSphereCenter %type <l> TransfiniteCorners InSphereCenter
...@@ -1101,6 +1107,20 @@ DefineConstants : ...@@ -1101,6 +1107,20 @@ DefineConstants :
} }
; ;
Enumeration :
FExpr tAFFECT StringExpr
{
$$ = List_Create(20,20,sizeof(doubleXstring));
doubleXstring v = {$1, $3};
List_Add($$, &v);
}
| Enumeration ',' FExpr tAFFECT StringExpr
{
doubleXstring v = {$3, $5};
List_Add($$, &v);
}
;
FloatParameterOptions : FloatParameterOptions :
| FloatParameterOptions FloatParameterOption | FloatParameterOptions FloatParameterOption
; ;
...@@ -1117,6 +1137,21 @@ FloatParameterOption : ...@@ -1117,6 +1137,21 @@ FloatParameterOption :
Free($2); Free($2);
List_Delete($3); List_Delete($3);
} }
| ',' tSTRING '{' Enumeration '}'
{
std::string key($2);
for(int i = 0; i < List_Nbr($4); i++){
doubleXstring v;
List_Read($4, i, &v);
floatOptions[key].push_back(v.d);
charOptions[key].push_back(v.s);
}
Free($2);
for(int i = 0; i < List_Nbr($4); i++)
Free(((doubleXstring*)List_Pointer($4, i))->s);
List_Delete($4);
}
| ',' tSTRING tBIGSTR | ',' tSTRING tBIGSTR
{ {
std::string key($2); std::string key($2);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment