taurus-org / taurus

Moved to https://gitlab.com/taurus-org/taurus
http://taurus-scada.org
43 stars 46 forks source link

The Validator we set seems not to be taken into account for TaurusValueLineEdit #1161

Closed marouanebj closed 3 years ago

marouanebj commented 3 years ago

Hi, The Validator seems not to be taken into account for TaurusValueLineEdit !

Here my Validator

class CLocaleQDoubleValidator(Qt.QDoubleValidator):
    """
    A QDoubleValidator using C locale and discarding comma ','
    """
    def __init__(self, *var):
        Qt.QDoubleValidator.__init__(self, *var)
        self._localeHolder = Qt.QLocale.c()
        self._localeHolder.setNumberOptions(Qt.QLocale.RejectGroupSeparator)
        self.setLocale(self._localeHolder)

Here my TaurusValueLineEdit code:

self.positionTaurusEdit = TaurusValueLineEdit(self)
self.positionTaurusEdit.setSingleStep(self.valueEditStep)
self.positionTaurusEditValidator = CLocaleQDoubleValidator(valueMin, valueMax, valueDecimals, self.positionTaurusEdit)
self.positionTaurusEdit.setModel(model + "/TheValue")
self.positionTaurusEdit.setValidator(self.positionTaurusEditValidator)

Here my QLineEdit code:

 qLineEdit = Qt.QLineEdit(self)
 qLineEdit.setToolTip(configToolTip)
 qLineEdit.setValidator(CLocaleQDoubleValidator(qLineEdit))

When I use the TaurusValueLineEdit and I type in "10,2.3" it is translated to 102.3 When I use the QEdit and I cannot type in "10,2.3" the comma ',' can note be used (That's normal according to my CLocaleQDoubleValidator)

Is this a Bug ? I'm I misusing it ? Is the Validator ignored in the TaurusValueLineEdit ? maybe because of the model that already have a format !

Regards, Marouane

cpascual commented 3 years ago

(Sorry for the late reply. I missed this question entirely)

TaurusValueLineEdit already sets its own validator for numerical values, in order to enforce range limits and handling units (keep in mind that in taurus, the values of an attribute are not "doubles" or "ints" but "Quantities"). It actually checks if the validator is appropriate for the value type each time the value is updated, and as such, it overwrites the one you are setting.

I do not see a straight-forward way to customize this without modifying the class itself (the code of TaurusValueLineEditexpects the API from the PintValidator when dealing with numerical values).

You may try to make your validator inherit from PintValidator. This way TaurusValueLineEdit may respect it (but I am not sure it would work) .

marouanebj commented 3 years ago

Thanks, I will try inherit PintValidator.

cpascual commented 3 years ago

Hi @marouanebj , did it work for you? If so, please let us know, so that we can close this issue

Thanks!

marouanebj commented 3 years ago
class PintCLocaleQDoubleValidator(PintValidator):
    """
    A PintValidator using C locale
    """
    def __init__(self, *var):
        PintValidator.__init__(self, *var)
        self._localeHolder = Qt.QLocale.c()
        self._localeHolder.setNumberOptions(Qt.QLocale.RejectGroupSeparator)
        self.setLocale(self._localeHolder)

I just tried this but I still have the same behavior.

cpascual commented 3 years ago

Running the following snippet seems to indicate that my proposed approach should work...

from taurus.qt.qtgui.application import TaurusApplication
from taurus.qt.qtgui.input import TaurusValueLineEdit
from taurus.qt.qtgui.util import PintValidator

class MyValidator(PintValidator):
    def __init__(self, *a):
        PintValidator.__init__(self, *a)
        print("MyValidator")

    def validate(self, input, pos):
        r = PintValidator.validate(self, input, pos)
        print(r, input, pos)
        return r

if __name__ == "__main__":
    import sys
    app = TaurusApplication(cmd_line_parser=None)

    w = TaurusValueLineEdit()
    w.setModel("sys/tg_test/1/double_scalar")
    v = MyValidator(w)
    w.setValidator(v)

    w.show()

    sys.exit(app.exec_())

Please note that PintValidator (and therefore MyValidator too) does not inherit from QDoubleValidator, but from QValidator, and therefore you may need to do some extra implementation yourself in the validate method to accomplish your goal.

marouanebj commented 3 years ago

OK I got it, Thanks