ImageReaderUI.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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, QUrl, pyqtSignal, QObject
  6. from PyQt5.QtQml import QQmlComponent, QQmlContext
  7. from UM.FlameProfiler import pyqtSlot
  8. from UM.Application import Application
  9. from UM.PluginRegistry import PluginRegistry
  10. from UM.Logger import Logger
  11. from UM.i18n import i18nCatalog
  12. catalog = i18nCatalog("cura")
  13. class ImageReaderUI(QObject):
  14. show_config_ui_trigger = pyqtSignal()
  15. def __init__(self, image_reader):
  16. super(ImageReaderUI, self).__init__()
  17. self.image_reader = image_reader
  18. self._ui_view = None
  19. self.show_config_ui_trigger.connect(self._actualShowConfigUI)
  20. self.default_width = 120
  21. self.default_depth = 120
  22. self._aspect = 1
  23. self._width = self.default_width
  24. self._depth = self.default_depth
  25. self.base_height = 1
  26. self.peak_height = 10
  27. self.smoothing = 1
  28. self.image_color_invert = False;
  29. self._ui_lock = threading.Lock()
  30. self._cancelled = False
  31. self._disable_size_callbacks = False
  32. def setWidthAndDepth(self, width, depth):
  33. self._aspect = width / depth
  34. self._width = width
  35. self._depth = depth
  36. def getWidth(self):
  37. return self._width
  38. def getDepth(self):
  39. return self._depth
  40. def getCancelled(self):
  41. return self._cancelled
  42. def waitForUIToClose(self):
  43. self._ui_lock.acquire()
  44. self._ui_lock.release()
  45. def showConfigUI(self):
  46. self._ui_lock.acquire()
  47. self._cancelled = False
  48. self.show_config_ui_trigger.emit()
  49. def _actualShowConfigUI(self):
  50. self._disable_size_callbacks = True
  51. if self._ui_view is None:
  52. self._createConfigUI()
  53. self._ui_view.show()
  54. self._ui_view.findChild(QObject, "Width").setProperty("text", str(self._width))
  55. self._ui_view.findChild(QObject, "Depth").setProperty("text", str(self._depth))
  56. self._disable_size_callbacks = False
  57. self._ui_view.findChild(QObject, "Base_Height").setProperty("text", str(self.base_height))
  58. self._ui_view.findChild(QObject, "Peak_Height").setProperty("text", str(self.peak_height))
  59. self._ui_view.findChild(QObject, "Smoothing").setProperty("value", self.smoothing)
  60. def _createConfigUI(self):
  61. if self._ui_view is None:
  62. Logger.log("d", "Creating ImageReader config UI")
  63. path = QUrl.fromLocalFile(os.path.join(PluginRegistry.getInstance().getPluginPath("ImageReader"), "ConfigUI.qml"))
  64. component = QQmlComponent(Application.getInstance()._engine, path)
  65. self._ui_context = QQmlContext(Application.getInstance()._engine.rootContext())
  66. self._ui_context.setContextProperty("manager", self)
  67. self._ui_view = component.create(self._ui_context)
  68. self._ui_view.setFlags(self._ui_view.flags() & ~Qt.WindowCloseButtonHint & ~Qt.WindowMinimizeButtonHint & ~Qt.WindowMaximizeButtonHint);
  69. self._disable_size_callbacks = False
  70. @pyqtSlot()
  71. def onOkButtonClicked(self):
  72. self._cancelled = False
  73. self._ui_view.close()
  74. self._ui_lock.release()
  75. @pyqtSlot()
  76. def onCancelButtonClicked(self):
  77. self._cancelled = True
  78. self._ui_view.close()
  79. self._ui_lock.release()
  80. @pyqtSlot(str)
  81. def onWidthChanged(self, value):
  82. if self._ui_view and not self._disable_size_callbacks:
  83. if len(value) > 0:
  84. self._width = float(value)
  85. else:
  86. self._width = 0
  87. self._depth = self._width / self._aspect
  88. self._disable_size_callbacks = True
  89. self._ui_view.findChild(QObject, "Depth").setProperty("text", str(self._depth))
  90. self._disable_size_callbacks = False
  91. @pyqtSlot(str)
  92. def onDepthChanged(self, value):
  93. if self._ui_view and not self._disable_size_callbacks:
  94. if len(value) > 0:
  95. self._depth = float(value)
  96. else:
  97. self._depth = 0
  98. self._width = self._depth * self._aspect
  99. self._disable_size_callbacks = True
  100. self._ui_view.findChild(QObject, "Width").setProperty("text", str(self._width))
  101. self._disable_size_callbacks = False
  102. @pyqtSlot(str)
  103. def onBaseHeightChanged(self, value):
  104. if (len(value) > 0):
  105. self.base_height = float(value)
  106. else:
  107. self.base_height = 0
  108. @pyqtSlot(str)
  109. def onPeakHeightChanged(self, value):
  110. if (len(value) > 0):
  111. self.peak_height = float(value)
  112. else:
  113. self.peak_height = 0
  114. @pyqtSlot(float)
  115. def onSmoothingChanged(self, value):
  116. self.smoothing = int(value)
  117. @pyqtSlot(int)
  118. def onImageColorInvertChanged(self, value):
  119. if (value == 1):
  120. self.image_color_invert = True
  121. else:
  122. self.image_color_invert = False