ContainerManager.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import copy
  4. import os.path
  5. import urllib.parse
  6. import uuid
  7. from typing import Dict, Union
  8. from PyQt5.QtCore import QObject, QUrl, QVariant
  9. from UM.FlameProfiler import pyqtSlot
  10. from PyQt5.QtWidgets import QMessageBox
  11. from UM.PluginRegistry import PluginRegistry
  12. from UM.SaveFile import SaveFile
  13. from UM.Platform import Platform
  14. from UM.MimeTypeDatabase import MimeTypeDatabase
  15. from UM.Logger import Logger
  16. from UM.Application import Application
  17. from UM.Settings.ContainerStack import ContainerStack
  18. from UM.Settings.DefinitionContainer import DefinitionContainer
  19. from UM.Settings.InstanceContainer import InstanceContainer
  20. from UM.MimeTypeDatabase import MimeTypeNotFoundError
  21. from UM.Settings.ContainerRegistry import ContainerRegistry
  22. from UM.i18n import i18nCatalog
  23. from cura.Settings.ExtruderManager import ExtruderManager
  24. from cura.Settings.ExtruderStack import ExtruderStack
  25. from cura.Machines.QualityManager import getMachineDefinitionIDForQualitySearch
  26. catalog = i18nCatalog("cura")
  27. ## Manager class that contains common actions to deal with containers in Cura.
  28. #
  29. # This is primarily intended as a class to be able to perform certain actions
  30. # from within QML. We want to be able to trigger things like removing a container
  31. # when a certain action happens. This can be done through this class.
  32. class ContainerManager(QObject):
  33. def __init__(self, parent = None):
  34. super().__init__(parent)
  35. self._application = Application.getInstance()
  36. self._container_registry = ContainerRegistry.getInstance()
  37. self._machine_manager = self._application.getMachineManager()
  38. self._material_manager = self._application.getMaterialManager()
  39. self._container_name_filters = {}
  40. @pyqtSlot(str, str, result=str)
  41. def getContainerMetaDataEntry(self, container_id, entry_name):
  42. metadatas = self._container_registry.findContainersMetadata(id = container_id)
  43. if not metadatas:
  44. Logger.log("w", "Could not get metadata of container %s because it was not found.", container_id)
  45. return ""
  46. return str(metadatas[0].get(entry_name, ""))
  47. ## Set a metadata entry of the specified container.
  48. #
  49. # This will set the specified entry of the container's metadata to the specified
  50. # value. Note that entries containing dictionaries can have their entries changed
  51. # by using "/" as a separator. For example, to change an entry "foo" in a
  52. # dictionary entry "bar", you can specify "bar/foo" as entry name.
  53. #
  54. # \param container_id \type{str} The ID of the container to change.
  55. # \param entry_name \type{str} The name of the metadata entry to change.
  56. # \param entry_value The new value of the entry.
  57. #
  58. # \return True if successful, False if not.
  59. # TODO: This is ONLY used by MaterialView for material containers. Maybe refactor this.
  60. @pyqtSlot("QVariant", str, str)
  61. def setContainerMetaDataEntry(self, container_node, entry_name, entry_value):
  62. root_material_id = container_node.metadata["base_file"]
  63. if self._container_registry.isReadOnly(root_material_id):
  64. Logger.log("w", "Cannot set metadata of read-only container %s.", root_material_id)
  65. return False
  66. material_group = self._material_manager.getMaterialGroup(root_material_id)
  67. entries = entry_name.split("/")
  68. entry_name = entries.pop()
  69. sub_item_changed = False
  70. if entries:
  71. root_name = entries.pop(0)
  72. root = material_group.root_material_node.metadata.get(root_name)
  73. item = root
  74. for _ in range(len(entries)):
  75. item = item.get(entries.pop(0), { })
  76. if item[entry_name] != entry_value:
  77. sub_item_changed = True
  78. item[entry_name] = entry_value
  79. entry_name = root_name
  80. entry_value = root
  81. container = material_group.root_material_node.getContainer()
  82. container.setMetaDataEntry(entry_name, entry_value)
  83. if sub_item_changed: #If it was only a sub-item that has changed then the setMetaDataEntry won't correctly notice that something changed, and we must manually signal that the metadata changed.
  84. container.metaDataChanged.emit(container)
  85. ## Set a setting property of the specified container.
  86. #
  87. # This will set the specified property of the specified setting of the container
  88. # and all containers that share the same base_file (if any). The latter only
  89. # happens for material containers.
  90. #
  91. # \param container_id \type{str} The ID of the container to change.
  92. # \param setting_key \type{str} The key of the setting.
  93. # \param property_name \type{str} The name of the property, eg "value".
  94. # \param property_value \type{str} The new value of the property.
  95. #
  96. # \return True if successful, False if not.
  97. @pyqtSlot(str, str, str, str, result = bool)
  98. def setContainerProperty(self, container_id, setting_key, property_name, property_value):
  99. if self._container_registry.isReadOnly(container_id):
  100. Logger.log("w", "Cannot set properties of read-only container %s.", container_id)
  101. return False
  102. containers = self._container_registry.findContainers(id = container_id)
  103. if not containers:
  104. Logger.log("w", "Could not set properties of container %s because it was not found.", container_id)
  105. return False
  106. container = containers[0]
  107. container.setProperty(setting_key, property_name, property_value)
  108. basefile = container.getMetaDataEntry("base_file", container_id)
  109. for sibbling_container in ContainerRegistry.getInstance().findInstanceContainers(base_file = basefile):
  110. if sibbling_container != container:
  111. sibbling_container.setProperty(setting_key, property_name, property_value)
  112. return True
  113. ## Get a setting property of the specified container.
  114. #
  115. # This will get the specified property of the specified setting of the
  116. # specified container.
  117. #
  118. # \param container_id The ID of the container to get the setting property
  119. # of.
  120. # \param setting_key The key of the setting to get the property of.
  121. # \param property_name The property to obtain.
  122. # \return The value of the specified property. The type of this property
  123. # value depends on the type of the property. For instance, the "value"
  124. # property of an integer setting will be a Python int, but the "value"
  125. # property of an enum setting will be a Python str.
  126. @pyqtSlot(str, str, str, result = QVariant)
  127. def getContainerProperty(self, container_id: str, setting_key: str, property_name: str):
  128. containers = self._container_registry.findContainers(id = container_id)
  129. if not containers:
  130. Logger.log("w", "Could not get properties of container %s because it was not found.", container_id)
  131. return ""
  132. container = containers[0]
  133. return container.getProperty(setting_key, property_name)
  134. ## Set the name of the specified material.
  135. @pyqtSlot("QVariant", str)
  136. def setMaterialName(self, material_node, new_name):
  137. root_material_id = material_node.metadata["base_file"]
  138. if self._container_registry.isReadOnly(root_material_id):
  139. Logger.log("w", "Cannot set name of read-only container %s.", root_material_id)
  140. return
  141. material_group = self._material_manager.getMaterialGroup(root_material_id)
  142. material_group.root_material_node.getContainer().setName(new_name)
  143. @pyqtSlot(str, result = str)
  144. def makeUniqueName(self, original_name):
  145. return self._container_registry.uniqueName(original_name)
  146. ## Get a list of string that can be used as name filters for a Qt File Dialog
  147. #
  148. # This will go through the list of available container types and generate a list of strings
  149. # out of that. The strings are formatted as "description (*.extension)" and can be directly
  150. # passed to a nameFilters property of a Qt File Dialog.
  151. #
  152. # \param type_name Which types of containers to list. These types correspond to the "type"
  153. # key of the plugin metadata.
  154. #
  155. # \return A string list with name filters.
  156. @pyqtSlot(str, result = "QStringList")
  157. def getContainerNameFilters(self, type_name):
  158. if not self._container_name_filters:
  159. self._updateContainerNameFilters()
  160. filters = []
  161. for filter_string, entry in self._container_name_filters.items():
  162. if not type_name or entry["type"] == type_name:
  163. filters.append(filter_string)
  164. filters.append("All Files (*)")
  165. return filters
  166. ## Export a container to a file
  167. #
  168. # \param container_id The ID of the container to export
  169. # \param file_type The type of file to save as. Should be in the form of "description (*.extension, *.ext)"
  170. # \param file_url_or_string The URL where to save the file.
  171. #
  172. # \return A dictionary containing a key "status" with a status code and a key "message" with a message
  173. # explaining the status.
  174. # The status code can be one of "error", "cancelled", "success"
  175. @pyqtSlot(str, str, QUrl, result = "QVariantMap")
  176. def exportContainer(self, container_id: str, file_type: str, file_url_or_string: Union[QUrl, str]) -> Dict[str, str]:
  177. if not container_id or not file_type or not file_url_or_string:
  178. return {"status": "error", "message": "Invalid arguments"}
  179. if isinstance(file_url_or_string, QUrl):
  180. file_url = file_url_or_string.toLocalFile()
  181. else:
  182. file_url = file_url_or_string
  183. if not file_url:
  184. return {"status": "error", "message": "Invalid path"}
  185. mime_type = None
  186. if file_type not in self._container_name_filters:
  187. try:
  188. mime_type = MimeTypeDatabase.getMimeTypeForFile(file_url)
  189. except MimeTypeNotFoundError:
  190. return {"status": "error", "message": "Unknown File Type"}
  191. else:
  192. mime_type = self._container_name_filters[file_type]["mime"]
  193. containers = self._container_registry.findContainers(id = container_id)
  194. if not containers:
  195. return {"status": "error", "message": "Container not found"}
  196. container = containers[0]
  197. if Platform.isOSX() and "." in file_url:
  198. file_url = file_url[:file_url.rfind(".")]
  199. for suffix in mime_type.suffixes:
  200. if file_url.endswith(suffix):
  201. break
  202. else:
  203. file_url += "." + mime_type.preferredSuffix
  204. if not Platform.isWindows():
  205. if os.path.exists(file_url):
  206. result = QMessageBox.question(None, catalog.i18nc("@title:window", "File Already Exists"),
  207. catalog.i18nc("@label Don't translate the XML tag <filename>!", "The file <filename>{0}</filename> already exists. Are you sure you want to overwrite it?").format(file_url))
  208. if result == QMessageBox.No:
  209. return {"status": "cancelled", "message": "User cancelled"}
  210. try:
  211. contents = container.serialize()
  212. except NotImplementedError:
  213. return {"status": "error", "message": "Unable to serialize container"}
  214. if contents is None:
  215. return {"status": "error", "message": "Serialization returned None. Unable to write to file"}
  216. with SaveFile(file_url, "w") as f:
  217. f.write(contents)
  218. return {"status": "success", "message": "Successfully exported container", "path": file_url}
  219. ## Imports a profile from a file
  220. #
  221. # \param file_url A URL that points to the file to import.
  222. #
  223. # \return \type{Dict} dict with a 'status' key containing the string 'success' or 'error', and a 'message' key
  224. # containing a message for the user
  225. @pyqtSlot(QUrl, result = "QVariantMap")
  226. def importMaterialContainer(self, file_url_or_string: Union[QUrl, str]) -> Dict[str, str]:
  227. if not file_url_or_string:
  228. return {"status": "error", "message": "Invalid path"}
  229. if isinstance(file_url_or_string, QUrl):
  230. file_url = file_url_or_string.toLocalFile()
  231. else:
  232. file_url = file_url_or_string
  233. if not file_url or not os.path.exists(file_url):
  234. return {"status": "error", "message": "Invalid path"}
  235. try:
  236. mime_type = MimeTypeDatabase.getMimeTypeForFile(file_url)
  237. except MimeTypeNotFoundError:
  238. return {"status": "error", "message": "Could not determine mime type of file"}
  239. container_type = self._container_registry.getContainerForMimeType(mime_type)
  240. if not container_type:
  241. return {"status": "error", "message": "Could not find a container to handle the specified file."}
  242. container_id = urllib.parse.unquote_plus(mime_type.stripExtension(os.path.basename(file_url)))
  243. container_id = self._container_registry.uniqueName(container_id)
  244. container = container_type(container_id)
  245. try:
  246. with open(file_url, "rt", encoding = "utf-8") as f:
  247. container.deserialize(f.read())
  248. except PermissionError:
  249. return {"status": "error", "message": "Permission denied when trying to read the file"}
  250. except Exception as ex:
  251. return {"status": "error", "message": str(ex)}
  252. container.setDirty(True)
  253. self._container_registry.addContainer(container)
  254. return {"status": "success", "message": "Successfully imported container {0}".format(container.getName())}
  255. ## Update the current active quality changes container with the settings from the user container.
  256. #
  257. # This will go through the active global stack and all active extruder stacks and merge the changes from the user
  258. # container into the quality_changes container. After that, the user container is cleared.
  259. #
  260. # \return \type{bool} True if successful, False if not.
  261. @pyqtSlot(result = bool)
  262. def updateQualityChanges(self):
  263. global_stack = Application.getInstance().getGlobalContainerStack()
  264. if not global_stack:
  265. return False
  266. self._machine_manager.blurSettings.emit()
  267. for stack in ExtruderManager.getInstance().getActiveGlobalAndExtruderStacks():
  268. # Find the quality_changes container for this stack and merge the contents of the top container into it.
  269. quality_changes = stack.qualityChanges
  270. if not quality_changes or self._container_registry.isReadOnly(quality_changes.getId()):
  271. Logger.log("e", "Could not update quality of a nonexistant or read only quality profile in stack %s", stack.getId())
  272. continue
  273. self._performMerge(quality_changes, stack.getTop())
  274. self._machine_manager.activeQualityChangesGroupChanged.emit()
  275. return True
  276. ## Clear the top-most (user) containers of the active stacks.
  277. @pyqtSlot()
  278. def clearUserContainers(self) -> None:
  279. self._machine_manager.blurSettings.emit()
  280. send_emits_containers = []
  281. # Go through global and extruder stacks and clear their topmost container (the user settings).
  282. for stack in ExtruderManager.getInstance().getActiveGlobalAndExtruderStacks():
  283. container = stack.userChanges
  284. container.clear()
  285. send_emits_containers.append(container)
  286. Application.getInstance().getMachineManager().correctExtruderSettings()
  287. for container in send_emits_containers:
  288. container.sendPostponedEmits()
  289. ## Create quality changes containers from the user containers in the active stacks.
  290. #
  291. # This will go through the global and extruder stacks and create quality_changes containers from
  292. # the user containers in each stack. These then replace the quality_changes containers in the
  293. # stack and clear the user settings.
  294. #
  295. # \return \type{bool} True if the operation was successfully, False if not.
  296. @pyqtSlot(str)
  297. def createQualityChanges(self, base_name):
  298. global_stack = Application.getInstance().getGlobalContainerStack()
  299. if not global_stack:
  300. return
  301. active_quality_name = self._machine_manager.activeQualityOrQualityChangesName
  302. if active_quality_name == "":
  303. Logger.log("w", "No quality container found in stack %s, cannot create profile", global_stack.getId())
  304. return
  305. self._machine_manager.blurSettings.emit()
  306. if base_name is None or base_name == "":
  307. base_name = active_quality_name
  308. unique_name = self._container_registry.uniqueName(base_name)
  309. # Go through the active stacks and create quality_changes containers from the user containers.
  310. for stack in ExtruderManager.getInstance().getActiveGlobalAndExtruderStacks():
  311. user_container = stack.userChanges
  312. quality_container = stack.quality
  313. quality_changes_container = stack.qualityChanges
  314. if not quality_container or not quality_changes_container:
  315. Logger.log("w", "No quality or quality changes container found in stack %s, ignoring it", stack.getId())
  316. continue
  317. extruder_definition_id = None
  318. if isinstance(stack, ExtruderStack):
  319. extruder_definition_id = stack.definition.getId()
  320. quality_type = quality_container.getMetaDataEntry("quality_type")
  321. new_changes = self._createQualityChanges(quality_type, unique_name, global_stack, extruder_definition_id)
  322. self._performMerge(new_changes, quality_changes_container, clear_settings = False)
  323. self._performMerge(new_changes, user_container)
  324. self._container_registry.addContainer(new_changes)
  325. #
  326. # Remove the given quality changes group
  327. #
  328. @pyqtSlot(QObject)
  329. def removeQualityChangesGroup(self, quality_changes_group):
  330. Logger.log("i", "Removing quality changes group [%s]", quality_changes_group.name)
  331. for node in quality_changes_group.getAllNodes():
  332. self._container_registry.removeContainer(node.metadata["id"])
  333. #
  334. # Rename a set of quality changes containers. Returns the new name.
  335. #
  336. @pyqtSlot(QObject, str, result = str)
  337. def renameQualityChangesGroup(self, quality_changes_group, new_name) -> str:
  338. Logger.log("i", "Renaming QualityChangesGroup[%s] to [%s]", quality_changes_group.name, new_name)
  339. self._machine_manager.blurSettings.emit()
  340. if new_name == quality_changes_group.name:
  341. Logger.log("i", "QualityChangesGroup name [%s] unchanged.", quality_changes_group.name)
  342. return new_name
  343. new_name = self._container_registry.uniqueName(new_name)
  344. for node in quality_changes_group.getAllNodes():
  345. node.getContainer().setName(new_name)
  346. self._machine_manager.activeQualityChanged.emit()
  347. self._machine_manager.activeQualityGroupChanged.emit()
  348. return new_name
  349. @pyqtSlot(str, "QVariantMap")
  350. def duplicateQualityChanges(self, quality_changes_name, quality_model_item):
  351. global_stack = Application.getInstance().getGlobalContainerStack()
  352. quality_group = quality_model_item["quality_group"]
  353. quality_changes_group = quality_model_item["quality_changes_group"]
  354. if quality_changes_group is None:
  355. # create global quality changes only
  356. new_quality_changes = self._createQualityChanges(quality_group.quality_type, quality_changes_name,
  357. global_stack, extruder_id = None)
  358. self._container_registry.addContainer(new_quality_changes)
  359. else:
  360. new_name = self._container_registry.uniqueName(quality_changes_name)
  361. for node in quality_changes_group.getAllNodes():
  362. container = node.getContainer()
  363. new_id = self._container_registry.uniqueName(container.getId())
  364. self._container_registry.addContainer(container.duplicate(new_id, new_name))
  365. @pyqtSlot("QVariant")
  366. def removeMaterial(self, material_node):
  367. root_material_id = material_node.metadata["base_file"]
  368. material_group = self._material_manager.getMaterialGroup(root_material_id)
  369. if not material_group:
  370. Logger.log("d", "Unable to remove the material with id %s, because it doesn't exist.", root_material_id)
  371. return
  372. nodes_to_remove = [material_group.root_material_node] + material_group.derived_material_node_list
  373. for node in nodes_to_remove:
  374. self._container_registry.removeContainer(node.metadata["id"])
  375. ## Create a duplicate of a material, which has the same GUID and base_file metadata
  376. #
  377. # \return \type{str} the id of the newly created container.
  378. @pyqtSlot("QVariant", result = str)
  379. def duplicateMaterial(self, material_node, new_base_id = None, new_metadata = None):
  380. root_material_id = material_node.metadata["base_file"]
  381. material_group = self._material_manager.getMaterialGroup(root_material_id)
  382. if not material_group:
  383. Logger.log("d", "Unable to duplicate the material with id %s, because it doesn't exist.", root_material_id)
  384. return
  385. base_container = material_group.root_material_node.getContainer()
  386. containers_to_copy = []
  387. for node in material_group.derived_material_node_list:
  388. containers_to_copy.append(node.getContainer())
  389. # Ensure all settings are saved.
  390. Application.getInstance().saveSettings()
  391. # Create a new ID & container to hold the data.
  392. new_containers = []
  393. if new_base_id is None:
  394. new_base_id = self._container_registry.uniqueName(base_container.getId())
  395. new_base_container = copy.deepcopy(base_container)
  396. new_base_container.getMetaData()["id"] = new_base_id
  397. new_base_container.getMetaData()["base_file"] = new_base_id
  398. if new_metadata is not None:
  399. for key, value in new_metadata.items():
  400. new_base_container.getMetaData()[key] = value
  401. new_containers.append(new_base_container)
  402. # Clone all of them.
  403. for container_to_copy in containers_to_copy:
  404. # Create unique IDs for every clone.
  405. new_id = new_base_id
  406. if container_to_copy.getMetaDataEntry("definition") != "fdmprinter":
  407. new_id += "_" + container_to_copy.getMetaDataEntry("definition")
  408. if container_to_copy.getMetaDataEntry("variant_name"):
  409. variant_name = container_to_copy.getMetaDataEntry("variant_name")
  410. new_id += "_" + variant_name.replace(" ", "_")
  411. new_container = copy.deepcopy(container_to_copy)
  412. new_container.getMetaData()["id"] = new_id
  413. new_container.getMetaData()["base_file"] = new_base_id
  414. if new_metadata is not None:
  415. for key, value in new_metadata.items():
  416. new_container.getMetaData()[key] = value
  417. new_containers.append(new_container)
  418. for container_to_add in new_containers:
  419. container_to_add.setDirty(True)
  420. ContainerRegistry.getInstance().addContainer(container_to_add)
  421. return new_base_id
  422. ## Create a new material by cloning Generic PLA for the current material diameter and setting the GUID to something unqiue
  423. #
  424. # \return \type{str} the id of the newly created container.
  425. @pyqtSlot(result = str)
  426. def createMaterial(self):
  427. # Ensure all settings are saved.
  428. Application.getInstance().saveSettings()
  429. global_stack = Application.getInstance().getGlobalContainerStack()
  430. approximate_diameter = str(round(global_stack.getProperty("material_diameter", "value")))
  431. root_material_id = "generic_pla"
  432. root_material_id = self._material_manager.getRootMaterialIDForDiameter(root_material_id, approximate_diameter)
  433. material_group = self._material_manager.getMaterialGroup(root_material_id)
  434. # Create a new ID & container to hold the data.
  435. new_id = self._container_registry.uniqueName("custom_material")
  436. new_metadata = {"name": catalog.i18nc("@label", "Custom Material"),
  437. "brand": catalog.i18nc("@label", "Custom"),
  438. "GUID": str(uuid.uuid4()),
  439. }
  440. self.duplicateMaterial(material_group.root_material_node,
  441. new_base_id = new_id,
  442. new_metadata = new_metadata)
  443. return new_id
  444. ## Get a list of materials that have the same GUID as the reference material
  445. #
  446. # \param material_id \type{str} the id of the material for which to get the linked materials.
  447. # \return \type{list} a list of names of materials with the same GUID
  448. @pyqtSlot("QVariant", result = "QStringList")
  449. def getLinkedMaterials(self, material_node):
  450. guid = material_node.metadata["GUID"]
  451. material_group_list = self._material_manager.getMaterialGroupListByGUID(guid)
  452. linked_material_names = []
  453. if material_group_list:
  454. for material_group in material_group_list:
  455. linked_material_names.append(material_group.root_material_node.metadata["name"])
  456. return linked_material_names
  457. ## Unlink a material from all other materials by creating a new GUID
  458. # \param material_id \type{str} the id of the material to create a new GUID for.
  459. @pyqtSlot("QVariant")
  460. def unlinkMaterial(self, material_node):
  461. # Get the material group
  462. material_group = self._material_manager.getMaterialGroup(material_node.metadata["base_file"])
  463. # Generate a new GUID
  464. new_guid = str(uuid.uuid4())
  465. # Update the GUID
  466. # NOTE: We only need to set the root material container because XmlMaterialProfile.setMetaDataEntry() will
  467. # take care of the derived containers too
  468. container = material_group.root_material_node.getContainer()
  469. container.setMetaDataEntry("GUID", new_guid)
  470. ## Get the singleton instance for this class.
  471. @classmethod
  472. def getInstance(cls) -> "ContainerManager":
  473. # Note: Explicit use of class name to prevent issues with inheritance.
  474. if ContainerManager.__instance is None:
  475. ContainerManager.__instance = cls()
  476. return ContainerManager.__instance
  477. __instance = None # type: "ContainerManager"
  478. # Factory function, used by QML
  479. @staticmethod
  480. def createContainerManager(engine, js_engine):
  481. return ContainerManager.getInstance()
  482. def _performMerge(self, merge_into, merge, clear_settings = True):
  483. assert isinstance(merge, type(merge_into))
  484. if merge == merge_into:
  485. return
  486. for key in merge.getAllKeys():
  487. merge_into.setProperty(key, "value", merge.getProperty(key, "value"))
  488. if clear_settings:
  489. merge.clear()
  490. def _updateContainerNameFilters(self) -> None:
  491. self._container_name_filters = {}
  492. for plugin_id, container_type in self._container_registry.getContainerTypes():
  493. # Ignore default container types since those are not plugins
  494. if container_type in (InstanceContainer, ContainerStack, DefinitionContainer):
  495. continue
  496. serialize_type = ""
  497. try:
  498. plugin_metadata = PluginRegistry.getInstance().getMetaData(plugin_id)
  499. if plugin_metadata:
  500. serialize_type = plugin_metadata["settings_container"]["type"]
  501. else:
  502. continue
  503. except KeyError as e:
  504. continue
  505. mime_type = self._container_registry.getMimeTypeForContainer(container_type)
  506. entry = {
  507. "type": serialize_type,
  508. "mime": mime_type,
  509. "container": container_type
  510. }
  511. suffix = mime_type.preferredSuffix
  512. if Platform.isOSX() and "." in suffix:
  513. # OSX's File dialog is stupid and does not allow selecting files with a . in its name
  514. suffix = suffix[suffix.index(".") + 1:]
  515. suffix_list = "*." + suffix
  516. for suffix in mime_type.suffixes:
  517. if suffix == mime_type.preferredSuffix:
  518. continue
  519. if Platform.isOSX() and "." in suffix:
  520. # OSX's File dialog is stupid and does not allow selecting files with a . in its name
  521. suffix = suffix[suffix.index("."):]
  522. suffix_list += ", *." + suffix
  523. name_filter = "{0} ({1})".format(mime_type.comment, suffix_list)
  524. self._container_name_filters[name_filter] = entry
  525. ## Creates a unique ID for a container by prefixing the name with the stack ID.
  526. #
  527. # This method creates a unique ID for a container by prefixing it with a specified stack ID.
  528. # This is done to ensure we have an easily identified ID for quality changes, which have the
  529. # same name across several stacks.
  530. #
  531. # \param stack_id The ID of the stack to prepend.
  532. # \param container_name The name of the container that we are creating a unique ID for.
  533. #
  534. # \return Container name prefixed with stack ID, in lower case with spaces replaced by underscores.
  535. def _createUniqueId(self, stack_id, container_name):
  536. result = stack_id + "_" + container_name
  537. result = result.lower()
  538. result.replace(" ", "_")
  539. return result
  540. ## Create a quality changes container for a specified quality container.
  541. #
  542. # \param quality_container The quality container to create a changes container for.
  543. # \param new_name The name of the new quality_changes container.
  544. # \param machine_definition The machine definition this quality changes container is specific to.
  545. # \param extruder_id
  546. #
  547. # \return A new quality_changes container with the specified container as base.
  548. def _createQualityChanges(self, quality_type, new_name, machine, extruder_id):
  549. base_id = machine.definition.getId() if extruder_id is None else extruder_id
  550. # Create a new quality_changes container for the quality.
  551. quality_changes = InstanceContainer(self._createUniqueId(base_id, new_name))
  552. quality_changes.setName(new_name)
  553. quality_changes.addMetaDataEntry("type", "quality_changes")
  554. quality_changes.addMetaDataEntry("quality_type", quality_type)
  555. # If we are creating a container for an extruder, ensure we add that to the container
  556. if extruder_id is not None:
  557. quality_changes.addMetaDataEntry("extruder", extruder_id)
  558. # If the machine specifies qualities should be filtered, ensure we match the current criteria.
  559. machine_definition_id = getMachineDefinitionIDForQualitySearch(machine)
  560. quality_changes.setDefinition(machine_definition_id)
  561. from cura.CuraApplication import CuraApplication
  562. quality_changes.addMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  563. return quality_changes
  564. ## Import single profile, file_url does not have to end with curaprofile
  565. @pyqtSlot(QUrl, result="QVariantMap")
  566. def importProfile(self, file_url):
  567. if not file_url.isValid():
  568. return
  569. path = file_url.toLocalFile()
  570. if not path:
  571. return
  572. return self._container_registry.importProfile(path)
  573. @pyqtSlot(QObject, QUrl, str)
  574. def exportQualityChangesGroup(self, quality_changes_group, file_url: QUrl, file_type: str):
  575. if not file_url.isValid():
  576. return
  577. path = file_url.toLocalFile()
  578. if not path:
  579. return
  580. container_list = [n.getContainer() for n in quality_changes_group.getAllNodes()]
  581. self._container_registry.exportQualityProfile(container_list, path, file_type)