ImageReaderUI.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. import os
  4. import threading
  5. from PyQt5.QtCore import Qt, QUrl, pyqtSignal, pyqtSlot, QObject
  6. from PyQt5.QtQml import QQmlComponent, QQmlContext
  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 = QUrl.fromLocalFile(os.path.join(PluginRegistry.getInstance().getPluginPath("ImageReader"), "ConfigUI.qml"))
  63. component = QQmlComponent(Application.getInstance()._engine, path)
  64. self._ui_context = QQmlContext(Application.getInstance()._engine.rootContext())
  65. self._ui_context.setContextProperty("manager", self)
  66. self._ui_view = component.create(self._ui_context)
  67. self._ui_view.setFlags(self._ui_view.flags() & ~Qt.WindowCloseButtonHint & ~Qt.WindowMinimizeButtonHint & ~Qt.WindowMaximizeButtonHint);
  68. self._disable_size_callbacks = False
  69. @pyqtSlot()
  70. def onOkButtonClicked(self):
  71. self._cancelled = False
  72. self._ui_view.close()
  73. self._ui_lock.release()
  74. @pyqtSlot()
  75. def onCancelButtonClicked(self):
  76. self._cancelled = True
  77. self._ui_view.close()
  78. self._ui_lock.release()
  79. @pyqtSlot(str)
  80. def onWidthChanged(self, value):
  81. if self._ui_view and not self._disable_size_callbacks:
  82. if len(value) > 0:
  83. self._width = float(value)
  84. else:
  85. self._width = 0
  86. self._depth = self._width / self._aspect
  87. self._disable_size_callbacks = True
  88. self._ui_view.findChild(QObject, "Depth").setProperty("text", str(self._depth))
  89. self._disable_size_callbacks = False
  90. @pyqtSlot(str)
  91. def onDepthChanged(self, value):
  92. if self._ui_view and not self._disable_size_callbacks:
  93. if len(value) > 0:
  94. self._depth = float(value)
  95. else:
  96. self._depth = 0
  97. self._width = self._depth * self._aspect
  98. self._disable_size_callbacks = True
  99. self._ui_view.findChild(QObject, "Width").setProperty("text", str(self._width))
  100. self._disable_size_callbacks = False
  101. @pyqtSlot(str)
  102. def onBaseHeightChanged(self, value):
  103. if (len(value) > 0):
  104. self.base_height = float(value)
  105. else:
  106. self.base_height = 0
  107. @pyqtSlot(str)
  108. def onPeakHeightChanged(self, value):
  109. if (len(value) > 0):
  110. self.peak_height = float(value)
  111. else:
  112. self.peak_height = 0
  113. @pyqtSlot(float)
  114. def onSmoothingChanged(self, value):
  115. self.smoothing = int(value)
  116. @pyqtSlot(int)
  117. def onImageColorInvertChanged(self, value):
  118. if (value == 1):
  119. self.image_color_invert = True
  120. else:
  121. self.image_color_invert = False