Add stack context for function evaluation

CURA-4186
This commit is contained in:
Lipu Fei 2017-08-23 14:39:05 +02:00
parent 7a2493dd2f
commit 759d44dbef
2 changed files with 25 additions and 8 deletions

View File

@ -7,7 +7,7 @@ from UM.Decorators import override
from UM.MimeTypeDatabase import MimeType, MimeTypeDatabase from UM.MimeTypeDatabase import MimeType, MimeTypeDatabase
from UM.Settings.ContainerStack import ContainerStack from UM.Settings.ContainerStack import ContainerStack
from UM.Settings.ContainerRegistry import ContainerRegistry from UM.Settings.ContainerRegistry import ContainerRegistry
from UM.Settings.Interfaces import ContainerInterface from UM.Settings.Interfaces import ContainerInterface, PropertyEvaluationContext
from . import Exceptions from . import Exceptions
from .CuraContainerStack import CuraContainerStack from .CuraContainerStack import CuraContainerStack
@ -57,21 +57,30 @@ class ExtruderStack(CuraContainerStack):
# \throws Exceptions.NoGlobalStackError Raised when trying to get a property from an extruder without # \throws Exceptions.NoGlobalStackError Raised when trying to get a property from an extruder without
# having a next stack set. # having a next stack set.
@override(ContainerStack) @override(ContainerStack)
def getProperty(self, key: str, property_name: str) -> Any: def getProperty(self, key: str, property_name: str, context: Optional[PropertyEvaluationContext] = None) -> Any:
if not self._next_stack: if not self._next_stack:
raise Exceptions.NoGlobalStackError("Extruder {id} is missing the next stack!".format(id = self.id)) raise Exceptions.NoGlobalStackError("Extruder {id} is missing the next stack!".format(id = self.id))
if context is None:
context = PropertyEvaluationContext()
context.pushContainer(self)
if not super().getProperty(key, "settable_per_extruder"): if not super().getProperty(key, "settable_per_extruder"):
return self.getNextStack().getProperty(key, property_name) result = self.getNextStack().getProperty(key, property_name, context)
context.popContainer()
return result
limit_to_extruder = super().getProperty(key, "limit_to_extruder") limit_to_extruder = super().getProperty(key, "limit_to_extruder")
if (limit_to_extruder is not None and limit_to_extruder != "-1") and self.getMetaDataEntry("position") != str(limit_to_extruder): if (limit_to_extruder is not None and limit_to_extruder != "-1") and self.getMetaDataEntry("position") != str(limit_to_extruder):
if str(limit_to_extruder) in self.getNextStack().extruders: if str(limit_to_extruder) in self.getNextStack().extruders:
result = self.getNextStack().extruders[str(limit_to_extruder)].getProperty(key, property_name) result = self.getNextStack().extruders[str(limit_to_extruder)].getProperty(key, property_name, context)
if result is not None: if result is not None:
context.popContainer()
return result return result
return super().getProperty(key, property_name) result = super().getProperty(key, property_name, context)
context.popContainer()
return result
@override(CuraContainerStack) @override(CuraContainerStack)
def _getMachineDefinition(self) -> ContainerInterface: def _getMachineDefinition(self) -> ContainerInterface:

View File

@ -11,6 +11,7 @@ from UM.MimeTypeDatabase import MimeType, MimeTypeDatabase
from UM.Settings.ContainerStack import ContainerStack from UM.Settings.ContainerStack import ContainerStack
from UM.Settings.SettingInstance import InstanceState from UM.Settings.SettingInstance import InstanceState
from UM.Settings.ContainerRegistry import ContainerRegistry from UM.Settings.ContainerRegistry import ContainerRegistry
from UM.Settings.Interfaces import PropertyEvaluationContext
from UM.Logger import Logger from UM.Logger import Logger
from . import Exceptions from . import Exceptions
@ -91,7 +92,7 @@ class GlobalStack(CuraContainerStack):
# #
# \return The value of the property for the specified setting, or None if not found. # \return The value of the property for the specified setting, or None if not found.
@override(ContainerStack) @override(ContainerStack)
def getProperty(self, key: str, property_name: str) -> Any: def getProperty(self, key: str, property_name: str, context: Optional[PropertyEvaluationContext] = None) -> Any:
if not self.definition.findDefinitions(key = key): if not self.definition.findDefinitions(key = key):
return None return None
@ -103,17 +104,24 @@ class GlobalStack(CuraContainerStack):
if resolve is not None: if resolve is not None:
return resolve return resolve
if context is None:
context = PropertyEvaluationContext()
context.pushContainer(self)
# Handle the "limit_to_extruder" property. # Handle the "limit_to_extruder" property.
limit_to_extruder = super().getProperty(key, "limit_to_extruder") limit_to_extruder = super().getProperty(key, "limit_to_extruder")
if limit_to_extruder is not None and limit_to_extruder != "-1" and limit_to_extruder in self._extruders: if limit_to_extruder is not None and limit_to_extruder != "-1" and limit_to_extruder in self._extruders:
if super().getProperty(key, "settable_per_extruder"): if super().getProperty(key, "settable_per_extruder"):
result = self._extruders[str(limit_to_extruder)].getProperty(key, property_name) result = self._extruders[str(limit_to_extruder)].getProperty(key, property_name, context)
if result is not None: if result is not None:
context.popContainer()
return result return result
else: else:
Logger.log("e", "Setting {setting} has limit_to_extruder but is not settable per extruder!", setting = key) Logger.log("e", "Setting {setting} has limit_to_extruder but is not settable per extruder!", setting = key)
return super().getProperty(key, property_name) result = super().getProperty(key, property_name, context)
context.popContainer()
return result
## Overridden from ContainerStack ## Overridden from ContainerStack
# #