taurus-org / taurus

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

DevDouble numbers in scientific notation are not shown correctly #1160

Closed yacremann closed 3 years ago

yacremann commented 3 years ago

Hi I think there is a bug in taurus / taurus designer: We observed that DevDouble attributes, which are set in jive to be displayed in scientific notation (for example 1.0e-12) are not displayed this way if I define a TaurusLabel. In this case, the TaurusLabel would show 0.00 Version of taurus: 4.7

Thanks! With best regards Yves Acremann

cpascual commented 3 years ago

Hi @yacremann ,

By default Taurus does not use the Tango display format (more precisely, it uses it partially, only to try to infer the "precision" of an attribute), but you can configure it to show it using the taurus.core.tango.util.TangoFormatter.

To set it directly from the GUI, if you use a TaurusForm, you can use the context menu (either for a single value or for the whole form):

image

If you want to do it programmatically for a TaurusLabel, see this snippet:

from taurus.qt.qtgui.application import TaurusApplication
from taurus.qt.qtgui.display import TaurusLabel
from taurus.core.tango.util import tangoFormatter

import sys
app = TaurusApplication()

w = TaurusLabel()
w.setModel("sys/tg_test/1/double_scalar")

# use the format string provided by the tango config
w.setFormat(tangoFormatter)

w.show()
sys.exit(app.exec_())

For more information about Formatter API, see this

For a detailed explanation on why we do not use the TangoFormatter by default, see this conversation: https://github.com/taurus-org/taurus/issues/1148#issuecomment-690061430

I hope this helps!

daesters commented 3 years ago

@cpascual This is great.

Reading the documentation, I've realized one can set the default formatter for the whole GUI, with TaurusBaseComponent.FORMAT = tangoFormatter

Using Taurus Designer to great the gui ''MainGui.ui''., the following python script does the rest:

import os.path
import sys

from PyQt5 import QtWidgets
from taurus.external.qt import Qt
from taurus.qt.qtgui.util.ui import UILoadable
from taurus.qt.qtgui.base import TaurusBaseComponent
from taurus.core.tango.util import tangoFormatter

@UILoadable(with_ui="_ui")
class DruckGuiImpl(Qt.QMainWindow):

    def __init__(self, parent=None):
        Qt.QMainWindow.__init__(self, parent)
        self.loadUi(filename="MainGui.ui", path=os.path.dirname(__file__))

if __name__ == "__main__":
    # Set default formatter
    TaurusBaseComponent.FORMAT = tangoFormatter
    app = QtWidgets.QApplication(sys.argv)
    dialog = DruckGuiImpl()
    dialog.show()
    sys.exit(app.exec_())`
yacremann commented 3 years ago

Dear @cpascual and @daesters Thanks a lot, this solves the problem!

Have a great day! Yves Acremann

cpascual commented 3 years ago

Hi @daesters and @yacremann

Yes, setting TaurusBaseComponent.FORMAT is possible, but it is discouraged. I understand that that one-liner is tempting, but instead I recommend to be as selective as possible when setting the formatter (i.e, set it at instance level better than at class level, and at specific class level better than at base-class level...)

Here is some more about this (sorry for not having linked to it from the beginning): https://github.com/taurus-org/taurus/wiki/Best-Practices-for-Taurus-4#formatter-api

daesters commented 3 years ago

Dear @cpascual Thanks for your comment. We'll keep that in mind. And thanks for the link, that is really helpfull

Cheers