WorkspaceDialog.py 12 KB

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