summaryrefslogtreecommitdiff
path: root/Widgets/ClockWidget.cpp
blob: 950c450504ce4862625ef4015739ede9a25799a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "ClockWidget.h"
#include "Painter.h"
#include <time.h>

ClockWidget::ClockWidget(Widget* parent)
    : Widget(parent)
{
    setWindowRelativeRect({ 0, 0, 100, 40 });
    startTimer(250);
}

ClockWidget::~ClockWidget()
{
}

void ClockWidget::onPaint(PaintEvent&)
{
    auto now = time(nullptr);
    auto& tm = *localtime(&now);
    
    char timeBuf[128];
    sprintf(timeBuf, "%02u:%02u:%02u ", tm.tm_hour, tm.tm_min, tm.tm_sec);
    
    Painter painter(*this);
    painter.fillRect(rect(), Color::MidGray);
    painter.drawText(rect(), timeBuf, Painter::TextAlignment::Center, Color::Black);
}

void ClockWidget::onTimer(TimerEvent&)
{
    auto now = time(nullptr);
    if (now == m_lastSeenTimestamp)
        return;
    m_lastSeenTimestamp = now;
    update();
}