Qt水波显示器

作者:cql,QQ:609162385
因为借鉴了他的控件,这里要感谢飞扬青云。
在这里插入图片描述

#ifndef KERNELWAVEWIDGET_H
#define KERNELWAVEWIDGET_H

#include <QWidget>
#include <QPaintEvent>
#include <QPainter>
class QTimer;

class KernelWaveWidget : public QWidget
{
    Q_OBJECT
    Q_ENUMS(PercentStyle)
    Q_PROPERTY(int minValue READ getMinValue WRITE setMinValue)
    Q_PROPERTY(int maxValue READ getMaxValue WRITE setMaxValue)
    Q_PROPERTY(int value READ getValue WRITE setValue)

    Q_PROPERTY(double borderWidth READ getBorderWidth WRITE setBorderWidth)
    Q_PROPERTY(double waterHeight READ getWaterHeight WRITE setWaterHeight)
    Q_PROPERTY(double waterDensity READ getWaterDensity WRITE setWaterDensity)

    Q_PROPERTY(bool showValue READ getShowValue WRITE setShowValue)
    Q_PROPERTY(bool showPercent READ getShowPercent WRITE setShowPercent)

    Q_PROPERTY(QColor borderColor READ getBorderColor WRITE setBorderColor)
    Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)
    Q_PROPERTY(QColor usedColor READ getUsedColor WRITE setUsedColor)
    Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor)

    Q_PROPERTY(PercentStyle percentStyle READ getPercentStyle WRITE setPercentStyle)
public:
    enum PercentStyle {
        PercentStyle_Rect = 0,          //矩形风格
        PercentStyle_Circle = 1,        //圆形风格
        PercentStyle_Ellipse = 2,       //椭圆风格
        PercentStyle_Cylinder = 3,      //圆柱风格
    };
    explicit KernelWaveWidget(QWidget *parent = 0);
     ~KernelWaveWidget();
protected:
    void paintEvent(QPaintEvent *);
    void drawBg(QPainter *painter);
    void drawProgress(QPainter *painter);
    void drawValue(QPainter *painter);

private:
    int maxValue;                   //最小值
    int minValue;                   //最大值
    int value;                      //目标值

    double borderWidth;             //边框宽度
    double waterHeight;             //水波高度
    double waterDensity;            //水波密度

    bool showValue;                 //是否显示值
    bool showPercent;               //是否显示百分比

    QColor startColor;
    QColor endColor;
    QColor borderColor;             //边框颜色
    QColor bgColor;                 //背景颜色
    QColor usedColor;               //进度颜色
    QColor textColor;               //文字颜色

    PercentStyle percentStyle;      //进度样式风格

    double offset;                  //水波偏移量
    QTimer *timer;                  //定时器绘制动画

    QString titleStr;//头
    QString unitStr;//单位
    QString valueStrA;
    QString valueStrB;
    QString valueStrC;

public:
    int getMinValue()               const;
    int getMaxValue()               const;
    int getValue()                  const;

    double getBorderWidth()         const;
    double getWaterHeight()         const;
    double getWaterDensity()        const;

    bool getShowPercent()           const;
    bool getShowValue()             const;

    QColor getBorderColor()         const;
    QColor getBgColor()             const;
    QColor getUsedColor()           const;
    QColor getTextColor()           const;

    PercentStyle getPercentStyle()  const;

    QSize sizeHint()                const;
    QSize minimumSizeHint()         const;

public Q_SLOTS:
    void setStartColor(QColor color);
    void setEndColor(QColor color);
    //设置范围值
    void setRange(int minValue, int maxValue);

    //设置最大最小值
    void setMinValue(int minValue);
    void setMaxValue(int maxValue);

    //设置目标值
    void setValue(int value);

    //设置边框宽度
    void setBorderWidth(double borderWidth);

    //设置水波高度
    void setWaterHeight(double waterHeight);

    //设置水波密度
    void setWaterDensity(double waterDensity);

    //设置是否显示值
    void setShowValue(bool showValue);

    //设置是否显示百分比
    void setShowPercent(bool showPercent);

    //设置边框颜色
    void setBorderColor(const QColor &borderColor);
    //设置背景颜色
    void setBgColor(const QColor &bgColor);
    //设置进度颜色
    void setUsedColor(const QColor &usedColor);
    //设置文本颜色
    void setTextColor(const QColor &textColor);

    //设置进度样式风格
    void setPercentStyle(PercentStyle percentStyle);

    void setTitleStr(QString p_str);//头
    void setUnitStr(QString p_str);//单位
    void setValueStrA(QString p_str);
    void setValueStrB(QString p_str);
    void setValueStrC(QString p_str);
Q_SIGNALS:
    void valueChanged(int value);

};

#endif // KERNELWAVEWIDGET_H

#pragma execution_character_set("utf-8")

#include "kernelwavewidget.h"
#include "qpainter.h"
#include "qmath.h"
#include "qtimer.h"
#include "qdebug.h"

KernelWaveWidget::KernelWaveWidget(QWidget *parent) : QWidget(parent)
{
    minValue = 0;
    maxValue = 100;
    value = 0;

    borderWidth = 20.0;
    waterHeight = 0.02;
    waterDensity = 2.0;

    showValue = true;
    showPercent = true;
    titleStr = "Paw";
    unitStr = "cmH2O";
    valueStrA = "20";
    valueStrB =  "0";
    valueStrC = "-4";

    startColor = QColor(50, 50, 50);
    endColor = QColor(200, 200, 200);
    borderColor = QColor(50, 50, 50);
    bgColor = QColor(100, 100, 100);
    usedColor = QColor(100, 184, 255);
    textColor = QColor(250, 250, 250);

    percentStyle = PercentStyle_Rect;

    offset = 0;
    timer = new QTimer(this);
    timer->setInterval(200);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));

    setFont(QFont("Arial", 8));
}

KernelWaveWidget::~KernelWaveWidget()
{
    if (timer->isActive()) {
        timer->stop();
    }
}


int KernelWaveWidget::getMinValue() const
{
    return this->minValue;
}

int KernelWaveWidget::getMaxValue() const
{
    return this->maxValue;
}

int KernelWaveWidget::getValue() const
{
    return this->value;
}

double KernelWaveWidget::getBorderWidth() const
{
    return this->borderWidth;
}

double KernelWaveWidget::getWaterHeight() const
{
    return this->waterHeight;
}

double KernelWaveWidget::getWaterDensity() const
{
    return this->waterDensity;
}

bool KernelWaveWidget::getShowValue() const
{
    return this->showValue;
}

bool KernelWaveWidget::getShowPercent() const
{
    return this->showPercent;
}

QColor KernelWaveWidget::getBorderColor() const
{
    return this->borderColor;
}

QColor KernelWaveWidget::getBgColor() const
{
    return this->bgColor;
}

QColor KernelWaveWidget::getUsedColor() const
{
    return this->usedColor;
}

QColor KernelWaveWidget::getTextColor() const
{
    return this->textColor;
}

KernelWaveWidget::PercentStyle KernelWaveWidget::getPercentStyle() const
{
    return this->percentStyle;
}

QSize KernelWaveWidget::sizeHint() const
{
    return QSize(200, 200);
}

QSize KernelWaveWidget::minimumSizeHint() const
{
    return QSize(10, 10);
}

void KernelWaveWidget::setStartColor(QColor color)
{
    startColor = color;
}

void KernelWaveWidget::setEndColor(QColor color)
{
    endColor = color;
}

void KernelWaveWidget::setRange(int minValue, int maxValue)
{
    //如果最小值大于或者等于最大值则不设置
    if (minValue >= maxValue) {
        return;
    }

    this->minValue = minValue;
    this->maxValue = maxValue;

    //如果目标值不在范围值内,则重新设置目标值
    if (value < minValue || value > maxValue) {
        setValue(value);
    }

    update();
}

void KernelWaveWidget::setMinValue(int minValue)
{
    setRange(minValue, maxValue);
}

void KernelWaveWidget::setMaxValue(int maxValue)
{
    setRange(minValue, maxValue);
}

void KernelWaveWidget::setValue(int value)
{
    //值小于最小值或者值大于最大值或者值和当前值一致则无需处理
    if (value < minValue || value > maxValue || value == this->value) {
        return;
    }

    this->value = value;
    update();
    emit valueChanged(value);

    //如果当前值为最小值或者最大值则停止浪,不要再浪了 ^_^
    if (this->value == minValue || this->value == maxValue) {
        if (timer->isActive()) {
            timer->stop();
        }
    } else {
        if (!timer->isActive()) {
            timer->start();
        }
    }
}

void KernelWaveWidget::setBorderWidth(double borderWidth)
{
    if (this->borderWidth != borderWidth) {
        this->borderWidth = borderWidth;
        update();
    }
}

void KernelWaveWidget::setWaterHeight(double waterHeight)
{
    if (waterHeight > 0.1) {
        return;
    }

    if (this->waterHeight != waterHeight) {
        this->waterHeight = waterHeight;
        update();
    }
}

void KernelWaveWidget::setWaterDensity(double waterDensity)
{
    if (this->waterDensity != waterDensity) {
        this->waterDensity = waterDensity;
        update();
    }
}

void KernelWaveWidget::setShowValue(bool showValue)
{
    if (this->showValue != showValue) {
        this->showValue = showValue;
        update();
    }
}

void KernelWaveWidget::setShowPercent(bool showPercent)
{
    if (this->showPercent != showPercent) {
        this->showPercent = showPercent;
        update();
    }
}

void KernelWaveWidget::setBgColor(const QColor &bgColor)
{
    if (this->bgColor != bgColor) {
        this->bgColor = bgColor;
        update();
    }
}

void KernelWaveWidget::setBorderColor(const QColor &borderColor)
{
    if (this->borderColor != borderColor) {
        this->borderColor = borderColor;
        update();
    }
}

void KernelWaveWidget::setUsedColor(const QColor &usedColor)
{
    if (this->usedColor != usedColor) {
        this->usedColor = usedColor;
        update();
    }
}

void KernelWaveWidget::setTextColor(const QColor &textColor)
{
    if (this->textColor != textColor) {
        this->textColor = textColor;
        update();
    }
}

void KernelWaveWidget::setPercentStyle(KernelWaveWidget::PercentStyle percentStyle)
{
    if (this->percentStyle != percentStyle) {
        this->percentStyle = percentStyle;
        update();
    }
}

void KernelWaveWidget::setTitleStr(QString p_str)
{
    if (this->titleStr != p_str) {
        this->titleStr = p_str;
        update();
    }
}

void KernelWaveWidget::setUnitStr(QString p_str)
{
    if (this->unitStr != p_str) {
        this->unitStr = p_str;
        update();
    }
}

void KernelWaveWidget::setValueStrA(QString p_str)
{
    if (this->valueStrA != p_str) {
        this->valueStrA = p_str;
        update();
    }
}

void KernelWaveWidget::setValueStrB(QString p_str)
{
    if (this->valueStrB != p_str) {
        this->valueStrB = p_str;
        update();
    }
}

void KernelWaveWidget::setValueStrC(QString p_str)
{
    if (this->valueStrC != p_str) {
        this->valueStrC = p_str;
        update();
    }
}

猜你喜欢

转载自blog.csdn.net/cqltbe131421/article/details/89082355