WorkspaceDialog.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. # Copyright (c) 2022 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt6.QtCore import pyqtSignal, QObject, pyqtProperty, QCoreApplication, QUrl
  4. from PyQt6.QtGui import QDesktopServices
  5. from typing import List, Optional, Dict, cast
  6. from cura.Settings.GlobalStack import GlobalStack
  7. from UM.Application import Application
  8. from UM.FlameProfiler import pyqtSlot
  9. from UM.i18n import i18nCatalog
  10. from UM.Logger import Logger
  11. from UM.Message import Message
  12. from UM.PluginRegistry import PluginRegistry
  13. from UM.Settings.ContainerRegistry import ContainerRegistry
  14. from .UpdatableMachinesModel import UpdatableMachinesModel
  15. import os
  16. import threading
  17. import time
  18. from cura.CuraApplication import CuraApplication
  19. i18n_catalog = i18nCatalog("cura")
  20. class WorkspaceDialog(QObject):
  21. showDialogSignal = pyqtSignal()
  22. def __init__(self, parent = None) -> None:
  23. super().__init__(parent)
  24. self._component = None
  25. self._context = None
  26. self._view = None
  27. self._qml_url = "WorkspaceDialog.qml"
  28. self._lock = threading.Lock()
  29. self._default_strategy = None
  30. self._result = {"machine": self._default_strategy,
  31. "quality_changes": self._default_strategy,
  32. "definition_changes": self._default_strategy,
  33. "material": self._default_strategy}
  34. self._override_machine = None
  35. self._visible = False
  36. self.showDialogSignal.connect(self.__show)
  37. self._has_quality_changes_conflict = False
  38. self._has_definition_changes_conflict = False
  39. self._has_machine_conflict = False
  40. self._has_material_conflict = False
  41. self._has_visible_settings_field = False
  42. self._num_visible_settings = 0
  43. self._num_user_settings = 0
  44. self._active_mode = ""
  45. self._quality_name = ""
  46. self._num_settings_overridden_by_quality_changes = 0
  47. self._quality_type = ""
  48. self._intent_name = ""
  49. self._machine_name = ""
  50. self._machine_type = ""
  51. self._variant_type = ""
  52. self._material_labels = []
  53. self._extruders = []
  54. self._objects_on_plate = False
  55. self._is_printer_group = False
  56. self._updatable_machines_model = UpdatableMachinesModel(self)
  57. self._missing_package_metadata: List[Dict[str, str]] = []
  58. self._plugin_registry: PluginRegistry = CuraApplication.getInstance().getPluginRegistry()
  59. self._install_missing_package_dialog: Optional[QObject] = None
  60. machineConflictChanged = pyqtSignal()
  61. qualityChangesConflictChanged = pyqtSignal()
  62. materialConflictChanged = pyqtSignal()
  63. numVisibleSettingsChanged = pyqtSignal()
  64. activeModeChanged = pyqtSignal()
  65. qualityNameChanged = pyqtSignal()
  66. hasVisibleSettingsFieldChanged = pyqtSignal()
  67. numSettingsOverridenByQualityChangesChanged = pyqtSignal()
  68. qualityTypeChanged = pyqtSignal()
  69. intentNameChanged = pyqtSignal()
  70. machineNameChanged = pyqtSignal()
  71. updatableMachinesChanged = pyqtSignal()
  72. materialLabelsChanged = pyqtSignal()
  73. objectsOnPlateChanged = pyqtSignal()
  74. numUserSettingsChanged = pyqtSignal()
  75. machineTypeChanged = pyqtSignal()
  76. variantTypeChanged = pyqtSignal()
  77. extrudersChanged = pyqtSignal()
  78. isPrinterGroupChanged = pyqtSignal()
  79. missingPackagesChanged = pyqtSignal()
  80. @pyqtProperty(bool, notify = isPrinterGroupChanged)
  81. def isPrinterGroup(self) -> bool:
  82. return self._is_printer_group
  83. def setIsPrinterGroup(self, value: bool):
  84. if value != self._is_printer_group:
  85. self._is_printer_group = value
  86. self.isPrinterGroupChanged.emit()
  87. @pyqtProperty(str, notify=variantTypeChanged)
  88. def variantType(self) -> str:
  89. return self._variant_type
  90. def setVariantType(self, variant_type: str) -> None:
  91. if self._variant_type != variant_type:
  92. self._variant_type = variant_type
  93. self.variantTypeChanged.emit()
  94. @pyqtProperty(str, notify=machineTypeChanged)
  95. def machineType(self) -> str:
  96. return self._machine_type
  97. def setMachineType(self, machine_type: str) -> None:
  98. self._machine_type = machine_type
  99. self.machineTypeChanged.emit()
  100. def setNumUserSettings(self, num_user_settings: int) -> None:
  101. if self._num_user_settings != num_user_settings:
  102. self._num_user_settings = num_user_settings
  103. self.numVisibleSettingsChanged.emit()
  104. @pyqtProperty(int, notify=numUserSettingsChanged)
  105. def numUserSettings(self) -> int:
  106. return self._num_user_settings
  107. @pyqtProperty(bool, notify=objectsOnPlateChanged)
  108. def hasObjectsOnPlate(self) -> bool:
  109. return self._objects_on_plate
  110. def setHasObjectsOnPlate(self, objects_on_plate):
  111. if self._objects_on_plate != objects_on_plate:
  112. self._objects_on_plate = objects_on_plate
  113. self.objectsOnPlateChanged.emit()
  114. @pyqtProperty("QVariantList", notify = materialLabelsChanged)
  115. def materialLabels(self) -> List[str]:
  116. return self._material_labels
  117. def setMaterialLabels(self, material_labels: List[str]) -> None:
  118. if self._material_labels != material_labels:
  119. self._material_labels = material_labels
  120. self.materialLabelsChanged.emit()
  121. @pyqtProperty("QVariantList", notify=extrudersChanged)
  122. def extruders(self):
  123. return self._extruders
  124. def setExtruders(self, extruders):
  125. if self._extruders != extruders:
  126. self._extruders = extruders
  127. self.extrudersChanged.emit()
  128. @pyqtProperty(str, notify = machineNameChanged)
  129. def machineName(self) -> str:
  130. return self._machine_name
  131. def setMachineName(self, machine_name: str) -> None:
  132. if self._machine_name != machine_name:
  133. self._machine_name = machine_name
  134. self.machineNameChanged.emit()
  135. @pyqtProperty(QObject, notify = updatableMachinesChanged)
  136. def updatableMachinesModel(self) -> UpdatableMachinesModel:
  137. return cast(UpdatableMachinesModel, self._updatable_machines_model)
  138. def setUpdatableMachines(self, updatable_machines: List[GlobalStack]) -> None:
  139. self._updatable_machines_model.update(updatable_machines)
  140. self.updatableMachinesChanged.emit()
  141. @pyqtProperty(str, notify=qualityTypeChanged)
  142. def qualityType(self) -> str:
  143. return self._quality_type
  144. def setQualityType(self, quality_type: str) -> None:
  145. if self._quality_type != quality_type:
  146. self._quality_type = quality_type
  147. self.qualityTypeChanged.emit()
  148. @pyqtProperty(int, notify=numSettingsOverridenByQualityChangesChanged)
  149. def numSettingsOverridenByQualityChanges(self) -> int:
  150. return self._num_settings_overridden_by_quality_changes
  151. def setNumSettingsOverriddenByQualityChanges(self, num_settings_overridden_by_quality_changes: int) -> None:
  152. self._num_settings_overridden_by_quality_changes = num_settings_overridden_by_quality_changes
  153. self.numSettingsOverridenByQualityChangesChanged.emit()
  154. @pyqtProperty(str, notify=qualityNameChanged)
  155. def qualityName(self) -> str:
  156. return self._quality_name
  157. def setQualityName(self, quality_name: str) -> None:
  158. if self._quality_name != quality_name:
  159. self._quality_name = quality_name
  160. self.qualityNameChanged.emit()
  161. @pyqtProperty(str, notify = intentNameChanged)
  162. def intentName(self) -> str:
  163. return self._intent_name
  164. def setIntentName(self, intent_name: str) -> None:
  165. if self._intent_name != intent_name:
  166. self._intent_name = intent_name
  167. self.intentNameChanged.emit()
  168. @pyqtProperty(str, notify=activeModeChanged)
  169. def activeMode(self) -> str:
  170. return self._active_mode
  171. def setActiveMode(self, active_mode: int) -> None:
  172. if active_mode == 0:
  173. self._active_mode = i18n_catalog.i18nc("@title:tab", "Recommended")
  174. else:
  175. self._active_mode = i18n_catalog.i18nc("@title:tab", "Custom")
  176. self.activeModeChanged.emit()
  177. @pyqtProperty(bool, notify = hasVisibleSettingsFieldChanged)
  178. def hasVisibleSettingsField(self) -> bool:
  179. return self._has_visible_settings_field
  180. def setHasVisibleSettingsField(self, has_visible_settings_field: bool) -> None:
  181. self._has_visible_settings_field = has_visible_settings_field
  182. self.hasVisibleSettingsFieldChanged.emit()
  183. @pyqtProperty(int, constant = True)
  184. def totalNumberOfSettings(self) -> int:
  185. general_definition_containers = ContainerRegistry.getInstance().findDefinitionContainers(id = "fdmprinter")
  186. if not general_definition_containers:
  187. return 0
  188. return len(general_definition_containers[0].getAllKeys())
  189. @pyqtProperty(int, notify = numVisibleSettingsChanged)
  190. def numVisibleSettings(self) -> int:
  191. return self._num_visible_settings
  192. def setNumVisibleSettings(self, num_visible_settings: int) -> None:
  193. if self._num_visible_settings != num_visible_settings:
  194. self._num_visible_settings = num_visible_settings
  195. self.numVisibleSettingsChanged.emit()
  196. @pyqtProperty(bool, notify = machineConflictChanged)
  197. def machineConflict(self) -> bool:
  198. return self._has_machine_conflict
  199. @pyqtProperty(bool, notify=qualityChangesConflictChanged)
  200. def qualityChangesConflict(self) -> bool:
  201. return self._has_quality_changes_conflict
  202. @pyqtProperty(bool, notify=materialConflictChanged)
  203. def materialConflict(self) -> bool:
  204. return self._has_material_conflict
  205. @pyqtSlot(str, str)
  206. def setResolveStrategy(self, key: str, strategy: Optional[str]) -> None:
  207. if key in self._result:
  208. self._result[key] = strategy
  209. def getMachineToOverride(self) -> str:
  210. return self._override_machine
  211. @pyqtSlot(str)
  212. def setMachineToOverride(self, machine_name: str) -> None:
  213. self._override_machine = machine_name
  214. @pyqtSlot()
  215. def closeBackend(self) -> None:
  216. """Close the backend: otherwise one could end up with "Slicing..."""
  217. Application.getInstance().getBackend().close()
  218. def setMaterialConflict(self, material_conflict: bool) -> None:
  219. if self._has_material_conflict != material_conflict:
  220. self._has_material_conflict = material_conflict
  221. self.materialConflictChanged.emit()
  222. def setMachineConflict(self, machine_conflict: bool) -> None:
  223. if self._has_machine_conflict != machine_conflict:
  224. self._has_machine_conflict = machine_conflict
  225. self.machineConflictChanged.emit()
  226. def setQualityChangesConflict(self, quality_changes_conflict: bool) -> None:
  227. if self._has_quality_changes_conflict != quality_changes_conflict:
  228. self._has_quality_changes_conflict = quality_changes_conflict
  229. self.qualityChangesConflictChanged.emit()
  230. def setMissingPackagesMetadata(self, missing_package_metadata: List[Dict[str, str]]) -> None:
  231. self._missing_package_metadata = missing_package_metadata
  232. self.missingPackagesChanged.emit()
  233. @pyqtProperty("QVariantList", notify=missingPackagesChanged)
  234. def missingPackages(self) -> List[Dict[str, str]]:
  235. return self._missing_package_metadata
  236. @pyqtSlot()
  237. def installMissingPackages(self) -> None:
  238. marketplace_plugin = PluginRegistry.getInstance().getPluginObject("Marketplace")
  239. if not marketplace_plugin:
  240. Logger.warning("Could not show dialog to install missing plug-ins. Is Marketplace plug-in not available?")
  241. marketplace_plugin.showInstallMissingPackageDialog(self._missing_package_metadata, self.showMissingMaterialsWarning) # type: ignore
  242. def getResult(self) -> Dict[str, Optional[str]]:
  243. if "machine" in self._result and self.updatableMachinesModel.count <= 1:
  244. self._result["machine"] = None
  245. if "quality_changes" in self._result and not self._has_quality_changes_conflict:
  246. self._result["quality_changes"] = None
  247. if "material" in self._result and not self._has_material_conflict:
  248. self._result["material"] = None
  249. # If the machine needs to be re-created, the definition_changes should also be re-created.
  250. # If the machine strategy is None, it means that there is no name conflict with existing ones. In this case
  251. # new definitions changes are created
  252. if "machine" in self._result:
  253. if self._result["machine"] == "new" or self._result["machine"] is None and self._result["definition_changes"] is None:
  254. self._result["definition_changes"] = "new"
  255. return self._result
  256. def _createViewFromQML(self) -> None:
  257. three_mf_reader_path = PluginRegistry.getInstance().getPluginPath("3MFReader")
  258. if three_mf_reader_path:
  259. path = os.path.join(three_mf_reader_path, self._qml_url)
  260. self._view = CuraApplication.getInstance().createQmlComponent(path, {"manager": self})
  261. def show(self) -> None:
  262. # Emit signal so the right thread actually shows the view.
  263. if threading.current_thread() != threading.main_thread():
  264. self._lock.acquire()
  265. # Reset the result
  266. self._result = {"machine": self._default_strategy,
  267. "quality_changes": self._default_strategy,
  268. "definition_changes": self._default_strategy,
  269. "material": self._default_strategy}
  270. self._visible = True
  271. self.showDialogSignal.emit()
  272. @pyqtSlot()
  273. def notifyClosed(self) -> None:
  274. """Used to notify the dialog so the lock can be released."""
  275. self._result = {} # The result should be cleared before hide, because after it is released the main thread lock
  276. self._visible = False
  277. try:
  278. self._lock.release()
  279. except:
  280. pass
  281. def hide(self) -> None:
  282. self._visible = False
  283. self._view.hide()
  284. try:
  285. self._lock.release()
  286. except:
  287. pass
  288. @pyqtSlot(bool)
  289. def _onVisibilityChanged(self, visible: bool) -> None:
  290. if not visible:
  291. try:
  292. self._lock.release()
  293. except:
  294. pass
  295. @pyqtSlot()
  296. def onOkButtonClicked(self) -> None:
  297. self._view.hide()
  298. self.hide()
  299. @pyqtSlot()
  300. def onCancelButtonClicked(self) -> None:
  301. self._result = {}
  302. self._view.hide()
  303. self.hide()
  304. def waitForClose(self) -> None:
  305. """Block thread until the dialog is closed."""
  306. if self._visible:
  307. if threading.current_thread() != threading.main_thread():
  308. self._lock.acquire()
  309. self._lock.release()
  310. else:
  311. # If this is not run from a separate thread, we need to ensure that the events are still processed.
  312. while self._visible:
  313. time.sleep(1 / 50)
  314. QCoreApplication.processEvents() # Ensure that the GUI does not freeze.
  315. @pyqtSlot()
  316. def showMissingMaterialsWarning(self) -> None:
  317. result_message = Message(
  318. i18n_catalog.i18nc("@info:status", "The material used in this project relies on some material definitions not available in Cura, this might produce undesirable print results. We highly recommend installing the full material package from the Marketplace."),
  319. lifetime=0,
  320. title=i18n_catalog.i18nc("@info:title", "Material profiles not installed"),
  321. message_type=Message.MessageType.WARNING
  322. )
  323. result_message.addAction(
  324. "learn_more",
  325. name=i18n_catalog.i18nc("@action:button", "Learn more"),
  326. icon="",
  327. description="Learn more about project materials.",
  328. button_align=Message.ActionButtonAlignment.ALIGN_LEFT,
  329. button_style=Message.ActionButtonStyle.LINK
  330. )
  331. result_message.addAction(
  332. "install_materials",
  333. name=i18n_catalog.i18nc("@action:button", "Install Materials"),
  334. icon="",
  335. description="Install missing materials from project file.",
  336. button_align=Message.ActionButtonAlignment.ALIGN_RIGHT,
  337. button_style=Message.ActionButtonStyle.DEFAULT
  338. )
  339. result_message.actionTriggered.connect(self._onMessageActionTriggered)
  340. result_message.show()
  341. def _onMessageActionTriggered(self, message: Message, sync_message_action: str) -> None:
  342. if sync_message_action == "install_materials":
  343. self.installMissingPackages()
  344. message.hide()
  345. elif sync_message_action == "learn_more":
  346. QDesktopServices.openUrl(QUrl("https://support.ultimaker.com/hc/en-us/articles/360011968360-Using-the-Ultimaker-Marketplace"))
  347. def __show(self) -> None:
  348. if self._view is None:
  349. self._createViewFromQML()
  350. if self._view:
  351. self._view.show()