ImageReaderUI.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os
  4. import threading
  5. from PyQt5.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 = 1
  25. self.peak_height = 10
  26. self.smoothing = 1
  27. self.image_color_invert = False;
  28. self._ui_lock = threading.Lock()
  29. self._cancelled = False
  30. self._disable_size_callbacks = False
  31. def setWidthAndDepth(self, width, depth):
  32. self._aspect = width / depth
  33. self._width = width
  34. self._depth = depth
  35. def getWidth(self):
  36. return self._width
  37. def getDepth(self):
  38. return self._depth
  39. def getCancelled(self):
  40. return self._cancelled
  41. def waitForUIToClose(self):
  42. self._ui_lock.acquire()
  43. self._ui_lock.release()
  44. def showConfigUI(self):
  45. self._ui_lock.acquire()
  46. self._cancelled = False
  47. self.show_config_ui_trigger.emit()
  48. def _actualShowConfigUI(self):
  49. self._disable_size_callbacks = True
  50. if self._ui_view is None:
  51. self._createConfigUI()
  52. self._ui_view.show()
  53. self._ui_view.findChild(QObject, "Width").setProperty("text", str(self._width))
  54. self._ui_view.findChild(QObject, "Depth").setProperty("text", str(self._depth))
  55. self._disable_size_callbacks = False
  56. self._ui_view.findChild(QObject, "Base_Height").setProperty("text", str(self.base_height))
  57. self._ui_view.findChild(QObject, "Peak_Height").setProperty("text", str(self.peak_height))
  58. self._ui_view.findChild(QObject, "Smoothing").setProperty("value", self.smoothing)
  59. def _createConfigUI(self):
  60. if self._ui_view is None:
  61. Logger.log("d", "Creating ImageReader config UI")
  62. path = os.path.join(PluginRegistry.getInstance().getPluginPath("ImageReader"), "ConfigUI.qml")
  63. self._ui_view = Application.getInstance().createQmlComponent(path, {"manager": self})
  64. self._ui_view.setFlags(self._ui_view.flags() & ~Qt.WindowCloseButtonHint & ~Qt.WindowMinimizeButtonHint & ~Qt.WindowMaximizeButtonHint);
  65. self._disable_size_callbacks = False
  66. @pyqtSlot()
  67. def onOkButtonClicked(self):
  68. self._cancelled = False
  69. self._ui_view.close()
  70. self._ui_lock.release()
  71. @pyqtSlot()
  72. def onCancelButtonClicked(self):
  73. self._cancelled = True
  74. self._ui_view.close()
  75. self._ui_lock.release()
  76. @pyqtSlot(str)
  77. def onWidthChanged(self, value):
  78. if self._ui_view and not self._disable_size_callbacks:
  79. if len(value) > 0:
  80. self._width = float(value.replace(",", "."))
  81. else:
  82. self._width = 0
  83. self._depth = self._width / self._aspect
  84. self._disable_size_callbacks = True
  85. self._ui_view.findChild(QObject, "Depth").setProperty("text", str(self._depth))
  86. self._disable_size_callbacks = False
  87. @pyqtSlot(str)
  88. def onDepthChanged(self, value):
  89. if self._ui_view and not self._disable_size_callbacks:
  90. if len(value) > 0:
  91. self._depth = float(value.replace(",", "."))
  92. else:
  93. self._depth = 0
  94. self._width = self._depth * self._aspect
  95. self._disable_size_callbacks = True
  96. self._ui_view.findChild(QObject, "Width").setProperty("text", str(self._width))
  97. self._disable_size_callbacks = False
  98. @pyqtSlot(str)
  99. def onBaseHeightChanged(self, value):
  100. if (len(value) > 0):
  101. self.base_height = float(value.replace(",", "."))
  102. else:
  103. self.base_height = 0
  104. @pyqtSlot(str)
  105. def onPeakHeightChanged(self, value):
  106. if (len(value) > 0):
  107. self.peak_height = float(value.replace(",", "."))
  108. else:
  109. self.peak_height = 0
  110. @pyqtSlot(float)
  111. def onSmoothingChanged(self, value):
  112. self.smoothing = int(value)
  113. @pyqtSlot(int)
  114. def onImageColorInvertChanged(self, value):
  115. self.image_color_invert = (value == 1)