summaryrefslogtreecommitdiff
path: root/Widgets/ClockWidget.cpp
blob: a4778e2ca2a598ce65a0ef877f9e9e4605383490 (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)
{
    setRect({ 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(127, 127, 127));
    painter.drawText(rect(), timeBuf, Painter::TextAlignment::Center, Color(0,0,0));
}

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