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

Commit

Permalink
Merge pull request #669 from srgblnch/spinboxUnits
Browse files Browse the repository at this point in the history
Spinbox was showing units even if the model use fragments to display only the magnitude
  • Loading branch information
Carlos Pascual authored Jan 23, 2018
2 parents ed04ae5 + 8cd10d2 commit eea2f58
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 44 deletions.
15 changes: 15 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,21 @@ def getOperationCallbacks(self):
'''
return []

def getDisplayValue(self, cache=True, fragmentName=None):
"""Reimplemented from class:`TaurusBaseWidget`"""
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The widgets inheriting from this class interact with
# writable models and therefore the fragmentName should fall back to 'wvalue'
# instead of 'rvalue'.
# But changing it now is delicate due to risk of introducing API
# incompatibilities for widgets already assuming the current default.
# So instead of reimplementing it here, the fix was constrained to
# TaurusValueLineEdit.getDisplayValue()
# TODO: Consider reimplementing this to use wvalue by default
return TaurusBaseWidget.getDisplayValue(self, cache=cache,
fragmentName=fragmentName)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

def getValue(self):
'''
This method must be implemented in derived classes to return
Expand Down
97 changes: 72 additions & 25 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,14 +143,15 @@ 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_)):
color = 'orange'
# apply style
style = 'TaurusValueLineEdit {color: %s; font-weight: %s}' %\
(color, weight)
style = ('TaurusValueLineEdit {color: %s; font-weight: %s}' %
(color, weight))
self.setStyleSheet(style)

def wheelEvent(self, evt):
Expand Down Expand Up @@ -197,12 +201,33 @@ def _stepBy(self, v):
value = self.getValue()
self.setValue(value + Quantity(v, value.units))

def getDisplayValue(self, cache=True, fragmentName=None):
"""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 TaurusBaseWritableWidget.getDisplayValue(
self, cache=cache, fragmentName='wvalue')
else:
return TaurusBaseWritableWidget.getDisplayValue(
self, 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 +283,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 +312,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_())

0 comments on commit eea2f58

Please sign in to comment.