ImageReaderUI.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # Copyright (c) 2020 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os
  4. import threading
  5. from PyQt6.QtCore import Qt, pyqtSignal, QObject
  6. from UM.FlameProfiler import pyqtSlot
  7. from UM.Application import Application
  8. from UM.PluginRegistry import PluginRegistry
  9. from UM.Logger import Logger
  10. from UM.i18n import i18nCatalog
  11. catalog = i18nCatalog("cura")
  12. class ImageReaderUI(QObject):
  13. show_config_ui_trigger = pyqtSignal()
  14. def __init__(self, image_reader):
  15. super(ImageReaderUI, self).__init__()
  16. self.image_reader = image_reader
  17. self._ui_view = None
  18. self.show_config_ui_trigger.connect(self._actualShowConfigUI)
  19. self.default_width = 120
  20. self.default_depth = 120
  21. self._aspect = 1
  22. self._width = self.default_width
  23. self._depth = self.default_depth
  24. self.base_height = 0.4
  25. self.peak_height = 2.5
  26. self.smoothing = 1
  27. self.lighter_is_higher = False
  28. self.use_transparency_model = True
  29. self.transmittance_1mm = 50.0 # based on pearl PLA
  30. self._ui_lock = threading.Lock()
  31. self._cancelled = False
  32. self._disable_size_callbacks = False
  33. def setWidthAndDepth(self, width, depth):
  34. self._aspect = width / depth
  35. self._width = width
  36. self._depth = depth
  37. def getWidth(self):
  38. return self._width
  39. def getDepth(self):
  40. return self._depth
  41. def getCancelled(self):
  42. return self._cancelled
  43. def waitForUIToClose(self):
  44. self._ui_lock.acquire()
  45. self._ui_lock.release()
  46. def showConfigUI(self):
  47. self._ui_lock.acquire()
  48. self._cancelled = False
  49. self.show_config_ui_trigger.emit()
  50. def _actualShowConfigUI(self):
  51. self._disable_size_callbacks = True
  52. if self._ui_view is None:
  53. self._createConfigUI()
  54. self._ui_view.show()
  55. self._ui_view.findChild(QObject, "Width").setProperty("text", str(self._width))
  56. self._ui_view.findChild(QObject, "Depth").setProperty("text", str(self._depth))
  57. self._disable_size_callbacks = False
  58. self._ui_view.findChild(QObject, "Base_Height").setProperty("text", str(self.base_height))
  59. self._ui_view.findChild(QObject, "Peak_Height").setProperty("text", str(self.peak_height))
  60. self._ui_view.findChild(QObject, "Transmittance").setProperty("text", str(self.transmittance_1mm))
  61. self._ui_view.findChild(QObject, "Smoothing").setProperty("value", self.smoothing)
  62. def _createConfigUI(self):
  63. if self._ui_view is None:
  64. Logger.log("d", "Creating ImageReader config UI")
  65. path = os.path.join(PluginRegistry.getInstance().getPluginPath("ImageReader"), "ConfigUI.qml")
  66. self._ui_view = Application.getInstance().createQmlComponent(path, {"manager": self})
  67. self._ui_view.setFlags(self._ui_view.flags() & ~Qt.WindowType.WindowCloseButtonHint & ~Qt.WindowType.WindowMinimizeButtonHint & ~Qt.WindowType.WindowMaximizeButtonHint)
  68. self._disable_size_callbacks = False
  69. @pyqtSlot()
  70. def onOkButtonClicked(self):
  71. self._cancelled = False
  72. self._ui_view.close()
  73. try:
  74. self._ui_lock.release()
  75. except RuntimeError:
  76. # We don't really care if it was held or not. Just make sure it's not held now
  77. pass
  78. @pyqtSlot()
  79. def onCancelButtonClicked(self):
  80. self._cancelled = True
  81. self._ui_view.close()
  82. try:
  83. self._ui_lock.release()
  84. except RuntimeError:
  85. # We don't really care if it was held or not. Just make sure it's not held now
  86. pass
  87. @pyqtSlot(str)
  88. def onWidthChanged(self, value):
  89. if self._ui_view and not self._disable_size_callbacks:
  90. if len(value) > 0:
  91. try:
  92. self._width = float(value.replace(",", "."))
  93. except ValueError: # Can happen with incomplete numbers, such as "-".
  94. self._width = 0
  95. else:
  96. self._width = 0
  97. self._depth = self._width / self._aspect
  98. self._disable_size_callbacks = True
  99. self._ui_view.findChild(QObject, "Depth").setProperty("text", str(self._depth))
  100. self._disable_size_callbacks = False
  101. @pyqtSlot(str)
  102. def onDepthChanged(self, value):
  103. if self._ui_view and not self._disable_size_callbacks:
  104. if len(value) > 0:
  105. try:
  106. self._depth = float(value.replace(",", "."))
  107. except ValueError: # Can happen with incomplete numbers, such as "-".
  108. self._depth = 0
  109. else:
  110. self._depth = 0
  111. self._width = self._depth * self._aspect
  112. self._disable_size_callbacks = True
  113. self._ui_view.findChild(QObject, "Width").setProperty("text", str(self._width))
  114. self._disable_size_callbacks = False
  115. @pyqtSlot(str)
  116. def onBaseHeightChanged(self, value):
  117. if len(value) > 0:
  118. try:
  119. self.base_height = float(value.replace(",", "."))
  120. except ValueError: # Can happen with incomplete numbers, such as "-".
  121. self.base_height = 0
  122. else:
  123. self.base_height = 0
  124. @pyqtSlot(str)
  125. def onPeakHeightChanged(self, value):
  126. if len(value) > 0:
  127. try:
  128. self.peak_height = float(value.replace(",", "."))
  129. if self.peak_height < 0:
  130. self.peak_height = 2.5
  131. except ValueError: # Can happen with incomplete numbers, such as "-".
  132. self.peak_height = 2.5 # restore default
  133. else:
  134. self.peak_height = 0
  135. @pyqtSlot(float)
  136. def onSmoothingChanged(self, value):
  137. self.smoothing = int(value)
  138. @pyqtSlot(int)
  139. def onImageColorInvertChanged(self, value):
  140. self.lighter_is_higher = (value == 1)
  141. @pyqtSlot(int)
  142. def onColorModelChanged(self, value):
  143. self.use_transparency_model = (value == 1)
  144. @pyqtSlot(int)
  145. def onTransmittanceChanged(self, value):
  146. self.transmittance_1mm = value