WorkspaceDialog.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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.Machines.Models.MachineListModel import MachineListModel
  7. from cura.Machines.Models.IntentTranslations import intent_translations
  8. from cura.Settings.GlobalStack import GlobalStack
  9. from UM.Application import Application
  10. from UM.FlameProfiler import pyqtSlot
  11. from UM.i18n import i18nCatalog
  12. from UM.Logger import Logger
  13. from UM.Message import Message
  14. from UM.PluginRegistry import PluginRegistry
  15. from UM.Settings.ContainerRegistry import ContainerRegistry
  16. import os
  17. import threading
  18. import time
  19. from cura.CuraApplication import CuraApplication
  20. from .SpecificSettingsModel import SpecificSettingsModel
  21. i18n_catalog = i18nCatalog("cura")
  22. class WorkspaceDialog(QObject):
  23. showDialogSignal = pyqtSignal()
  24. def __init__(self, parent = None) -> None:
  25. super().__init__(parent)
  26. self._component = None
  27. self._context = None
  28. self._view = None
  29. self._qml_url = "WorkspaceDialog.qml"
  30. self._lock = threading.Lock()
  31. self._default_strategy = None
  32. self._result = {
  33. "machine": self._default_strategy,
  34. "quality_changes": self._default_strategy,
  35. "definition_changes": self._default_strategy,
  36. "material": self._default_strategy,
  37. }
  38. self._override_machine = None
  39. self._visible = False
  40. self.showDialogSignal.connect(self.__show)
  41. self._has_quality_changes_conflict = False
  42. self._has_definition_changes_conflict = False
  43. self._has_machine_conflict = False
  44. self._has_material_conflict = False
  45. self._has_visible_settings_field = False
  46. self._num_visible_settings = 0
  47. self._num_user_settings = 0
  48. self._active_mode = ""
  49. self._quality_name = ""
  50. self._num_settings_overridden_by_quality_changes = 0
  51. self._quality_type = ""
  52. self._intent_name = ""
  53. self._machine_name = ""
  54. self._machine_type = ""
  55. self._variant_type = ""
  56. self._current_machine_name = ""
  57. self._material_labels = []
  58. self._extruders = []
  59. self._objects_on_plate = False
  60. self._is_printer_group = False
  61. self._updatable_machines_model = MachineListModel(self, listenToChanges = False, showCloudPrinters = True)
  62. self._missing_package_metadata: List[Dict[str, str]] = []
  63. self._plugin_registry: PluginRegistry = CuraApplication.getInstance().getPluginRegistry()
  64. self._install_missing_package_dialog: Optional[QObject] = None
  65. self._is_abstract_machine = False
  66. self._is_networked_machine = False
  67. self._is_compatible_machine = False
  68. self._allow_create_machine = True
  69. self._exported_settings_model = SpecificSettingsModel()
  70. self._exported_settings_model.modelChanged.connect(self.exportedSettingModelChanged.emit)
  71. self._current_machine_pos_index = 0
  72. self._is_ucp = False
  73. machineConflictChanged = pyqtSignal()
  74. qualityChangesConflictChanged = pyqtSignal()
  75. materialConflictChanged = pyqtSignal()
  76. numVisibleSettingsChanged = pyqtSignal()
  77. activeModeChanged = pyqtSignal()
  78. qualityNameChanged = pyqtSignal()
  79. hasVisibleSettingsFieldChanged = pyqtSignal()
  80. numSettingsOverridenByQualityChangesChanged = pyqtSignal()
  81. qualityTypeChanged = pyqtSignal()
  82. intentNameChanged = pyqtSignal()
  83. machineNameChanged = pyqtSignal()
  84. updatableMachinesChanged = pyqtSignal()
  85. isAbstractMachineChanged = pyqtSignal()
  86. isNetworkedChanged = pyqtSignal()
  87. materialLabelsChanged = pyqtSignal()
  88. objectsOnPlateChanged = pyqtSignal()
  89. numUserSettingsChanged = pyqtSignal()
  90. machineTypeChanged = pyqtSignal()
  91. variantTypeChanged = pyqtSignal()
  92. extrudersChanged = pyqtSignal()
  93. isPrinterGroupChanged = pyqtSignal()
  94. missingPackagesChanged = pyqtSignal()
  95. isCompatibleMachineChanged = pyqtSignal()
  96. isUcpChanged = pyqtSignal()
  97. exportedSettingModelChanged = pyqtSignal()
  98. @pyqtProperty(bool, notify = isPrinterGroupChanged)
  99. def isPrinterGroup(self) -> bool:
  100. return self._is_printer_group
  101. def setIsPrinterGroup(self, value: bool):
  102. if value != self._is_printer_group:
  103. self._is_printer_group = value
  104. self.isPrinterGroupChanged.emit()
  105. @pyqtProperty(str, notify=variantTypeChanged)
  106. def variantType(self) -> str:
  107. return self._variant_type
  108. def setVariantType(self, variant_type: str) -> None:
  109. if self._variant_type != variant_type:
  110. self._variant_type = variant_type
  111. self.variantTypeChanged.emit()
  112. @pyqtProperty(str, notify=machineTypeChanged)
  113. def machineType(self) -> str:
  114. return self._machine_type
  115. def setMachineType(self, machine_type: str) -> None:
  116. self._machine_type = machine_type
  117. self.machineTypeChanged.emit()
  118. def setNumUserSettings(self, num_user_settings: int) -> None:
  119. if self._num_user_settings != num_user_settings:
  120. self._num_user_settings = num_user_settings
  121. self.numVisibleSettingsChanged.emit()
  122. @pyqtProperty(int, notify=numUserSettingsChanged)
  123. def numUserSettings(self) -> int:
  124. return self._num_user_settings
  125. @pyqtProperty(bool, notify=objectsOnPlateChanged)
  126. def hasObjectsOnPlate(self) -> bool:
  127. return self._objects_on_plate
  128. def setHasObjectsOnPlate(self, objects_on_plate):
  129. if self._objects_on_plate != objects_on_plate:
  130. self._objects_on_plate = objects_on_plate
  131. self.objectsOnPlateChanged.emit()
  132. @pyqtProperty("QVariantList", notify = materialLabelsChanged)
  133. def materialLabels(self) -> List[str]:
  134. return self._material_labels
  135. def setMaterialLabels(self, material_labels: List[str]) -> None:
  136. if self._material_labels != material_labels:
  137. self._material_labels = material_labels
  138. self.materialLabelsChanged.emit()
  139. @pyqtProperty("QVariantList", notify=extrudersChanged)
  140. def extruders(self):
  141. return self._extruders
  142. def setExtruders(self, extruders):
  143. if self._extruders != extruders:
  144. self._extruders = extruders
  145. self.extrudersChanged.emit()
  146. @pyqtProperty(str, notify = machineNameChanged)
  147. def machineName(self) -> str:
  148. return self._machine_name
  149. def setMachineName(self, machine_name: str) -> None:
  150. if self._machine_name != machine_name:
  151. self._machine_name = machine_name
  152. self.machineNameChanged.emit()
  153. def setCurrentMachineName(self, machine: str) -> None:
  154. self._current_machine_name = machine
  155. @pyqtProperty(str, notify = machineNameChanged)
  156. def currentMachineName(self) -> str:
  157. return self._current_machine_name
  158. @staticmethod
  159. def getIndexOfCurrentMachine(list_of_dicts, key, value, defaultIndex):
  160. for i, d in enumerate(list_of_dicts):
  161. if d.get(key) == value: # found the dictionary
  162. return i
  163. return defaultIndex
  164. @pyqtProperty(int, notify = machineNameChanged)
  165. def currentMachinePositionIndex(self):
  166. return self._current_machine_pos_index
  167. @pyqtProperty(QObject, notify = updatableMachinesChanged)
  168. def updatableMachinesModel(self) -> MachineListModel:
  169. if self._current_machine_name != "":
  170. self._current_machine_pos_index = self.getIndexOfCurrentMachine(self._updatable_machines_model.getItems(), "id", self._current_machine_name, defaultIndex = 0)
  171. else:
  172. self._current_machine_pos_index = 0
  173. return cast(MachineListModel, self._updatable_machines_model)
  174. def setUpdatableMachines(self, updatable_machines: List[GlobalStack]) -> None:
  175. self._updatable_machines_model.set_machines_filter(updatable_machines)
  176. self.updatableMachinesChanged.emit()
  177. @pyqtProperty(bool, notify = isAbstractMachineChanged)
  178. def isAbstractMachine(self) -> bool:
  179. return self._is_abstract_machine
  180. @pyqtSlot(bool)
  181. def setIsAbstractMachine(self, is_abstract_machine: bool) -> None:
  182. self._is_abstract_machine = is_abstract_machine
  183. self.isAbstractMachineChanged.emit()
  184. @pyqtProperty(bool, notify = isNetworkedChanged)
  185. def isNetworked(self) -> bool:
  186. return self._is_networked_machine
  187. @pyqtSlot(bool)
  188. def setIsNetworkedMachine(self, is_networked_machine: bool) -> None:
  189. self._is_networked_machine = is_networked_machine
  190. self.isNetworkedChanged.emit()
  191. @pyqtProperty(str, notify=qualityTypeChanged)
  192. def qualityType(self) -> str:
  193. return self._quality_type
  194. def setQualityType(self, quality_type: str) -> None:
  195. if self._quality_type != quality_type:
  196. self._quality_type = quality_type
  197. self.qualityTypeChanged.emit()
  198. @pyqtProperty(int, notify=numSettingsOverridenByQualityChangesChanged)
  199. def numSettingsOverridenByQualityChanges(self) -> int:
  200. return self._num_settings_overridden_by_quality_changes
  201. def setNumSettingsOverriddenByQualityChanges(self, num_settings_overridden_by_quality_changes: int) -> None:
  202. self._num_settings_overridden_by_quality_changes = num_settings_overridden_by_quality_changes
  203. self.numSettingsOverridenByQualityChangesChanged.emit()
  204. @pyqtProperty(str, notify=qualityNameChanged)
  205. def qualityName(self) -> str:
  206. return self._quality_name
  207. def setQualityName(self, quality_name: str) -> None:
  208. if self._quality_name != quality_name:
  209. self._quality_name = quality_name
  210. self.qualityNameChanged.emit()
  211. @pyqtProperty(str, notify = intentNameChanged)
  212. def intentName(self) -> str:
  213. return self._intent_name
  214. def setIntentName(self, intent_name: str) -> None:
  215. if self._intent_name != intent_name:
  216. try:
  217. self._intent_name = intent_translations[intent_name]["name"]
  218. except:
  219. self._intent_name = intent_name.title()
  220. self.intentNameChanged.emit()
  221. if not self._intent_name:
  222. self._intent_name = intent_translations["default"]["name"]
  223. self.intentNameChanged.emit()
  224. @pyqtProperty(str, notify=activeModeChanged)
  225. def activeMode(self) -> str:
  226. return self._active_mode
  227. def setActiveMode(self, active_mode: int) -> None:
  228. if active_mode == 0:
  229. self._active_mode = i18n_catalog.i18nc("@title:tab", "Recommended")
  230. else:
  231. self._active_mode = i18n_catalog.i18nc("@title:tab", "Custom")
  232. self.activeModeChanged.emit()
  233. @pyqtProperty(bool, notify = hasVisibleSettingsFieldChanged)
  234. def hasVisibleSettingsField(self) -> bool:
  235. return self._has_visible_settings_field
  236. def setHasVisibleSettingsField(self, has_visible_settings_field: bool) -> None:
  237. self._has_visible_settings_field = has_visible_settings_field
  238. self.hasVisibleSettingsFieldChanged.emit()
  239. @pyqtProperty(int, constant = True)
  240. def totalNumberOfSettings(self) -> int:
  241. general_definition_containers = ContainerRegistry.getInstance().findDefinitionContainers(id = "fdmprinter")
  242. if not general_definition_containers:
  243. return 0
  244. return len(general_definition_containers[0].getAllKeys())
  245. @pyqtProperty(int, notify = numVisibleSettingsChanged)
  246. def numVisibleSettings(self) -> int:
  247. return self._num_visible_settings
  248. def setNumVisibleSettings(self, num_visible_settings: int) -> None:
  249. if self._num_visible_settings != num_visible_settings:
  250. self._num_visible_settings = num_visible_settings
  251. self.numVisibleSettingsChanged.emit()
  252. @pyqtProperty(bool, notify = machineConflictChanged)
  253. def machineConflict(self) -> bool:
  254. return self._has_machine_conflict
  255. @pyqtProperty(bool, notify=qualityChangesConflictChanged)
  256. def qualityChangesConflict(self) -> bool:
  257. return self._has_quality_changes_conflict
  258. @pyqtProperty(bool, notify=materialConflictChanged)
  259. def materialConflict(self) -> bool:
  260. return self._has_material_conflict
  261. @pyqtSlot(str, str)
  262. def setResolveStrategy(self, key: str, strategy: Optional[str]) -> None:
  263. if key in self._result:
  264. self._result[key] = strategy
  265. def getMachineToOverride(self) -> str:
  266. return self._override_machine
  267. @pyqtSlot(str)
  268. def setMachineToOverride(self, machine_name: str) -> None:
  269. self._override_machine = machine_name
  270. self.updateCompatibleMachine()
  271. def updateCompatibleMachine(self):
  272. registry = ContainerRegistry.getInstance()
  273. containers_expected = registry.findDefinitionContainers(name=self._machine_type)
  274. containers_selected = registry.findContainerStacks(id=self._override_machine)
  275. if len(containers_expected) == 1 and len(containers_selected) == 1:
  276. new_compatible_machine = (containers_expected[0] == containers_selected[0].definition)
  277. if new_compatible_machine != self._is_compatible_machine:
  278. self._is_compatible_machine = new_compatible_machine
  279. self.isCompatibleMachineChanged.emit()
  280. @pyqtProperty(bool, notify = isCompatibleMachineChanged)
  281. def isCompatibleMachine(self) -> bool:
  282. return self._is_compatible_machine
  283. def setIsUcp(self, isUcp: bool) -> None:
  284. if isUcp != self._is_ucp:
  285. self._is_ucp = isUcp
  286. self.isUcpChanged.emit()
  287. @pyqtProperty(bool, notify=isUcpChanged)
  288. def isUcp(self):
  289. return self._is_ucp
  290. def setAllowCreatemachine(self, allow_create_machine):
  291. self._allow_create_machine = allow_create_machine
  292. @pyqtProperty(bool, constant = True)
  293. def allowCreateMachine(self):
  294. return self._allow_create_machine
  295. @pyqtProperty(QObject, notify=exportedSettingModelChanged)
  296. def exportedSettingModel(self):
  297. return self._exported_settings_model
  298. @pyqtProperty("QVariantList", notify=exportedSettingModelChanged)
  299. def exportedSettingModelItems(self):
  300. return self._exported_settings_model.items
  301. @pyqtProperty(int, notify=exportedSettingModelChanged)
  302. def exportedSettingModelRowCount(self):
  303. return self._exported_settings_model.rowCount()
  304. @pyqtSlot()
  305. def closeBackend(self) -> None:
  306. """Close the backend: otherwise one could end up with "Slicing..."""
  307. Application.getInstance().getBackend().close()
  308. def setMaterialConflict(self, material_conflict: bool) -> None:
  309. if self._has_material_conflict != material_conflict:
  310. self._has_material_conflict = material_conflict
  311. self.materialConflictChanged.emit()
  312. def setMachineConflict(self, machine_conflict: bool) -> None:
  313. if self._has_machine_conflict != machine_conflict:
  314. self._has_machine_conflict = machine_conflict
  315. self.machineConflictChanged.emit()
  316. def setQualityChangesConflict(self, quality_changes_conflict: bool) -> None:
  317. if self._has_quality_changes_conflict != quality_changes_conflict:
  318. self._has_quality_changes_conflict = quality_changes_conflict
  319. self.qualityChangesConflictChanged.emit()
  320. def setMissingPackagesMetadata(self, missing_package_metadata: List[Dict[str, str]]) -> None:
  321. self._missing_package_metadata = missing_package_metadata
  322. self.missingPackagesChanged.emit()
  323. @pyqtProperty("QVariantList", notify=missingPackagesChanged)
  324. def missingPackages(self) -> List[Dict[str, str]]:
  325. return self._missing_package_metadata
  326. @pyqtSlot()
  327. def installMissingPackages(self) -> None:
  328. marketplace_plugin = PluginRegistry.getInstance().getPluginObject("Marketplace")
  329. if not marketplace_plugin:
  330. Logger.warning("Could not show dialog to install missing plug-ins. Is Marketplace plug-in not available?")
  331. marketplace_plugin.showInstallMissingPackageDialog(self._missing_package_metadata, self.showMissingMaterialsWarning) # type: ignore
  332. def getResult(self) -> Dict[str, Optional[str]]:
  333. if "machine" in self._result and self.updatableMachinesModel.count <= 1:
  334. self._result["machine"] = None
  335. if "quality_changes" in self._result and not self._has_quality_changes_conflict:
  336. self._result["quality_changes"] = None
  337. if "material" in self._result and not self._has_material_conflict:
  338. self._result["material"] = None
  339. # If the machine needs to be re-created, the definition_changes should also be re-created.
  340. # If the machine strategy is None, it means that there is no name conflict with existing ones. In this case
  341. # new definitions changes are created
  342. if "machine" in self._result:
  343. if self._result["machine"] == "new" or self._result["machine"] is None and self._result["definition_changes"] is None:
  344. self._result["definition_changes"] = "new"
  345. return self._result
  346. def _createViewFromQML(self) -> None:
  347. three_mf_reader_path = PluginRegistry.getInstance().getPluginPath("3MFReader")
  348. if three_mf_reader_path:
  349. path = os.path.join(three_mf_reader_path, self._qml_url)
  350. self._view = CuraApplication.getInstance().createQmlComponent(path, {"manager": self})
  351. def show(self) -> None:
  352. # Emit signal so the right thread actually shows the view.
  353. if threading.current_thread() != threading.main_thread():
  354. self._lock.acquire()
  355. # Reset the result
  356. self._result = {
  357. "machine": self._default_strategy,
  358. "quality_changes": self._default_strategy,
  359. "definition_changes": self._default_strategy,
  360. "material": self._default_strategy,
  361. }
  362. self._visible = True
  363. self.showDialogSignal.emit()
  364. @pyqtSlot()
  365. def notifyClosed(self) -> None:
  366. """Used to notify the dialog so the lock can be released."""
  367. self._result = {} # The result should be cleared before hide, because after it is released the main thread lock
  368. self._visible = False
  369. try:
  370. self._lock.release()
  371. except:
  372. pass
  373. def hide(self) -> None:
  374. self._visible = False
  375. self._view.hide()
  376. try:
  377. self._lock.release()
  378. except:
  379. pass
  380. @pyqtSlot(bool)
  381. def _onVisibilityChanged(self, visible: bool) -> None:
  382. if not visible:
  383. try:
  384. self._lock.release()
  385. except:
  386. pass
  387. @pyqtSlot()
  388. def onOkButtonClicked(self) -> None:
  389. self._view.hide()
  390. self.hide()
  391. @pyqtSlot()
  392. def onCancelButtonClicked(self) -> None:
  393. self._result = {}
  394. self._view.hide()
  395. self.hide()
  396. def waitForClose(self) -> None:
  397. """Block thread until the dialog is closed."""
  398. if self._visible:
  399. if threading.current_thread() != threading.main_thread():
  400. self._lock.acquire()
  401. self._lock.release()
  402. else:
  403. # If this is not run from a separate thread, we need to ensure that the events are still processed.
  404. while self._visible:
  405. time.sleep(1 / 50)
  406. QCoreApplication.processEvents() # Ensure that the GUI does not freeze.
  407. @pyqtSlot()
  408. def showMissingMaterialsWarning(self) -> None:
  409. result_message = Message(
  410. i18n_catalog.i18nc("@info:status",
  411. "Some of the packages used in the project file are currently not installed in Cura, this might produce undesirable print results. We highly recommend installing the all required packages from the Marketplace."),
  412. lifetime=0,
  413. title=i18n_catalog.i18nc("@info:title", "Some required packages are not installed"),
  414. message_type=Message.MessageType.WARNING
  415. )
  416. result_message.addAction(
  417. "learn_more",
  418. name=i18n_catalog.i18nc("@action:button", "Learn more"),
  419. icon="",
  420. description=i18n_catalog.i18nc("@label", "Learn more about project packages."),
  421. button_align=Message.ActionButtonAlignment.ALIGN_LEFT,
  422. button_style=Message.ActionButtonStyle.LINK
  423. )
  424. result_message.addAction(
  425. "install_packages",
  426. name=i18n_catalog.i18nc("@action:button", "Install Packages"),
  427. icon="",
  428. description=i18n_catalog.i18nc("@label", "Install missing packages from project file."),
  429. button_align=Message.ActionButtonAlignment.ALIGN_RIGHT,
  430. button_style=Message.ActionButtonStyle.DEFAULT
  431. )
  432. result_message.actionTriggered.connect(self._onMessageActionTriggered)
  433. result_message.show()
  434. def _onMessageActionTriggered(self, message: Message, sync_message_action: str) -> None:
  435. if sync_message_action == "install_materials":
  436. self.installMissingPackages()
  437. message.hide()
  438. elif sync_message_action == "learn_more":
  439. QDesktopServices.openUrl(QUrl("https://support.ultimaker.com/hc/en-us/articles/360011968360-Using-the-Ultimaker-Marketplace"))
  440. def __show(self) -> None:
  441. if self._view is None:
  442. self._createViewFromQML()
  443. if self._view:
  444. self._view.show()