WorkspaceDialog.py 12 KB

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