how to force scientific notation for a property

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
uwestoehr
Veteran
Posts: 4961
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany
Contact:

how to force scientific notation for a property

Post by uwestoehr »

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.
User avatar
jnxd
Posts: 951
Joined: Mon Mar 30, 2015 2:30 pm
Contact:

Re: how to force scientific notation for a property

Post by jnxd »

uwestoehr wrote: Sat Jul 30, 2022 1:39 pm
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.
What happens if one enters "1e"?
My latest (or last) project: B-spline Construction Project.
User avatar
uwestoehr
Veteran
Posts: 4961
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany
Contact:

Re: how to force scientific notation for a property

Post by uwestoehr »

jnxd wrote: Sat Jul 30, 2022 2:14 pm What happens if one enters "1e"?
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.
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: how to force scientific notation for a property

Post by wmayer »

Replacing "f" with "g" is not an option at all because it makes everything worse:

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)
But even for small numbers, i.e. abs(x) < 1 it doesn't always work as expected

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)
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
User avatar
uwestoehr
Veteran
Posts: 4961
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany
Contact:

Re: how to force scientific notation for a property

Post by uwestoehr »

wmayer wrote: Sat Jul 30, 2022 4:13 pm Replacing "f" with "g" is not an option at all because it makes everything worse:
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?
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: how to force scientific notation for a property

Post by wmayer »

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?
But then we break again compatibility with older FreeCAD versions.

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.
User avatar
uwestoehr
Veteran
Posts: 4961
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany
Contact:

Re: how to force scientific notation for a property

Post by uwestoehr »

wmayer wrote: Sat Jul 30, 2022 4:47 pm
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?
But then we break again compatibility with older FreeCAD versions.
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.).
User avatar
uwestoehr
Veteran
Posts: 4961
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany
Contact:

Re: how to force scientific notation for a property

Post by uwestoehr »

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?:
FreeCAD_jZzedDSJ6u.gif
FreeCAD_jZzedDSJ6u.gif (130.79 KiB) Viewed 1368 times

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.
User avatar
uwestoehr
Veteran
Posts: 4961
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany
Contact:

Re: how to force scientific notation for a property

Post by uwestoehr »

uwestoehr wrote: Sat Jul 30, 2022 5:24 pm Do you mean this workaround?:
Hmm, I could make for now as default an expression. Then the user sees the content and by clicking on it, the expression editor pops up allowing to input in scientific notation.
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: how to force scientific notation for a property

Post by wmayer »

Do you mean this workaround?:
No, but this is an alternative way.
Post Reply