Files
2026-10-16 09:06:03 +08:00

49 lines
2.0 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#ifndef LEDINDICATOR_H
#define LEDINDICATOR_H
#include <QColor>
#include <QMetaObject>
#include <QWidget>
// ===================== 指示灯(自绘圆形渐变 + 外发光) =====================
// 状态:灰=未运行/关闭/正常,绿=运行/开启/到位,红=故障/液位低,
// 黄=检测中;红闪/黄闪按 500ms 周期闪烁(透明度 40%↔100%)。
class LedIndicator : public QWidget
{
Q_OBJECT
public:
enum State { Off, Green, Red, RedBlink, Yellow, YellowBlink };
explicit LedIndicator(const QString& text, QWidget* parent = nullptr);
void setState(State s);
void setText(const QString& t);
void setTextPixelSize(int px) { m_textPx = px; updateGeometry(); update(); } // 文字字号
void setRadius(int r) { m_radius = r; updateGeometry(); update(); } // 圆形指示点半径
void setDotAfterText(bool after) { m_dotAfter = after; updateGeometry(); update(); } // 圆点画在文字后面
void setTextColor(const QColor& c) { m_textColor = c; update(); } // 文字颜色,默认 C::TEXT
// 文字是否随闪烁状态一起明暗(默认只闪圆点,文字常亮)
void setBlinkText(bool on) { m_blinkText = on; update(); }
State state() const { return m_state; }
QSize sizeHint() const override; // 宽度按文字实际宽度计算,避免文字被裁
protected:
void paintEvent(QPaintEvent*) override;
private:
int textLeft() const { return m_radius * 2 + 18; } // 文字起点:圆点区 + 左侧 10px 留白
State m_state = Off;
QString m_text;
QMetaObject::Connection m_blinkConn; // 订阅全局闪烁节拍(见 BlinkBeat)
bool m_on = true;
int m_radius = 16; // 圆形指示点 32×32
int m_textPx = 24; // 文字字号
bool m_dotAfter = false; // false=圆点在文字前,true=圆点在文字后
QColor m_textColor; // 无效色 = 用默认 C::TEXT
bool m_blinkText = false; // true=文字随闪烁明暗,false=文字常亮
};
#endif // LEDINDICATOR_H