clock.cpp 3.56 KB
#include "clock.h"

#include <QPainter>
#include <QDateTime>
#include <QTime>
#include <QTimer>
#include <QtDebug>

Clock::Clock(QWidget *parent) : QWidget(parent)
{
    ticking = true;

    background.load(":/images/images/main/002_time_bg_number.png");

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(checkUpdate()));
    timer->start(33);
}

void Clock::checkUpdate()
{
    if (ticking)
    {
        QTime cur = QDateTime::currentDateTime().time();
        if (cur.second() != last.second())
        {
            last = cur;
            update();
        }
    }
    else
        update();
}

void Clock::paintEvent(QPaintEvent */*event*/)
{
    QTime time = QDateTime::currentDateTime().time();

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.drawImage(0, 0, background);
    painter.translate(width() / 2, height() / 2);

    int side = qMin(width(), height());
    painter.scale(side / 356.0, side / 355.0);

    QPen hourHand(Qt::white, 11, Qt::SolidLine, Qt::RoundCap);
    QPen hourArm(Qt::white, 4);
    QPen hourLine(Qt::white, 5);

    QPen minHand(hourHand);
    QPen minArm(hourArm);
    QPen minLine(Qt::white, 2);

    QPen secHand(Qt::red, 3);

    QPen capPen(Qt::white, 4);
    QBrush capBrush(Qt::black, Qt::SolidPattern);

    QFont numberFont("Roboto");
    numberFont.setPixelSize(50);

    QFontMetrics numberFontMetrics(numberFont);

    QPen numberPen(Qt::white);
    numberPen.setWidth(2);
/*
    // Lines
    painter.setPen(minLine);
    for (int j = 0; j < 60; ++j) {
        if ((j % 5) != 0)
//            painter.drawLine(92, 0, 96, 0);
            painter.drawLine(165, 0, 178, 0);
        painter.rotate(6.0);
    }

    painter.setPen(hourLine);
    for (int i = 0; i < 12; ++i) {
//        painter.drawLine(88, 0, 96, 0);
//        painter.drawLine(92, 0, 96, 0);
        painter.drawLine(165, 0, 178, 0);
        painter.rotate(30.0);
    }

    // Numbers
    painter.setPen(numberPen);
    painter.setFont(numberFont);

    QTransform transform;
//    QPoint targetPoint(0, -75);
    QPoint targetPoint(0, -138);
    QPoint transformedTarget(targetPoint);

    QString strings[] = { "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11" };
    for (int i = 0; i < 12; i++) {
        QString string = strings[i];
        QRect boundingRect = numberFontMetrics.boundingRect(string);
        boundingRect.moveCenter(transformedTarget);
        painter.drawText(boundingRect, Qt::AlignCenter, string);

        transform.rotate(30.0);
        transformedTarget = transform.map(targetPoint);
    }
*/
    // Needles
    qreal second = time.second() + time.msec() / 1000.0;
    if (ticking)
        second = time.second();

    qreal minute = time.minute() + second / 60.0;
    qreal hour = time.hour() + minute / 60.0;

    // Sec
    painter.save();
    painter.rotate(6.0 * second);
    painter.setPen(secHand);
    painter.drawLine(0, 28, 0, -155);
//    painter.drawImage(-28, -8, secondImg);
    painter.restore();

    // Min
    painter.save();
    painter.rotate(6.0 * minute);
    painter.setPen(minArm);
    painter.drawLine(0, 0, 0, -30);
    painter.setPen(minHand);
    painter.drawLine(0, -27, 0, -125);
//    painter.drawImage(-8, -9, minuteImg);
    painter.restore();

    // Hour
    painter.save();
    painter.rotate(30.0 * hour);
    painter.setPen(hourArm);
    painter.drawLine(0, 0, 0, -30);
    painter.setPen(hourHand);
    painter.drawLine(0, -27, 0, -80);
    painter.restore();

    // Cap
    painter.setPen(capPen);
    painter.setBrush(capBrush);
    painter.drawEllipse(-8, -8, 16, 16);
}