Skip to content

Commit

Permalink
Merge pull request #3314 from Russ4262/SpinBox_Issues
Browse files Browse the repository at this point in the history
[Path] Fix broken `Gui::QuantitySpinBox` class issues
  • Loading branch information
sliptonic committed Apr 8, 2020
2 parents cf474f6 + ad784bc commit e343e10
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
62 changes: 44 additions & 18 deletions src/Mod/Path/PathScripts/PathGui.py
Expand Up @@ -45,41 +45,68 @@ def translate(context, text, disambig=None):
else:
PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule())


def updateInputField(obj, prop, widget, onBeforeChange=None):
'''updateInputField(obj, prop, widget) ... update obj's property prop with the value of widget.
The property's value is only assigned if the new value differs from the current value.
This prevents onChanged notifications where the value didn't actually change.
Gui::InputField and Gui::QuantitySpinBox widgets are supported - and the property can
be of type Quantity or Float.
If onBeforeChange is specified it is called before a new value is assigned to the property.
Returns True if a new value was assigned, False otherwise (new value is the same as the current).
'''
The property's value is only assigned if the new value differs from the current value.
This prevents onChanged notifications where the value didn't actually change.
Gui::InputField and Gui::QuantitySpinBox widgets are supported - and the property can
be of type Quantity or Float.
If onBeforeChange is specified it is called before a new value is assigned to the property.
Returns True if a new value was assigned, False otherwise (new value is the same as the current).
'''
value = FreeCAD.Units.Quantity(widget.text()).Value
attr = PathUtil.getProperty(obj, prop)
attrValue = attr.Value if hasattr(attr, 'Value') else attr

isDiff = False
if not PathGeom.isRoughly(attrValue, value):
isDiff = True
else:
if hasattr(obj, 'ExpressionEngine'):
noExpr = True
for (prp, expr) in obj.ExpressionEngine:
if prp == prop:
noExpr = False
PathLog.debug('prop = "expression": {} = "{}"'.format(prp, expr))
value = FreeCAD.Units.Quantity(obj.evalExpression(expr)).Value
if not PathGeom.isRoughly(attrValue, value):
isDiff = True
break
if noExpr:
widget.setReadOnly(False)
widget.setStyleSheet("color: black")
else:
widget.setReadOnly(True)
widget.setStyleSheet("color: gray")
widget.update()

if isDiff:
PathLog.debug("updateInputField(%s, %s): %.2f -> %.2f" % (obj.Label, prop, attr, value))
if onBeforeChange:
onBeforeChange(obj)
PathUtil.setProperty(obj, prop, value)
return True

return False


class QuantitySpinBox:
'''Controller class to interface a Gui::QuantitySpinBox.
The spin box gets bound to a given property and supports update in both directions.
QuatitySpinBox(widget, obj, prop, onBeforeChange=None)
widget ... expected to be reference to a Gui::QuantitySpinBox
obj ... document object
prop ... canonical name of the (sub-) property
onBeforeChange ... an optional callback being executed before the value of the property is changed
'''
The spin box gets bound to a given property and supports update in both directions.
QuatitySpinBox(widget, obj, prop, onBeforeChange=None)
widget ... expected to be reference to a Gui::QuantitySpinBox
obj ... document object
prop ... canonical name of the (sub-) property
onBeforeChange ... an optional callback being executed before the value of the property is changed
'''

def __init__(self, widget, obj, prop, onBeforeChange=None):
self.obj = obj
self.widget = widget
self.prop = prop
self.onBeforeChange = onBeforeChange

attr = PathUtil.getProperty(self.obj, self.prop)
if attr is not None:
if hasattr(attr, 'Value'):
Expand All @@ -95,16 +122,16 @@ def expression(self):
if self.valid:
return self.widget.property('expression')
return ''

def setMinimum(self, quantity):
if self.valid:
value = quantity.Value if hasattr(quantity, 'Value') else quantity
self.widget.setProperty('setMinimum', value)

def updateSpinBox(self, quantity=None):
'''updateSpinBox(quantity=None) ... update the display value of the spin box.
If no value is provided the value of the bound property is used.
quantity can be of type Quantity or Float.'''
If no value is provided the value of the bound property is used.
quantity can be of type Quantity or Float.'''
if self.valid:
if quantity is None:
quantity = PathUtil.getProperty(self.obj, self.prop)
Expand All @@ -116,4 +143,3 @@ def updateProperty(self):
if self.valid:
return updateInputField(self.obj, self.prop, self.widget, self.onBeforeChange)
return None

8 changes: 5 additions & 3 deletions src/Mod/Path/PathScripts/PathOpGui.py
Expand Up @@ -157,8 +157,6 @@ def getIcon(self):
else:
return ":/icons/Path-OpActive.svg"

#return self.OpIcon

def getTaskPanelOpPage(self, obj):
'''getTaskPanelOpPage(obj) ... use the stored information to instantiate the receiver op's page controller.'''
mod = importlib.import_module(self.OpPageModule)
Expand Down Expand Up @@ -190,6 +188,7 @@ def setupContextMenu(self, vobj, menu):
action.triggered.connect(self.setEdit)
menu.addAction(action)


class TaskPanelPage(object):
'''Base class for all task panel pages.'''

Expand Down Expand Up @@ -377,7 +376,7 @@ def setupCoolant(self, obj, combo):
combo.clear()
combo.addItems(options)
combo.blockSignals(False)

if hasattr(obj, 'CoolantMode'):
self.selectInComboBox(obj.CoolantMode, combo)

Expand Down Expand Up @@ -704,10 +703,13 @@ def getForm(self):

def haveStartDepth(self):
return PathOp.FeatureDepths & self.features

def haveFinalDepth(self):
return PathOp.FeatureDepths & self.features and not PathOp.FeatureNoFinalDepth & self.features

def haveFinishDepth(self):
return PathOp.FeatureDepths & self.features and PathOp.FeatureFinishDepth & self.features

def haveStepDown(self):
return PathOp.FeatureStepDown & self. features

Expand Down

0 comments on commit e343e10

Please sign in to comment.