Problem with negative values in InputField

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
DevJohan
Posts: 41
Joined: Sun Jul 13, 2014 2:36 pm
Location: Stockholm, Sweden

Problem with negative values in InputField

Post by DevJohan »

Hi,

For quite some time I've had a problem with the placement dialog concerning negative values. Any negative value is replaced with DOUBLE_MIN. This problem also breaks the use of stepping the value with up and down keys or using the mouse wheel to change value.

To Illustrate the problem just create an object e.g. a cube in parts. Then select the cube and choose the data tab in the property view. Click placement property and to open the placement dialog. Select a value e.g. "Translation, X:" and press "arrow down key" repeatedly. When the value passes 0, it becomes -1 and after that nothing changes when pressing the key more times.

The problem is due to the fact that the number in the InputField is formated based on the locale. For me, this means a negative value is represented with locale().negativeSign(). The Quantity parser doesn't recognize this negative sign which is distinct from a ascii '-' sign.

My solution for fixing this problem is to replace this and other locale dependent characters previous to passing the string to the Quantity parser. The locale().groupSeparator() is already removed to fixup the entered string. I don't think changing the parser to recognize more types of negative signs is the right way.

Let me know what you think and I'll make the appropriate changes.

Cheers,
Johan
wmayer
Founder
Posts: 20246
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Problem with negative values in InputField

Post by wmayer »

Hi Johan,

what again is your OS? Currently, I'm under Windows 7 and I can't see this behaviour.
Any negative value is replaced with DOUBLE_MIN
How do you come to this conclusion?
The problem is due to the fact that the number in the InputField is formated based on the locale. For me, this means a negative value is represented with locale().negativeSign(). The Quantity parser doesn't recognize this negative sign which is distinct from a ascii '-' sign.
Hm, that's funny (or not). Here is a little Python script which tells me that on Windows the minus character is really the minus character and not an obscure Unicode sign.

Code: Select all

from PySide import QtCore
loc=QtCore.QLocale()
loc.negativeSign()
ord(loc.negativeSign()) # => 45
ord('-') # => 45
My solution for fixing this problem is to replace this and other locale dependent characters previous to passing the string to the Quantity parser. The locale().groupSeparator() is already removed to fixup the entered string. I don't think changing the parser to recognize more types of negative signs is the right way.

Let me know what you think and I'll make the appropriate changes.
Yes, you're right. We should replace the "apparent" minus with the real ASCII minus character.
User avatar
DevJohan
Posts: 41
Joined: Sun Jul 13, 2014 2:36 pm
Location: Stockholm, Sweden

Re: Problem with negative values in InputField

Post by DevJohan »

Hi,
I'm on Ubuntu 14.04 64-bit, but I think I had the same problem on 13.10.
wmayer wrote:
Any negative value is replaced with DOUBLE_MIN
How do you come to this conclusion?
From QuantityParser.c line 1453-1454

Code: Select all

#line 33 "QuantityParser.y"
    { QuantResult = Quantity(DOUBLE_MIN); /* empty input */ }
the unrecognized negative sign aborts parse() with an empty input.

For me your script gives

Code: Select all

from PySide import QtCore
loc=QtCore.QLocale()
loc.negativeSign() # => u'\u2212'
ord(loc.negativeSign()) # => 8722
ord('-') # => 45
wmayer wrote:Yes, you're right. We should replace the "apparent" minus with the real ASCII minus character.
I'll make the necessary changes and leave a link to the branch here. The "QuantitySpinBox" also seems to be affected.
wmayer
Founder
Posts: 20246
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Problem with negative values in InputField

Post by wmayer »

OK, thanks!
jmaustpc
Veteran
Posts: 11207
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: Problem with negative values in InputField

Post by jmaustpc »

I do not see the bug here...perhaps that's due to my KDE Language/local settings (English GB, Australia).

OS: Ubuntu 14.04.1 LTS
Word size: 64-bit
Version: 0.15.3783 (Git)
Branch: master
Hash: 4dee80f4e121541801fd4d8552e66ba26dc2c9b5
Python version: 2.7.6
Qt version: 4.8.6
Coin version: 4.0.0a
SoQt version: 1.6.0a
OCC version: 6.7.1
User avatar
DevJohan
Posts: 41
Joined: Sun Jul 13, 2014 2:36 pm
Location: Stockholm, Sweden

Re: Problem with negative values in InputField

Post by DevJohan »

What UnitSchema are you using? The imperial one skips the group separators added by default in MKS and Internal

UnitsSchemaMKS::, UnitsSchemaInternal:: and UnitsSchemaImperial1::schemaTranslate(...):

Code: Select all

    return QString::fromUtf8("%L1 %2").arg(quant.getValue() / factor).arg(unitString);
UnitsSchemaImperialDecimal::schemaTranslate(...):

Code: Select all

    QLocale Lc = QLocale::system();
    Lc.setNumberOptions(Lc.OmitGroupSeparator | Lc.RejectGroupSeparator);
    QString Ln = Lc.toString(quant.getValue() / factor);
    return QString::fromLatin1("%1 %2").arg(Ln).arg(unitString);
    //return QString::fromLatin1("%L1 %2").arg(quant.getValue() / factor).arg(unitString);
wmayer
Founder
Posts: 20246
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Problem with negative values in InputField

Post by wmayer »

I don't know why for UnitsSchemaImperialDecimal this is handled differently. Maybe you should contact jriegel directly via PM.

For me of course all unit systems work properly with negative numbers.
jmaustpc
Veteran
Posts: 11207
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: Problem with negative values in InputField

Post by jmaustpc »

My Units are set to the FreeCAD default, mm/kg/s/degree

Jim
User avatar
jriegel
Founder
Posts: 3369
Joined: Sun Feb 15, 2009 5:29 pm
Location: Ulm, Germany
Contact:

Re: Problem with negative values in InputField

Post by jriegel »

Im not quit sure I understand the problem!?

If there are different unicode numbers for the minus sign, that's no problem. I need the number and I add it to the parster (scanner).
Stop whining - start coding!
wmayer
Founder
Posts: 20246
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Problem with negative values in InputField

Post by wmayer »

Unicode codepage is u'\u2212'

Code: Select all

s=u'\u2212'
print s.encode("utf-8") # => prints "-"
s.encode("utf-8") # => '\xe2\x88\x92'
Post Reply