WorkspaceDialog.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import pyqtSignal, QObject, pyqtProperty, QCoreApplication
  4. from UM.FlameProfiler import pyqtSlot
  5. from UM.PluginRegistry import PluginRegistry
  6. from UM.Application import Application
  7. from UM.i18n import i18nCatalog
  8. from UM.Settings.ContainerRegistry import ContainerRegistry
  9. import os
  10. import threading
  11. import time
  12. i18n_catalog = i18nCatalog("cura")
  13. class WorkspaceDialog(QObject):
  14. showDialogSignal = pyqtSignal()
  15. def __init__(self, parent = None):
  16. super().__init__(parent)
  17. self._component = None
  18. self._context = None
  19. self._view = None
  20. self._qml_url = "WorkspaceDialog.qml"
  21. self._lock = threading.Lock()
  22. self._default_strategy = None
  23. self._result = {"machine": self._default_strategy,
  24. "quality_changes": self._default_strategy,
  25. "definition_changes": self._default_strategy,
  26. "material": self._default_strategy}
  27. self._visible = False
  28. self.showDialogSignal.connect(self.__show)
  29. self._has_quality_changes_conflict = False
  30. self._has_definition_changes_conflict = False
  31. self._has_machine_conflict = False
  32. self._has_material_conflict = False
  33. self._has_visible_settings_field = False
  34. self._num_visible_settings = 0
  35. self._num_user_settings = 0
  36. self._active_mode = ""
  37. self._quality_name = ""
  38. self._num_settings_overridden_by_quality_changes = 0
  39. self._quality_type = ""
  40. self._intent_name = ""
  41. self._machine_name = ""
  42. self._machine_type = ""
  43. self._variant_type = ""
  44. self._material_labels = []
  45. self._extruders = []
  46. self._objects_on_plate = False
  47. self._is_printer_group = False
  48. machineConflictChanged = pyqtSignal()
  49. qualityChangesConflictChanged = pyqtSignal()
  50. materialConflictChanged = pyqtSignal()
  51. numVisibleSettingsChanged = pyqtSignal()
  52. activeModeChanged = pyqtSignal()
  53. qualityNameChanged = pyqtSignal()
  54. hasVisibleSettingsFieldChanged = pyqtSignal()
  55. numSettingsOverridenByQualityChangesChanged = pyqtSignal()
  56. qualityTypeChanged = pyqtSignal()
  57. intentNameChanged = pyqtSignal()
  58. machineNameChanged = pyqtSignal()
  59. materialLabelsChanged = pyqtSignal()
  60. objectsOnPlateChanged = pyqtSignal()
  61. numUserSettingsChanged = pyqtSignal()
  62. machineTypeChanged = pyqtSignal()
  63. variantTypeChanged = pyqtSignal()
  64. extrudersChanged = pyqtSignal()
  65. isPrinterGroupChanged = pyqtSignal()
  66. @pyqtProperty(bool, notify = isPrinterGroupChanged)
  67. def isPrinterGroup(self) -> bool:
  68. return self._is_printer_group
  69. def setIsPrinterGroup(self, value: bool):
  70. if value != self._is_printer_group:
  71. self._is_printer_group = value
  72. self.isPrinterGroupChanged.emit()
  73. @pyqtProperty(str, notify=variantTypeChanged)
  74. def variantType(self):
  75. return self._variant_type
  76. def setVariantType(self, variant_type):
  77. if self._variant_type != variant_type:
  78. self._variant_type = variant_type
  79. self.variantTypeChanged.emit()
  80. @pyqtProperty(str, notify=machineTypeChanged)
  81. def machineType(self):
  82. return self._machine_type
  83. def setMachineType(self, machine_type):
  84. self._machine_type = machine_type
  85. self.machineTypeChanged.emit()
  86. def setNumUserSettings(self, num_user_settings):
  87. if self._num_user_settings != num_user_settings:
  88. self._num_user_settings = num_user_settings
  89. self.numVisibleSettingsChanged.emit()
  90. @pyqtProperty(int, notify=numUserSettingsChanged)
  91. def numUserSettings(self):
  92. return self._num_user_settings
  93. @pyqtProperty(bool, notify=objectsOnPlateChanged)
  94. def hasObjectsOnPlate(self):
  95. return self._objects_on_plate
  96. def setHasObjectsOnPlate(self, objects_on_plate):
  97. if self._objects_on_plate != objects_on_plate:
  98. self._objects_on_plate = objects_on_plate
  99. self.objectsOnPlateChanged.emit()
  100. @pyqtProperty("QVariantList", notify = materialLabelsChanged)
  101. def materialLabels(self):
  102. return self._material_labels
  103. def setMaterialLabels(self, material_labels):
  104. if self._material_labels != material_labels:
  105. self._material_labels = material_labels
  106. self.materialLabelsChanged.emit()
  107. @pyqtProperty("QVariantList", notify=extrudersChanged)
  108. def extruders(self):
  109. return self._extruders
  110. def setExtruders(self, extruders):
  111. if self._extruders != extruders:
  112. self._extruders = extruders
  113. self.extrudersChanged.emit()
  114. @pyqtProperty(str, notify = machineNameChanged)
  115. def machineName(self):
  116. return self._machine_name
  117. def setMachineName(self, machine_name):
  118. if self._machine_name != machine_name:
  119. self._machine_name = machine_name
  120. self.machineNameChanged.emit()
  121. @pyqtProperty(str, notify=qualityTypeChanged)
  122. def qualityType(self):
  123. return self._quality_type
  124. def setQualityType(self, quality_type):
  125. if self._quality_type != quality_type:
  126. self._quality_type = quality_type
  127. self.qualityTypeChanged.emit()
  128. @pyqtProperty(int, notify=numSettingsOverridenByQualityChangesChanged)
  129. def numSettingsOverridenByQualityChanges(self):
  130. return self._num_settings_overridden_by_quality_changes
  131. def setNumSettingsOverriddenByQualityChanges(self, num_settings_overridden_by_quality_changes):
  132. self._num_settings_overridden_by_quality_changes = num_settings_overridden_by_quality_changes
  133. self.numSettingsOverridenByQualityChangesChanged.emit()
  134. @pyqtProperty(str, notify=qualityNameChanged)
  135. def qualityName(self):
  136. return self._quality_name
  137. def setQualityName(self, quality_name):
  138. if self._quality_name != quality_name:
  139. self._quality_name = quality_name
  140. self.qualityNameChanged.emit()
  141. @pyqtProperty(str, notify = intentNameChanged)
  142. def intentName(self) -> str:
  143. return self._intent_name
  144. def setIntentName(self, intent_name: str) -> None:
  145. if self._intent_name != intent_name:
  146. self._intent_name = intent_name
  147. self.intentNameChanged.emit()
  148. @pyqtProperty(str, notify=activeModeChanged)
  149. def activeMode(self):
  150. return self._active_mode
  151. def setActiveMode(self, active_mode):
  152. if active_mode == 0:
  153. self._active_mode = i18n_catalog.i18nc("@title:tab", "Recommended")
  154. else:
  155. self._active_mode = i18n_catalog.i18nc("@title:tab", "Custom")
  156. self.activeModeChanged.emit()
  157. @pyqtProperty(int, notify = hasVisibleSettingsFieldChanged)
  158. def hasVisibleSettingsField(self):
  159. return self._has_visible_settings_field
  160. def setHasVisibleSettingsField(self, has_visible_settings_field):
  161. self._has_visible_settings_field = has_visible_settings_field
  162. self.hasVisibleSettingsFieldChanged.emit()
  163. @pyqtProperty(int, constant = True)
  164. def totalNumberOfSettings(self):
  165. general_definition_containers = ContainerRegistry.getInstance().findDefinitionContainers(id = "fdmprinter")
  166. if not general_definition_containers:
  167. return 0
  168. return len(general_definition_containers[0].getAllKeys())
  169. @pyqtProperty(int, notify = numVisibleSettingsChanged)
  170. def numVisibleSettings(self):
  171. return self._num_visible_settings
  172. def setNumVisibleSettings(self, num_visible_settings):
  173. if self._num_visible_settings != num_visible_settings:
  174. self._num_visible_settings = num_visible_settings
  175. self.numVisibleSettingsChanged.emit()
  176. @pyqtProperty(bool, notify = machineConflictChanged)
  177. def machineConflict(self):
  178. return self._has_machine_conflict
  179. @pyqtProperty(bool, notify=qualityChangesConflictChanged)
  180. def qualityChangesConflict(self):
  181. return self._has_quality_changes_conflict
  182. @pyqtProperty(bool, notify=materialConflictChanged)
  183. def materialConflict(self):
  184. return self._has_material_conflict
  185. @pyqtSlot(str, str)
  186. def setResolveStrategy(self, key, strategy):
  187. if key in self._result:
  188. self._result[key] = strategy
  189. @pyqtSlot()
  190. def closeBackend(self):
  191. """Close the backend: otherwise one could end up with "Slicing..."""
  192. Application.getInstance().getBackend().close()
  193. def setMaterialConflict(self, material_conflict):
  194. if self._has_material_conflict != material_conflict:
  195. self._has_material_conflict = material_conflict
  196. self.materialConflictChanged.emit()
  197. def setMachineConflict(self, machine_conflict):
  198. if self._has_machine_conflict != machine_conflict:
  199. self._has_machine_conflict = machine_conflict
  200. self.machineConflictChanged.emit()
  201. def setQualityChangesConflict(self, quality_changes_conflict):
  202. if self._has_quality_changes_conflict != quality_changes_conflict:
  203. self._has_quality_changes_conflict = quality_changes_conflict
  204. self.qualityChangesConflictChanged.emit()
  205. def getResult(self):
  206. if "machine" in self._result and not self._has_machine_conflict:
  207. self._result["machine"] = None
  208. if "quality_changes" in self._result and not self._has_quality_changes_conflict:
  209. self._result["quality_changes"] = None
  210. if "material" in self._result and not self._has_material_conflict:
  211. self._result["material"] = None
  212. # If the machine needs to be re-created, the definition_changes should also be re-created.
  213. # If the machine strategy is None, it means that there is no name conflict with existing ones. In this case
  214. # new definitions changes are created
  215. if "machine" in self._result:
  216. if self._result["machine"] == "new" or self._result["machine"] is None and self._result["definition_changes"] is None:
  217. self._result["definition_changes"] = "new"
  218. return self._result
  219. def _createViewFromQML(self):
  220. path = os.path.join(PluginRegistry.getInstance().getPluginPath("3MFReader"), self._qml_url)
  221. self._view = Application.getInstance().createQmlComponent(path, {"manager": self})
  222. def show(self):
  223. # Emit signal so the right thread actually shows the view.
  224. if threading.current_thread() != threading.main_thread():
  225. self._lock.acquire()
  226. # Reset the result
  227. self._result = {"machine": self._default_strategy,
  228. "quality_changes": self._default_strategy,
  229. "definition_changes": self._default_strategy,
  230. "material": self._default_strategy}
  231. self._visible = True
  232. self.showDialogSignal.emit()
  233. @pyqtSlot()
  234. def notifyClosed(self):
  235. """Used to notify the dialog so the lock can be released."""
  236. self._result = {} # The result should be cleared before hide, because after it is released the main thread lock
  237. self._visible = False
  238. try:
  239. self._lock.release()
  240. except:
  241. pass
  242. def hide(self):
  243. self._visible = False
  244. self._view.hide()
  245. try:
  246. self._lock.release()
  247. except:
  248. pass
  249. @pyqtSlot(bool)
  250. def _onVisibilityChanged(self, visible):
  251. if not visible:
  252. try:
  253. self._lock.release()
  254. except:
  255. pass
  256. @pyqtSlot()
  257. def onOkButtonClicked(self):
  258. self._view.hide()
  259. self.hide()
  260. @pyqtSlot()
  261. def onCancelButtonClicked(self):
  262. self._result = {}
  263. self._view.hide()
  264. self.hide()
  265. def waitForClose(self):
  266. """Block thread until the dialog is closed."""
  267. if self._visible:
  268. if threading.current_thread() != threading.main_thread():
  269. self._lock.acquire()
  270. self._lock.release()
  271. else:
  272. # If this is not run from a separate thread, we need to ensure that the events are still processed.
  273. while self._visible:
  274. time.sleep(1 / 50)
  275. QCoreApplication.processEvents() # Ensure that the GUI does not freeze.
  276. def __show(self):
  277. if self._view is None:
  278. self._createViewFromQML()
  279. if self._view:
  280. self._view.show()