From d8ab3be507acd9670e9ea7833f080713b17fe385 Mon Sep 17 00:00:00 2001 From: Christophe Geuzaine <cgeuzaine@uliege.be> Date: Sat, 18 Apr 2020 12:37:20 +0200 Subject: [PATCH] new NumberFormat ONELAB parameter attribute --- Common/GmshMessage.cpp | 2 ++ Fltk/inputRange.h | 3 ++- Fltk/inputValue.h | 18 +++++++++++++++++- Fltk/onelabGroup.cpp | 2 ++ Fltk/outputRange.h | 18 ++++++++++++++---- 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/Common/GmshMessage.cpp b/Common/GmshMessage.cpp index ad95614a4f..0f979af9f9 100644 --- a/Common/GmshMessage.cpp +++ b/Common/GmshMessage.cpp @@ -1391,6 +1391,8 @@ void Msg::ExchangeOnelabParameter(const std::string &key, ps[0].setAttribute("Closed", copt["Closed"][0]); if(noClosed && fopt.count("Closed")) ps[0].setAttribute("Closed", fopt["Closed"][0] ? "1" : "0"); + if(copt.count("NumberFormat")) + ps[0].setAttribute("NumberFormat", copt["NumberFormat"][0]); _setStandardOptions(&ps[0], fopt, copt); _onelabClient->set(ps[0]); #endif diff --git a/Fltk/inputRange.h b/Fltk/inputRange.h index 67a2f93cde..4e1e7c2b9d 100644 --- a/Fltk/inputRange.h +++ b/Fltk/inputRange.h @@ -25,7 +25,7 @@ private: Fl_Toggle_Button *_loop_butt; Fl_Button *_range_butt, *_graph_butt; Fl_Menu_Button *_graph_menu; - std::string _loop_val, _graph_val; + std::string _loop_val, _graph_val, _number_format; double _min, _max, _step, _max_number; std::vector<double> _choices; std::string _range, _range_tooltip; @@ -366,6 +366,7 @@ public: } int color() { return _input->color(); } Fl_Value_Input *input() { return _input; } + void numberFormat(const std::string &fmt) { _input->numberFormat(fmt); } }; #endif diff --git a/Fltk/inputValue.h b/Fltk/inputValue.h index 4526258649..664d8f7703 100644 --- a/Fltk/inputValue.h +++ b/Fltk/inputValue.h @@ -8,16 +8,32 @@ #include <FL/Fl.H> #include <FL/Fl_Value_Input.H> +#include <string> // same as FL_Value_Input, but prints values in engineering notation class inputValue : public Fl_Value_Input { +private: + std::string _number_format; public: inputValue(int x, int y, int w, int h, const char *l = 0) : Fl_Value_Input(x, y, w, h, l) { } - virtual int format(char *buffer) { return sprintf(buffer, "%g", value()); } + void numberFormat(const std::string &fmt) { _number_format = fmt; } + virtual int format(char *buffer) + { + if(_number_format.empty()) { + return sprintf(buffer, "%g", value()); + } + else { + if(_number_format.find("d") != std::string::npos || + _number_format.find("u") != std::string::npos) + return sprintf(buffer, _number_format.c_str(), (int)value()); + else + return sprintf(buffer, _number_format.c_str(), value()); + } + } }; // same as inputValue, but forces the underlying FL_Value_Input to always accept diff --git a/Fltk/onelabGroup.cpp b/Fltk/onelabGroup.cpp index dee59eb3b1..5b44eb619f 100644 --- a/Fltk/onelabGroup.cpp +++ b/Fltk/onelabGroup.cpp @@ -894,6 +894,7 @@ Fl_Widget *onelabGroup::_addParameterWidget(onelab::number &p, int ww, int hh, if(p.getReadOnly()) { outputRange *but = new outputRange(1, 1, ww, hh); but->callback(onelab_number_output_range_cb, (void *)path); + but->numberFormat(p.getAttribute("NumberFormat")); but->value(p.getValue()); but->align(FL_ALIGN_RIGHT | FL_ALIGN_CLIP); but->graph(p.getAttribute("Graph")); @@ -904,6 +905,7 @@ Fl_Widget *onelabGroup::_addParameterWidget(onelab::number &p, int ww, int hh, // general number input inputRange *but = new inputRange(1, 1, ww, hh, onelab::parameter::maxNumber(), p.getAttribute("ReadOnlyRange") == "1"); + but->numberFormat(p.getAttribute("NumberFormat")); but->value(p.getValue()); but->minimum(p.getMin()); but->maximum(p.getMax()); diff --git a/Fltk/outputRange.h b/Fltk/outputRange.h index 53820ebd19..59eead4f00 100644 --- a/Fltk/outputRange.h +++ b/Fltk/outputRange.h @@ -20,7 +20,7 @@ private: Fl_Output *_output; Fl_Button *_graph_butt; Fl_Menu_Button *_graph_menu; - std::string _graph_val; + std::string _graph_val, _number_format; void _set_graph_value(const std::string &val, bool update_menu = true) { _graph_val = val; @@ -140,9 +140,18 @@ public: } void value(double val) { - char tmp[128]; - sprintf(tmp, "%g", val); - _output->value(tmp); + char buffer[256]; + if(_number_format.empty()) { + sprintf(buffer, "%g", val); + } + else{ + if(_number_format.find("d") != std::string::npos || + _number_format.find("u") != std::string::npos) + sprintf(buffer, _number_format.c_str(), (int)val); + else + sprintf(buffer, _number_format.c_str(), val); + } + _output->value(buffer); } void graph(const std::string &val) { _set_graph_value(val); } std::string graph() { return _graph_val; } @@ -151,6 +160,7 @@ public: _output->color(col); _output->textcolor(fl_contrast(FL_FOREGROUND_COLOR, col)); } + void numberFormat(const std::string &fmt) { _number_format = fmt; } }; #endif -- GitLab