led.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @文件 :led.py
  5. @时间 :2022/01/14 09:31:20
  6. @作者 :None
  7. @版本 :2.0
  8. @说明 :LED灯
  9. '''
  10. # from numpy import array, uint8
  11. from utils.qt import QApplication, QColor, QSize, QPushButton
  12. class Led(QPushButton):
  13. black = QColor(0x00, 0x00, 0x00)
  14. white = QColor(0xff, 0xff, 0xff)
  15. blue = QColor(0x73, 0xce, 0xf4)
  16. green = QColor(0xad, 0xff, 0x2f)
  17. orange = QColor(0xff, 0xa5, 0x00)
  18. purple = QColor(0xaf, 0x00, 0xff)
  19. red = QColor(0xf4, 0x37, 0x53)
  20. yellow = QColor(0xff, 0xff, 0x00)
  21. capsule = 1
  22. circle = 2
  23. rectangle = 3
  24. def __init__(self, parent, first_color=green, second_color=red, third_color=yellow, off_color=black, shape=circle):
  25. super().__init__()
  26. self._qss = 'QPushButton {{ \
  27. border: 3px solid lightgray; \
  28. border-radius: {}px; \
  29. background-color: \
  30. QLinearGradient( \
  31. y1: 0, y2: 1, \
  32. stop: 0 white, \
  33. stop: 0.2 #{}, \
  34. stop: 0.8 #{}, \
  35. stop: 1 #{} \
  36. ); \
  37. }}'
  38. self._first_qss = ''
  39. self._second_qss = ''
  40. self._third_qss = ''
  41. self._off_qss = ''
  42. self._status = 0
  43. self._end_radius = 0
  44. # Properties that will trigger changes on qss.
  45. self.__first_color = None
  46. self.__second_color = None
  47. self.__third_color = None
  48. self.__off_color = None
  49. self.__shape = None
  50. self.__height = 0
  51. self._first_color = first_color
  52. self._second_color = second_color
  53. self._third_color = third_color
  54. self._off_color = off_color
  55. self._shape = shape
  56. self._height = self.sizeHint().height()
  57. # =================================================== Reimplemented Methods
  58. def sizeHint(self):
  59. res_h = QApplication.desktop().screenGeometry().height()
  60. # res_w = QApplication.desktop().screenGeometry().width()
  61. # res_w, res_h = pyautogui.size() # Available resolution geometry
  62. if self._shape == Led.capsule:
  63. base_w = 50
  64. base_h = 30
  65. elif self._shape == Led.circle:
  66. base_w = 30
  67. base_h = 30
  68. elif self._shape == Led.rectangle:
  69. base_w = 40
  70. base_h = 30
  71. width = int(base_w * res_h / 1080)
  72. height = int(base_h * res_h / 1080)
  73. return QSize(width, height)
  74. def resizeEvent(self, event):
  75. self._height = self.size().height()
  76. QPushButton.resizeEvent(self, event)
  77. def setFixedSize(self, width, height):
  78. self._height = height
  79. if self._shape == Led.circle:
  80. QPushButton.setFixedSize(self, height, height)
  81. else:
  82. QPushButton.setFixedSize(self, width, height)
  83. # ============================================================== Properties
  84. # 一级故障
  85. @property
  86. def _first_color(self):
  87. return self.__first_color
  88. @_first_color.setter
  89. def _first_color(self, color):
  90. self.__first_color = color
  91. self._update_first_qss()
  92. @_first_color.deleter
  93. def _first_color(self):
  94. del self.__first_color
  95. # 二级故障
  96. @property
  97. def _second_color(self):
  98. return self.__second_color
  99. @_second_color.setter
  100. def _second_color(self, color):
  101. self.__second_color = color
  102. self._update_second_qss()
  103. @_second_color.deleter
  104. def _second_color(self):
  105. del self.__second_color
  106. # 三级故障
  107. @property
  108. def _third_color(self):
  109. return self.__third_color
  110. @_third_color.setter
  111. def _third_color(self, color):
  112. self.__third_color = color
  113. self._update_third_qss()
  114. @_third_color.deleter
  115. def _third_color(self):
  116. del self.__third_color
  117. # 无故障
  118. @property
  119. def _off_color(self):
  120. return self.__off_color
  121. @_off_color.setter
  122. def _off_color(self, color):
  123. self.__off_color = color
  124. self._update_off_qss()
  125. @_off_color.deleter
  126. def _off_color(self):
  127. del self.__off_color
  128. @property
  129. def _shape(self):
  130. return self.__shape
  131. @_shape.setter
  132. def _shape(self, shape):
  133. self.__shape = shape
  134. self._update_end_radius()
  135. self._update_first_qss()
  136. self._update_second_qss()
  137. self._update_third_qss()
  138. self._update_off_qss()
  139. self.set_status(self._status)
  140. @_shape.deleter
  141. def _shape(self):
  142. del self.__shape
  143. @property
  144. def _height(self):
  145. return self.__height
  146. @_height.setter
  147. def _height(self, height):
  148. self.__height = height
  149. self._update_end_radius()
  150. self._update_first_qss()
  151. self._update_second_qss()
  152. self._update_third_qss()
  153. self._update_off_qss()
  154. self.set_status(self._status)
  155. @_height.deleter
  156. def _height(self):
  157. del self.__height
  158. # ================================================================= Methods
  159. def _update_first_qss(self):
  160. color, grad = self._get_gradient(self.__first_color)
  161. self._first_qss = self._qss.format(self._end_radius, grad, color, color)
  162. def _update_second_qss(self):
  163. color, grad = self._get_gradient(self.__second_color)
  164. self._second_qss = self._qss.format(self._end_radius, grad, color, color)
  165. def _update_third_qss(self):
  166. color, grad = self._get_gradient(self.__third_color)
  167. self._third_qss = self._qss.format(self._end_radius, grad, color, color)
  168. def _update_off_qss(self):
  169. color, grad = self._get_gradient(self.__off_color)
  170. self._off_qss = self._qss.format(self._end_radius, grad, color, color)
  171. def _get_gradient(self, color):
  172. grad = QColor(int((self.white.red() - color.red()) / 2) + color.red(), int((self.white.green() - color.green()) / 2) + color.green(), int((self.white.blue() - color.blue()) / 2) + color.blue())
  173. grad = '{:02X}{:02X}{:02X}'.format(grad.red(), grad.green(), grad.blue())
  174. color = '{:02X}{:02X}{:02X}'.format(color.red(), color.green(), color.blue())
  175. return color, grad
  176. def _update_end_radius(self):
  177. if self.__shape == Led.rectangle:
  178. self._end_radius = int(self.__height / 10)
  179. else:
  180. self._end_radius = int(self.__height / 2)
  181. def _toggle_first(self):
  182. self.setStyleSheet(self._first_qss)
  183. def _toggle_second(self):
  184. self.setStyleSheet(self._second_qss)
  185. def _toggle_third(self):
  186. self.setStyleSheet(self._third_qss)
  187. def _toggle_off(self):
  188. self.setStyleSheet(self._off_qss)
  189. def set_first_color(self, color):
  190. self._first_color = color
  191. def set_second_color(self, color):
  192. self._second_color = color
  193. def set_third_color(self, color):
  194. self._third_color = color
  195. def set_off_color(self, color):
  196. self._off_color = color
  197. def set_shape(self, shape):
  198. self._shape = shape
  199. def set_status(self, status):
  200. self._status = status
  201. if self._status == 0:
  202. self._toggle_off()
  203. elif self._status == 1:
  204. self._toggle_first()
  205. elif self._status == 2:
  206. self._toggle_second()
  207. else:
  208. self._toggle_third()