MachineManager.py 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import collections
  4. import time
  5. from typing import Any, Callable, List, Dict, TYPE_CHECKING, Optional, cast
  6. from UM.ConfigurationErrorMessage import ConfigurationErrorMessage
  7. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  8. from UM.Settings.InstanceContainer import InstanceContainer
  9. from UM.Settings.Interfaces import ContainerInterface
  10. from UM.Signal import Signal
  11. from PyQt5.QtCore import QObject, pyqtProperty, pyqtSignal, QTimer
  12. from UM.FlameProfiler import pyqtSlot
  13. from UM import Util
  14. from UM.Logger import Logger
  15. from UM.Message import Message
  16. from UM.Settings.SettingFunction import SettingFunction
  17. from UM.Signal import postponeSignals, CompressTechnique
  18. import cura.CuraApplication
  19. from cura.Machines.ContainerNode import ContainerNode #For typing.
  20. from cura.Machines.QualityChangesGroup import QualityChangesGroup #For typing.
  21. from cura.Machines.QualityGroup import QualityGroup #For typing.
  22. from cura.Machines.QualityManager import getMachineDefinitionIDForQualitySearch
  23. from cura.PrinterOutputDevice import PrinterOutputDevice
  24. from cura.PrinterOutput.ConfigurationModel import ConfigurationModel
  25. from cura.PrinterOutput.ExtruderConfigurationModel import ExtruderConfigurationModel
  26. from cura.PrinterOutput.MaterialOutputModel import MaterialOutputModel
  27. from cura.Settings.CuraContainerRegistry import CuraContainerRegistry
  28. from cura.Settings.ExtruderManager import ExtruderManager
  29. from cura.Settings.ExtruderStack import ExtruderStack
  30. from .CuraStackBuilder import CuraStackBuilder
  31. from UM.i18n import i18nCatalog
  32. catalog = i18nCatalog("cura")
  33. if TYPE_CHECKING:
  34. from cura.Settings.CuraContainerStack import CuraContainerStack
  35. from cura.Settings.GlobalStack import GlobalStack
  36. from cura.Machines.MaterialManager import MaterialManager
  37. from cura.Machines.QualityManager import QualityManager
  38. from cura.Machines.VariantManager import VariantManager
  39. class MachineManager(QObject):
  40. def __init__(self, parent: QObject = None) -> None:
  41. super().__init__(parent)
  42. self._active_container_stack = None # type: Optional[ExtruderManager]
  43. self._global_container_stack = None # type: Optional[GlobalStack]
  44. self._current_root_material_id = {} # type: Dict[str, str]
  45. self._current_quality_group = None # type: Optional[QualityGroup]
  46. self._current_quality_changes_group = None # type: Optional[QualityChangesGroup]
  47. self._default_extruder_position = "0" # to be updated when extruders are switched on and off
  48. self.machine_extruder_material_update_dict = collections.defaultdict(list) #type: Dict[str, List[Callable[[], None]]]
  49. self._instance_container_timer = QTimer() #type: QTimer
  50. self._instance_container_timer.setInterval(250)
  51. self._instance_container_timer.setSingleShot(True)
  52. self._instance_container_timer.timeout.connect(self.__emitChangedSignals)
  53. self._application = cura.CuraApplication.CuraApplication.getInstance() #type: cura.CuraApplication.CuraApplication
  54. self._application.globalContainerStackChanged.connect(self._onGlobalContainerChanged)
  55. self._application.getContainerRegistry().containerLoadComplete.connect(self._onContainersChanged)
  56. ## When the global container is changed, active material probably needs to be updated.
  57. self.globalContainerChanged.connect(self.activeMaterialChanged)
  58. self.globalContainerChanged.connect(self.activeVariantChanged)
  59. self.globalContainerChanged.connect(self.activeQualityChanged)
  60. self.globalContainerChanged.connect(self.activeQualityChangesGroupChanged)
  61. self.globalContainerChanged.connect(self.activeQualityGroupChanged)
  62. self._stacks_have_errors = None # type: Optional[bool]
  63. self._empty_container = CuraContainerRegistry.getInstance().getEmptyInstanceContainer() #type: InstanceContainer
  64. self._empty_definition_changes_container = CuraContainerRegistry.getInstance().findContainers(id = "empty_definition_changes")[0] #type: InstanceContainer
  65. self._empty_variant_container = CuraContainerRegistry.getInstance().findContainers(id = "empty_variant")[0] #type: InstanceContainer
  66. self._empty_material_container = CuraContainerRegistry.getInstance().findContainers(id = "empty_material")[0] #type: InstanceContainer
  67. self._empty_quality_container = CuraContainerRegistry.getInstance().findContainers(id = "empty_quality")[0] #type: InstanceContainer
  68. self._empty_quality_changes_container = CuraContainerRegistry.getInstance().findContainers(id = "empty_quality_changes")[0] #type: InstanceContainer
  69. self._onGlobalContainerChanged()
  70. ExtruderManager.getInstance().activeExtruderChanged.connect(self._onActiveExtruderStackChanged)
  71. self._onActiveExtruderStackChanged()
  72. ExtruderManager.getInstance().activeExtruderChanged.connect(self.activeMaterialChanged)
  73. ExtruderManager.getInstance().activeExtruderChanged.connect(self.activeVariantChanged)
  74. ExtruderManager.getInstance().activeExtruderChanged.connect(self.activeQualityChanged)
  75. self.globalContainerChanged.connect(self.activeStackChanged)
  76. self.globalValueChanged.connect(self.activeStackValueChanged)
  77. ExtruderManager.getInstance().activeExtruderChanged.connect(self.activeStackChanged)
  78. self.activeStackChanged.connect(self.activeStackValueChanged)
  79. self._application.getPreferences().addPreference("cura/active_machine", "")
  80. self._printer_output_devices = [] # type: List[PrinterOutputDevice]
  81. self._application.getOutputDeviceManager().outputDevicesChanged.connect(self._onOutputDevicesChanged)
  82. # There might already be some output devices by the time the signal is connected
  83. self._onOutputDevicesChanged()
  84. self._current_printer_configuration = ConfigurationModel() # Indicates the current configuration setup in this printer
  85. self.activeMaterialChanged.connect(self._onCurrentConfigurationChanged)
  86. self.activeVariantChanged.connect(self._onCurrentConfigurationChanged)
  87. # Force to compute the current configuration
  88. self._onCurrentConfigurationChanged()
  89. self._application.callLater(self.setInitialActiveMachine)
  90. self._material_incompatible_message = Message(catalog.i18nc("@info:status",
  91. "The selected material is incompatible with the selected machine or configuration."),
  92. title = catalog.i18nc("@info:title", "Incompatible Material")) #type: Message
  93. containers = CuraContainerRegistry.getInstance().findInstanceContainers(id = self.activeMaterialId) #type: List[InstanceContainer]
  94. if containers:
  95. containers[0].nameChanged.connect(self._onMaterialNameChanged)
  96. self._material_manager = self._application.getMaterialManager() #type: MaterialManager
  97. self._variant_manager = self._application.getVariantManager() #type: VariantManager
  98. self._quality_manager = self._application.getQualityManager() #type: QualityManager
  99. # When the materials lookup table gets updated, it can mean that a material has its name changed, which should
  100. # be reflected on the GUI. This signal emission makes sure that it happens.
  101. self._material_manager.materialsUpdated.connect(self.rootMaterialChanged)
  102. # When the materials get updated, it can be that an activated material's diameter gets changed. In that case,
  103. # a material update should be triggered to make sure that the machine still has compatible materials activated.
  104. self._material_manager.materialsUpdated.connect(self._updateUponMaterialMetadataChange)
  105. self.rootMaterialChanged.connect(self._onRootMaterialChanged)
  106. # Emit the printerConnectedStatusChanged when either globalContainerChanged or outputDevicesChanged are emitted
  107. self.globalContainerChanged.connect(self.printerConnectedStatusChanged)
  108. self.outputDevicesChanged.connect(self.printerConnectedStatusChanged)
  109. activeQualityGroupChanged = pyqtSignal()
  110. activeQualityChangesGroupChanged = pyqtSignal()
  111. globalContainerChanged = pyqtSignal() # Emitted whenever the global stack is changed (ie: when changing between printers, changing a global profile, but not when changing a value)
  112. activeMaterialChanged = pyqtSignal()
  113. activeVariantChanged = pyqtSignal()
  114. activeQualityChanged = pyqtSignal()
  115. activeStackChanged = pyqtSignal() # Emitted whenever the active stack is changed (ie: when changing between extruders, changing a profile, but not when changing a value)
  116. extruderChanged = pyqtSignal()
  117. globalValueChanged = pyqtSignal() # Emitted whenever a value inside global container is changed.
  118. activeStackValueChanged = pyqtSignal() # Emitted whenever a value inside the active stack is changed.
  119. activeStackValidationChanged = pyqtSignal() # Emitted whenever a validation inside active container is changed
  120. stacksValidationChanged = pyqtSignal() # Emitted whenever a validation is changed
  121. numberExtrudersEnabledChanged = pyqtSignal() # Emitted when the number of extruders that are enabled changed
  122. blurSettings = pyqtSignal() # Emitted to force fields in the advanced sidebar to un-focus, so they update properly
  123. outputDevicesChanged = pyqtSignal()
  124. currentConfigurationChanged = pyqtSignal() # Emitted every time the current configurations of the machine changes
  125. printerConnectedStatusChanged = pyqtSignal() # Emitted every time the active machine change or the outputdevices change
  126. rootMaterialChanged = pyqtSignal()
  127. def setInitialActiveMachine(self) -> None:
  128. active_machine_id = self._application.getPreferences().getValue("cura/active_machine")
  129. if active_machine_id != "" and CuraContainerRegistry.getInstance().findContainerStacksMetadata(id = active_machine_id):
  130. # An active machine was saved, so restore it.
  131. self.setActiveMachine(active_machine_id)
  132. def _onOutputDevicesChanged(self) -> None:
  133. self._printer_output_devices = []
  134. for printer_output_device in self._application.getOutputDeviceManager().getOutputDevices():
  135. if isinstance(printer_output_device, PrinterOutputDevice):
  136. self._printer_output_devices.append(printer_output_device)
  137. self.outputDevicesChanged.emit()
  138. @pyqtProperty(QObject, notify = currentConfigurationChanged)
  139. def currentConfiguration(self) -> ConfigurationModel:
  140. return self._current_printer_configuration
  141. def _onCurrentConfigurationChanged(self) -> None:
  142. if not self._global_container_stack:
  143. return
  144. # Create the configuration model with the current data in Cura
  145. self._current_printer_configuration.printerType = self._global_container_stack.definition.getName()
  146. self._current_printer_configuration.extruderConfigurations = []
  147. for extruder in self._global_container_stack.extruders.values():
  148. extruder_configuration = ExtruderConfigurationModel()
  149. # For compare just the GUID is needed at this moment
  150. mat_type = extruder.material.getMetaDataEntry("material") if extruder.material != self._empty_material_container else None
  151. mat_guid = extruder.material.getMetaDataEntry("GUID") if extruder.material != self._empty_material_container else None
  152. mat_color = extruder.material.getMetaDataEntry("color_name") if extruder.material != self._empty_material_container else None
  153. mat_brand = extruder.material.getMetaDataEntry("brand") if extruder.material != self._empty_material_container else None
  154. mat_name = extruder.material.getMetaDataEntry("name") if extruder.material != self._empty_material_container else None
  155. material_model = MaterialOutputModel(mat_guid, mat_type, mat_color, mat_brand, mat_name)
  156. extruder_configuration.position = int(extruder.getMetaDataEntry("position"))
  157. extruder_configuration.material = material_model
  158. extruder_configuration.hotendID = extruder.variant.getName() if extruder.variant != self._empty_variant_container else None
  159. self._current_printer_configuration.extruderConfigurations.append(extruder_configuration)
  160. self._current_printer_configuration.buildplateConfiguration = self._global_container_stack.getProperty("machine_buildplate_type", "value") if self._global_container_stack.variant != self._empty_variant_container else None
  161. self.currentConfigurationChanged.emit()
  162. @pyqtSlot(QObject, result = bool)
  163. def matchesConfiguration(self, configuration: ConfigurationModel) -> bool:
  164. return self._current_printer_configuration == configuration
  165. @pyqtProperty("QVariantList", notify = outputDevicesChanged)
  166. def printerOutputDevices(self) -> List[PrinterOutputDevice]:
  167. return self._printer_output_devices
  168. @pyqtProperty(int, constant=True)
  169. def totalNumberOfSettings(self) -> int:
  170. general_definition_containers = CuraContainerRegistry.getInstance().findDefinitionContainers(id = "fdmprinter")
  171. if not general_definition_containers:
  172. return 0
  173. return len(general_definition_containers[0].getAllKeys())
  174. def _onGlobalContainerChanged(self) -> None:
  175. if self._global_container_stack:
  176. try:
  177. self._global_container_stack.nameChanged.disconnect(self._onMachineNameChanged)
  178. except TypeError: # pyQtSignal gives a TypeError when disconnecting from something that was already disconnected.
  179. pass
  180. try:
  181. self._global_container_stack.containersChanged.disconnect(self._onContainersChanged)
  182. except TypeError:
  183. pass
  184. try:
  185. self._global_container_stack.propertyChanged.disconnect(self._onPropertyChanged)
  186. except TypeError:
  187. pass
  188. for extruder_stack in ExtruderManager.getInstance().getActiveExtruderStacks():
  189. extruder_stack.propertyChanged.disconnect(self._onPropertyChanged)
  190. extruder_stack.containersChanged.disconnect(self._onContainersChanged)
  191. # Update the local global container stack reference
  192. self._global_container_stack = self._application.getGlobalContainerStack()
  193. if self._global_container_stack:
  194. self.updateDefaultExtruder()
  195. self.updateNumberExtrudersEnabled()
  196. self.globalContainerChanged.emit()
  197. # after switching the global stack we reconnect all the signals and set the variant and material references
  198. if self._global_container_stack:
  199. self._application.getPreferences().setValue("cura/active_machine", self._global_container_stack.getId())
  200. self._global_container_stack.nameChanged.connect(self._onMachineNameChanged)
  201. self._global_container_stack.containersChanged.connect(self._onContainersChanged)
  202. self._global_container_stack.propertyChanged.connect(self._onPropertyChanged)
  203. # Global stack can have only a variant if it is a buildplate
  204. global_variant = self._global_container_stack.variant
  205. if global_variant != self._empty_variant_container:
  206. if global_variant.getMetaDataEntry("hardware_type") != "buildplate":
  207. self._global_container_stack.setVariant(self._empty_variant_container)
  208. # set the global material to empty as we now use the extruder stack at all times - CURA-4482
  209. global_material = self._global_container_stack.material
  210. if global_material != self._empty_material_container:
  211. self._global_container_stack.setMaterial(self._empty_material_container)
  212. # Listen for changes on all extruder stacks
  213. for extruder_stack in ExtruderManager.getInstance().getActiveExtruderStacks():
  214. extruder_stack.propertyChanged.connect(self._onPropertyChanged)
  215. extruder_stack.containersChanged.connect(self._onContainersChanged)
  216. if self._global_container_stack.getId() in self.machine_extruder_material_update_dict:
  217. for func in self.machine_extruder_material_update_dict[self._global_container_stack.getId()]:
  218. self._application.callLater(func)
  219. del self.machine_extruder_material_update_dict[self._global_container_stack.getId()]
  220. self.activeQualityGroupChanged.emit()
  221. def _onActiveExtruderStackChanged(self) -> None:
  222. self.blurSettings.emit() # Ensure no-one has focus.
  223. old_active_container_stack = self._active_container_stack
  224. self._active_container_stack = ExtruderManager.getInstance().getActiveExtruderStack()
  225. if old_active_container_stack != self._active_container_stack:
  226. # Many methods and properties related to the active quality actually depend
  227. # on _active_container_stack. If it changes, then the properties change.
  228. self.activeQualityChanged.emit()
  229. def __emitChangedSignals(self) -> None:
  230. self.activeQualityChanged.emit()
  231. self.activeVariantChanged.emit()
  232. self.activeMaterialChanged.emit()
  233. self.rootMaterialChanged.emit()
  234. def _onContainersChanged(self, container: ContainerInterface) -> None:
  235. self._instance_container_timer.start()
  236. def _onPropertyChanged(self, key: str, property_name: str) -> None:
  237. if property_name == "value":
  238. # Notify UI items, such as the "changed" star in profile pull down menu.
  239. self.activeStackValueChanged.emit()
  240. ## Given a global_stack, make sure that it's all valid by searching for this quality group and applying it again
  241. def _initMachineState(self, global_stack: "CuraContainerStack") -> None:
  242. material_dict = {}
  243. for position, extruder in global_stack.extruders.items():
  244. material_dict[position] = extruder.material.getMetaDataEntry("base_file")
  245. self._current_root_material_id = material_dict
  246. # Update materials to make sure that the diameters match with the machine's
  247. for position in global_stack.extruders:
  248. self.updateMaterialWithVariant(position)
  249. global_quality = global_stack.quality
  250. quality_type = global_quality.getMetaDataEntry("quality_type")
  251. global_quality_changes = global_stack.qualityChanges
  252. global_quality_changes_name = global_quality_changes.getName()
  253. # Try to set the same quality/quality_changes as the machine specified.
  254. # If the quality/quality_changes is not available, switch to the default or the first quality that's available.
  255. same_quality_found = False
  256. quality_groups = self._application.getQualityManager().getQualityGroups(global_stack)
  257. if global_quality_changes.getId() != "empty_quality_changes":
  258. quality_changes_groups = self._application.getQualityManager().getQualityChangesGroups(global_stack)
  259. new_quality_changes_group = quality_changes_groups.get(global_quality_changes_name)
  260. if new_quality_changes_group is not None:
  261. self._setQualityChangesGroup(new_quality_changes_group)
  262. same_quality_found = True
  263. Logger.log("i", "Machine '%s' quality changes set to '%s'",
  264. global_stack.getName(), new_quality_changes_group.name)
  265. else:
  266. new_quality_group = quality_groups.get(quality_type)
  267. if new_quality_group is not None:
  268. self._setQualityGroup(new_quality_group, empty_quality_changes = True)
  269. same_quality_found = True
  270. Logger.log("i", "Machine '%s' quality set to '%s'",
  271. global_stack.getName(), new_quality_group.quality_type)
  272. # Could not find the specified quality/quality_changes, switch to the preferred quality if available,
  273. # otherwise the first quality that's available, otherwise empty (not supported).
  274. if not same_quality_found:
  275. Logger.log("i", "Machine '%s' could not find quality_type '%s' and quality_changes '%s'. "
  276. "Available quality types are [%s]. Switching to default quality.",
  277. global_stack.getName(), quality_type, global_quality_changes_name,
  278. ", ".join(quality_groups.keys()))
  279. preferred_quality_type = global_stack.getMetaDataEntry("preferred_quality_type")
  280. quality_group = quality_groups.get(preferred_quality_type)
  281. if quality_group is None:
  282. if quality_groups:
  283. quality_group = list(quality_groups.values())[0]
  284. self._setQualityGroup(quality_group, empty_quality_changes = True)
  285. @pyqtSlot(str)
  286. def setActiveMachine(self, stack_id: str) -> None:
  287. self.blurSettings.emit() # Ensure no-one has focus.
  288. container_registry = CuraContainerRegistry.getInstance()
  289. containers = container_registry.findContainerStacks(id = stack_id)
  290. if not containers:
  291. return
  292. global_stack = containers[0]
  293. if not global_stack.isValid():
  294. # Mark global stack as invalid
  295. ConfigurationErrorMessage.getInstance().addFaultyContainers(global_stack.getId())
  296. return # We're done here
  297. ExtruderManager.getInstance().setActiveExtruderIndex(0) # Switch to first extruder
  298. self._global_container_stack = global_stack
  299. self._application.setGlobalContainerStack(global_stack)
  300. ExtruderManager.getInstance()._globalContainerStackChanged()
  301. self._initMachineState(containers[0])
  302. self._onGlobalContainerChanged()
  303. self.__emitChangedSignals()
  304. ## Given a definition id, return the machine with this id.
  305. # Optional: add a list of keys and values to filter the list of machines with the given definition id
  306. # \param definition_id \type{str} definition id that needs to look for
  307. # \param metadata_filter \type{dict} list of metadata keys and values used for filtering
  308. @staticmethod
  309. def getMachine(definition_id: str, metadata_filter: Dict[str, str] = None) -> Optional["GlobalStack"]:
  310. machines = CuraContainerRegistry.getInstance().findContainerStacks(type = "machine", **metadata_filter)
  311. for machine in machines:
  312. if machine.definition.getId() == definition_id:
  313. return machine
  314. return None
  315. @pyqtSlot(str, str)
  316. def addMachine(self, name: str, definition_id: str) -> None:
  317. new_stack = CuraStackBuilder.createMachine(name, definition_id)
  318. if new_stack:
  319. # Instead of setting the global container stack here, we set the active machine and so the signals are emitted
  320. self.setActiveMachine(new_stack.getId())
  321. else:
  322. Logger.log("w", "Failed creating a new machine!")
  323. def _checkStacksHaveErrors(self) -> bool:
  324. time_start = time.time()
  325. if self._global_container_stack is None: #No active machine.
  326. return False
  327. if self._global_container_stack.hasErrors():
  328. Logger.log("d", "Checking global stack for errors took %0.2f s and we found an error" % (time.time() - time_start))
  329. return True
  330. # Not a very pretty solution, but the extruder manager doesn't really know how many extruders there are
  331. machine_extruder_count = self._global_container_stack.getProperty("machine_extruder_count", "value")
  332. extruder_stacks = ExtruderManager.getInstance().getMachineExtruders(self._global_container_stack.getId())
  333. count = 1 # we start with the global stack
  334. for stack in extruder_stacks:
  335. md = stack.getMetaData()
  336. if "position" in md and int(md["position"]) >= machine_extruder_count:
  337. continue
  338. count += 1
  339. if stack.hasErrors():
  340. Logger.log("d", "Checking %s stacks for errors took %.2f s and we found an error in stack [%s]" % (count, time.time() - time_start, str(stack)))
  341. return True
  342. Logger.log("d", "Checking %s stacks for errors took %.2f s" % (count, time.time() - time_start))
  343. return False
  344. ## Check if the global_container has instances in the user container
  345. @pyqtProperty(bool, notify = activeStackValueChanged)
  346. def hasUserSettings(self) -> bool:
  347. if not self._global_container_stack:
  348. return False
  349. if self._global_container_stack.getTop().findInstances():
  350. return True
  351. stacks = list(ExtruderManager.getInstance().getMachineExtruders(self._global_container_stack.getId()))
  352. for stack in stacks:
  353. if stack.getTop().findInstances():
  354. return True
  355. return False
  356. @pyqtProperty(int, notify = activeStackValueChanged)
  357. def numUserSettings(self) -> int:
  358. if not self._global_container_stack:
  359. return 0
  360. num_user_settings = 0
  361. num_user_settings += len(self._global_container_stack.getTop().findInstances())
  362. stacks = list(ExtruderManager.getInstance().getMachineExtruders(self._global_container_stack.getId()))
  363. for stack in stacks:
  364. num_user_settings += len(stack.getTop().findInstances())
  365. return num_user_settings
  366. ## Delete a user setting from the global stack and all extruder stacks.
  367. # \param key \type{str} the name of the key to delete
  368. @pyqtSlot(str)
  369. def clearUserSettingAllCurrentStacks(self, key: str) -> None:
  370. if not self._global_container_stack:
  371. return
  372. send_emits_containers = []
  373. top_container = self._global_container_stack.getTop()
  374. top_container.removeInstance(key, postpone_emit=True)
  375. send_emits_containers.append(top_container)
  376. linked = not self._global_container_stack.getProperty(key, "settable_per_extruder") or \
  377. self._global_container_stack.getProperty(key, "limit_to_extruder") != "-1"
  378. if not linked:
  379. stack = ExtruderManager.getInstance().getActiveExtruderStack()
  380. stacks = [stack]
  381. else:
  382. stacks = ExtruderManager.getInstance().getMachineExtruders(self._global_container_stack.getId())
  383. for stack in stacks:
  384. if stack is not None:
  385. container = stack.getTop()
  386. container.removeInstance(key, postpone_emit=True)
  387. send_emits_containers.append(container)
  388. for container in send_emits_containers:
  389. container.sendPostponedEmits()
  390. ## Check if none of the stacks contain error states
  391. # Note that the _stacks_have_errors is cached due to performance issues
  392. # Calling _checkStack(s)ForErrors on every change is simply too expensive
  393. @pyqtProperty(bool, notify = stacksValidationChanged)
  394. def stacksHaveErrors(self) -> bool:
  395. return bool(self._stacks_have_errors)
  396. @pyqtProperty(str, notify = globalContainerChanged)
  397. def activeMachineDefinitionName(self) -> str:
  398. if self._global_container_stack:
  399. return self._global_container_stack.definition.getName()
  400. return ""
  401. @pyqtProperty(str, notify = globalContainerChanged)
  402. def activeMachineName(self) -> str:
  403. if self._global_container_stack:
  404. return self._global_container_stack.getName()
  405. return ""
  406. @pyqtProperty(str, notify = globalContainerChanged)
  407. def activeMachineId(self) -> str:
  408. if self._global_container_stack:
  409. return self._global_container_stack.getId()
  410. return ""
  411. @pyqtProperty(bool, notify = printerConnectedStatusChanged)
  412. def printerConnected(self):
  413. return bool(self._printer_output_devices)
  414. @pyqtProperty(str, notify = printerConnectedStatusChanged)
  415. def activeMachineNetworkKey(self) -> str:
  416. if self._global_container_stack:
  417. return self._global_container_stack.getMetaDataEntry("um_network_key", "")
  418. return ""
  419. @pyqtProperty(str, notify = printerConnectedStatusChanged)
  420. def activeMachineNetworkGroupName(self) -> str:
  421. if self._global_container_stack:
  422. return self._global_container_stack.getMetaDataEntry("connect_group_name", "")
  423. return ""
  424. @pyqtProperty(QObject, notify = globalContainerChanged)
  425. def activeMachine(self) -> Optional["GlobalStack"]:
  426. return self._global_container_stack
  427. @pyqtProperty(str, notify = activeStackChanged)
  428. def activeStackId(self) -> str:
  429. if self._active_container_stack:
  430. return self._active_container_stack.getId()
  431. return ""
  432. @pyqtProperty(QObject, notify = activeStackChanged)
  433. def activeStack(self) -> Optional["ExtruderStack"]:
  434. return self._active_container_stack
  435. @pyqtProperty(str, notify=activeMaterialChanged)
  436. def activeMaterialId(self) -> str:
  437. if self._active_container_stack:
  438. material = self._active_container_stack.material
  439. if material:
  440. return material.getId()
  441. return ""
  442. ## Gets a dict with the active materials ids set in all extruder stacks and the global stack
  443. # (when there is one extruder, the material is set in the global stack)
  444. #
  445. # \return The material ids in all stacks
  446. @pyqtProperty("QVariantMap", notify = activeMaterialChanged)
  447. def allActiveMaterialIds(self) -> Dict[str, str]:
  448. result = {}
  449. active_stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
  450. for stack in active_stacks:
  451. material_container = stack.material
  452. if not material_container:
  453. continue
  454. result[stack.getId()] = material_container.getId()
  455. return result
  456. ## Gets the layer height of the currently active quality profile.
  457. #
  458. # This is indicated together with the name of the active quality profile.
  459. #
  460. # \return The layer height of the currently active quality profile. If
  461. # there is no quality profile, this returns 0.
  462. @pyqtProperty(float, notify = activeQualityGroupChanged)
  463. def activeQualityLayerHeight(self) -> float:
  464. if not self._global_container_stack:
  465. return 0
  466. if self._current_quality_changes_group:
  467. value = self._global_container_stack.getRawProperty("layer_height", "value", skip_until_container = self._global_container_stack.qualityChanges.getId())
  468. if isinstance(value, SettingFunction):
  469. value = value(self._global_container_stack)
  470. return value
  471. elif self._current_quality_group:
  472. value = self._global_container_stack.getRawProperty("layer_height", "value", skip_until_container = self._global_container_stack.quality.getId())
  473. if isinstance(value, SettingFunction):
  474. value = value(self._global_container_stack)
  475. return value
  476. return 0
  477. @pyqtProperty(str, notify = activeVariantChanged)
  478. def globalVariantName(self) -> str:
  479. if self._global_container_stack:
  480. variant = self._global_container_stack.variant
  481. if variant and not isinstance(variant, type(self._empty_variant_container)):
  482. return variant.getName()
  483. return ""
  484. @pyqtProperty(str, notify = activeQualityGroupChanged)
  485. def activeQualityType(self) -> str:
  486. quality_type = ""
  487. if self._active_container_stack:
  488. if self._current_quality_group:
  489. quality_type = self._current_quality_group.quality_type
  490. return quality_type
  491. @pyqtProperty(bool, notify = activeQualityGroupChanged)
  492. def isActiveQualitySupported(self) -> bool:
  493. is_supported = False
  494. if self._global_container_stack:
  495. if self._current_quality_group:
  496. is_supported = self._current_quality_group.is_available
  497. return is_supported
  498. ## Returns whether there is anything unsupported in the current set-up.
  499. #
  500. # The current set-up signifies the global stack and all extruder stacks,
  501. # so this indicates whether there is any container in any of the container
  502. # stacks that is not marked as supported.
  503. @pyqtProperty(bool, notify = activeQualityChanged)
  504. def isCurrentSetupSupported(self) -> bool:
  505. if not self._global_container_stack:
  506. return False
  507. for stack in [self._global_container_stack] + list(self._global_container_stack.extruders.values()):
  508. for container in stack.getContainers():
  509. if not container:
  510. return False
  511. if not Util.parseBool(container.getMetaDataEntry("supported", True)):
  512. return False
  513. return True
  514. ## Check if a container is read_only
  515. @pyqtSlot(str, result = bool)
  516. def isReadOnly(self, container_id: str) -> bool:
  517. return CuraContainerRegistry.getInstance().isReadOnly(container_id)
  518. ## Copy the value of the setting of the current extruder to all other extruders as well as the global container.
  519. @pyqtSlot(str)
  520. def copyValueToExtruders(self, key: str) -> None:
  521. if self._active_container_stack is None or self._global_container_stack is None:
  522. return
  523. new_value = self._active_container_stack.getProperty(key, "value")
  524. extruder_stacks = [stack for stack in ExtruderManager.getInstance().getMachineExtruders(self._global_container_stack.getId())]
  525. # check in which stack the value has to be replaced
  526. for extruder_stack in extruder_stacks:
  527. if extruder_stack != self._active_container_stack and extruder_stack.getProperty(key, "value") != new_value:
  528. extruder_stack.userChanges.setProperty(key, "value", new_value) # TODO: nested property access, should be improved
  529. ## Copy the value of all manually changed settings of the current extruder to all other extruders.
  530. @pyqtSlot()
  531. def copyAllValuesToExtruders(self) -> None:
  532. if self._active_container_stack is None or self._global_container_stack is None:
  533. return
  534. extruder_stacks = list(self._global_container_stack.extruders.values())
  535. for extruder_stack in extruder_stacks:
  536. if extruder_stack != self._active_container_stack:
  537. for key in self._active_container_stack.userChanges.getAllKeys():
  538. new_value = self._active_container_stack.getProperty(key, "value")
  539. # check if the value has to be replaced
  540. extruder_stack.userChanges.setProperty(key, "value", new_value)
  541. @pyqtProperty(str, notify = activeVariantChanged)
  542. def activeVariantName(self) -> str:
  543. if self._active_container_stack:
  544. variant = self._active_container_stack.variant
  545. if variant:
  546. return variant.getName()
  547. return ""
  548. @pyqtProperty(str, notify = activeVariantChanged)
  549. def activeVariantId(self) -> str:
  550. if self._active_container_stack:
  551. variant = self._active_container_stack.variant
  552. if variant:
  553. return variant.getId()
  554. return ""
  555. @pyqtProperty(str, notify = activeVariantChanged)
  556. def activeVariantBuildplateName(self) -> str:
  557. if self._global_container_stack:
  558. variant = self._global_container_stack.variant
  559. if variant:
  560. return variant.getName()
  561. return ""
  562. @pyqtProperty(str, notify = globalContainerChanged)
  563. def activeDefinitionId(self) -> str:
  564. if self._global_container_stack:
  565. return self._global_container_stack.definition.id
  566. return ""
  567. ## Get the Definition ID to use to select quality profiles for the currently active machine
  568. # \returns DefinitionID (string) if found, empty string otherwise
  569. @pyqtProperty(str, notify = globalContainerChanged)
  570. def activeQualityDefinitionId(self) -> str:
  571. if self._global_container_stack:
  572. return getMachineDefinitionIDForQualitySearch(self._global_container_stack.definition)
  573. return ""
  574. ## Gets how the active definition calls variants
  575. # Caveat: per-definition-variant-title is currently not translated (though the fallback is)
  576. @pyqtProperty(str, notify = globalContainerChanged)
  577. def activeDefinitionVariantsName(self) -> str:
  578. fallback_title = catalog.i18nc("@label", "Nozzle")
  579. if self._global_container_stack:
  580. return self._global_container_stack.definition.getMetaDataEntry("variants_name", fallback_title)
  581. return fallback_title
  582. @pyqtSlot(str, str)
  583. def renameMachine(self, machine_id: str, new_name: str) -> None:
  584. container_registry = CuraContainerRegistry.getInstance()
  585. machine_stack = container_registry.findContainerStacks(id = machine_id)
  586. if machine_stack:
  587. new_name = container_registry.createUniqueName("machine", machine_stack[0].getName(), new_name, machine_stack[0].definition.getName())
  588. machine_stack[0].setName(new_name)
  589. self.globalContainerChanged.emit()
  590. @pyqtSlot(str)
  591. def removeMachine(self, machine_id: str) -> None:
  592. # If the machine that is being removed is the currently active machine, set another machine as the active machine.
  593. activate_new_machine = (self._global_container_stack and self._global_container_stack.getId() == machine_id)
  594. # activate a new machine before removing a machine because this is safer
  595. if activate_new_machine:
  596. machine_stacks = CuraContainerRegistry.getInstance().findContainerStacksMetadata(type = "machine")
  597. other_machine_stacks = [s for s in machine_stacks if s["id"] != machine_id]
  598. if other_machine_stacks:
  599. self.setActiveMachine(other_machine_stacks[0]["id"])
  600. metadata = CuraContainerRegistry.getInstance().findContainerStacksMetadata(id = machine_id)[0]
  601. network_key = metadata["um_network_key"] if "um_network_key" in metadata else None
  602. ExtruderManager.getInstance().removeMachineExtruders(machine_id)
  603. containers = CuraContainerRegistry.getInstance().findInstanceContainersMetadata(type = "user", machine = machine_id)
  604. for container in containers:
  605. CuraContainerRegistry.getInstance().removeContainer(container["id"])
  606. CuraContainerRegistry.getInstance().removeContainer(machine_id)
  607. # If the printer that is being removed is a network printer, the hidden printers have to be also removed
  608. if network_key:
  609. metadata_filter = {"um_network_key": network_key}
  610. hidden_containers = CuraContainerRegistry.getInstance().findContainerStacks(type = "machine", **metadata_filter)
  611. if hidden_containers:
  612. # This reuses the method and remove all printers recursively
  613. self.removeMachine(hidden_containers[0].getId())
  614. @pyqtProperty(bool, notify = globalContainerChanged)
  615. def hasMaterials(self) -> bool:
  616. if self._global_container_stack:
  617. return Util.parseBool(self._global_container_stack.getMetaDataEntry("has_materials", False))
  618. return False
  619. @pyqtProperty(bool, notify = globalContainerChanged)
  620. def hasVariants(self) -> bool:
  621. if self._global_container_stack:
  622. return Util.parseBool(self._global_container_stack.getMetaDataEntry("has_variants", False))
  623. return False
  624. @pyqtProperty(bool, notify = globalContainerChanged)
  625. def hasVariantBuildplates(self) -> bool:
  626. if self._global_container_stack:
  627. return Util.parseBool(self._global_container_stack.getMetaDataEntry("has_variant_buildplates", False))
  628. return False
  629. ## The selected buildplate is compatible if it is compatible with all the materials in all the extruders
  630. @pyqtProperty(bool, notify = activeMaterialChanged)
  631. def variantBuildplateCompatible(self) -> bool:
  632. if not self._global_container_stack:
  633. return True
  634. buildplate_compatible = True # It is compatible by default
  635. extruder_stacks = self._global_container_stack.extruders.values()
  636. for stack in extruder_stacks:
  637. if not stack.isEnabled:
  638. continue
  639. material_container = stack.material
  640. if material_container == self._empty_material_container:
  641. continue
  642. if material_container.getMetaDataEntry("buildplate_compatible"):
  643. buildplate_compatible = buildplate_compatible and material_container.getMetaDataEntry("buildplate_compatible")[self.activeVariantBuildplateName]
  644. return buildplate_compatible
  645. ## The selected buildplate is usable if it is usable for all materials OR it is compatible for one but not compatible
  646. # for the other material but the buildplate is still usable
  647. @pyqtProperty(bool, notify = activeMaterialChanged)
  648. def variantBuildplateUsable(self) -> bool:
  649. if not self._global_container_stack:
  650. return True
  651. # Here the next formula is being calculated:
  652. # result = (not (material_left_compatible and material_right_compatible)) and
  653. # (material_left_compatible or material_left_usable) and
  654. # (material_right_compatible or material_right_usable)
  655. result = not self.variantBuildplateCompatible
  656. extruder_stacks = self._global_container_stack.extruders.values()
  657. for stack in extruder_stacks:
  658. material_container = stack.material
  659. if material_container == self._empty_material_container:
  660. continue
  661. buildplate_compatible = material_container.getMetaDataEntry("buildplate_compatible")[self.activeVariantBuildplateName] if material_container.getMetaDataEntry("buildplate_compatible") else True
  662. buildplate_usable = material_container.getMetaDataEntry("buildplate_recommended")[self.activeVariantBuildplateName] if material_container.getMetaDataEntry("buildplate_recommended") else True
  663. result = result and (buildplate_compatible or buildplate_usable)
  664. return result
  665. ## Get the Definition ID of a machine (specified by ID)
  666. # \param machine_id string machine id to get the definition ID of
  667. # \returns DefinitionID if found, None otherwise
  668. @pyqtSlot(str, result = str)
  669. def getDefinitionByMachineId(self, machine_id: str) -> Optional[str]:
  670. containers = CuraContainerRegistry.getInstance().findContainerStacks(id = machine_id)
  671. if containers:
  672. return containers[0].definition.getId()
  673. return None
  674. def getIncompatibleSettingsOnEnabledExtruders(self, container: InstanceContainer) -> List[str]:
  675. if self._global_container_stack is None:
  676. return []
  677. extruder_count = self._global_container_stack.getProperty("machine_extruder_count", "value")
  678. result = [] # type: List[str]
  679. for setting_instance in container.findInstances():
  680. setting_key = setting_instance.definition.key
  681. setting_enabled = self._global_container_stack.getProperty(setting_key, "enabled")
  682. if not setting_enabled:
  683. # A setting is not visible anymore
  684. result.append(setting_key)
  685. Logger.log("d", "Reset setting [%s] from [%s] because the setting is no longer enabled", setting_key, container)
  686. continue
  687. if not self._global_container_stack.getProperty(setting_key, "type") in ("extruder", "optional_extruder"):
  688. continue
  689. old_value = container.getProperty(setting_key, "value")
  690. if int(old_value) < 0:
  691. continue
  692. if int(old_value) >= extruder_count or not self._global_container_stack.extruders[str(old_value)].isEnabled:
  693. result.append(setting_key)
  694. Logger.log("d", "Reset setting [%s] in [%s] because its old value [%s] is no longer valid", setting_key, container, old_value)
  695. return result
  696. ## Update extruder number to a valid value when the number of extruders are changed, or when an extruder is changed
  697. def correctExtruderSettings(self) -> None:
  698. if self._global_container_stack is None:
  699. return
  700. for setting_key in self.getIncompatibleSettingsOnEnabledExtruders(self._global_container_stack.userChanges):
  701. self._global_container_stack.userChanges.removeInstance(setting_key)
  702. add_user_changes = self.getIncompatibleSettingsOnEnabledExtruders(self._global_container_stack.qualityChanges)
  703. for setting_key in add_user_changes:
  704. # Apply quality changes that are incompatible to user changes, so we do not change the quality changes itself.
  705. self._global_container_stack.userChanges.setProperty(setting_key, "value", self._default_extruder_position)
  706. if add_user_changes:
  707. caution_message = Message(catalog.i18nc(
  708. "@info:generic",
  709. "Settings have been changed to match the current availability of extruders: [%s]" % ", ".join(add_user_changes)),
  710. lifetime=0,
  711. title = catalog.i18nc("@info:title", "Settings updated"))
  712. caution_message.show()
  713. ## Set the amount of extruders on the active machine (global stack)
  714. # \param extruder_count int the number of extruders to set
  715. def setActiveMachineExtruderCount(self, extruder_count: int) -> None:
  716. if self._global_container_stack is None:
  717. return
  718. extruder_manager = self._application.getExtruderManager()
  719. definition_changes_container = self._global_container_stack.definitionChanges
  720. if not self._global_container_stack or definition_changes_container == self._empty_definition_changes_container:
  721. return
  722. previous_extruder_count = self._global_container_stack.getProperty("machine_extruder_count", "value")
  723. if extruder_count == previous_extruder_count:
  724. return
  725. definition_changes_container.setProperty("machine_extruder_count", "value", extruder_count)
  726. self.updateDefaultExtruder()
  727. self.updateNumberExtrudersEnabled()
  728. self.correctExtruderSettings()
  729. # Check to see if any objects are set to print with an extruder that will no longer exist
  730. root_node = self._application.getController().getScene().getRoot()
  731. for node in DepthFirstIterator(root_node): #type: ignore #Ignore type error because iter() should get called automatically by Python syntax.
  732. if node.getMeshData():
  733. extruder_nr = node.callDecoration("getActiveExtruderPosition")
  734. if extruder_nr is not None and int(extruder_nr) > extruder_count - 1:
  735. node.callDecoration("setActiveExtruder", extruder_manager.getExtruderStack(extruder_count - 1).getId())
  736. # Make sure one of the extruder stacks is active
  737. extruder_manager.setActiveExtruderIndex(0)
  738. # Move settable_per_extruder values out of the global container
  739. # After CURA-4482 this should not be the case anymore, but we still want to support older project files.
  740. global_user_container = self._global_container_stack.userChanges
  741. # Make sure extruder_stacks exists
  742. extruder_stacks = [] #type: List[ExtruderStack]
  743. if previous_extruder_count == 1:
  744. extruder_stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
  745. global_user_container = self._global_container_stack.userChanges
  746. for setting_instance in global_user_container.findInstances():
  747. setting_key = setting_instance.definition.key
  748. settable_per_extruder = self._global_container_stack.getProperty(setting_key, "settable_per_extruder")
  749. if settable_per_extruder:
  750. limit_to_extruder = int(self._global_container_stack.getProperty(setting_key, "limit_to_extruder"))
  751. extruder_stack = extruder_stacks[max(0, limit_to_extruder)]
  752. extruder_stack.userChanges.setProperty(setting_key, "value", global_user_container.getProperty(setting_key, "value"))
  753. global_user_container.removeInstance(setting_key)
  754. # Signal that the global stack has changed
  755. self._application.globalContainerStackChanged.emit()
  756. self.forceUpdateAllSettings()
  757. @pyqtSlot(int, result = QObject)
  758. def getExtruder(self, position: int) -> Optional[ExtruderStack]:
  759. extruder = None
  760. if self._global_container_stack:
  761. extruder = self._global_container_stack.extruders.get(str(position))
  762. return extruder
  763. def updateDefaultExtruder(self) -> None:
  764. if self._global_container_stack is None:
  765. return
  766. extruder_items = sorted(self._global_container_stack.extruders.items())
  767. old_position = self._default_extruder_position
  768. new_default_position = "0"
  769. for position, extruder in extruder_items:
  770. if extruder.isEnabled:
  771. new_default_position = position
  772. break
  773. if new_default_position != old_position:
  774. self._default_extruder_position = new_default_position
  775. self.extruderChanged.emit()
  776. def updateNumberExtrudersEnabled(self) -> None:
  777. if self._global_container_stack is None:
  778. return
  779. definition_changes_container = self._global_container_stack.definitionChanges
  780. machine_extruder_count = self._global_container_stack.getProperty("machine_extruder_count", "value")
  781. extruder_count = 0
  782. for position, extruder in self._global_container_stack.extruders.items():
  783. if extruder.isEnabled and int(position) < machine_extruder_count:
  784. extruder_count += 1
  785. if self.numberExtrudersEnabled != extruder_count:
  786. definition_changes_container.setProperty("extruders_enabled_count", "value", extruder_count)
  787. self.numberExtrudersEnabledChanged.emit()
  788. @pyqtProperty(int, notify = numberExtrudersEnabledChanged)
  789. def numberExtrudersEnabled(self) -> int:
  790. if self._global_container_stack is None:
  791. return 1
  792. return self._global_container_stack.definitionChanges.getProperty("extruders_enabled_count", "value")
  793. @pyqtProperty(str, notify = extruderChanged)
  794. def defaultExtruderPosition(self) -> str:
  795. return self._default_extruder_position
  796. ## This will fire the propertiesChanged for all settings so they will be updated in the front-end
  797. @pyqtSlot()
  798. def forceUpdateAllSettings(self) -> None:
  799. if self._global_container_stack is None:
  800. return
  801. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  802. property_names = ["value", "resolve", "validationState"]
  803. for container in [self._global_container_stack] + list(self._global_container_stack.extruders.values()):
  804. for setting_key in container.getAllKeys():
  805. container.propertiesChanged.emit(setting_key, property_names)
  806. @pyqtSlot(int, bool)
  807. def setExtruderEnabled(self, position: int, enabled: bool) -> None:
  808. extruder = self.getExtruder(position)
  809. if not extruder or self._global_container_stack is None:
  810. Logger.log("w", "Could not find extruder on position %s", position)
  811. return
  812. extruder.setEnabled(enabled)
  813. self.updateDefaultExtruder()
  814. self.updateNumberExtrudersEnabled()
  815. self.correctExtruderSettings()
  816. # ensure that the quality profile is compatible with current combination, or choose a compatible one if available
  817. self._updateQualityWithMaterial()
  818. self.extruderChanged.emit()
  819. # update material compatibility color
  820. self.activeQualityGroupChanged.emit()
  821. # update items in SettingExtruder
  822. ExtruderManager.getInstance().extrudersChanged.emit(self._global_container_stack.getId())
  823. # Make sure the front end reflects changes
  824. self.forceUpdateAllSettings()
  825. # Also trigger the build plate compatibility to update
  826. self.activeMaterialChanged.emit()
  827. def _onMachineNameChanged(self) -> None:
  828. self.globalContainerChanged.emit()
  829. def _onMaterialNameChanged(self) -> None:
  830. self.activeMaterialChanged.emit()
  831. def _onQualityNameChanged(self) -> None:
  832. self.activeQualityChanged.emit()
  833. def _getContainerChangedSignals(self) -> List[Signal]:
  834. if self._global_container_stack is None:
  835. return []
  836. stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
  837. stacks.append(self._global_container_stack)
  838. return [ s.containersChanged for s in stacks ]
  839. @pyqtSlot(str, str, str)
  840. def setSettingForAllExtruders(self, setting_name: str, property_name: str, property_value: str) -> None:
  841. if self._global_container_stack is None:
  842. return
  843. for key, extruder in self._global_container_stack.extruders.items():
  844. container = extruder.userChanges
  845. container.setProperty(setting_name, property_name, property_value)
  846. ## Reset all setting properties of a setting for all extruders.
  847. # \param setting_name The ID of the setting to reset.
  848. @pyqtSlot(str)
  849. def resetSettingForAllExtruders(self, setting_name: str) -> None:
  850. if self._global_container_stack is None:
  851. return
  852. for key, extruder in self._global_container_stack.extruders.items():
  853. container = extruder.userChanges
  854. container.removeInstance(setting_name)
  855. @pyqtProperty("QVariantList", notify = globalContainerChanged)
  856. def currentExtruderPositions(self) -> List[str]:
  857. if self._global_container_stack is None:
  858. return []
  859. return sorted(list(self._global_container_stack.extruders.keys()))
  860. ## Update _current_root_material_id when the current root material was changed.
  861. def _onRootMaterialChanged(self) -> None:
  862. self._current_root_material_id = {}
  863. if self._global_container_stack:
  864. for position in self._global_container_stack.extruders:
  865. self._current_root_material_id[position] = self._global_container_stack.extruders[position].material.getMetaDataEntry("base_file")
  866. @pyqtProperty("QVariant", notify = rootMaterialChanged)
  867. def currentRootMaterialId(self) -> Dict[str, str]:
  868. return self._current_root_material_id
  869. ## Return the variant names in the extruder stack(s).
  870. ## For the variant in the global stack, use activeVariantBuildplateName
  871. @pyqtProperty("QVariant", notify = activeVariantChanged)
  872. def activeVariantNames(self) -> Dict[str, str]:
  873. result = {}
  874. active_stacks = ExtruderManager.getInstance().getActiveExtruderStacks()
  875. for stack in active_stacks:
  876. variant_container = stack.variant
  877. position = stack.getMetaDataEntry("position")
  878. if variant_container and variant_container != self._empty_variant_container:
  879. result[position] = variant_container.getName()
  880. return result
  881. #
  882. # Sets all quality and quality_changes containers to empty_quality and empty_quality_changes containers
  883. # for all stacks in the currently active machine.
  884. #
  885. def _setEmptyQuality(self) -> None:
  886. if self._global_container_stack is None:
  887. return
  888. self._current_quality_group = None
  889. self._current_quality_changes_group = None
  890. self._global_container_stack.quality = self._empty_quality_container
  891. self._global_container_stack.qualityChanges = self._empty_quality_changes_container
  892. for extruder in self._global_container_stack.extruders.values():
  893. extruder.quality = self._empty_quality_container
  894. extruder.qualityChanges = self._empty_quality_changes_container
  895. self.activeQualityGroupChanged.emit()
  896. self.activeQualityChangesGroupChanged.emit()
  897. def _setQualityGroup(self, quality_group: Optional[QualityGroup], empty_quality_changes: bool = True) -> None:
  898. if self._global_container_stack is None:
  899. return
  900. if quality_group is None:
  901. self._setEmptyQuality()
  902. return
  903. if quality_group.node_for_global is None or quality_group.node_for_global.getContainer() is None:
  904. return
  905. for node in quality_group.nodes_for_extruders.values():
  906. if node.getContainer() is None:
  907. return
  908. self._current_quality_group = quality_group
  909. if empty_quality_changes:
  910. self._current_quality_changes_group = None
  911. # Set quality and quality_changes for the GlobalStack
  912. self._global_container_stack.quality = quality_group.node_for_global.getContainer()
  913. if empty_quality_changes:
  914. self._global_container_stack.qualityChanges = self._empty_quality_changes_container
  915. # Set quality and quality_changes for each ExtruderStack
  916. for position, node in quality_group.nodes_for_extruders.items():
  917. self._global_container_stack.extruders[str(position)].quality = node.getContainer()
  918. if empty_quality_changes:
  919. self._global_container_stack.extruders[str(position)].qualityChanges = self._empty_quality_changes_container
  920. self.activeQualityGroupChanged.emit()
  921. self.activeQualityChangesGroupChanged.emit()
  922. def _fixQualityChangesGroupToNotSupported(self, quality_changes_group: QualityChangesGroup) -> None:
  923. nodes = [quality_changes_group.node_for_global] + list(quality_changes_group.nodes_for_extruders.values())
  924. containers = [n.getContainer() for n in nodes if n is not None]
  925. for container in containers:
  926. if container:
  927. container.setMetaDataEntry("quality_type", "not_supported")
  928. quality_changes_group.quality_type = "not_supported"
  929. def _setQualityChangesGroup(self, quality_changes_group: QualityChangesGroup) -> None:
  930. if self._global_container_stack is None:
  931. return #Can't change that.
  932. quality_type = quality_changes_group.quality_type
  933. # A custom quality can be created based on "not supported".
  934. # In that case, do not set quality containers to empty.
  935. quality_group = None
  936. if quality_type != "not_supported":
  937. quality_group_dict = self._quality_manager.getQualityGroups(self._global_container_stack)
  938. quality_group = quality_group_dict.get(quality_type)
  939. if quality_group is None:
  940. self._fixQualityChangesGroupToNotSupported(quality_changes_group)
  941. quality_changes_container = self._empty_quality_changes_container
  942. quality_container = self._empty_quality_container
  943. if quality_changes_group.node_for_global and quality_changes_group.node_for_global.getContainer():
  944. quality_changes_container = cast(InstanceContainer, quality_changes_group.node_for_global.getContainer())
  945. if quality_group is not None and quality_group.node_for_global and quality_group.node_for_global.getContainer():
  946. quality_container = quality_group.node_for_global.getContainer()
  947. self._global_container_stack.quality = quality_container
  948. self._global_container_stack.qualityChanges = quality_changes_container
  949. for position, extruder in self._global_container_stack.extruders.items():
  950. quality_changes_node = quality_changes_group.nodes_for_extruders.get(position)
  951. quality_node = None
  952. if quality_group is not None:
  953. quality_node = quality_group.nodes_for_extruders.get(position)
  954. quality_changes_container = self._empty_quality_changes_container
  955. quality_container = self._empty_quality_container
  956. if quality_changes_node and quality_changes_node.getContainer():
  957. quality_changes_container = cast(InstanceContainer, quality_changes_node.getContainer())
  958. if quality_node and quality_node.getContainer():
  959. quality_container = quality_node.getContainer()
  960. extruder.quality = quality_container
  961. extruder.qualityChanges = quality_changes_container
  962. self._current_quality_group = quality_group
  963. self._current_quality_changes_group = quality_changes_group
  964. self.activeQualityGroupChanged.emit()
  965. self.activeQualityChangesGroupChanged.emit()
  966. def _setVariantNode(self, position: str, container_node: ContainerNode) -> None:
  967. if container_node.getContainer() is None or self._global_container_stack is None:
  968. return
  969. self._global_container_stack.extruders[position].variant = container_node.getContainer()
  970. self.activeVariantChanged.emit()
  971. def _setGlobalVariant(self, container_node: ContainerNode) -> None:
  972. if self._global_container_stack is None:
  973. return
  974. self._global_container_stack.variant = container_node.getContainer()
  975. if not self._global_container_stack.variant:
  976. self._global_container_stack.variant = self._application.empty_variant_container
  977. def _setMaterial(self, position: str, container_node: ContainerNode = None) -> None:
  978. if self._global_container_stack is None:
  979. return
  980. if container_node and container_node.getContainer():
  981. self._global_container_stack.extruders[position].material = container_node.getContainer()
  982. root_material_id = container_node.getMetaDataEntry("base_file", None)
  983. else:
  984. self._global_container_stack.extruders[position].material = self._empty_material_container
  985. root_material_id = None
  986. # The _current_root_material_id is used in the MaterialMenu to see which material is selected
  987. if root_material_id != self._current_root_material_id[position]:
  988. self._current_root_material_id[position] = root_material_id
  989. self.rootMaterialChanged.emit()
  990. def activeMaterialsCompatible(self) -> bool:
  991. # check material - variant compatibility
  992. if self._global_container_stack is not None:
  993. if Util.parseBool(self._global_container_stack.getMetaDataEntry("has_materials", False)):
  994. for position, extruder in self._global_container_stack.extruders.items():
  995. if extruder.isEnabled and not extruder.material.getMetaDataEntry("compatible"):
  996. return False
  997. if not extruder.material.getMetaDataEntry("compatible"):
  998. return False
  999. return True
  1000. ## Update current quality type and machine after setting material
  1001. def _updateQualityWithMaterial(self, *args: Any) -> None:
  1002. if self._global_container_stack is None:
  1003. return
  1004. Logger.log("i", "Updating quality/quality_changes due to material change")
  1005. current_quality_type = None
  1006. if self._current_quality_group:
  1007. current_quality_type = self._current_quality_group.quality_type
  1008. candidate_quality_groups = self._quality_manager.getQualityGroups(self._global_container_stack)
  1009. available_quality_types = {qt for qt, g in candidate_quality_groups.items() if g.is_available}
  1010. Logger.log("d", "Current quality type = [%s]", current_quality_type)
  1011. if not self.activeMaterialsCompatible():
  1012. if current_quality_type is not None:
  1013. Logger.log("i", "Active materials are not compatible, setting all qualities to empty (Not Supported).")
  1014. self._setEmptyQuality()
  1015. return
  1016. if not available_quality_types:
  1017. if self._current_quality_changes_group is None:
  1018. Logger.log("i", "No available quality types found, setting all qualities to empty (Not Supported).")
  1019. self._setEmptyQuality()
  1020. return
  1021. if current_quality_type in available_quality_types:
  1022. Logger.log("i", "Current available quality type [%s] is available, applying changes.", current_quality_type)
  1023. self._setQualityGroup(candidate_quality_groups[current_quality_type], empty_quality_changes = False)
  1024. return
  1025. # The current quality type is not available so we use the preferred quality type if it's available,
  1026. # otherwise use one of the available quality types.
  1027. quality_type = sorted(list(available_quality_types))[0]
  1028. preferred_quality_type = self._global_container_stack.getMetaDataEntry("preferred_quality_type")
  1029. if preferred_quality_type in available_quality_types:
  1030. quality_type = preferred_quality_type
  1031. Logger.log("i", "The current quality type [%s] is not available, switching to [%s] instead",
  1032. current_quality_type, quality_type)
  1033. self._setQualityGroup(candidate_quality_groups[quality_type], empty_quality_changes = True)
  1034. def updateMaterialWithVariant(self, position: Optional[str]) -> None:
  1035. if self._global_container_stack is None:
  1036. return
  1037. if position is None:
  1038. position_list = list(self._global_container_stack.extruders.keys())
  1039. else:
  1040. position_list = [position]
  1041. for position_item in position_list:
  1042. extruder = self._global_container_stack.extruders[position_item]
  1043. current_material_base_name = extruder.material.getMetaDataEntry("base_file")
  1044. current_variant_name = None
  1045. if extruder.variant.getId() != self._empty_variant_container.getId():
  1046. current_variant_name = extruder.variant.getMetaDataEntry("name")
  1047. from UM.Settings.Interfaces import PropertyEvaluationContext
  1048. from cura.Settings.CuraContainerStack import _ContainerIndexes
  1049. context = PropertyEvaluationContext(extruder)
  1050. context.context["evaluate_from_container_index"] = _ContainerIndexes.DefinitionChanges
  1051. material_diameter = extruder.getProperty("material_diameter", "value", context)
  1052. candidate_materials = self._material_manager.getAvailableMaterials(
  1053. self._global_container_stack.definition,
  1054. current_variant_name,
  1055. material_diameter)
  1056. if not candidate_materials:
  1057. self._setMaterial(position_item, container_node = None)
  1058. continue
  1059. if current_material_base_name in candidate_materials:
  1060. new_material = candidate_materials[current_material_base_name]
  1061. self._setMaterial(position_item, new_material)
  1062. continue
  1063. # The current material is not available, find the preferred one
  1064. material_node = self._material_manager.getDefaultMaterial(self._global_container_stack, position_item, current_variant_name)
  1065. if material_node is not None:
  1066. self._setMaterial(position_item, material_node)
  1067. ## Given a printer definition name, select the right machine instance. In case it doesn't exist, create a new
  1068. # instance with the same network key.
  1069. @pyqtSlot(str)
  1070. def switchPrinterType(self, machine_name: str) -> None:
  1071. # Don't switch if the user tries to change to the same type of printer
  1072. if self._global_container_stack is None or self.activeMachineDefinitionName == machine_name:
  1073. return
  1074. # Get the definition id corresponding to this machine name
  1075. machine_definition_id = CuraContainerRegistry.getInstance().findDefinitionContainers(name = machine_name)[0].getId()
  1076. # Try to find a machine with the same network key
  1077. new_machine = self.getMachine(machine_definition_id, metadata_filter = {"um_network_key": self.activeMachineNetworkKey})
  1078. # If there is no machine, then create a new one and set it to the non-hidden instance
  1079. if not new_machine:
  1080. new_machine = CuraStackBuilder.createMachine(machine_definition_id + "_sync", machine_definition_id)
  1081. if not new_machine:
  1082. return
  1083. new_machine.setMetaDataEntry("um_network_key", self.activeMachineNetworkKey)
  1084. new_machine.setMetaDataEntry("connect_group_name", self.activeMachineNetworkGroupName)
  1085. new_machine.setMetaDataEntry("hidden", False)
  1086. else:
  1087. Logger.log("i", "Found a %s with the key %s. Let's use it!", machine_name, self.activeMachineNetworkKey)
  1088. new_machine.setMetaDataEntry("hidden", False)
  1089. # Set the current printer instance to hidden (the metadata entry must exist)
  1090. self._global_container_stack.setMetaDataEntry("hidden", True)
  1091. self.setActiveMachine(new_machine.getId())
  1092. @pyqtSlot(QObject)
  1093. def applyRemoteConfiguration(self, configuration: ConfigurationModel) -> None:
  1094. if self._global_container_stack is None:
  1095. return
  1096. self.blurSettings.emit()
  1097. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  1098. self.switchPrinterType(configuration.printerType)
  1099. for extruder_configuration in configuration.extruderConfigurations:
  1100. position = str(extruder_configuration.position)
  1101. variant_container_node = self._variant_manager.getVariantNode(self._global_container_stack.definition.getId(), extruder_configuration.hotendID)
  1102. material_container_node = self._material_manager.getMaterialNodeByType(self._global_container_stack, position, extruder_configuration.hotendID, extruder_configuration.material.guid)
  1103. if variant_container_node:
  1104. self._setVariantNode(position, variant_container_node)
  1105. else:
  1106. self._global_container_stack.extruders[position].variant = self._empty_variant_container
  1107. if material_container_node:
  1108. self._setMaterial(position, material_container_node)
  1109. else:
  1110. self._global_container_stack.extruders[position].material = self._empty_material_container
  1111. self.updateMaterialWithVariant(position)
  1112. if configuration.buildplateConfiguration is not None:
  1113. global_variant_container_node = self._variant_manager.getBuildplateVariantNode(self._global_container_stack.definition.getId(), configuration.buildplateConfiguration)
  1114. if global_variant_container_node:
  1115. self._setGlobalVariant(global_variant_container_node)
  1116. else:
  1117. self._global_container_stack.variant = self._empty_variant_container
  1118. else:
  1119. self._global_container_stack.variant = self._empty_variant_container
  1120. self._updateQualityWithMaterial()
  1121. # See if we need to show the Discard or Keep changes screen
  1122. if self.hasUserSettings and self._application.getPreferences().getValue("cura/active_mode") == 1:
  1123. self._application.discardOrKeepProfileChanges()
  1124. ## Find all container stacks that has the pair 'key = value' in its metadata and replaces the value with 'new_value'
  1125. def replaceContainersMetadata(self, key: str, value: str, new_value: str) -> None:
  1126. machines = CuraContainerRegistry.getInstance().findContainerStacks(type = "machine")
  1127. for machine in machines:
  1128. if machine.getMetaDataEntry(key) == value:
  1129. machine.setMetaDataEntry(key, new_value)
  1130. ## This method checks if the name of the group stored in the definition container is correct.
  1131. # After updating from 3.2 to 3.3 some group names may be temporary. If there is a mismatch in the name of the group
  1132. # then all the container stacks are updated, both the current and the hidden ones.
  1133. def checkCorrectGroupName(self, device_id: str, group_name: str) -> None:
  1134. if self._global_container_stack and device_id == self.activeMachineNetworkKey:
  1135. # Check if the connect_group_name is correct. If not, update all the containers connected to the same printer
  1136. if self.activeMachineNetworkGroupName != group_name:
  1137. metadata_filter = {"um_network_key": self.activeMachineNetworkKey}
  1138. containers = CuraContainerRegistry.getInstance().findContainerStacks(type = "machine", **metadata_filter)
  1139. for container in containers:
  1140. container.setMetaDataEntry("connect_group_name", group_name)
  1141. ## This method checks if there is an instance connected to the given network_key
  1142. def existNetworkInstances(self, network_key: str) -> bool:
  1143. metadata_filter = {"um_network_key": network_key}
  1144. containers = CuraContainerRegistry.getInstance().findContainerStacks(type = "machine", **metadata_filter)
  1145. return bool(containers)
  1146. @pyqtSlot("QVariant")
  1147. def setGlobalVariant(self, container_node: ContainerNode) -> None:
  1148. self.blurSettings.emit()
  1149. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  1150. self._setGlobalVariant(container_node)
  1151. self.updateMaterialWithVariant(None) # Update all materials
  1152. self._updateQualityWithMaterial()
  1153. @pyqtSlot(str, str)
  1154. def setMaterialById(self, position: str, root_material_id: str) -> None:
  1155. if self._global_container_stack is None:
  1156. return
  1157. machine_definition_id = self._global_container_stack.definition.id
  1158. position = str(position)
  1159. extruder_stack = self._global_container_stack.extruders[position]
  1160. variant_name = extruder_stack.variant.getName()
  1161. material_diameter = extruder_stack.approximateMaterialDiameter
  1162. material_node = self._material_manager.getMaterialNode(machine_definition_id, variant_name, material_diameter, root_material_id)
  1163. self.setMaterial(position, material_node)
  1164. ## global_stack: if you want to provide your own global_stack instead of the current active one
  1165. # if you update an active machine, special measures have to be taken.
  1166. @pyqtSlot(str, "QVariant")
  1167. def setMaterial(self, position: str, container_node, global_stack: Optional["GlobalStack"] = None) -> None:
  1168. if global_stack is not None and global_stack != self._global_container_stack:
  1169. global_stack.extruders[position].material = container_node.getContainer()
  1170. return
  1171. position = str(position)
  1172. self.blurSettings.emit()
  1173. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  1174. self._setMaterial(position, container_node)
  1175. self._updateQualityWithMaterial()
  1176. # See if we need to show the Discard or Keep changes screen
  1177. if self.hasUserSettings and self._application.getPreferences().getValue("cura/active_mode") == 1:
  1178. self._application.discardOrKeepProfileChanges()
  1179. @pyqtSlot(str, str)
  1180. def setVariantByName(self, position: str, variant_name: str) -> None:
  1181. if self._global_container_stack is None:
  1182. return
  1183. machine_definition_id = self._global_container_stack.definition.id
  1184. variant_node = self._variant_manager.getVariantNode(machine_definition_id, variant_name)
  1185. self.setVariant(position, variant_node)
  1186. @pyqtSlot(str, "QVariant")
  1187. def setVariant(self, position: str, container_node: ContainerNode) -> None:
  1188. position = str(position)
  1189. self.blurSettings.emit()
  1190. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  1191. self._setVariantNode(position, container_node)
  1192. self.updateMaterialWithVariant(position)
  1193. self._updateQualityWithMaterial()
  1194. # See if we need to show the Discard or Keep changes screen
  1195. if self.hasUserSettings and self._application.getPreferences().getValue("cura/active_mode") == 1:
  1196. self._application.discardOrKeepProfileChanges()
  1197. @pyqtSlot(str)
  1198. def setQualityGroupByQualityType(self, quality_type: str) -> None:
  1199. if self._global_container_stack is None:
  1200. return
  1201. # Get all the quality groups for this global stack and filter out by quality_type
  1202. quality_group_dict = self._quality_manager.getQualityGroups(self._global_container_stack)
  1203. quality_group = quality_group_dict[quality_type]
  1204. self.setQualityGroup(quality_group)
  1205. ## Optionally provide global_stack if you want to use your own
  1206. # The active global_stack is treated differently.
  1207. @pyqtSlot(QObject)
  1208. def setQualityGroup(self, quality_group: QualityGroup, no_dialog: bool = False, global_stack: Optional["GlobalStack"] = None) -> None:
  1209. if global_stack is not None and global_stack != self._global_container_stack:
  1210. if quality_group is None:
  1211. Logger.log("e", "Could not set quality group because quality group is None")
  1212. return
  1213. if quality_group.node_for_global is None:
  1214. Logger.log("e", "Could not set quality group [%s] because it has no node_for_global", str(quality_group))
  1215. return
  1216. global_stack.quality = quality_group.node_for_global.getContainer()
  1217. for extruder_nr, extruder_stack in global_stack.extruders.items():
  1218. extruder_stack.quality = quality_group.nodes_for_extruders[extruder_nr].getContainer()
  1219. return
  1220. self.blurSettings.emit()
  1221. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  1222. self._setQualityGroup(quality_group)
  1223. # See if we need to show the Discard or Keep changes screen
  1224. if not no_dialog and self.hasUserSettings and self._application.getPreferences().getValue("cura/active_mode") == 1:
  1225. self._application.discardOrKeepProfileChanges()
  1226. @pyqtProperty(QObject, fset = setQualityGroup, notify = activeQualityGroupChanged)
  1227. def activeQualityGroup(self) -> Optional[QualityGroup]:
  1228. return self._current_quality_group
  1229. @pyqtSlot(QObject)
  1230. def setQualityChangesGroup(self, quality_changes_group: QualityChangesGroup, no_dialog: bool = False) -> None:
  1231. self.blurSettings.emit()
  1232. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  1233. self._setQualityChangesGroup(quality_changes_group)
  1234. # See if we need to show the Discard or Keep changes screen
  1235. if not no_dialog and self.hasUserSettings and self._application.getPreferences().getValue("cura/active_mode") == 1:
  1236. self._application.discardOrKeepProfileChanges()
  1237. @pyqtSlot()
  1238. def resetToUseDefaultQuality(self) -> None:
  1239. if self._global_container_stack is None:
  1240. return
  1241. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  1242. self._setQualityGroup(self._current_quality_group)
  1243. for stack in [self._global_container_stack] + list(self._global_container_stack.extruders.values()):
  1244. stack.userChanges.clear()
  1245. @pyqtProperty(QObject, fset = setQualityChangesGroup, notify = activeQualityChangesGroupChanged)
  1246. def activeQualityChangesGroup(self) -> Optional[QualityChangesGroup]:
  1247. return self._current_quality_changes_group
  1248. @pyqtProperty(str, notify = activeQualityGroupChanged)
  1249. def activeQualityOrQualityChangesName(self) -> str:
  1250. name = self._empty_quality_container.getName()
  1251. if self._current_quality_changes_group:
  1252. name = self._current_quality_changes_group.name
  1253. elif self._current_quality_group:
  1254. name = self._current_quality_group.name
  1255. return name
  1256. def _updateUponMaterialMetadataChange(self) -> None:
  1257. if self._global_container_stack is None:
  1258. return
  1259. with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue):
  1260. self.updateMaterialWithVariant(None)
  1261. self._updateQualityWithMaterial()