123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- #!/usr/bin/env python
- # -*- encoding: utf-8 -*-
- '''
- @文件 :led.py
- @时间 :2022/01/14 09:31:20
- @作者 :None
- @版本 :2.0
- @说明 :LED灯
- '''
- # from numpy import array, uint8
- from utils.qt import QApplication, QColor, QSize, QPushButton
- class Led(QPushButton):
- black = QColor(0x00, 0x00, 0x00)
- white = QColor(0xff, 0xff, 0xff)
- blue = QColor(0x73, 0xce, 0xf4)
- green = QColor(0xad, 0xff, 0x2f)
- orange = QColor(0xff, 0xa5, 0x00)
- purple = QColor(0xaf, 0x00, 0xff)
- red = QColor(0xf4, 0x37, 0x53)
- yellow = QColor(0xff, 0xff, 0x00)
- capsule = 1
- circle = 2
- rectangle = 3
- def __init__(self, parent, first_color=green, second_color=red, third_color=yellow, off_color=black, shape=circle):
- super().__init__()
- self._qss = 'QPushButton {{ \
- border: 3px solid lightgray; \
- border-radius: {}px; \
- background-color: \
- QLinearGradient( \
- y1: 0, y2: 1, \
- stop: 0 white, \
- stop: 0.2 #{}, \
- stop: 0.8 #{}, \
- stop: 1 #{} \
- ); \
- }}'
- self._first_qss = ''
- self._second_qss = ''
- self._third_qss = ''
- self._off_qss = ''
- self._status = 0
- self._end_radius = 0
- # Properties that will trigger changes on qss.
- self.__first_color = None
- self.__second_color = None
- self.__third_color = None
- self.__off_color = None
- self.__shape = None
- self.__height = 0
- self._first_color = first_color
- self._second_color = second_color
- self._third_color = third_color
- self._off_color = off_color
- self._shape = shape
- self._height = self.sizeHint().height()
- # =================================================== Reimplemented Methods
- def sizeHint(self):
- res_h = QApplication.desktop().screenGeometry().height()
- # res_w = QApplication.desktop().screenGeometry().width()
- # res_w, res_h = pyautogui.size() # Available resolution geometry
- if self._shape == Led.capsule:
- base_w = 50
- base_h = 30
- elif self._shape == Led.circle:
- base_w = 30
- base_h = 30
- elif self._shape == Led.rectangle:
- base_w = 40
- base_h = 30
- width = int(base_w * res_h / 1080)
- height = int(base_h * res_h / 1080)
- return QSize(width, height)
- def resizeEvent(self, event):
- self._height = self.size().height()
- QPushButton.resizeEvent(self, event)
- def setFixedSize(self, width, height):
- self._height = height
- if self._shape == Led.circle:
- QPushButton.setFixedSize(self, height, height)
- else:
- QPushButton.setFixedSize(self, width, height)
- # ============================================================== Properties
- # 一级故障
- @property
- def _first_color(self):
- return self.__first_color
- @_first_color.setter
- def _first_color(self, color):
- self.__first_color = color
- self._update_first_qss()
- @_first_color.deleter
- def _first_color(self):
- del self.__first_color
- # 二级故障
- @property
- def _second_color(self):
- return self.__second_color
- @_second_color.setter
- def _second_color(self, color):
- self.__second_color = color
- self._update_second_qss()
- @_second_color.deleter
- def _second_color(self):
- del self.__second_color
- # 三级故障
- @property
- def _third_color(self):
- return self.__third_color
- @_third_color.setter
- def _third_color(self, color):
- self.__third_color = color
- self._update_third_qss()
- @_third_color.deleter
- def _third_color(self):
- del self.__third_color
- # 无故障
- @property
- def _off_color(self):
- return self.__off_color
- @_off_color.setter
- def _off_color(self, color):
- self.__off_color = color
- self._update_off_qss()
- @_off_color.deleter
- def _off_color(self):
- del self.__off_color
- @property
- def _shape(self):
- return self.__shape
- @_shape.setter
- def _shape(self, shape):
- self.__shape = shape
- self._update_end_radius()
- self._update_first_qss()
- self._update_second_qss()
- self._update_third_qss()
- self._update_off_qss()
- self.set_status(self._status)
- @_shape.deleter
- def _shape(self):
- del self.__shape
- @property
- def _height(self):
- return self.__height
- @_height.setter
- def _height(self, height):
- self.__height = height
- self._update_end_radius()
- self._update_first_qss()
- self._update_second_qss()
- self._update_third_qss()
- self._update_off_qss()
- self.set_status(self._status)
- @_height.deleter
- def _height(self):
- del self.__height
- # ================================================================= Methods
- def _update_first_qss(self):
- color, grad = self._get_gradient(self.__first_color)
- self._first_qss = self._qss.format(self._end_radius, grad, color, color)
- def _update_second_qss(self):
- color, grad = self._get_gradient(self.__second_color)
- self._second_qss = self._qss.format(self._end_radius, grad, color, color)
- def _update_third_qss(self):
- color, grad = self._get_gradient(self.__third_color)
- self._third_qss = self._qss.format(self._end_radius, grad, color, color)
- def _update_off_qss(self):
- color, grad = self._get_gradient(self.__off_color)
- self._off_qss = self._qss.format(self._end_radius, grad, color, color)
- def _get_gradient(self, color):
- 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())
- grad = '{:02X}{:02X}{:02X}'.format(grad.red(), grad.green(), grad.blue())
- color = '{:02X}{:02X}{:02X}'.format(color.red(), color.green(), color.blue())
- return color, grad
- def _update_end_radius(self):
- if self.__shape == Led.rectangle:
- self._end_radius = int(self.__height / 10)
- else:
- self._end_radius = int(self.__height / 2)
- def _toggle_first(self):
- self.setStyleSheet(self._first_qss)
- def _toggle_second(self):
- self.setStyleSheet(self._second_qss)
- def _toggle_third(self):
- self.setStyleSheet(self._third_qss)
- def _toggle_off(self):
- self.setStyleSheet(self._off_qss)
- def set_first_color(self, color):
- self._first_color = color
- def set_second_color(self, color):
- self._second_color = color
- def set_third_color(self, color):
- self._third_color = color
- def set_off_color(self, color):
- self._off_color = color
- def set_shape(self, shape):
- self._shape = shape
- def set_status(self, status):
- self._status = status
- if self._status == 0:
- self._toggle_off()
- elif self._status == 1:
- self._toggle_first()
- elif self._status == 2:
- self._toggle_second()
- else:
- self._toggle_third()
|