ContainerManager.py 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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 Any, Dict, List, Union
  8. from PyQt5.QtCore import QObject, QUrl, QVariant
  9. from UM.FlameProfiler import pyqtSlot
  10. from PyQt5.QtWidgets import QMessageBox
  11. from UM.Util import parseBool
  12. from UM.PluginRegistry import PluginRegistry
  13. from UM.SaveFile import SaveFile
  14. from UM.Platform import Platform
  15. from UM.MimeTypeDatabase import MimeTypeDatabase
  16. from UM.Logger import Logger
  17. from UM.Application import Application
  18. from UM.Settings.ContainerStack import ContainerStack
  19. from UM.Settings.DefinitionContainer import DefinitionContainer
  20. from UM.Settings.InstanceContainer import InstanceContainer
  21. from cura.QualityManager import QualityManager
  22. from UM.MimeTypeDatabase import MimeTypeNotFoundError
  23. from UM.Settings.ContainerRegistry import ContainerRegistry
  24. from UM.i18n import i18nCatalog
  25. from cura.Settings.ExtruderManager import ExtruderManager
  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._material_manager
  39. self._container_name_filters = {}
  40. ## Create a duplicate of the specified container
  41. #
  42. # This will create and add a duplicate of the container corresponding
  43. # to the container ID.
  44. #
  45. # \param container_id \type{str} The ID of the container to duplicate.
  46. #
  47. # \return The ID of the new container, or an empty string if duplication failed.
  48. @pyqtSlot(str, result = str)
  49. def duplicateContainer(self, container_id):
  50. #TODO: It should be able to duplicate a container of which only the metadata is known.
  51. containers = self._container_registry.findContainers(id = container_id)
  52. if not containers:
  53. Logger.log("w", "Could duplicate container %s because it was not found.", container_id)
  54. return ""
  55. container = containers[0]
  56. new_container = self.duplicateContainerInstance(container)
  57. return new_container.getId()
  58. ## Create a duplicate of the given container instance
  59. #
  60. # This will create and add a duplicate of the container that was passed.
  61. #
  62. # \param container \type{ContainerInterface} The container to duplicate.
  63. #
  64. # \return The duplicated container, or None if duplication failed.
  65. def duplicateContainerInstance(self, container):
  66. new_container = None
  67. new_name = self._container_registry.uniqueName(container.getName())
  68. # Only InstanceContainer has a duplicate method at the moment.
  69. # So fall back to serialize/deserialize when no duplicate method exists.
  70. if hasattr(container, "duplicate"):
  71. new_container = container.duplicate(new_name)
  72. else:
  73. new_container = container.__class__(new_name)
  74. new_container.deserialize(container.serialize())
  75. new_container.setName(new_name)
  76. # TODO: we probably don't want to add it to the registry here!
  77. if new_container:
  78. self._container_registry.addContainer(new_container)
  79. return new_container
  80. ## Change the name of a specified container to a new name.
  81. #
  82. # \param container_id \type{str} The ID of the container to change the name of.
  83. # \param new_id \type{str} The new ID of the container.
  84. # \param new_name \type{str} The new name of the specified container.
  85. #
  86. # \return True if successful, False if not.
  87. @pyqtSlot(str, str, str, result = bool)
  88. def renameContainer(self, container_id, new_id, new_name):
  89. containers = self._container_registry.findContainers(id = container_id)
  90. if not containers:
  91. Logger.log("w", "Could rename container %s because it was not found.", container_id)
  92. return False
  93. container = containers[0]
  94. # First, remove the container from the registry. This will clean up any files related to the container.
  95. self._container_registry.removeContainer(container_id)
  96. # Ensure we have a unique name for the container
  97. new_name = self._container_registry.uniqueName(new_name)
  98. # Then, update the name and ID of the container
  99. container.setName(new_name)
  100. container._id = new_id # TODO: Find a nicer way to set a new, unique ID
  101. # Finally, re-add the container so it will be properly serialized again.
  102. self._container_registry.addContainer(container)
  103. return True
  104. ## Remove the specified container.
  105. #
  106. # \param container_id \type{str} The ID of the container to remove.
  107. #
  108. # \return True if the container was successfully removed, False if not.
  109. @pyqtSlot(str, result = bool)
  110. def removeContainer(self, container_id):
  111. containers = self._container_registry.findContainers(id = container_id)
  112. if not containers:
  113. Logger.log("w", "Could not remove container %s because it was not found.", container_id)
  114. return False
  115. self._container_registry.removeContainer(containers[0].getId())
  116. return True
  117. ## Merge a container with another.
  118. #
  119. # This will try to merge one container into the other, by going through the container
  120. # and setting the right properties on the other container.
  121. #
  122. # \param merge_into_id \type{str} The ID of the container to merge into.
  123. # \param merge_id \type{str} The ID of the container to merge.
  124. #
  125. # \return True if successfully merged, False if not.
  126. @pyqtSlot(str, result = bool)
  127. def mergeContainers(self, merge_into_id, merge_id):
  128. containers = self._container_registry.findContainers(id = merge_into_id)
  129. if not containers:
  130. Logger.log("w", "Could merge into container %s because it was not found.", merge_into_id)
  131. return False
  132. merge_into = containers[0]
  133. containers = self._container_registry.findContainers(id = merge_id)
  134. if not containers:
  135. Logger.log("w", "Could not merge container %s because it was not found", merge_id)
  136. return False
  137. merge = containers[0]
  138. if not isinstance(merge, type(merge_into)):
  139. Logger.log("w", "Cannot merge two containers of different types")
  140. return False
  141. self._performMerge(merge_into, merge)
  142. return True
  143. ## Clear the contents of a container.
  144. #
  145. # \param container_id \type{str} The ID of the container to clear.
  146. #
  147. # \return True if successful, False if not.
  148. @pyqtSlot(str, result = bool)
  149. def clearContainer(self, container_id):
  150. if self._container_registry.isReadOnly(container_id):
  151. Logger.log("w", "Cannot clear read-only container %s", container_id)
  152. return False
  153. containers = self._container_registry.findContainers(id = container_id)
  154. if not containers:
  155. Logger.log("w", "Could clear container %s because it was not found.", container_id)
  156. return False
  157. containers[0].clear()
  158. return True
  159. @pyqtSlot(str, str, result=str)
  160. def getContainerMetaDataEntry(self, container_id, entry_name):
  161. metadatas = self._container_registry.findContainersMetadata(id = container_id)
  162. if not metadatas:
  163. Logger.log("w", "Could not get metadata of container %s because it was not found.", container_id)
  164. return ""
  165. return str(metadatas[0].get(entry_name, ""))
  166. ## Set a metadata entry of the specified container.
  167. #
  168. # This will set the specified entry of the container's metadata to the specified
  169. # value. Note that entries containing dictionaries can have their entries changed
  170. # by using "/" as a separator. For example, to change an entry "foo" in a
  171. # dictionary entry "bar", you can specify "bar/foo" as entry name.
  172. #
  173. # \param container_id \type{str} The ID of the container to change.
  174. # \param entry_name \type{str} The name of the metadata entry to change.
  175. # \param entry_value The new value of the entry.
  176. #
  177. # \return True if successful, False if not.
  178. # TODO: This is ONLY used by MaterialView for material containers. Maybe refactor this.
  179. @pyqtSlot("QVariant", str, str)
  180. def setContainerMetaDataEntry(self, container_node, entry_name, entry_value):
  181. root_material_id = container_node.metadata["base_file"]
  182. if self._container_registry.isReadOnly(root_material_id):
  183. Logger.log("w", "Cannot set metadata of read-only container %s.", root_material_id)
  184. return False
  185. material_group = self._material_manager.getMaterialGroup(root_material_id)
  186. entries = entry_name.split("/")
  187. entry_name = entries.pop()
  188. sub_item_changed = False
  189. if entries:
  190. root_name = entries.pop(0)
  191. root = material_group.root_material_node.metadata.get(root_name)
  192. item = root
  193. for _ in range(len(entries)):
  194. item = item.get(entries.pop(0), { })
  195. if item[entry_name] != entry_value:
  196. sub_item_changed = True
  197. item[entry_name] = entry_value
  198. entry_name = root_name
  199. entry_value = root
  200. container = material_group.root_material_node.getContainer()
  201. container.setMetaDataEntry(entry_name, entry_value)
  202. 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.
  203. container.metaDataChanged.emit(container)
  204. ## Set a setting property of the specified container.
  205. #
  206. # This will set the specified property of the specified setting of the container
  207. # and all containers that share the same base_file (if any). The latter only
  208. # happens for material containers.
  209. #
  210. # \param container_id \type{str} The ID of the container to change.
  211. # \param setting_key \type{str} The key of the setting.
  212. # \param property_name \type{str} The name of the property, eg "value".
  213. # \param property_value \type{str} The new value of the property.
  214. #
  215. # \return True if successful, False if not.
  216. @pyqtSlot(str, str, str, str, result = bool)
  217. def setContainerProperty(self, container_id, setting_key, property_name, property_value):
  218. if self._container_registry.isReadOnly(container_id):
  219. Logger.log("w", "Cannot set properties of read-only container %s.", container_id)
  220. return False
  221. containers = self._container_registry.findContainers(id = container_id)
  222. if not containers:
  223. Logger.log("w", "Could not set properties of container %s because it was not found.", container_id)
  224. return False
  225. container = containers[0]
  226. container.setProperty(setting_key, property_name, property_value)
  227. basefile = container.getMetaDataEntry("base_file", container_id)
  228. for sibbling_container in ContainerRegistry.getInstance().findInstanceContainers(base_file = basefile):
  229. if sibbling_container != container:
  230. sibbling_container.setProperty(setting_key, property_name, property_value)
  231. return True
  232. ## Get a setting property of the specified container.
  233. #
  234. # This will get the specified property of the specified setting of the
  235. # specified container.
  236. #
  237. # \param container_id The ID of the container to get the setting property
  238. # of.
  239. # \param setting_key The key of the setting to get the property of.
  240. # \param property_name The property to obtain.
  241. # \return The value of the specified property. The type of this property
  242. # value depends on the type of the property. For instance, the "value"
  243. # property of an integer setting will be a Python int, but the "value"
  244. # property of an enum setting will be a Python str.
  245. @pyqtSlot(str, str, str, result = QVariant)
  246. def getContainerProperty(self, container_id: str, setting_key: str, property_name: str):
  247. containers = self._container_registry.findContainers(id = container_id)
  248. if not containers:
  249. Logger.log("w", "Could not get properties of container %s because it was not found.", container_id)
  250. return ""
  251. container = containers[0]
  252. return container.getProperty(setting_key, property_name)
  253. ## Set the name of the specified container.
  254. @pyqtSlot(str, str, result = bool)
  255. def setContainerName(self, container_id, new_name):
  256. if self._container_registry.isReadOnly(container_id):
  257. Logger.log("w", "Cannot set name of read-only container %s.", container_id)
  258. return False
  259. containers = self._container_registry.findContainers(id = container_id) #We need to get the full container, not just metadata, since we need to know whether it's read-only.
  260. if not containers:
  261. Logger.log("w", "Could not set name of container %s because it was not found.", container_id)
  262. return False
  263. containers[0].setName(new_name)
  264. return True
  265. ## Find instance containers matching certain criteria.
  266. #
  267. # This effectively forwards to
  268. # ContainerRegistry::findInstanceContainersMetadata.
  269. #
  270. # \param criteria A dict of key - value pairs to search for.
  271. #
  272. # \return A list of container IDs that match the given criteria.
  273. @pyqtSlot("QVariantMap", result = "QVariantList")
  274. def findInstanceContainers(self, criteria):
  275. return [entry["id"] for entry in self._container_registry.findInstanceContainersMetadata(**criteria)]
  276. @pyqtSlot(str, result = bool)
  277. def isContainerUsed(self, container_id):
  278. Logger.log("d", "Checking if container %s is currently used", container_id)
  279. # check if this is a material container. If so, check if any material with the same base is being used by any
  280. # stacks.
  281. container_ids_to_check = [container_id]
  282. container_results = self._container_registry.findInstanceContainersMetadata(id = container_id, type = "material")
  283. if container_results:
  284. this_container = container_results[0]
  285. material_base_file = this_container["id"]
  286. if "base_file" in this_container:
  287. material_base_file = this_container["base_file"]
  288. # check all material container IDs with the same base
  289. material_containers = self._container_registry.findInstanceContainersMetadata(base_file = material_base_file,
  290. type = "material")
  291. if material_containers:
  292. container_ids_to_check = [container["id"] for container in material_containers]
  293. all_stacks = self._container_registry.findContainerStacks()
  294. for stack in all_stacks:
  295. for used_container_id in container_ids_to_check:
  296. if used_container_id in [child.getId() for child in stack.getContainers()]:
  297. Logger.log("d", "The container is in use by %s", stack.getId())
  298. return True
  299. return False
  300. @pyqtSlot(str, result = str)
  301. def makeUniqueName(self, original_name):
  302. return self._container_registry.uniqueName(original_name)
  303. ## Get a list of string that can be used as name filters for a Qt File Dialog
  304. #
  305. # This will go through the list of available container types and generate a list of strings
  306. # out of that. The strings are formatted as "description (*.extension)" and can be directly
  307. # passed to a nameFilters property of a Qt File Dialog.
  308. #
  309. # \param type_name Which types of containers to list. These types correspond to the "type"
  310. # key of the plugin metadata.
  311. #
  312. # \return A string list with name filters.
  313. @pyqtSlot(str, result = "QStringList")
  314. def getContainerNameFilters(self, type_name):
  315. if not self._container_name_filters:
  316. self._updateContainerNameFilters()
  317. filters = []
  318. for filter_string, entry in self._container_name_filters.items():
  319. if not type_name or entry["type"] == type_name:
  320. filters.append(filter_string)
  321. filters.append("All Files (*)")
  322. return filters
  323. ## Export a container to a file
  324. #
  325. # \param container_id The ID of the container to export
  326. # \param file_type The type of file to save as. Should be in the form of "description (*.extension, *.ext)"
  327. # \param file_url_or_string The URL where to save the file.
  328. #
  329. # \return A dictionary containing a key "status" with a status code and a key "message" with a message
  330. # explaining the status.
  331. # The status code can be one of "error", "cancelled", "success"
  332. @pyqtSlot(str, str, QUrl, result = "QVariantMap")
  333. def exportContainer(self, container_id: str, file_type: str, file_url_or_string: Union[QUrl, str]) -> Dict[str, str]:
  334. if not container_id or not file_type or not file_url_or_string:
  335. return {"status": "error", "message": "Invalid arguments"}
  336. if isinstance(file_url_or_string, QUrl):
  337. file_url = file_url_or_string.toLocalFile()
  338. else:
  339. file_url = file_url_or_string
  340. if not file_url:
  341. return {"status": "error", "message": "Invalid path"}
  342. mime_type = None
  343. if file_type not in self._container_name_filters:
  344. try:
  345. mime_type = MimeTypeDatabase.getMimeTypeForFile(file_url)
  346. except MimeTypeNotFoundError:
  347. return {"status": "error", "message": "Unknown File Type"}
  348. else:
  349. mime_type = self._container_name_filters[file_type]["mime"]
  350. containers = self._container_registry.findContainers(id = container_id)
  351. if not containers:
  352. return {"status": "error", "message": "Container not found"}
  353. container = containers[0]
  354. if Platform.isOSX() and "." in file_url:
  355. file_url = file_url[:file_url.rfind(".")]
  356. for suffix in mime_type.suffixes:
  357. if file_url.endswith(suffix):
  358. break
  359. else:
  360. file_url += "." + mime_type.preferredSuffix
  361. if not Platform.isWindows():
  362. if os.path.exists(file_url):
  363. result = QMessageBox.question(None, catalog.i18nc("@title:window", "File Already Exists"),
  364. 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))
  365. if result == QMessageBox.No:
  366. return {"status": "cancelled", "message": "User cancelled"}
  367. try:
  368. contents = container.serialize()
  369. except NotImplementedError:
  370. return {"status": "error", "message": "Unable to serialize container"}
  371. if contents is None:
  372. return {"status": "error", "message": "Serialization returned None. Unable to write to file"}
  373. with SaveFile(file_url, "w") as f:
  374. f.write(contents)
  375. return {"status": "success", "message": "Successfully exported container", "path": file_url}
  376. ## Imports a profile from a file
  377. #
  378. # \param file_url A URL that points to the file to import.
  379. #
  380. # \return \type{Dict} dict with a 'status' key containing the string 'success' or 'error', and a 'message' key
  381. # containing a message for the user
  382. @pyqtSlot(QUrl, result = "QVariantMap")
  383. def importMaterialContainer(self, file_url_or_string: Union[QUrl, str]) -> Dict[str, str]:
  384. if not file_url_or_string:
  385. return {"status": "error", "message": "Invalid path"}
  386. if isinstance(file_url_or_string, QUrl):
  387. file_url = file_url_or_string.toLocalFile()
  388. else:
  389. file_url = file_url_or_string
  390. if not file_url or not os.path.exists(file_url):
  391. return {"status": "error", "message": "Invalid path"}
  392. try:
  393. mime_type = MimeTypeDatabase.getMimeTypeForFile(file_url)
  394. except MimeTypeNotFoundError:
  395. return {"status": "error", "message": "Could not determine mime type of file"}
  396. container_type = self._container_registry.getContainerForMimeType(mime_type)
  397. if not container_type:
  398. return {"status": "error", "message": "Could not find a container to handle the specified file."}
  399. container_id = urllib.parse.unquote_plus(mime_type.stripExtension(os.path.basename(file_url)))
  400. container_id = self._container_registry.uniqueName(container_id)
  401. container = container_type(container_id)
  402. try:
  403. with open(file_url, "rt", encoding = "utf-8") as f:
  404. container.deserialize(f.read())
  405. except PermissionError:
  406. return {"status": "error", "message": "Permission denied when trying to read the file"}
  407. except Exception as ex:
  408. return {"status": "error", "message": str(ex)}
  409. container.setDirty(True)
  410. self._container_registry.addContainer(container)
  411. return {"status": "success", "message": "Successfully imported container {0}".format(container.getName())}
  412. ## Update the current active quality changes container with the settings from the user container.
  413. #
  414. # This will go through the active global stack and all active extruder stacks and merge the changes from the user
  415. # container into the quality_changes container. After that, the user container is cleared.
  416. #
  417. # \return \type{bool} True if successful, False if not.
  418. @pyqtSlot(result = bool)
  419. def updateQualityChanges(self):
  420. global_stack = Application.getInstance().getGlobalContainerStack()
  421. if not global_stack:
  422. return False
  423. self._machine_manager.blurSettings.emit()
  424. for stack in ExtruderManager.getInstance().getActiveGlobalAndExtruderStacks():
  425. # Find the quality_changes container for this stack and merge the contents of the top container into it.
  426. quality_changes = stack.qualityChanges
  427. if not quality_changes or self._container_registry.isReadOnly(quality_changes.getId()):
  428. Logger.log("e", "Could not update quality of a nonexistant or read only quality profile in stack %s", stack.getId())
  429. continue
  430. self._performMerge(quality_changes, stack.getTop())
  431. self._machine_manager.activeQualityChanged.emit()
  432. return True
  433. ## Clear the top-most (user) containers of the active stacks.
  434. @pyqtSlot()
  435. def clearUserContainers(self) -> None:
  436. self._machine_manager.blurSettings.emit()
  437. send_emits_containers = []
  438. # Go through global and extruder stacks and clear their topmost container (the user settings).
  439. for stack in ExtruderManager.getInstance().getActiveGlobalAndExtruderStacks():
  440. container = stack.getTop()
  441. container.clear()
  442. send_emits_containers.append(container)
  443. for container in send_emits_containers:
  444. container.sendPostponedEmits()
  445. ## Create quality changes containers from the user containers in the active stacks.
  446. #
  447. # This will go through the global and extruder stacks and create quality_changes containers from
  448. # the user containers in each stack. These then replace the quality_changes containers in the
  449. # stack and clear the user settings.
  450. #
  451. # \return \type{bool} True if the operation was successfully, False if not.
  452. @pyqtSlot(str, result = bool)
  453. def createQualityChanges(self, base_name):
  454. global_stack = Application.getInstance().getGlobalContainerStack()
  455. if not global_stack:
  456. return False
  457. active_quality_name = self._machine_manager.activeQualityName
  458. if active_quality_name == "":
  459. Logger.log("w", "No quality container found in stack %s, cannot create profile", global_stack.getId())
  460. return False
  461. self._machine_manager.blurSettings.emit()
  462. if base_name is None or base_name == "":
  463. base_name = active_quality_name
  464. unique_name = self._container_registry.uniqueName(base_name)
  465. # Go through the active stacks and create quality_changes containers from the user containers.
  466. for stack in ExtruderManager.getInstance().getActiveGlobalAndExtruderStacks():
  467. user_container = stack.getTop()
  468. quality_container = stack.quality
  469. quality_changes_container = stack.qualityChanges
  470. if not quality_container or not quality_changes_container:
  471. Logger.log("w", "No quality or quality changes container found in stack %s, ignoring it", stack.getId())
  472. continue
  473. extruder_id = None if stack is global_stack else QualityManager.getInstance().getParentMachineDefinition(stack.getBottom()).getId()
  474. new_changes = self._createQualityChanges(quality_container, unique_name,
  475. Application.getInstance().getGlobalContainerStack().getBottom(),
  476. extruder_id)
  477. self._performMerge(new_changes, quality_changes_container, clear_settings = False)
  478. self._performMerge(new_changes, user_container)
  479. self._container_registry.addContainer(new_changes)
  480. stack.replaceContainer(stack.getContainerIndex(quality_changes_container), new_changes)
  481. self._machine_manager.activeQualityChanged.emit()
  482. return True
  483. ## Remove all quality changes containers matching a specified name.
  484. #
  485. # This will search for quality_changes containers matching the supplied name and remove them.
  486. # Note that if the machine specifies that qualities should be filtered by machine and/or material
  487. # only the containers related to the active machine/material are removed.
  488. #
  489. # \param quality_name The name of the quality changes to remove.
  490. #
  491. # \return \type{bool} True if successful, False if not.
  492. @pyqtSlot(str, result = bool)
  493. def removeQualityChanges(self, quality_name):
  494. Logger.log("d", "Attempting to remove the quality change containers with name %s", quality_name)
  495. containers_found = False
  496. if not quality_name:
  497. return containers_found # Without a name we will never find a container to remove.
  498. # If the container that is being removed is the currently active quality, set another quality as the active quality
  499. activate_quality = quality_name == self._machine_manager.activeQualityName
  500. activate_quality_type = None
  501. global_stack = Application.getInstance().getGlobalContainerStack()
  502. if not global_stack or not quality_name:
  503. return ""
  504. machine_definition = QualityManager.getInstance().getParentMachineDefinition(global_stack.getBottom())
  505. for container in QualityManager.getInstance().findQualityChangesByName(quality_name, machine_definition):
  506. containers_found = True
  507. if activate_quality and not activate_quality_type:
  508. activate_quality_type = container.getMetaDataEntry("quality")
  509. self._container_registry.removeContainer(container.getId())
  510. if not containers_found:
  511. Logger.log("d", "Unable to remove quality containers, as we did not find any by the name of %s", quality_name)
  512. elif activate_quality:
  513. definition_id = "fdmprinter" if not self._machine_manager.filterQualityByMachine else self._machine_manager.activeDefinitionId
  514. containers = self._container_registry.findInstanceContainersMetadata(type = "quality", definition = definition_id, quality_type = activate_quality_type)
  515. if containers:
  516. self._machine_manager.setActiveQuality(containers[0]["id"])
  517. self._machine_manager.activeQualityChanged.emit()
  518. return containers_found
  519. ## Rename a set of quality changes containers.
  520. #
  521. # This will search for quality_changes containers matching the supplied name and rename them.
  522. # Note that if the machine specifies that qualities should be filtered by machine and/or material
  523. # only the containers related to the active machine/material are renamed.
  524. #
  525. # \param quality_name The name of the quality changes containers to rename.
  526. # \param new_name The new name of the quality changes.
  527. #
  528. # \return True if successful, False if not.
  529. @pyqtSlot(str, str, result = bool)
  530. def renameQualityChanges(self, quality_name, new_name):
  531. Logger.log("d", "User requested QualityChanges container rename of %s to %s", quality_name, new_name)
  532. if not quality_name or not new_name:
  533. return False
  534. if quality_name == new_name:
  535. Logger.log("w", "Unable to rename %s to %s, because they are the same.", quality_name, new_name)
  536. return True
  537. global_stack = Application.getInstance().getGlobalContainerStack()
  538. if not global_stack:
  539. return False
  540. self._machine_manager.blurSettings.emit()
  541. new_name = self._container_registry.uniqueName(new_name)
  542. container_registry = self._container_registry
  543. containers_to_rename = self._container_registry.findInstanceContainersMetadata(type = "quality_changes", name = quality_name)
  544. for container in containers_to_rename:
  545. stack_id = global_stack.getId()
  546. if "extruder" in container:
  547. stack_id = container["extruder"]
  548. container_registry.renameContainer(container["id"], new_name, self._createUniqueId(stack_id, new_name))
  549. if not containers_to_rename:
  550. Logger.log("e", "Unable to rename %s, because we could not find the profile", quality_name)
  551. self._machine_manager.activeQualityChanged.emit()
  552. return True
  553. ## Duplicate a specified set of quality or quality_changes containers.
  554. #
  555. # This will search for containers matching the specified name. If the container is a "quality" type container, a new
  556. # quality_changes container will be created with the specified quality as base. If the container is a "quality_changes"
  557. # container, it is simply duplicated and renamed.
  558. #
  559. # \param quality_name The name of the quality to duplicate.
  560. #
  561. # \return A string containing the name of the duplicated containers, or an empty string if it failed.
  562. @pyqtSlot(str, str, result = str)
  563. def duplicateQualityOrQualityChanges(self, quality_name, base_name):
  564. global_stack = Application.getInstance().getGlobalContainerStack()
  565. if not global_stack or not quality_name:
  566. return ""
  567. machine_definition = global_stack.definition
  568. active_stacks = ExtruderManager.getInstance().getActiveGlobalAndExtruderStacks()
  569. if active_stacks is None:
  570. return ""
  571. material_metadatas = [stack.material.getMetaData() for stack in active_stacks]
  572. result = self._duplicateQualityOrQualityChangesForMachineType(quality_name, base_name,
  573. QualityManager.getInstance().getParentMachineDefinition(machine_definition),
  574. material_metadatas)
  575. return result[0].getName() if result else ""
  576. ## Duplicate a quality or quality changes profile specific to a machine type
  577. #
  578. # \param quality_name The name of the quality or quality changes container to duplicate.
  579. # \param base_name The desired name for the new container.
  580. # \param machine_definition The machine with the specific machine type.
  581. # \param material_metadatas Metadata of materials
  582. # \return List of duplicated quality profiles.
  583. def _duplicateQualityOrQualityChangesForMachineType(self, quality_name: str, base_name: str, machine_definition: DefinitionContainer, material_metadatas: List[Dict[str, Any]]) -> List[InstanceContainer]:
  584. Logger.log("d", "Attempting to duplicate the quality %s", quality_name)
  585. if base_name is None:
  586. base_name = quality_name
  587. # Try to find a Quality with the name.
  588. container = QualityManager.getInstance().findQualityByName(quality_name, machine_definition, material_metadatas)
  589. if container:
  590. Logger.log("d", "We found a quality to duplicate.")
  591. return self._duplicateQualityForMachineType(container, base_name, machine_definition)
  592. Logger.log("d", "We found a quality_changes to duplicate.")
  593. # Assume it is a quality changes.
  594. return self._duplicateQualityChangesForMachineType(quality_name, base_name, machine_definition)
  595. # Duplicate a quality profile
  596. def _duplicateQualityForMachineType(self, quality_container, base_name, machine_definition) -> List[InstanceContainer]:
  597. if base_name is None:
  598. base_name = quality_container.getName()
  599. new_name = self._container_registry.uniqueName(base_name)
  600. new_change_instances = []
  601. # Handle the global stack first.
  602. global_changes = self._createQualityChanges(quality_container, new_name, machine_definition, None)
  603. new_change_instances.append(global_changes)
  604. self._container_registry.addContainer(global_changes)
  605. # Handle the extruders if present.
  606. extruders = machine_definition.getMetaDataEntry("machine_extruder_trains")
  607. if extruders:
  608. for extruder_id in extruders:
  609. extruder = extruders[extruder_id]
  610. new_changes = self._createQualityChanges(quality_container, new_name, machine_definition, extruder)
  611. new_change_instances.append(new_changes)
  612. self._container_registry.addContainer(new_changes)
  613. return new_change_instances
  614. # Duplicate a quality changes container
  615. def _duplicateQualityChangesForMachineType(self, quality_changes_name, base_name, machine_definition) -> List[InstanceContainer]:
  616. new_change_instances = []
  617. for container in QualityManager.getInstance().findQualityChangesByName(quality_changes_name,
  618. machine_definition):
  619. base_id = container.getMetaDataEntry("extruder")
  620. if not base_id:
  621. base_id = container.getDefinition().getId()
  622. new_unique_id = self._createUniqueId(base_id, base_name)
  623. new_container = container.duplicate(new_unique_id, base_name)
  624. new_change_instances.append(new_container)
  625. self._container_registry.addContainer(new_container)
  626. return new_change_instances
  627. @pyqtSlot("QVariant")
  628. def removeMaterial(self, material_node):
  629. root_material_id = material_node.metadata["base_file"]
  630. material_group = self._material_manager.getMaterialGroup(root_material_id)
  631. if not material_group:
  632. Logger.log("d", "Unable to remove the material with id %s, because it doesn't exist.", root_material_id)
  633. return
  634. nodes_to_remove = [material_group.root_material_node] + material_group.derived_material_node_list
  635. for node in nodes_to_remove:
  636. self._container_registry.removeContainer(node.metadata["id"])
  637. ## Create a duplicate of a material, which has the same GUID and base_file metadata
  638. #
  639. # \return \type{str} the id of the newly created container.
  640. @pyqtSlot("QVariant")
  641. def duplicateMaterial(self, material_node, new_base_id = None, new_metadata = None):
  642. root_material_id = material_node.metadata["base_file"]
  643. material_group = self._material_manager.getMaterialGroup(root_material_id)
  644. if not material_group:
  645. Logger.log("d", "Unable to duplicate the material with id %s, because it doesn't exist.", root_material_id)
  646. return
  647. base_container = material_group.root_material_node.getContainer()
  648. containers_to_copy = []
  649. for node in material_group.derived_material_node_list:
  650. containers_to_copy.append(node.getContainer())
  651. # Ensure all settings are saved.
  652. Application.getInstance().saveSettings()
  653. # Create a new ID & container to hold the data.
  654. new_containers = []
  655. if new_base_id is None:
  656. new_base_id = self._container_registry.uniqueName(base_container.getId())
  657. new_base_container = copy.deepcopy(base_container)
  658. new_base_container.getMetaData()["id"] = new_base_id
  659. new_base_container.getMetaData()["base_file"] = new_base_id
  660. if new_metadata is not None:
  661. for key, value in new_metadata.items():
  662. new_base_container.getMetaData()[key] = value
  663. new_containers.append(new_base_container)
  664. # Clone all of them.
  665. for container_to_copy in containers_to_copy:
  666. # Create unique IDs for every clone.
  667. new_id = new_base_id
  668. if container_to_copy.getMetaDataEntry("definition") != "fdmprinter":
  669. new_id += "_" + container_to_copy.getMetaDataEntry("definition")
  670. if container_to_copy.getMetaDataEntry("variant_name"):
  671. variant_name = container_to_copy.getMetaDataEntry("variant_name")
  672. new_id += "_" + variant_name.replace(" ", "_")
  673. new_container = copy.deepcopy(container_to_copy)
  674. new_container.getMetaData()["id"] = new_id
  675. new_container.getMetaData()["base_file"] = new_base_id
  676. if new_metadata is not None:
  677. for key, value in new_metadata.items():
  678. new_container.getMetaData()[key] = value
  679. new_containers.append(new_container)
  680. for container_to_add in new_containers:
  681. container_to_add.setDirty(True)
  682. ContainerRegistry.getInstance().addContainer(container_to_add)
  683. ## Create a new material by cloning Generic PLA for the current material diameter and setting the GUID to something unqiue
  684. #
  685. # \return \type{str} the id of the newly created container.
  686. @pyqtSlot()
  687. def createMaterial(self):
  688. # Ensure all settings are saved.
  689. Application.getInstance().saveSettings()
  690. global_stack = Application.getInstance().getGlobalContainerStack()
  691. approximate_diameter = str(round(global_stack.getProperty("material_diameter", "value")))
  692. root_material_id = "generic_pla"
  693. root_material_id = self._material_manager.getRootMaterialIDForDiameter(root_material_id, approximate_diameter)
  694. material_group = self._material_manager.getMaterialGroup(root_material_id)
  695. # Create a new ID & container to hold the data.
  696. new_id = self._container_registry.uniqueName("custom_material")
  697. new_metadata = {"name": catalog.i18nc("@label", "Custom Material"),
  698. "brand": catalog.i18nc("@label", "Custom"),
  699. "GUID": str(uuid.uuid4()),
  700. }
  701. self.duplicateMaterial(material_group.root_material_node,
  702. new_base_id = new_id,
  703. new_metadata = new_metadata)
  704. ## Get a list of materials that have the same GUID as the reference material
  705. #
  706. # \param material_id \type{str} the id of the material for which to get the linked materials.
  707. # \return \type{list} a list of names of materials with the same GUID
  708. @pyqtSlot("QVariant", result = "QStringList")
  709. def getLinkedMaterials(self, material_node):
  710. guid = material_node.metadata["GUID"]
  711. material_group_list = self._material_manager.getMaterialGroupListByGUID(guid)
  712. linked_material_names = []
  713. if material_group_list:
  714. for material_group in material_group_list:
  715. linked_material_names.append(material_group.root_material_node.metadata["name"])
  716. return linked_material_names
  717. ## Unlink a material from all other materials by creating a new GUID
  718. # \param material_id \type{str} the id of the material to create a new GUID for.
  719. @pyqtSlot("QVariant")
  720. def unlinkMaterial(self, material_node):
  721. # Get the material group
  722. material_group = self._material_manager.getMaterialGroup(material_node.metadata["base_file"])
  723. # Generate a new GUID
  724. new_guid = str(uuid.uuid4())
  725. # Update the GUID
  726. # NOTE: We only need to set the root material container because XmlMaterialProfile.setMetaDataEntry() will
  727. # take care of the derived containers too
  728. container = material_group.root_material_node.getContainer()
  729. container.setMetaDataEntry("GUID", new_guid)
  730. ## Get the singleton instance for this class.
  731. @classmethod
  732. def getInstance(cls) -> "ContainerManager":
  733. # Note: Explicit use of class name to prevent issues with inheritance.
  734. if ContainerManager.__instance is None:
  735. ContainerManager.__instance = cls()
  736. return ContainerManager.__instance
  737. __instance = None # type: "ContainerManager"
  738. # Factory function, used by QML
  739. @staticmethod
  740. def createContainerManager(engine, js_engine):
  741. return ContainerManager.getInstance()
  742. def _performMerge(self, merge_into, merge, clear_settings = True):
  743. assert isinstance(merge, type(merge_into))
  744. if merge == merge_into:
  745. return
  746. for key in merge.getAllKeys():
  747. merge_into.setProperty(key, "value", merge.getProperty(key, "value"))
  748. if clear_settings:
  749. merge.clear()
  750. def _updateContainerNameFilters(self) -> None:
  751. self._container_name_filters = {}
  752. for plugin_id, container_type in self._container_registry.getContainerTypes():
  753. # Ignore default container types since those are not plugins
  754. if container_type in (InstanceContainer, ContainerStack, DefinitionContainer):
  755. continue
  756. serialize_type = ""
  757. try:
  758. plugin_metadata = PluginRegistry.getInstance().getMetaData(plugin_id)
  759. if plugin_metadata:
  760. serialize_type = plugin_metadata["settings_container"]["type"]
  761. else:
  762. continue
  763. except KeyError as e:
  764. continue
  765. mime_type = self._container_registry.getMimeTypeForContainer(container_type)
  766. entry = {
  767. "type": serialize_type,
  768. "mime": mime_type,
  769. "container": container_type
  770. }
  771. suffix = mime_type.preferredSuffix
  772. if Platform.isOSX() and "." in suffix:
  773. # OSX's File dialog is stupid and does not allow selecting files with a . in its name
  774. suffix = suffix[suffix.index(".") + 1:]
  775. suffix_list = "*." + suffix
  776. for suffix in mime_type.suffixes:
  777. if suffix == mime_type.preferredSuffix:
  778. continue
  779. if Platform.isOSX() and "." in suffix:
  780. # OSX's File dialog is stupid and does not allow selecting files with a . in its name
  781. suffix = suffix[suffix.index("."):]
  782. suffix_list += ", *." + suffix
  783. name_filter = "{0} ({1})".format(mime_type.comment, suffix_list)
  784. self._container_name_filters[name_filter] = entry
  785. ## Creates a unique ID for a container by prefixing the name with the stack ID.
  786. #
  787. # This method creates a unique ID for a container by prefixing it with a specified stack ID.
  788. # This is done to ensure we have an easily identified ID for quality changes, which have the
  789. # same name across several stacks.
  790. #
  791. # \param stack_id The ID of the stack to prepend.
  792. # \param container_name The name of the container that we are creating a unique ID for.
  793. #
  794. # \return Container name prefixed with stack ID, in lower case with spaces replaced by underscores.
  795. def _createUniqueId(self, stack_id, container_name):
  796. result = stack_id + "_" + container_name
  797. result = result.lower()
  798. result.replace(" ", "_")
  799. return result
  800. ## Create a quality changes container for a specified quality container.
  801. #
  802. # \param quality_container The quality container to create a changes container for.
  803. # \param new_name The name of the new quality_changes container.
  804. # \param machine_definition The machine definition this quality changes container is specific to.
  805. # \param extruder_id
  806. #
  807. # \return A new quality_changes container with the specified container as base.
  808. def _createQualityChanges(self, quality_container, new_name, machine_definition, extruder_id):
  809. base_id = machine_definition.getId() if extruder_id is None else extruder_id
  810. # Create a new quality_changes container for the quality.
  811. quality_changes = InstanceContainer(self._createUniqueId(base_id, new_name))
  812. quality_changes.setName(new_name)
  813. quality_changes.addMetaDataEntry("type", "quality_changes")
  814. quality_changes.addMetaDataEntry("quality_type", quality_container.getMetaDataEntry("quality_type"))
  815. # If we are creating a container for an extruder, ensure we add that to the container
  816. if extruder_id is not None:
  817. quality_changes.addMetaDataEntry("extruder", extruder_id)
  818. # If the machine specifies qualities should be filtered, ensure we match the current criteria.
  819. if not machine_definition.getMetaDataEntry("has_machine_quality"):
  820. quality_changes.setDefinition("fdmprinter")
  821. else:
  822. quality_changes.setDefinition(QualityManager.getInstance().getParentMachineDefinition(machine_definition).getId())
  823. from cura.CuraApplication import CuraApplication
  824. quality_changes.addMetaDataEntry("setting_version", CuraApplication.SettingVersion)
  825. return quality_changes
  826. ## Import profiles from a list of file_urls.
  827. # Each QUrl item must end with .curaprofile, or it will not be imported.
  828. #
  829. # \param QVariant<QUrl>, essentially a list with QUrl objects.
  830. # \return Dict with keys status, text
  831. @pyqtSlot("QVariantList", result="QVariantMap")
  832. def importProfiles(self, file_urls):
  833. status = "ok"
  834. results = {"ok": [], "error": []}
  835. for file_url in file_urls:
  836. if not file_url.isValid():
  837. continue
  838. path = file_url.toLocalFile()
  839. if not path:
  840. continue
  841. if not path.endswith(".curaprofile"):
  842. continue
  843. single_result = self._container_registry.importProfile(path)
  844. if single_result["status"] == "error":
  845. status = "error"
  846. results[single_result["status"]].append(single_result["message"])
  847. return {
  848. "status": status,
  849. "message": "\n".join(results["ok"] + results["error"])}
  850. ## Import single profile, file_url does not have to end with curaprofile
  851. @pyqtSlot(QUrl, result="QVariantMap")
  852. def importProfile(self, file_url):
  853. if not file_url.isValid():
  854. return
  855. path = file_url.toLocalFile()
  856. if not path:
  857. return
  858. return self._container_registry.importProfile(path)
  859. @pyqtSlot("QVariantList", QUrl, str)
  860. def exportProfile(self, instance_id: str, file_url: QUrl, file_type: str) -> None:
  861. if not file_url.isValid():
  862. return
  863. path = file_url.toLocalFile()
  864. if not path:
  865. return
  866. self._container_registry.exportProfile(instance_id, path, file_type)