ContainerManager.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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.MachineTools 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.getTop()
  284. container.clear()
  285. send_emits_containers.append(container)
  286. for container in send_emits_containers:
  287. container.sendPostponedEmits()
  288. ## Create quality changes containers from the user containers in the active stacks.
  289. #
  290. # This will go through the global and extruder stacks and create quality_changes containers from
  291. # the user containers in each stack. These then replace the quality_changes containers in the
  292. # stack and clear the user settings.
  293. #
  294. # \return \type{bool} True if the operation was successfully, False if not.
  295. @pyqtSlot(str)
  296. def createQualityChanges(self, base_name):
  297. global_stack = Application.getInstance().getGlobalContainerStack()
  298. if not global_stack:
  299. return
  300. active_quality_name = self._machine_manager.activeQualityOrQualityChangesName
  301. if active_quality_name == "":
  302. Logger.log("w", "No quality container found in stack %s, cannot create profile", global_stack.getId())
  303. return
  304. self._machine_manager.blurSettings.emit()
  305. if base_name is None or base_name == "":
  306. base_name = active_quality_name
  307. unique_name = self._container_registry.uniqueName(base_name)
  308. # Go through the active stacks and create quality_changes containers from the user containers.
  309. for stack in ExtruderManager.getInstance().getActiveGlobalAndExtruderStacks():
  310. user_container = stack.userChanges
  311. quality_container = stack.quality
  312. quality_changes_container = stack.qualityChanges
  313. if not quality_container or not quality_changes_container:
  314. Logger.log("w", "No quality or quality changes container found in stack %s, ignoring it", stack.getId())
  315. continue
  316. extruder_definition_id = None
  317. if isinstance(stack, ExtruderStack):
  318. extruder_definition_id = stack.definition.getId()
  319. quality_type = quality_container.getMetaDataEntry("quality_type")
  320. new_changes = self._createQualityChanges(quality_type, unique_name, global_stack, extruder_definition_id)
  321. self._performMerge(new_changes, quality_changes_container, clear_settings = False)
  322. self._performMerge(new_changes, user_container)
  323. self._container_registry.addContainer(new_changes)
  324. #
  325. # Remove the given quality changes group
  326. #
  327. @pyqtSlot(QObject)
  328. def removeQualityChangesGroup(self, quality_changes_group):
  329. Logger.log("i", "Removing quality changes group [%s]", quality_changes_group.name)
  330. for node in quality_changes_group.getAllNodes():
  331. self._container_registry.removeContainer(node.metadata["id"])
  332. #
  333. # Rename a set of quality changes containers. Returns the new name.
  334. #
  335. @pyqtSlot(QObject, str, result = str)
  336. def renameQualityChangesGroup(self, quality_changes_group, new_name) -> str:
  337. Logger.log("i", "Renaming QualityChangesGroup[%s] to [%s]", quality_changes_group.name, new_name)
  338. self._machine_manager.blurSettings.emit()
  339. if new_name == quality_changes_group.name:
  340. Logger.log("i", "QualityChangesGroup name [%s] unchanged.", quality_changes_group.name)
  341. return new_name
  342. new_name = self._container_registry.uniqueName(new_name)
  343. for node in quality_changes_group.getAllNodes():
  344. node.getContainer().setName(new_name)
  345. self._machine_manager.activeQualityChanged.emit()
  346. self._machine_manager.activeQualityGroupChanged.emit()
  347. return new_name
  348. @pyqtSlot(str, "QVariantMap")
  349. def duplicateQualityChanges(self, quality_changes_name, quality_model_item):
  350. global_stack = Application.getInstance().getGlobalContainerStack()
  351. quality_group = quality_model_item["quality_group"]
  352. quality_changes_group = quality_model_item["quality_changes_group"]
  353. if quality_changes_group is None:
  354. # create global quality changes only
  355. new_quality_changes = self._createQualityChanges(quality_group.quality_type, quality_changes_name,
  356. global_stack, extruder_id = None)
  357. self._container_registry.addContainer(new_quality_changes)
  358. else:
  359. new_name = self._container_registry.uniqueName(quality_changes_name)
  360. for node in quality_changes_group.getAllNodes():
  361. container = node.getContainer()
  362. new_id = self._container_registry.uniqueName(container.getId())
  363. self._container_registry.addContainer(container.duplicate(new_id, new_name))
  364. @pyqtSlot("QVariant")
  365. def removeMaterial(self, material_node):
  366. root_material_id = material_node.metadata["base_file"]
  367. material_group = self._material_manager.getMaterialGroup(root_material_id)
  368. if not material_group:
  369. Logger.log("d", "Unable to remove the material with id %s, because it doesn't exist.", root_material_id)
  370. return
  371. nodes_to_remove = [material_group.root_material_node] + material_group.derived_material_node_list
  372. for node in nodes_to_remove:
  373. self._container_registry.removeContainer(node.metadata["id"])
  374. ## Create a duplicate of a material, which has the same GUID and base_file metadata
  375. #
  376. # \return \type{str} the id of the newly created container.
  377. @pyqtSlot("QVariant", result = str)
  378. def duplicateMaterial(self, material_node, new_base_id = None, new_metadata = None):
  379. root_material_id = material_node.metadata["base_file"]
  380. material_group = self._material_manager.getMaterialGroup(root_material_id)
  381. if not material_group:
  382. Logger.log("d", "Unable to duplicate the material with id %s, because it doesn't exist.", root_material_id)
  383. return
  384. base_container = material_group.root_material_node.getContainer()
  385. containers_to_copy = []
  386. for node in material_group.derived_material_node_list:
  387. containers_to_copy.append(node.getContainer())
  388. # Ensure all settings are saved.
  389. Application.getInstance().saveSettings()
  390. # Create a new ID & container to hold the data.
  391. new_containers = []
  392. if new_base_id is None:
  393. new_base_id = self._container_registry.uniqueName(base_container.getId())
  394. new_base_container = copy.deepcopy(base_container)
  395. new_base_container.getMetaData()["id"] = new_base_id
  396. new_base_container.getMetaData()["base_file"] = new_base_id
  397. if new_metadata is not None:
  398. for key, value in new_metadata.items():
  399. new_base_container.getMetaData()[key] = value
  400. new_containers.append(new_base_container)
  401. # Clone all of them.
  402. for container_to_copy in containers_to_copy:
  403. # Create unique IDs for every clone.
  404. new_id = new_base_id
  405. if container_to_copy.getMetaDataEntry("definition") != "fdmprinter":
  406. new_id += "_" + container_to_copy.getMetaDataEntry("definition")
  407. if container_to_copy.getMetaDataEntry("variant_name"):
  408. variant_name = container_to_copy.getMetaDataEntry("variant_name")
  409. new_id += "_" + variant_name.replace(" ", "_")
  410. new_container = copy.deepcopy(container_to_copy)
  411. new_container.getMetaData()["id"] = new_id
  412. new_container.getMetaData()["base_file"] = new_base_id
  413. if new_metadata is not None:
  414. for key, value in new_metadata.items():
  415. new_container.getMetaData()[key] = value
  416. new_containers.append(new_container)
  417. for container_to_add in new_containers:
  418. container_to_add.setDirty(True)
  419. ContainerRegistry.getInstance().addContainer(container_to_add)
  420. return new_base_id
  421. ## Create a new material by cloning Generic PLA for the current material diameter and setting the GUID to something unqiue
  422. #
  423. # \return \type{str} the id of the newly created container.
  424. @pyqtSlot(result = str)
  425. def createMaterial(self):
  426. # Ensure all settings are saved.
  427. Application.getInstance().saveSettings()
  428. global_stack = Application.getInstance().getGlobalContainerStack()
  429. approximate_diameter = str(round(global_stack.getProperty("material_diameter", "value")))
  430. root_material_id = "generic_pla"
  431. root_material_id = self._material_manager.getRootMaterialIDForDiameter(root_material_id, approximate_diameter)
  432. material_group = self._material_manager.getMaterialGroup(root_material_id)
  433. # Create a new ID & container to hold the data.
  434. new_id = self._container_registry.uniqueName("custom_material")
  435. new_metadata = {"name": catalog.i18nc("@label", "Custom Material"),
  436. "brand": catalog.i18nc("@label", "Custom"),
  437. "GUID": str(uuid.uuid4()),
  438. }
  439. self.duplicateMaterial(material_group.root_material_node,
  440. new_base_id = new_id,
  441. new_metadata = new_metadata)
  442. return new_id
  443. ## Get a list of materials that have the same GUID as the reference material
  444. #
  445. # \param material_id \type{str} the id of the material for which to get the linked materials.
  446. # \return \type{list} a list of names of materials with the same GUID
  447. @pyqtSlot("QVariant", result = "QStringList")
  448. def getLinkedMaterials(self, material_node):
  449. guid = material_node.metadata["GUID"]
  450. material_group_list = self._material_manager.getMaterialGroupListByGUID(guid)
  451. linked_material_names = []
  452. if material_group_list:
  453. for material_group in material_group_list:
  454. linked_material_names.append(material_group.root_material_node.metadata["name"])
  455. return linked_material_names
  456. ## Unlink a material from all other materials by creating a new GUID
  457. # \param material_id \type{str} the id of the material to create a new GUID for.
  458. @pyqtSlot("QVariant")
  459. def unlinkMaterial(self, material_node):
  460. # Get the material group
  461. material_group = self._material_manager.getMaterialGroup(material_node.metadata["base_file"])
  462. # Generate a new GUID
  463. new_guid = str(uuid.uuid4())
  464. # Update the GUID
  465. # NOTE: We only need to set the root material container because XmlMaterialProfile.setMetaDataEntry() will
  466. # take care of the derived containers too
  467. container = material_group.root_material_node.getContainer()
  468. container.setMetaDataEntry("GUID", new_guid)
  469. ## Get the singleton instance for this class.
  470. @classmethod
  471. def getInstance(cls) -> "ContainerManager":
  472. # Note: Explicit use of class name to prevent issues with inheritance.
  473. if ContainerManager.__instance is None:
  474. ContainerManager.__instance = cls()
  475. return ContainerManager.__instance
  476. __instance = None # type: "ContainerManager"
  477. # Factory function, used by QML
  478. @staticmethod
  479. def createContainerManager(engine, js_engine):
  480. return ContainerManager.getInstance()
  481. def _performMerge(self, merge_into, merge, clear_settings = True):
  482. assert isinstance(merge, type(merge_into))
  483. if merge == merge_into:
  484. return
  485. for key in merge.getAllKeys():
  486. merge_into.setProperty(key, "value", merge.getProperty(key, "value"))
  487. if clear_settings:
  488. merge.clear()
  489. def _updateContainerNameFilters(self) -> None:
  490. self._container_name_filters = {}
  491. for plugin_id, container_type in self._container_registry.getContainerTypes():
  492. # Ignore default container types since those are not plugins
  493. if container_type in (InstanceContainer, ContainerStack, DefinitionContainer):
  494. continue
  495. serialize_type = ""
  496. try:
  497. plugin_metadata = PluginRegistry.getInstance().getMetaData(plugin_id)
  498. if plugin_metadata:
  499. serialize_type = plugin_metadata["settings_container"]["type"]
  500. else:
  501. continue
  502. except KeyError as e:
  503. continue
  504. mime_type = self._container_registry.getMimeTypeForContainer(container_type)
  505. entry = {
  506. "type": serialize_type,
  507. "mime": mime_type,
  508. "container": container_type
  509. }
  510. suffix = mime_type.preferredSuffix
  511. if Platform.isOSX() and "." in suffix:
  512. # OSX's File dialog is stupid and does not allow selecting files with a . in its name
  513. suffix = suffix[suffix.index(".") + 1:]
  514. suffix_list = "*." + suffix
  515. for suffix in mime_type.suffixes:
  516. if suffix == mime_type.preferredSuffix:
  517. continue
  518. if Platform.isOSX() and "." in suffix:
  519. # OSX's File dialog is stupid and does not allow selecting files with a . in its name
  520. suffix = suffix[suffix.index("."):]
  521. suffix_list += ", *." + suffix
  522. name_filter = "{0} ({1})".format(mime_type.comment, suffix_list)
  523. self._container_name_filters[name_filter] = entry
  524. ## Creates a unique ID for a container by prefixing the name with the stack ID.
  525. #
  526. # This method creates a unique ID for a container by prefixing it with a specified stack ID.
  527. # This is done to ensure we have an easily identified ID for quality changes, which have the
  528. # same name across several stacks.
  529. #
  530. # \param stack_id The ID of the stack to prepend.
  531. # \param container_name The name of the container that we are creating a unique ID for.
  532. #
  533. # \return Container name prefixed with stack ID, in lower case with spaces replaced by underscores.
  534. def _createUniqueId(self, stack_id, container_name):
  535. result = stack_id + "_" + container_name
  536. result = result.lower()
  537. result.replace(" ", "_")
  538. return result
  539. ## Create a quality changes container for a specified quality container.
  540. #
  541. # \param quality_container The quality container to create a changes container for.
  542. # \param new_name The name of the new quality_changes container.
  543. # \param machine_definition The machine definition this quality changes container is specific to.
  544. # \param extruder_id
  545. #
  546. # \return A new quality_changes container with the specified container as base.
  547. def _createQualityChanges(self, quality_type, new_name, machine, extruder_id):
  548. base_id = machine.definition.getId() if extruder_id is None else extruder_id
  549. # Create a new quality_changes container for the quality.
  550. quality_changes = InstanceContainer(self._createUniqueId(base_id, new_name))
  551. quality_changes.setName(new_name)
  552. quality_changes.addMetaDataEntry("type", "quality_changes")
  553. quality_changes.addMetaDataEntry("quality_type", quality_type)
  554. # If we are creating a container for an extruder, ensure we add that to the container
  555. if extruder_id is not None:
  556. quality_changes.addMetaDataEntry("extruder", extruder_id)
  557. # If the machine specifies qualities should be filtered, ensure we match the current criteria.
  558. machine_definition_id = getMachineDefinitionIDForQualitySearch(machine)
  559. quality_changes.setDefinition(machine_definition_id)
  560. from cura.CuraApplication import CuraApplication
  561. quality_changes.addMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  562. return quality_changes
  563. ## Import single profile, file_url does not have to end with curaprofile
  564. @pyqtSlot(QUrl, result="QVariantMap")
  565. def importProfile(self, file_url):
  566. if not file_url.isValid():
  567. return
  568. path = file_url.toLocalFile()
  569. if not path:
  570. return
  571. return self._container_registry.importProfile(path)
  572. @pyqtSlot(QObject, QUrl, str)
  573. def exportQualityChangesGroup(self, quality_changes_group, file_url: QUrl, file_type: str):
  574. if not file_url.isValid():
  575. return
  576. path = file_url.toLocalFile()
  577. if not path:
  578. return
  579. container_list = [n.getContainer() for n in quality_changes_group.getAllNodes()]
  580. self._container_registry.exportQualityProfile(container_list, path, file_type)