multicooktimebar.cpp 1.45 KB
#include "multicooktimebar.h"

#include <QPainter>
#include <QTime>
#include <QDebug>

MultiCookTimeBar::MultiCookTimeBar(QWidget *parent) : QWidget(parent)
{
    format = "hh:mm";
    timePosition = 0;

    checkTimer.start(100);
    connect(&checkTimer, SIGNAL(timeout()), SLOT(check()));
}

void MultiCookTimeBar::showNow()
{
    timePosition = 0;
    update();
}

void MultiCookTimeBar::showPrev()
{
    timePosition--;
    update();
}

void MultiCookTimeBar::showNext()
{
    timePosition++;
    update();
}

void MultiCookTimeBar::paintEvent(QPaintEvent */*event*/)
{
    int w = width();
    int h = height();
    int y = h / 2;

    QPoint c1(w * 2 / 10, y);
    QPoint c2(w * 5 / 10, y);
    QPoint c3(w * 8 / 10, y);

    QRect rect(0, 0, w, h);

    QTime t = QTime::currentTime();

    QString s1 = t.addSecs(200 * (timePosition + 0)).toString(format);
    QString s2 = t.addSecs(200 * (timePosition + 3)).toString(format);
    QString s3 = t.addSecs(200 * (timePosition + 6)).toString(format);

    QPainter painter(this);
    painter.setPen(Qt::white);

    rect.moveCenter(c1);
    painter.drawText(rect, Qt::AlignCenter, s1);

    rect.moveCenter(c2);
    painter.drawText(rect, Qt::AlignCenter, s2);

    rect.moveCenter(c3);
    painter.drawText(rect, Qt::AlignCenter, s3);
}

void MultiCookTimeBar::check()
{
    QString s = QTime::currentTime().toString(format);
    if (lastDraw.isNull() || s != lastDraw)
    {
        update();
        lastDraw = s;
    }
}