WorkspaceDialog.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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_overriden_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_overriden_by_quality_changes
  129. def setNumSettingsOverridenByQualityChanges(self, num_settings_overriden_by_quality_changes):
  130. self._num_settings_overriden_by_quality_changes = num_settings_overriden_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. return len(ContainerRegistry.getInstance().findDefinitionContainers(id="fdmprinter")[0].getAllKeys())
  157. @pyqtProperty(int, notify = numVisibleSettingsChanged)
  158. def numVisibleSettings(self):
  159. return self._num_visible_settings
  160. def setNumVisibleSettings(self, num_visible_settings):
  161. if self._num_visible_settings != num_visible_settings:
  162. self._num_visible_settings = num_visible_settings
  163. self.numVisibleSettingsChanged.emit()
  164. @pyqtProperty(bool, notify = machineConflictChanged)
  165. def machineConflict(self):
  166. return self._has_machine_conflict
  167. @pyqtProperty(bool, notify=qualityChangesConflictChanged)
  168. def qualityChangesConflict(self):
  169. return self._has_quality_changes_conflict
  170. @pyqtProperty(bool, notify=materialConflictChanged)
  171. def materialConflict(self):
  172. return self._has_material_conflict
  173. @pyqtSlot(str, str)
  174. def setResolveStrategy(self, key, strategy):
  175. if key in self._result:
  176. self._result[key] = strategy
  177. ## Close the backend: otherwise one could end up with "Slicing..."
  178. @pyqtSlot()
  179. def closeBackend(self):
  180. Application.getInstance().getBackend().close()
  181. def setMaterialConflict(self, material_conflict):
  182. if self._has_material_conflict != material_conflict:
  183. self._has_material_conflict = material_conflict
  184. self.materialConflictChanged.emit()
  185. def setMachineConflict(self, machine_conflict):
  186. if self._has_machine_conflict != machine_conflict:
  187. self._has_machine_conflict = machine_conflict
  188. self.machineConflictChanged.emit()
  189. def setQualityChangesConflict(self, quality_changes_conflict):
  190. if self._has_quality_changes_conflict != quality_changes_conflict:
  191. self._has_quality_changes_conflict = quality_changes_conflict
  192. self.qualityChangesConflictChanged.emit()
  193. def getResult(self):
  194. if "machine" in self._result and not self._has_machine_conflict:
  195. self._result["machine"] = None
  196. if "quality_changes" in self._result and not self._has_quality_changes_conflict:
  197. self._result["quality_changes"] = None
  198. if "material" in self._result and not self._has_material_conflict:
  199. self._result["material"] = None
  200. # If the machine needs to be re-created, the definition_changes should also be re-created.
  201. # If the machine strategy is None, it means that there is no name conflict with existing ones. In this case
  202. # new definitions changes are created
  203. if "machine" in self._result:
  204. if self._result["machine"] == "new" or self._result["machine"] is None and self._result["definition_changes"] is None:
  205. self._result["definition_changes"] = "new"
  206. return self._result
  207. def _createViewFromQML(self):
  208. path = os.path.join(PluginRegistry.getInstance().getPluginPath("3MFReader"), self._qml_url)
  209. self._view = Application.getInstance().createQmlComponent(path, {"manager": self})
  210. def show(self):
  211. # Emit signal so the right thread actually shows the view.
  212. if threading.current_thread() != threading.main_thread():
  213. self._lock.acquire()
  214. # Reset the result
  215. self._result = {"machine": self._default_strategy,
  216. "quality_changes": self._default_strategy,
  217. "definition_changes": self._default_strategy,
  218. "material": self._default_strategy}
  219. self._visible = True
  220. self.showDialogSignal.emit()
  221. @pyqtSlot()
  222. ## Used to notify the dialog so the lock can be released.
  223. def notifyClosed(self):
  224. self._result = {} # The result should be cleared before hide, because after it is released the main thread lock
  225. self._visible = False
  226. try:
  227. self._lock.release()
  228. except:
  229. pass
  230. def hide(self):
  231. self._visible = False
  232. self._view.hide()
  233. try:
  234. self._lock.release()
  235. except:
  236. pass
  237. @pyqtSlot(bool)
  238. def _onVisibilityChanged(self, visible):
  239. if not visible:
  240. try:
  241. self._lock.release()
  242. except:
  243. pass
  244. @pyqtSlot()
  245. def onOkButtonClicked(self):
  246. self._view.hide()
  247. self.hide()
  248. @pyqtSlot()
  249. def onCancelButtonClicked(self):
  250. self._result = {}
  251. self._view.hide()
  252. self.hide()
  253. ## Block thread until the dialog is closed.
  254. def waitForClose(self):
  255. if self._visible:
  256. if threading.current_thread() != threading.main_thread():
  257. self._lock.acquire()
  258. self._lock.release()
  259. else:
  260. # If this is not run from a separate thread, we need to ensure that the events are still processed.
  261. while self._visible:
  262. time.sleep(1 / 50)
  263. QCoreApplication.processEvents() # Ensure that the GUI does not freeze.
  264. def __show(self):
  265. if self._view is None:
  266. self._createViewFromQML()
  267. if self._view:
  268. self._view.show()