Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Spinbox was showing units even if the model use fragments to display only the magnitude #669

Merged
merged 6 commits into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/taurus/qt/qtgui/base/taurusbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,30 @@ def getOperationCallbacks(self):
'''
return []

def getDisplayValue(self, cache=True, fragmentName=None):
"""Returns a string representation of the model value associated with
this component.

:param cache: (bool) (ignored, just for bck-compat).
:param fragmentName: (str or None) the returned value will correspond
to the given fragmentName. If None passed,
self.modelFragmentName will be used, and if None is
set, the defaultFragmentName of the model will be used
instead.

:return: (str) a string representation of the model value.
"""
# @fixme: Even the widgets inheriting from this class interacts with
# writable models (then the fragmentName by default that one
# expects is 'wvalue' and not 'rvalue') it has been considered
# that perhaps this could be considered an API modification
# instead of a bugfix.
# The bugfix impact has been bounded only within the
# TaurusValueLineEdit.
return super(TaurusBaseWritableWidget,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would avoid using super (see my other comment)

self).getDisplayValue(cache=cache,
fragmentName=fragmentName)

def getValue(self):
'''
This method must be implemented in derived classes to return
Expand Down
95 changes: 72 additions & 23 deletions lib/taurus/qt/qtgui/input/tauruslineedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@
##
#############################################################################

"""This module provides a set of basic taurus widgets based on QLineEdit"""

__all__ = ["TaurusValueLineEdit"]

__docformat__ = 'restructuredtext'
"""
This module provides a set of basic taurus widgets based on QLineEdit
"""

import sys
import numpy
Expand All @@ -37,6 +35,10 @@
from taurus.qt.qtgui.util import PintValidator
from taurus.core import DataType, DataFormat, TaurusEventType

__all__ = ["TaurusValueLineEdit"]

__docformat__ = 'restructuredtext'


class TaurusValueLineEdit(Qt.QLineEdit, TaurusBaseWritableWidget):

Expand Down Expand Up @@ -72,7 +74,8 @@ def _updateValidator(self, value):
if units != val.units:
val.setUnits(units)

# @TODO Other validators can be configured for other types (e.g. with string lengths, tango names,...)
# @TODO Other validators can be configured for other types
# (e.g. with string lengths, tango names,...)
else:
self.setValidator(None)
self.debug("Validator disabled")
Expand All @@ -93,9 +96,9 @@ def _onEditingFinished(self):
if self._autoApply:
self.writeValue()

#-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
# ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
# TaurusBaseWritableWidget overwriting
#-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
# ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
def notifyValueChanged(self, *args):
'''reimplement to avoid autoapply on every partial edition'''
self.emitValueChanged()
Expand Down Expand Up @@ -140,7 +143,8 @@ def updateStyle(self):
color, weight = 'black', 'normal'
# also check alarms (if applicable)
modelObj = self.getModelObj()
if modelObj and modelObj.type in [DataType.Integer, DataType.Float]:
if modelObj and modelObj.type in [DataType.Integer,
DataType.Float]:
min_, max_ = modelObj.alarms
if ((min_ is not None and value < min_) or
(max_ is not None and value > max_)):
Expand Down Expand Up @@ -197,12 +201,35 @@ def _stepBy(self, v):
value = self.getValue()
self.setValue(value + Quantity(v, value.units))

def getDisplayValue(self, cache=True, fragmentName=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not critical , but... most taurus code avoids using super because of some issues with diamond inheritance schemes, so I would prefer the following implementation:

def getDisplayValue(self, cache=True, fragmentName=None):
    if fragmentName is None and self.modelFragmentName is None:
        fragmentName = 'wvalue'
    return TaurusBaseWidget.getDisplayValue(self, cache=cache, fragmentName=fragmentName)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then this TaurusValueLineEdit.getDisplayValue() may call the newer TaurusBaseWritableWidget.getDisplayValue() instead of the supersuperclass. Even it's an empty call right now, if this is propagated up one would realise better the scale.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100% right. It should be TaurusBaseWritableWidget.getDisplayValue

My mistake

"""Returns a string representation of the model value associated with
this component. As this is a writable widget, if there is no fragment
specified, the default behaviour is to display the wvalue.

:param cache: (bool) (ignored, just for bck-compat).
:param fragmentName: (str or None) the returned value will correspond
to the given fragmentName. If None passed,
self.modelFragmentName will be used, and if None is
set, the defaultFragmentName of the model will be used
instead.

:return: (str) a string representation of the model value.
"""
if fragmentName is None and self.modelFragmentName is None:
return super(TaurusValueLineEdit,
self).getDisplayValue(cache=cache,
fragmentName='wvalue')
else:
return super(TaurusValueLineEdit,
self).getDisplayValue(cache=cache,
fragmentName=fragmentName)

def setValue(self, v):
model = self.getModelObj()
if model is None:
v_str = str(v)
else:
v_str = str(self.displayValue(v))
v_str = str(self.getDisplayValue(v))
v_str = v_str.strip()
self.setText(v_str)

Expand Down Expand Up @@ -258,23 +285,25 @@ def getQtDesignerPluginInfo(cls):
ret['icon'] = "designer:lineedit.png"
return ret

#-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
# ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
# QT properties
#-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
# ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~

model = Qt.pyqtProperty("QString", TaurusBaseWritableWidget.getModel,
TaurusBaseWritableWidget.setModel,
TaurusBaseWritableWidget.resetModel)

useParentModel = Qt.pyqtProperty("bool", TaurusBaseWritableWidget.getUseParentModel,
TaurusBaseWritableWidget.setUseParentModel,
TaurusBaseWritableWidget.resetUseParentModel)
useParentModel = \
Qt.pyqtProperty("bool", TaurusBaseWritableWidget.getUseParentModel,
TaurusBaseWritableWidget.setUseParentModel,
TaurusBaseWritableWidget.resetUseParentModel)

autoApply = Qt.pyqtProperty("bool", TaurusBaseWritableWidget.getAutoApply,
TaurusBaseWritableWidget.setAutoApply,
TaurusBaseWritableWidget.resetAutoApply)

forcedApply = Qt.pyqtProperty("bool", TaurusBaseWritableWidget.getForcedApply,
forcedApply = Qt.pyqtProperty("bool",
TaurusBaseWritableWidget.getForcedApply,
TaurusBaseWritableWidget.setForcedApply,
TaurusBaseWritableWidget.resetForcedApply)

Expand All @@ -285,21 +314,41 @@ def getQtDesignerPluginInfo(cls):

def main():
import sys
from taurus.qt.qtgui.application import TaurusApplication
import taurus.qt.qtgui.application
Application = taurus.qt.qtgui.application.TaurusApplication

app = TaurusApplication()
app = Application.instance()
owns_app = app is None

if owns_app:
import taurus.core.util.argparse
parser = taurus.core.util.argparse.get_taurus_parser()
parser.usage = "%prog [options] <full_attribute_name(s)>"
app = Application(sys.argv, cmd_line_parser=parser,
app_name="Taurus lineedit demo", app_version="1.0",
org_domain="Taurus", org_name="Tango community")

args = app.get_command_line_args()

form = Qt.QWidget()
layout = Qt.QVBoxLayout()
form.setLayout(layout)
for m in ('sys/tg_test/1/double_scalar',
'sys/tg_test/1/double_scalar'
):
if len(args) == 0:
models = ['sys/tg_test/1/double_scalar', 'sys/tg_test/1/double_scalar']
else:
models = args
for model in models:
w = TaurusValueLineEdit()
w.setModel(m)
w.setModel(model)
layout.addWidget(w)
form.resize(300, 50)
form.show()
sys.exit(app.exec_())

if owns_app:
sys.exit(app.exec_())
else:
return form


if __name__ == "__main__":
sys.exit(main())
81 changes: 62 additions & 19 deletions lib/taurus/qt/qtgui/input/taurusspinbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@
##
#############################################################################

"""This module provides a set of basic taurus widgets based on QAbstractSpinBox"""

__all__ = ["TaurusValueSpinBox", "TaurusValueSpinBoxEx"]

__docformat__ = 'restructuredtext'
"""
This module provides a set of basic taurus widgets based on QAbstractSpinBox
"""

from taurus.external.qt import Qt

from tauruslineedit import TaurusValueLineEdit
from taurus.qt.qtgui.icon import getStandardIcon
from taurus.external.pint import Quantity

__all__ = ["TaurusValueSpinBox", "TaurusValueSpinBoxEx"]

__docformat__ = 'restructuredtext'


class TaurusValueSpinBox(Qt.QAbstractSpinBox):

Expand Down Expand Up @@ -64,9 +66,9 @@ def getValue(self):
def keyPressEvent(self, evt):
return self.lineEdit().keyPressEvent(evt)

#-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
# ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
# Mandatory overload from QAbstractSpinBox
#-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
# ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~

def stepBy(self, steps):
self.setValue(self.getValue() + self._getSingleStepQuantity() * steps)
Expand Down Expand Up @@ -99,9 +101,9 @@ def stepEnabled(self):
ret |= Qt.QAbstractSpinBox.StepDownEnabled
return ret

#-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
# ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
# Overload from QAbstractSpinBox
#-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
# ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~

def validate(self, input, pos):
"""Overloaded to use the current validator from the TaurusValueLineEdit,
Expand All @@ -113,9 +115,9 @@ def validate(self, input, pos):
return Qt.QAbstractSpinBox.validate(self, input, pos)
return val.validate(input, pos)

#-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
# ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
# Model related methods
#-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
# ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~

def setModel(self, model):
self.lineEdit().setModel(model)
Expand Down Expand Up @@ -195,6 +197,7 @@ def getQtDesignerPluginInfo(cls):
forcedApply = Qt.pyqtProperty("bool", getForcedApply, setForcedApply,
resetForcedApply)


_S = """
QSpinBox::up-button {
border-width: 0px;
Expand Down Expand Up @@ -247,14 +250,54 @@ def __setattr__(self, name, value):
setattr(self.spinBox, name, value)


if __name__ == "__main__":
def main():
import sys
from taurus.qt.qtgui.application import TaurusApplication
app = TaurusApplication()

w = TaurusValueSpinBox()
w.setModel('sys/tg_test/1/double_scalar')
w.resize(300, 50)
import taurus.qt.qtgui.application
Application = taurus.qt.qtgui.application.TaurusApplication

app = Application.instance()
owns_app = app is None

if owns_app:
import taurus.core.util.argparse
parser = taurus.core.util.argparse.get_taurus_parser()
parser.usage = "%prog [options] <full_attribute_name(s)>"
app = Application(sys.argv, cmd_line_parser=parser,
app_name="Taurus spinbox demo", app_version="1.0",
org_domain="Taurus", org_name="Tango community")

args = app.get_command_line_args()

if len(args) == 0:
w = TaurusValueSpinBox()
w.setModel('sys/tg_test/1/double_scalar')
w.resize(300, 50)
else:
w = Qt.QWidget()
layout = Qt.QGridLayout()
w.setLayout(layout)
for model in args:
label = TaurusValueSpinBox()
label.setModel(model)
layout.addWidget(label)
w.resize(300, 50)
w.show()

sys.exit(app.exec_())
if owns_app:
sys.exit(app.exec_())
else:
return w


if __name__ == "__main__":
main()
# import sys
# from taurus.qt.qtgui.application import TaurusApplication
# app = TaurusApplication()

# w = TaurusValueSpinBox()
# w.setModel('sys/tg_test/1/double_scalar')
# w.resize(300, 50)
# w.show()

# sys.exit(app.exec_())