ContainerManager.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. import os.path
  4. import urllib
  5. from PyQt5.QtCore import QObject, pyqtSlot, pyqtProperty, pyqtSignal, QUrl
  6. from PyQt5.QtWidgets import QMessageBox
  7. import UM.PluginRegistry
  8. import UM.Settings
  9. import UM.SaveFile
  10. import UM.Platform
  11. import UM.MimeTypeDatabase
  12. import UM.Logger
  13. import cura.Settings
  14. from UM.MimeTypeDatabase import MimeTypeNotFoundError
  15. from UM.i18n import i18nCatalog
  16. catalog = i18nCatalog("cura")
  17. ## Manager class that contains common actions to deal with containers in Cura.
  18. #
  19. # This is primarily intended as a class to be able to perform certain actions
  20. # from within QML. We want to be able to trigger things like removing a container
  21. # when a certain action happens. This can be done through this class.
  22. class ContainerManager(QObject):
  23. def __init__(self, parent = None):
  24. super().__init__(parent)
  25. self._container_registry = UM.Settings.ContainerRegistry.getInstance()
  26. self._machine_manager = UM.Application.getInstance().getMachineManager()
  27. self._container_name_filters = {}
  28. ## Create a duplicate of the specified container
  29. #
  30. # This will create and add a duplicate of the container corresponding
  31. # to the container ID.
  32. #
  33. # \param container_id \type{str} The ID of the container to duplicate.
  34. #
  35. # \return The ID of the new container, or an empty string if duplication failed.
  36. @pyqtSlot(str, result = str)
  37. def duplicateContainer(self, container_id):
  38. containers = self._container_registry.findContainers(None, id = container_id)
  39. if not containers:
  40. UM.Logger.log("w", "Could duplicate container %s because it was not found.", container_id)
  41. return ""
  42. container = containers[0]
  43. new_container = None
  44. new_name = self._container_registry.uniqueName(container.getName())
  45. # Only InstanceContainer has a duplicate method at the moment.
  46. # So fall back to serialize/deserialize when no duplicate method exists.
  47. if hasattr(container, "duplicate"):
  48. new_container = container.duplicate(new_name)
  49. else:
  50. new_container = container.__class__(new_name)
  51. new_container.deserialize(container.serialize())
  52. new_container.setName(new_name)
  53. if new_container:
  54. self._container_registry.addContainer(new_container)
  55. return new_container.getId()
  56. ## Change the name of a specified container to a new name.
  57. #
  58. # \param container_id \type{str} The ID of the container to change the name of.
  59. # \param new_id \type{str} The new ID of the container.
  60. # \param new_name \type{str} The new name of the specified container.
  61. #
  62. # \return True if successful, False if not.
  63. @pyqtSlot(str, str, str, result = bool)
  64. def renameContainer(self, container_id, new_id, new_name):
  65. containers = self._container_registry.findContainers(None, id = container_id)
  66. if not containers:
  67. UM.Logger.log("w", "Could rename container %s because it was not found.", container_id)
  68. return False
  69. container = containers[0]
  70. # First, remove the container from the registry. This will clean up any files related to the container.
  71. self._container_registry.removeContainer(container)
  72. # Ensure we have a unique name for the container
  73. new_name = self._container_registry.uniqueName(new_name)
  74. # Then, update the name and ID of the container
  75. container.setName(new_name)
  76. container._id = new_id # TODO: Find a nicer way to set a new, unique ID
  77. # Finally, re-add the container so it will be properly serialized again.
  78. self._container_registry.addContainer(container)
  79. return True
  80. ## Remove the specified container.
  81. #
  82. # \param container_id \type{str} The ID of the container to remove.
  83. #
  84. # \return True if the container was successfully removed, False if not.
  85. @pyqtSlot(str, result = bool)
  86. def removeContainer(self, container_id):
  87. containers = self._container_registry.findContainers(None, id = container_id)
  88. if not containers:
  89. UM.Logger.log("w", "Could remove container %s because it was not found.", container_id)
  90. return False
  91. self._container_registry.removeContainer(containers[0].getId())
  92. return True
  93. ## Merge a container with another.
  94. #
  95. # This will try to merge one container into the other, by going through the container
  96. # and setting the right properties on the other container.
  97. #
  98. # \param merge_into_id \type{str} The ID of the container to merge into.
  99. # \param merge_id \type{str} The ID of the container to merge.
  100. #
  101. # \return True if successfully merged, False if not.
  102. @pyqtSlot(str, result = bool)
  103. def mergeContainers(self, merge_into_id, merge_id):
  104. containers = self._container_registry.findContainers(None, id = merge_into_id)
  105. if not containers:
  106. UM.Logger.log("w", "Could merge into container %s because it was not found.", merge_into_id)
  107. return False
  108. merge_into = containers[0]
  109. containers = self._container_registry.findContainers(None, id = merge_id)
  110. if not containers:
  111. UM.Logger.log("w", "Could not merge container %s because it was not found", merge_id)
  112. return False
  113. merge = containers[0]
  114. if not isinstance(merge, type(merge_into)):
  115. UM.Logger.log("w", "Cannot merge two containers of different types")
  116. return False
  117. self._performMerge(merge_into, merge)
  118. return True
  119. ## Clear the contents of a container.
  120. #
  121. # \param container_id \type{str} The ID of the container to clear.
  122. #
  123. # \return True if successful, False if not.
  124. @pyqtSlot(str, result = bool)
  125. def clearContainer(self, container_id):
  126. containers = self._container_registry.findContainers(None, id = container_id)
  127. if not containers:
  128. UM.Logger.log("w", "Could clear container %s because it was not found.", container_id)
  129. return False
  130. if containers[0].isReadOnly():
  131. UM.Logger.log("w", "Cannot clear read-only container %s", container_id)
  132. return False
  133. containers[0].clear()
  134. return True
  135. ## Set a metadata entry of the specified container.
  136. #
  137. # This will set the specified entry of the container's metadata to the specified
  138. # value. Note that entries containing dictionaries can have their entries changed
  139. # by using "/" as a separator. For example, to change an entry "foo" in a
  140. # dictionary entry "bar", you can specify "bar/foo" as entry name.
  141. #
  142. # \param container_id \type{str} The ID of the container to change.
  143. # \param entry_name \type{str} The name of the metadata entry to change.
  144. # \param entry_value The new value of the entry.
  145. #
  146. # \return True if successful, False if not.
  147. @pyqtSlot(str, str, str, result = bool)
  148. def setContainerMetaDataEntry(self, container_id, entry_name, entry_value):
  149. containers = self._container_registry.findContainers(None, id = container_id)
  150. if not containers:
  151. UM.Logger.log("w", "Could not set metadata of container %s because it was not found.", container_id)
  152. return False
  153. container = containers[0]
  154. if container.isReadOnly():
  155. UM.Logger.log("w", "Cannot set metadata of read-only container %s.", container_id)
  156. return False
  157. entries = entry_name.split("/")
  158. entry_name = entries.pop()
  159. if entries:
  160. root_name = entries.pop(0)
  161. root = container.getMetaDataEntry(root_name)
  162. item = root
  163. for entry in entries:
  164. item = item.get(entries.pop(0), { })
  165. item[entry_name] = entry_value
  166. entry_name = root_name
  167. entry_value = root
  168. container.setMetaDataEntry(entry_name, entry_value)
  169. return True
  170. ## Set the name of the specified container.
  171. @pyqtSlot(str, str, result = bool)
  172. def setContainerName(self, container_id, new_name):
  173. containers = self._container_registry.findContainers(None, id = container_id)
  174. if not containers:
  175. UM.Logger.log("w", "Could not set name of container %s because it was not found.", container_id)
  176. return False
  177. container = containers[0]
  178. if container.isReadOnly():
  179. UM.Logger.log("w", "Cannot set name of read-only container %s.", container_id)
  180. return False
  181. container.setName(new_name)
  182. return True
  183. ## Find instance containers matching certain criteria.
  184. #
  185. # This effectively forwards to ContainerRegistry::findInstanceContainers.
  186. #
  187. # \param criteria A dict of key - value pairs to search for.
  188. #
  189. # \return A list of container IDs that match the given criteria.
  190. @pyqtSlot("QVariantMap", result = "QVariantList")
  191. def findInstanceContainers(self, criteria):
  192. result = []
  193. for entry in self._container_registry.findInstanceContainers(**criteria):
  194. result.append(entry.getId())
  195. return result
  196. @pyqtSlot(str, result = bool)
  197. def isContainerUsed(self, container_id):
  198. UM.Logger.log("d", "Checking if container %s is currently used in the active stacks", container_id)
  199. for stack in cura.Settings.ExtruderManager.getInstance().getActiveGlobalAndExtruderStacks():
  200. if container_id in [child.getId() for child in stack.getContainers()]:
  201. UM.Logger.log("d", "The container is in use by %s", stack.getId())
  202. return True
  203. return False
  204. ## Get a list of string that can be used as name filters for a Qt File Dialog
  205. #
  206. # This will go through the list of available container types and generate a list of strings
  207. # out of that. The strings are formatted as "description (*.extension)" and can be directly
  208. # passed to a nameFilters property of a Qt File Dialog.
  209. #
  210. # \param type_name Which types of containers to list. These types correspond to the "type"
  211. # key of the plugin metadata.
  212. #
  213. # \return A string list with name filters.
  214. @pyqtSlot(str, result = "QStringList")
  215. def getContainerNameFilters(self, type_name):
  216. if not self._container_name_filters:
  217. self._updateContainerNameFilters()
  218. filters = []
  219. for filter_string, entry in self._container_name_filters.items():
  220. if not type_name or entry["type"] == type_name:
  221. filters.append(filter_string)
  222. filters.append("All Files (*)")
  223. return filters
  224. ## Export a container to a file
  225. #
  226. # \param container_id The ID of the container to export
  227. # \param file_type The type of file to save as. Should be in the form of "description (*.extension, *.ext)"
  228. # \param file_url The URL where to save the file.
  229. #
  230. # \return A dictionary containing a key "status" with a status code and a key "message" with a message
  231. # explaining the status.
  232. # The status code can be one of "error", "cancelled", "success"
  233. @pyqtSlot(str, str, QUrl, result = "QVariantMap")
  234. def exportContainer(self, container_id, file_type, file_url):
  235. if not container_id or not file_type or not file_url:
  236. return { "status": "error", "message": "Invalid arguments"}
  237. if isinstance(file_url, QUrl):
  238. file_url = file_url.toLocalFile()
  239. if not file_url:
  240. return { "status": "error", "message": "Invalid path"}
  241. mime_type = None
  242. if not file_type in self._container_name_filters:
  243. try:
  244. mime_type = UM.MimeTypeDatabase.getMimeTypeForFile(file_url)
  245. except MimeTypeNotFoundError:
  246. return { "status": "error", "message": "Unknown File Type" }
  247. else:
  248. mime_type = self._container_name_filters[file_type]["mime"]
  249. containers = self._container_registry.findContainers(None, id = container_id)
  250. if not containers:
  251. return { "status": "error", "message": "Container not found"}
  252. container = containers[0]
  253. if UM.Platform.isOSX() and "." in file_url:
  254. file_url = file_url[:file_url.rfind(".")]
  255. for suffix in mime_type.suffixes:
  256. if file_url.endswith(suffix):
  257. break
  258. else:
  259. file_url += "." + mime_type.preferredSuffix
  260. if not UM.Platform.isWindows():
  261. if os.path.exists(file_url):
  262. result = QMessageBox.question(None, catalog.i18nc("@title:window", "File Already Exists"),
  263. catalog.i18nc("@label", "The file <filename>{0}</filename> already exists. Are you sure you want to overwrite it?").format(file_url))
  264. if result == QMessageBox.No:
  265. return { "status": "cancelled", "message": "User cancelled"}
  266. try:
  267. contents = container.serialize()
  268. except NotImplementedError:
  269. return { "status": "error", "message": "Unable to serialize container"}
  270. with UM.SaveFile(file_url, "w") as f:
  271. f.write(contents)
  272. return { "status": "success", "message": "Succesfully exported container", "path": file_url}
  273. ## Imports a profile from a file
  274. #
  275. # \param file_url A URL that points to the file to import.
  276. #
  277. # \return \type{Dict} dict with a 'status' key containing the string 'success' or 'error', and a 'message' key
  278. # containing a message for the user
  279. @pyqtSlot(QUrl, result = "QVariantMap")
  280. def importContainer(self, file_url):
  281. if not file_url:
  282. return { "status": "error", "message": "Invalid path"}
  283. if isinstance(file_url, QUrl):
  284. file_url = file_url.toLocalFile()
  285. if not file_url or not os.path.exists(file_url):
  286. return { "status": "error", "message": "Invalid path" }
  287. try:
  288. mime_type = UM.MimeTypeDatabase.getMimeTypeForFile(file_url)
  289. except MimeTypeNotFoundError:
  290. return { "status": "error", "message": "Could not determine mime type of file" }
  291. container_type = UM.Settings.ContainerRegistry.getContainerForMimeType(mime_type)
  292. if not container_type:
  293. return { "status": "error", "message": "Could not find a container to handle the specified file."}
  294. container_id = urllib.parse.unquote_plus(mime_type.stripExtension(os.path.basename(file_url)))
  295. container_id = self._container_registry.uniqueName(container_id)
  296. container = container_type(container_id)
  297. try:
  298. with open(file_url, "rt") as f:
  299. container.deserialize(f.read())
  300. except PermissionError:
  301. return { "status": "error", "message": "Permission denied when trying to read the file"}
  302. container.setName(container_id)
  303. self._container_registry.addContainer(container)
  304. return { "status": "success", "message": "Successfully imported container {0}".format(container.getName()) }
  305. ## Update the current active quality changes container with the settings from the user container.
  306. #
  307. # This will go through the active global stack and all active extruder stacks and merge the changes from the user
  308. # container into the quality_changes container. After that, the user container is cleared.
  309. #
  310. # \return \type{bool} True if successful, False if not.
  311. @pyqtSlot(result = bool)
  312. def updateQualityChanges(self):
  313. global_stack = UM.Application.getInstance().getGlobalContainerStack()
  314. if not global_stack:
  315. return False
  316. self._machine_manager.blurSettings.emit()
  317. for stack in cura.Settings.ExtruderManager.getInstance().getActiveGlobalAndExtruderStacks():
  318. # Find the quality_changes container for this stack and merge the contents of the top container into it.
  319. quality_changes = stack.findContainer(type = "quality_changes")
  320. if not quality_changes or quality_changes.isReadOnly():
  321. UM.Logger.log("e", "Could not update quality of a nonexistant or read only quality profile in stack %s", stack.getId())
  322. continue
  323. self._performMerge(quality_changes, stack.getTop())
  324. self._machine_manager.activeQualityChanged.emit()
  325. return True
  326. ## Clear the top-most (user) containers of the active stacks.
  327. @pyqtSlot()
  328. def clearUserContainers(self):
  329. self._machine_manager.blurSettings.emit()
  330. # Go through global and extruder stacks and clear their topmost container (the user settings).
  331. for stack in cura.Settings.ExtruderManager.getInstance().getActiveGlobalAndExtruderStacks():
  332. stack.getTop().clear()
  333. ## Create quality changes containers from the user containers in the active stacks.
  334. #
  335. # This will go through the global and extruder stacks and create quality_changes containers from
  336. # the user containers in each stack. These then replace the quality_changes containers in the
  337. # stack and clear the user settings.
  338. #
  339. # \return \type{bool} True if the operation was successfully, False if not.
  340. @pyqtSlot(str, result = bool)
  341. def createQualityChanges(self, base_name):
  342. global_stack = UM.Application.getInstance().getGlobalContainerStack()
  343. if not global_stack:
  344. return False
  345. active_quality_name = self._machine_manager.activeQualityName
  346. if active_quality_name == "":
  347. UM.Logger.log("w", "No quality container found in stack %s, cannot create profile", global_stack.getId())
  348. return False
  349. self._machine_manager.blurSettings.emit()
  350. if base_name is None:
  351. base_name = active_quality_name
  352. unique_name = self._container_registry.uniqueName(base_name)
  353. # Go through the active stacks and create quality_changes containers from the user containers.
  354. for stack in cura.Settings.ExtruderManager.getInstance().getActiveGlobalAndExtruderStacks():
  355. user_container = stack.getTop()
  356. quality_container = stack.findContainer(type = "quality")
  357. quality_changes_container = stack.findContainer(type = "quality_changes")
  358. if not quality_container or not quality_changes_container:
  359. UM.Logger.log("w", "No quality or quality changes container found in stack %s, ignoring it", stack.getId())
  360. continue
  361. new_changes = self._createQualityChanges(quality_container, unique_name, stack.getId())
  362. self._performMerge(new_changes, user_container)
  363. self._container_registry.addContainer(new_changes)
  364. stack.replaceContainer(stack.getContainerIndex(quality_changes_container), new_changes)
  365. self._machine_manager.activeQualityChanged.emit()
  366. return True
  367. ## Remove all quality changes containers matching a specified name.
  368. #
  369. # This will search for quality_changes containers matching the supplied name and remove them.
  370. # Note that if the machine specifies that qualities should be filtered by machine and/or material
  371. # only the containers related to the active machine/material are removed.
  372. #
  373. # \param quality_name The name of the quality changes to remove.
  374. #
  375. # \return \type{bool} True if successful, False if not.
  376. @pyqtSlot(str, result = bool)
  377. def removeQualityChanges(self, quality_name):
  378. UM.Logger.log("d", "Attempting to remove the quality change containers with name %s", quality_name)
  379. containers_found = False
  380. if not quality_name:
  381. return containers_found # Without a name we will never find a container to remove.
  382. # If the container that is being removed is the currently active quality, set another quality as the active quality
  383. activate_quality = quality_name == self._machine_manager.activeQualityName
  384. activate_quality_type = None
  385. for container in self._getFilteredContainers(name = quality_name, type = "quality_changes"):
  386. containers_found = True
  387. if activate_quality and not activate_quality_type:
  388. activate_quality_type = container.getMetaDataEntry("quality")
  389. self._container_registry.removeContainer(container.getId())
  390. if not containers_found:
  391. UM.Logger.log("d", "Unable to remove quality containers, as we did not find any by the name of %s", quality_name)
  392. elif activate_quality:
  393. definition_id = "fdmprinter" if not self._machine_manager.filterQualityByMachine else self._machine_manager.activeDefinitionId
  394. containers = self._container_registry.findInstanceContainers(type = "quality", definition = definition_id, quality_type = activate_quality_type)
  395. if containers:
  396. self._machine_manager.setActiveQuality(containers[0].getId())
  397. self._machine_manager.activeQualityChanged.emit()
  398. return containers_found
  399. ## Rename a set of quality changes containers.
  400. #
  401. # This will search for quality_changes containers matching the supplied name and rename them.
  402. # Note that if the machine specifies that qualities should be filtered by machine and/or material
  403. # only the containers related to the active machine/material are renamed.
  404. #
  405. # \param quality_name The name of the quality changes containers to rename.
  406. # \param new_name The new name of the quality changes.
  407. #
  408. # \return True if successful, False if not.
  409. @pyqtSlot(str, str, result = bool)
  410. def renameQualityChanges(self, quality_name, new_name):
  411. UM.Logger.log("d", "User requested QualityChanges container rename of %s to %s", quality_name, new_name)
  412. if not quality_name or not new_name:
  413. return False
  414. if quality_name == new_name:
  415. UM.Logger.log("w", "Unable to rename %s to %s, because they are the same.", quality_name, new_name)
  416. return True
  417. global_stack = UM.Application.getInstance().getGlobalContainerStack()
  418. if not global_stack:
  419. return False
  420. self._machine_manager.blurSettings.emit()
  421. new_name = self._container_registry.uniqueName(new_name)
  422. container_registry = self._container_registry
  423. for container in self._getFilteredContainers(name = quality_name, type = "quality_changes"):
  424. stack_id = container.getMetaDataEntry("extruder", global_stack.getId())
  425. container_registry.renameContainer(container.getId(), new_name, self._createUniqueId(stack_id, new_name))
  426. self._machine_manager.activeQualityChanged.emit()
  427. return True
  428. ## Duplicate a specified set of quality or quality_changes containers.
  429. #
  430. # This will search for containers matching the specified name. If the container is a "quality" type container, a new
  431. # quality_changes container will be created with the specified quality as base. If the container is a "quality_changes"
  432. # container, it is simply duplicated and renamed.
  433. #
  434. # \param quality_name The name of the quality to duplicate.
  435. #
  436. # \return A string containing the name of the duplicated containers, or an empty string if it failed.
  437. @pyqtSlot(str, str, result = str)
  438. def duplicateQualityOrQualityChanges(self, quality_name, base_name):
  439. global_stack = UM.Application.getInstance().getGlobalContainerStack()
  440. if not global_stack or not quality_name:
  441. return ""
  442. UM.Logger.log("d", "Attempting to duplicate the quality %s", quality_name)
  443. containers = self._container_registry.findInstanceContainers(name = quality_name)
  444. if not containers:
  445. UM.Logger.log("d", "Unable to duplicate the quality %s, because it doesn't exist.", quality_name)
  446. return ""
  447. if base_name is None:
  448. base_name = quality_name
  449. new_name = self._container_registry.uniqueName(base_name)
  450. container_type = containers[0].getMetaDataEntry("type")
  451. if container_type == "quality":
  452. for container in self._getFilteredContainers(name = quality_name, type = "quality"):
  453. for stack in cura.Settings.ExtruderManager.getInstance().getActiveGlobalAndExtruderStacks():
  454. new_changes = self._createQualityChanges(container, new_name, stack.getId())
  455. self._container_registry.addContainer(new_changes)
  456. elif container_type == "quality_changes":
  457. for container in self._getFilteredContainers(name = quality_name, type = "quality_changes"):
  458. stack_id = container.getMetaDataEntry("extruder", global_stack.getId())
  459. new_container = container.duplicate(self._createUniqueId(stack_id, new_name), new_name)
  460. self._container_registry.addContainer(new_container)
  461. else:
  462. return ""
  463. return new_name
  464. # Factory function, used by QML
  465. @staticmethod
  466. def createContainerManager(engine, js_engine):
  467. return ContainerManager()
  468. def _performMerge(self, merge_into, merge):
  469. assert isinstance(merge, type(merge_into))
  470. if merge == merge_into:
  471. return
  472. for key in merge.getAllKeys():
  473. merge_into.setProperty(key, "value", merge.getProperty(key, "value"))
  474. merge.clear()
  475. def _updateContainerNameFilters(self):
  476. self._container_name_filters = {}
  477. for plugin_id, container_type in UM.Settings.ContainerRegistry.getContainerTypes():
  478. # Ignore default container types since those are not plugins
  479. if container_type in (UM.Settings.InstanceContainer, UM.Settings.ContainerStack, UM.Settings.DefinitionContainer):
  480. continue
  481. serialize_type = ""
  482. try:
  483. plugin_metadata = UM.PluginRegistry.getInstance().getMetaData(plugin_id)
  484. if plugin_metadata:
  485. serialize_type = plugin_metadata["settings_container"]["type"]
  486. else:
  487. continue
  488. except KeyError as e:
  489. continue
  490. mime_type = UM.Settings.ContainerRegistry.getMimeTypeForContainer(container_type)
  491. entry = {
  492. "type": serialize_type,
  493. "mime": mime_type,
  494. "container": container_type
  495. }
  496. suffix = mime_type.preferredSuffix
  497. if UM.Platform.isOSX() and "." in suffix:
  498. # OSX's File dialog is stupid and does not allow selecting files with a . in its name
  499. suffix = suffix[suffix.index(".") + 1:]
  500. suffix_list = "*." + suffix
  501. for suffix in mime_type.suffixes:
  502. if suffix == mime_type.preferredSuffix:
  503. continue
  504. if UM.Platform.isOSX() and "." in suffix:
  505. # OSX's File dialog is stupid and does not allow selecting files with a . in its name
  506. suffix = suffix[suffix.index("."):]
  507. suffix_list += ", *." + suffix
  508. name_filter = "{0} ({1})".format(mime_type.comment, suffix_list)
  509. self._container_name_filters[name_filter] = entry
  510. ## Return a generator that iterates over a set of containers that are filtered by machine and material when needed.
  511. #
  512. # \param kwargs Initial search criteria that the containers need to match.
  513. #
  514. # \return A generator that iterates over the list of containers matching the search criteria.
  515. def _getFilteredContainers(self, **kwargs):
  516. global_stack = UM.Application.getInstance().getGlobalContainerStack()
  517. if not global_stack:
  518. return False
  519. criteria = kwargs
  520. filter_by_material = False
  521. if global_stack.getMetaDataEntry("has_machine_quality"):
  522. criteria["definition"] = global_stack.getBottom().getId()
  523. filter_by_material = global_stack.getMetaDataEntry("has_materials")
  524. material_ids = []
  525. if filter_by_material:
  526. for stack in cura.Settings.ExtruderManager.getInstance().getActiveGlobalAndExtruderStacks():
  527. material_ids.append(stack.findContainer(type = "material").getId())
  528. containers = self._container_registry.findInstanceContainers(**criteria)
  529. for container in containers:
  530. # If the machine specifies we should filter by material, exclude containers that do not match any active material.
  531. if filter_by_material and container.getMetaDataEntry("material") not in material_ids:
  532. continue
  533. yield container
  534. ## Creates a unique ID for a container by prefixing the name with the stack ID.
  535. #
  536. # This method creates a unique ID for a container by prefixing it with a specified stack ID.
  537. # This is done to ensure we have an easily identified ID for quality changes, which have the
  538. # same name across several stacks.
  539. #
  540. # \param stack_id The ID of the stack to prepend.
  541. # \param container_name The name of the container that we are creating a unique ID for.
  542. #
  543. # \return Container name prefixed with stack ID, in lower case with spaces replaced by underscores.
  544. def _createUniqueId(self, stack_id, container_name):
  545. result = stack_id + "_" + container_name
  546. result = result.lower()
  547. result.replace(" ", "_")
  548. return result
  549. ## Create a quality changes container for a specified quality container.
  550. #
  551. # \param quality_container The quality container to create a changes container for.
  552. # \param new_name The name of the new quality_changes container.
  553. # \param stack_id The ID of the container stack the new container "belongs to". It is used primarily to ensure a unique ID.
  554. #
  555. # \return A new quality_changes container with the specified container as base.
  556. def _createQualityChanges(self, quality_container, new_name, stack_id):
  557. global_stack = UM.Application.getInstance().getGlobalContainerStack()
  558. assert global_stack is not None
  559. # Create a new quality_changes container for the quality.
  560. quality_changes = UM.Settings.InstanceContainer(self._createUniqueId(stack_id, new_name))
  561. quality_changes.setName(new_name)
  562. quality_changes.addMetaDataEntry("type", "quality_changes")
  563. quality_changes.addMetaDataEntry("quality", quality_container.getMetaDataEntry("quality_type"))
  564. # If we are creating a container for an extruder, ensure we add that to the container
  565. if stack_id != global_stack.getId():
  566. quality_changes.addMetaDataEntry("extruder", stack_id)
  567. # If the machine specifies qualities should be filtered, ensure we match the current criteria.
  568. if not global_stack.getMetaDataEntry("has_machine_quality"):
  569. quality_changes.setDefinition(self._container_registry.findContainers(id = "fdmprinter")[0])
  570. else:
  571. quality_changes.setDefinition(global_stack.getBottom())
  572. if global_stack.getMetaDataEntry("has_materials"):
  573. material = quality_container.getMetaDataEntry("material")
  574. quality_changes.addMetaDataEntry("material", material)
  575. return quality_changes