| 20 | |
| 21 | |
| 22 | class WaterWidget(QWidget): |
| 23 | |
| 24 | def __init__(self, *args, **kwargs): |
| 25 | super(WaterWidget, self).__init__(*args, **kwargs) |
| 26 | # 浪高百分比 |
| 27 | self._waterHeight = 1 |
| 28 | # 密度 |
| 29 | self._waterDensity = 1 |
| 30 | # 波浪颜色1 |
| 31 | self._waterFgColor = QColor(33, 178, 148) |
| 32 | # 波浪颜色2 |
| 33 | self._waterBgColor = QColor(33, 178, 148, 100) |
| 34 | self.minimum = 0 |
| 35 | self.maximum = 0 |
| 36 | self._value = 0 |
| 37 | self._offset = 0 |
| 38 | # 每隔100ms刷新波浪(模拟波浪动态) |
| 39 | self._updateTimer = QTimer(self, timeout=self.update) |
| 40 | self._updateTimer.start(100) |
| 41 | |
| 42 | def update(self): |
| 43 | if self.minimum >= self.maximum: |
| 44 | return |
| 45 | super(WaterWidget, self).update() |
| 46 | |
| 47 | def paintEvent(self, event): |
| 48 | super(WaterWidget, self).paintEvent(event) |
| 49 | if self.minimum >= self.maximum: |
| 50 | return |
| 51 | if not self._updateTimer.isActive(): |
| 52 | return |
| 53 | |
| 54 | # 正弦曲线公式 y = A * sin(ωx + φ) + k |
| 55 | # 当前值所占百分比 |
| 56 | percent = 1 - (self._value - self.minimum) / \ |
| 57 | (self.maximum - self.minimum) |
| 58 | # w表示周期,6为人为定义 |
| 59 | w = 6 * self.waterDensity * math.pi / self.width() |
| 60 | # A振幅 高度百分比,1/26为人为定义 |
| 61 | A = self.height() * self.waterHeight * 1 / 26 |
| 62 | # k 高度百分比 |
| 63 | k = self.height() * percent |
| 64 | |
| 65 | # 波浪1 |
| 66 | waterPath1 = QPainterPath() |
| 67 | waterPath1.moveTo(0, self.height()) # 起点在左下角 |
| 68 | # 波浪2 |
| 69 | waterPath2 = QPainterPath() |
| 70 | waterPath2.moveTo(0, self.height()) # 起点在左下角 |
| 71 | |
| 72 | # 偏移 |
| 73 | self._offset += 0.6 |
| 74 | if self._offset > self.width() / 2: |
| 75 | self._offset = 0 |
| 76 | |
| 77 | for i in range(self.width() + 1): |
| 78 | # 从x轴开始计算y轴点 |
| 79 | y = A * math.sin(w * i + self._offset) + k |
no outgoing calls
no test coverage detected