Skip to content

Commit 341a253

Browse files
authored
Merge pull request #58 from ottolink-dev/fix/industrial-value-format
fix(qt): render the value readout in the format the attribute declared
2 parents 5c3e096 + cd80631 commit 341a253

4 files changed

Lines changed: 104 additions & 12 deletions

File tree

‎MetaUI/qt/include/meta_qt/designs/industrial/param_slider.hpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ class ParamSlider : public Control<float>
9898
float value_ = 0.f;
9999
bool log_scale_ = false;
100100
int decimals_ = 2;
101+
102+
/** @brief The attribute's declared format spec, e.g. "{:.2e}".
103+
*
104+
* Kept whole rather than reduced to a decimal count, because the
105+
* presentation type carries as much intent as the precision does. A rate
106+
* declared over five decades wants 1.00e-03, not 0.001, and certainly not
107+
* the eight characters of 0.00000100 that a fixed readout produces.
108+
*/
109+
std::string format_;
101110
std::string label_;
102111
std::string category_;
103112
std::string key_; ///< attribute name, for the defaults lookup on reset

‎MetaUI/qt/include/meta_qt/ui/number_format.hpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,26 @@
55
#include <QString>
66
#include <algorithm>
77
#include <cmath>
8+
#include <string>
89
namespace meta::qt
910
{
11+
/** @brief True when a std::format spec asks for scientific or general form.
12+
*
13+
* Looks only at the presentation type, the last character before the closing
14+
* brace, so "{:.2e}" and "{:10.3E}" both match while "{:.2f}" does not. A
15+
* spec that names one of these is asking for an exponent, and the readout has
16+
* to give it one rather than substituting a fixed rendering of its own.
17+
*/
18+
inline bool has_exponent_format(const std::string &spec)
19+
{
20+
const auto close = spec.find_last_of('}');
21+
if (close == std::string::npos || close == 0)
22+
return false;
23+
24+
const char type = spec[close - 1];
25+
return type == 'e' || type == 'E' || type == 'g' || type == 'G';
26+
}
27+
1028
// Ordinary values stay compact; small nonzero values must never read as zero.
1129
inline QString display_float(float value, int decimals = 2)
1230
{

‎MetaUI/qt/src/designs/industrial/param_slider.cpp‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Public License. The full license is in the file LICENSE, distributed with
33
this software. */
44
#include "meta_qt/designs/industrial/param_slider.hpp"
5+
#include <format>
6+
57
#include "meta_qt/ui/number_format.hpp"
68

79
#include <algorithm>
@@ -49,7 +51,8 @@ ParamSlider::ParamSlider(Attribute<float> &attr,
4951
log_scale_ = meta::common::try_get<bool>(attr,
5052
meta::keys::ui::log_scale,
5153
false);
52-
decimals_ = meta::common::try_get_format_decimals(meta::common::format(attr));
54+
format_ = meta::common::format(attr);
55+
decimals_ = meta::common::try_get_format_decimals(format_);
5356

5457
unbounded_ = !has_usable_range(min_, max_);
5558

@@ -526,7 +529,29 @@ void ParamSlider::commit_value(float value)
526529

527530
QString ParamSlider::format_value(float value) const
528531
{
529-
return display_float(value);
532+
// Honour the presentation type the attribute declared. A rate spanning five
533+
// decades declares "{:.2e}" precisely so its readout stays three characters
534+
// of mantissa and an exponent; rendering it fixed gives 0.00000100, which
535+
// does not fit the value field and reads as a truncated number rather than
536+
// a small one.
537+
//
538+
// Only e and g are routed here. A fixed spec goes to display_float, which
539+
// widens the precision rather than letting a small non-zero value round away
540+
// to 0.00, and that is the behaviour a fixed readout wants.
541+
if (has_exponent_format(format_))
542+
{
543+
try
544+
{
545+
return QString::fromStdString(
546+
std::vformat(format_, std::make_format_args(value)));
547+
}
548+
catch (const std::format_error &)
549+
{
550+
// A malformed spec is a host bug, not a reason to render nothing.
551+
}
552+
}
553+
554+
return display_float(value, decimals_);
530555
}
531556

532557
void ParamSlider::refresh_field()

‎tests/test_qt/test_slider_mapping/main.cpp‎

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ void flush()
4949
}
5050

5151
/// The value field a slider owns, so a typed commit can be exercised.
52-
QLineEdit *field_of(QWidget *slider)
53-
{
54-
return slider->findChild<QLineEdit *>();
55-
}
52+
QLineEdit *field_of(QWidget *slider) { return slider->findChild<QLineEdit *>(); }
5653

5754
/// Press, drag and release at `x`, which is how a rail is actually driven.
5855
void click_rail(QWidget *slider, int x)
@@ -125,8 +122,7 @@ int main(int argc, char **argv)
125122
const float linear_value = linear_slider.get();
126123
const float log_value = log_slider.get();
127124

128-
std::cout << "same press: linear=" << linear_value << " log=" << log_value
129-
<< '\n';
125+
std::cout << "same press: linear=" << linear_value << " log=" << log_value << '\n';
130126

131127
check(linear_value > 0.f, "the linear rail responded to a press");
132128
check(log_value < linear_value,
@@ -170,8 +166,7 @@ int main(int argc, char **argv)
170166
Q_EMIT field->editingFinished();
171167
flush();
172168

173-
check(slider.get() > 64.f,
174-
"a typed value above the rail maximum is accepted");
169+
check(slider.get() > 64.f, "a typed value above the rail maximum is accepted");
175170
check(std::abs(slider.get() - 512.f) < 1e-2f,
176171
"a typed value is held to the real maximum, not to the rail");
177172
}
@@ -203,8 +198,53 @@ int main(int argc, char **argv)
203198
Q_EMIT field->editingFinished();
204199
flush();
205200

206-
check(slider.get() <= 64.f,
207-
"a hard maximum of 64 still clamps a typed value");
201+
check(slider.get() <= 64.f, "a hard maximum of 64 still clamps a typed value");
202+
}
203+
}
204+
205+
// --- the readout honours the declared presentation type -----------------
206+
//
207+
// HydraulicParticle declares its rates as "{:.2e}" over [1e-6, 1e-1]. Rendered
208+
// fixed, 1e-3 comes out as 0.00100000 and overruns the value field; Otto saw
209+
// it clipped to a run of zeroes with no decimal point in sight.
210+
{
211+
auto *attr = make_attr(container, "drag_rate", 1e-3f, 1e-6f, 1e-1f);
212+
attr->metadata().add(meta::keys::ui::format, std::string("{:.2e}"));
213+
attr->metadata().add(meta::keys::ui::log_scale, true);
214+
215+
industrial::ParamSlider slider(*attr, ctx);
216+
slider.resize(400, 36);
217+
flush();
218+
219+
QLineEdit *field = field_of(&slider);
220+
check(field != nullptr, "the rate slider has a value field");
221+
222+
if (field)
223+
{
224+
std::cout << "scientific readout: " << field->text().toStdString() << '\n';
225+
check(field->text().contains('e') || field->text().contains('E'),
226+
"a {:.2e} attribute renders with an exponent");
227+
check(field->text().size() <= 10,
228+
"the scientific readout is short enough for the value field");
229+
}
230+
}
231+
232+
// --- a fixed spec still widens rather than rounding a small value away ---
233+
{
234+
auto *attr = make_attr(container, "fixed_small", 0.001f, 0.f, 1.f);
235+
attr->metadata().add(meta::keys::ui::format, std::string("{:.2f}"));
236+
237+
industrial::ParamSlider slider(*attr, ctx);
238+
slider.resize(400, 36);
239+
flush();
240+
241+
QLineEdit *field = field_of(&slider);
242+
if (field)
243+
{
244+
std::cout << "fixed readout: " << field->text().toStdString() << '\n';
245+
check(field->text() != "0.00",
246+
"a small value under a fixed spec does not read as zero");
247+
check(!field->text().contains('e'), "a fixed spec is not promoted to scientific");
208248
}
209249
}
210250

0 commit comments

Comments
 (0)