preheattempgauge.cpp 1.61 KB
#include "preheattempgauge.h"

#include <QPainter>
#include <QtDebug>

PreheatTempGauge::PreheatTempGauge(QWidget *parent) : QWidget(parent)
{
    border.load(":/images/images/auto/graphe_hit_01.png");
    indicator.load(":/images/images/auto/graphe_hit_02.png");
    body.load(":/images/images/auto/graphe_hit_03.png");

    value = 0;
    min = 0;
    max = 1;
}

void PreheatTempGauge::setValue(int value)
{
    this->value = qBound(min, value, max);

    update();
}

void PreheatTempGauge::setMinimum(int minimum)
{
    min = minimum;
    max = qMax(min, max);
    value = qBound(min, value, max);

    update();
}

void PreheatTempGauge::setMaximum(int maximum)
{
    max = maximum;
    min = qMin(min, max);
    value = qBound(min, value, max);

    update();
}

void PreheatTempGauge::paintEvent(QPaintEvent */*event*/)
{
    QPainter painter(this);
    painter.setBrush(Qt::NoBrush);
    painter.setPen(Qt::NoPen);

    qreal percentage = (qreal) (value - min) / qMax(max - min, 1);
    percentage = qBound((qreal) 0.0, percentage, (qreal) 1.0);

    QRect targetRect(
                (border.size().width() - body.size().width()) / 2 + indicator.size().width() / 2,
                (border.size().height() - body.size().height()) / 2 + indicator.size().height(),
                body.size().width() * percentage, body.height());

    QRect sourceRect(0, 0, body.size().width() * percentage, body.height());

    painter.drawPixmap(targetRect, body, sourceRect);
    painter.drawPixmap(indicator.size().width() / 2, indicator.size().height(), border);
    painter.drawPixmap(targetRect.right() - indicator.size().width() / 2, 0, indicator);
}