how to force scientific notation for a property
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
how to force scientific notation for a property
When dealing with simulations you often have to specify small numbers (tolerances etc.). The problem is that in the property editor you don't see them.
For example a solver tolerance has to be set to "1e-10". The property is an App::PropertyFloat. The user might use 3 decimal in the FC-wide perefences. Tehn he only sees "0.000". He has therefore no knowledge that the tolerance it 1e-10 nor can he change it to 1e-9.
To fix this, I need to activate scientific notation for this property in the property editor.
In Gui::PropertyEditor::PropertyFloatItem (PropertyItem.cpp) I see that we hardcoded to floating point notation.
This simple change fixes at least the viewing issue:
https://github.com/FreeCAD/FreeCAD/pull/7281
However, I could not yet find out how to fix the editing as well. Currently, to input "1e-8"one has to first write "18", the place the cursor after the 1 then input "e-". This is very inconvenient.
For example a solver tolerance has to be set to "1e-10". The property is an App::PropertyFloat. The user might use 3 decimal in the FC-wide perefences. Tehn he only sees "0.000". He has therefore no knowledge that the tolerance it 1e-10 nor can he change it to 1e-9.
To fix this, I need to activate scientific notation for this property in the property editor.
In Gui::PropertyEditor::PropertyFloatItem (PropertyItem.cpp) I see that we hardcoded to floating point notation.
This simple change fixes at least the viewing issue:
https://github.com/FreeCAD/FreeCAD/pull/7281
However, I could not yet find out how to fix the editing as well. Currently, to input "1e-8"one has to first write "18", the place the cursor after the 1 then input "e-". This is very inconvenient.
Re: how to force scientific notation for a property
Nothing, the "e" is not accepted as input. Test this out by any float property of your choice.
Moreover, even with my proposed fix, the correctly displayed "1er-8" will be transformed to "0.000" when trying to change it. I am pretty sure there is just a 'f' hardcoded somewhere but I cannot find it yet.
Re: how to force scientific notation for a property
Replacing "f" with "g" is not an option at all because it makes everything worse:
But even for small numbers, i.e. abs(x) < 1 it doesn't always work as expected
So, I would never use the "g" but either "f" or "e". "f" is fine for most (CAD) cases and "e" is handy for extremely small or extremely high values like you often have in FEM.
Links:
https://doc.qt.io/qt-5/qstring.html#argument-formats
Code: Select all
from PySide2 import QtCore
QtCore.QLocale().toString(13.91, 'g', 2) # => 14 (Bad)
QtCore.QLocale().toString(13.91, 'e', 2) # => 1.39e+01 (slightly better)
QtCore.QLocale().toString(13.91, 'f', 2) # => 13.91 (OK)
QtCore.QLocale().toString(13.91, 'g', 3) # => 13.9 (Still bad)
QtCore.QLocale().toString(13.91, 'g', 4) # => 13.91 (OK now)
QtCore.QLocale().toString(13333.91, 'g', 4) # => 1.333e+04 (= 13330.0) (Very bad)
Code: Select all
QtCore.QLocale().toString(1e-7, 'g', 4) # 1e-07 (OK)
QtCore.QLocale().toString(1e-7, 'e', 2) # 1.00e-07 (OK)
QtCore.QLocale().toString(1.12345e-7, 'g', 2) # => 1.1e-07 (Bad)
QtCore.QLocale().toString(1.12345e-7, 'e', 2) # => 1.12e-07 (OK)
Links:
https://doc.qt.io/qt-5/qstring.html#argument-formats
Re: how to force scientific notation for a property
I see. But how can I fix my issue then?
In the properties I found now
App::PropertyPrecision
that you once implemented. This could do the job
However, I don't know yet how to make a property change in Python (the solver FEM code is in Python).
In C++ we have the HandleChangedPropertyType class. What is the analogon for Python?
Re: how to force scientific notation for a property
But then we break again compatibility with older FreeCAD versions.App::PropertyPrecision
that you once implemented. This could do the job
However, I don't know yet how to make a property change in Python (the solver FEM code is in Python).
In C++ we have the HandleChangedPropertyType class. What is the analogon for Python?
In order to enter values in scientific notation a QuantitySpinBox can be instructed to do that. However, at the moment it can only be done programmatically and what's needed is a way that a user can do it. Therefore in the first step the context-menu must be extended by two entries Fixed & Scientific notation.
In a second step a helper function should be implemented that does the correct conversion to a QString.
Re: how to force scientific notation for a property
But this was my question:
- in C++ I can change the property and to keep backwards compatibility, I use handleChangedPropertyType .
- for Python I need the same an need to know what dies the same as handleChangedPropertyType in Python.
Background: apart from this problem here, I want to implement for FEM to use the different properties we already have (App:PropertyForce etc.).
Re: how to force scientific notation for a property
wmayer wrote: ↑Sat Jul 30, 2022 4:47 pm In order to enter values in scientific notation a QuantitySpinBox can be instructed to do that. However, at the moment it can only be done programmatically and what's needed is a way that a user can do it. Therefore in the first step the context-menu must be extended by two entries Fixed & Scientific notation.
Do you mean this workaround?:
This is what I am describing now for the Wiki. However, the point is that the user is not aware that "0.000" can mean the value is smaller and that he has to use the context-menu to see this.
Re: how to force scientific notation for a property
No, but this is an alternative way.Do you mean this workaround?: