WorkspaceDialog.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import QUrl, pyqtSignal, QObject, pyqtProperty, QCoreApplication
  4. from UM.FlameProfiler import pyqtSlot
  5. from PyQt5.QtQml import QQmlComponent, QQmlContext
  6. from UM.PluginRegistry import PluginRegistry
  7. from UM.Application import Application
  8. from UM.Logger import Logger
  9. from UM.i18n import i18nCatalog
  10. from UM.Settings.ContainerRegistry import ContainerRegistry
  11. import os
  12. import threading
  13. import time
  14. i18n_catalog = i18nCatalog("cura")
  15. class WorkspaceDialog(QObject):
  16. showDialogSignal = pyqtSignal()
  17. def __init__(self, parent = None):
  18. super().__init__(parent)
  19. self._component = None
  20. self._context = None
  21. self._view = None
  22. self._qml_url = "WorkspaceDialog.qml"
  23. self._lock = threading.Lock()
  24. self._default_strategy = None
  25. self._result = {"machine": self._default_strategy,
  26. "quality_changes": self._default_strategy,
  27. "definition_changes": self._default_strategy,
  28. "material": self._default_strategy}
  29. self._visible = False
  30. self.showDialogSignal.connect(self.__show)
  31. self._has_quality_changes_conflict = False
  32. self._has_definition_changes_conflict = False
  33. self._has_machine_conflict = False
  34. self._has_material_conflict = False
  35. self._num_visible_settings = 0
  36. self._num_user_settings = 0
  37. self._active_mode = ""
  38. self._quality_name = ""
  39. self._num_settings_overriden_by_quality_changes = 0
  40. self._quality_type = ""
  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. machineConflictChanged = pyqtSignal()
  48. qualityChangesConflictChanged = pyqtSignal()
  49. definitionChangesConflictChanged = pyqtSignal()
  50. materialConflictChanged = pyqtSignal()
  51. numVisibleSettingsChanged = pyqtSignal()
  52. activeModeChanged = pyqtSignal()
  53. qualityNameChanged = 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, constant = True)
  141. def totalNumberOfSettings(self):
  142. return len(ContainerRegistry.getInstance().findDefinitionContainers(id="fdmprinter")[0].getAllKeys())
  143. @pyqtProperty(int, notify = numVisibleSettingsChanged)
  144. def numVisibleSettings(self):
  145. return self._num_visible_settings
  146. def setNumVisibleSettings(self, num_visible_settings):
  147. if self._num_visible_settings != num_visible_settings:
  148. self._num_visible_settings = num_visible_settings
  149. self.numVisibleSettingsChanged.emit()
  150. @pyqtProperty(bool, notify = machineConflictChanged)
  151. def machineConflict(self):
  152. return self._has_machine_conflict
  153. @pyqtProperty(bool, notify=qualityChangesConflictChanged)
  154. def qualityChangesConflict(self):
  155. return self._has_quality_changes_conflict
  156. @pyqtProperty(bool, notify=definitionChangesConflictChanged)
  157. def definitionChangesConflict(self):
  158. return self._has_definition_changes_conflict
  159. @pyqtProperty(bool, notify=materialConflictChanged)
  160. def materialConflict(self):
  161. return self._has_material_conflict
  162. @pyqtSlot(str, str)
  163. def setResolveStrategy(self, key, strategy):
  164. if key in self._result:
  165. self._result[key] = strategy
  166. ## Close the backend: otherwise one could end up with "Slicing..."
  167. @pyqtSlot()
  168. def closeBackend(self):
  169. Application.getInstance().getBackend().close()
  170. def setMaterialConflict(self, material_conflict):
  171. if self._has_material_conflict != material_conflict:
  172. self._has_material_conflict = material_conflict
  173. self.materialConflictChanged.emit()
  174. def setMachineConflict(self, machine_conflict):
  175. if self._has_machine_conflict != machine_conflict:
  176. self._has_machine_conflict = machine_conflict
  177. self.machineConflictChanged.emit()
  178. def setQualityChangesConflict(self, quality_changes_conflict):
  179. if self._has_quality_changes_conflict != quality_changes_conflict:
  180. self._has_quality_changes_conflict = quality_changes_conflict
  181. self.qualityChangesConflictChanged.emit()
  182. def setDefinitionChangesConflict(self, definition_changes_conflict):
  183. if self._has_definition_changes_conflict != definition_changes_conflict:
  184. self._has_definition_changes_conflict = definition_changes_conflict
  185. self.definitionChangesConflictChanged.emit()
  186. def getResult(self):
  187. if "machine" in self._result and not self._has_machine_conflict:
  188. self._result["machine"] = None
  189. if "quality_changes" in self._result and not self._has_quality_changes_conflict:
  190. self._result["quality_changes"] = None
  191. if "definition_changes" in self._result and not self._has_definition_changes_conflict:
  192. self._result["definition_changes"] = None
  193. if "material" in self._result and not self._has_material_conflict:
  194. self._result["material"] = None
  195. # If the machine needs to be re-created, the definition_changes should also be re-created.
  196. # If the machine strategy is None, it means that there is no name conflict with existing ones. In this case
  197. # new definitions changes are created
  198. if "machine" in self._result:
  199. if self._result["machine"] == "new" or self._result["machine"] is None and self._result["definition_changes"] is None:
  200. self._result["definition_changes"] = "new"
  201. return self._result
  202. def _createViewFromQML(self):
  203. path = QUrl.fromLocalFile(os.path.join(PluginRegistry.getInstance().getPluginPath("3MFReader"), self._qml_url))
  204. self._component = QQmlComponent(Application.getInstance()._engine, path)
  205. self._context = QQmlContext(Application.getInstance()._engine.rootContext())
  206. self._context.setContextProperty("manager", self)
  207. self._view = self._component.create(self._context)
  208. if self._view is None:
  209. Logger.log("c", "QQmlComponent status %s", self._component.status())
  210. Logger.log("c", "QQmlComponent error string %s", self._component.errorString())
  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()
  239. def onOkButtonClicked(self):
  240. self._view.hide()
  241. self.hide()
  242. @pyqtSlot()
  243. def onCancelButtonClicked(self):
  244. self._result = {}
  245. self._view.hide()
  246. self.hide()
  247. ## Block thread until the dialog is closed.
  248. def waitForClose(self):
  249. if self._visible:
  250. if threading.current_thread() != threading.main_thread():
  251. self._lock.acquire()
  252. self._lock.release()
  253. else:
  254. # If this is not run from a separate thread, we need to ensure that the events are still processed.
  255. while self._visible:
  256. time.sleep(1 / 50)
  257. QCoreApplication.processEvents() # Ensure that the GUI does not freeze.
  258. def __show(self):
  259. if self._view is None:
  260. self._createViewFromQML()
  261. if self._view:
  262. self._view.show()