-
Notifications
You must be signed in to change notification settings - Fork 11
/
clockapplet.cpp
58 lines (48 loc) · 1.23 KB
/
clockapplet.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "clockapplet.h"
#include <QtCore/QTimer>
#include <QtCore/QDateTime>
#include <QtGui/QGraphicsScene>
#include "textgraphicsitem.h"
#include "panelwindow.h"
ClockApplet::ClockApplet(PanelWindow* panelWindow)
: Applet(panelWindow)
{
m_timer = new QTimer();
m_timer->setSingleShot(true);
connect(m_timer, SIGNAL(timeout()), this, SLOT(updateContent()));
m_textItem = new TextGraphicsItem(this);
m_textItem->setColor(Qt::white);
m_textItem->setFont(m_panelWindow->font());
}
ClockApplet::~ClockApplet()
{
delete m_textItem;
delete m_timer;
}
bool ClockApplet::init()
{
updateContent();
setInteractive(true);
return true;
}
void ClockApplet::layoutChanged()
{
m_textItem->setPos((m_size.width() - m_textItem->boundingRect().size().width())/2.0, m_panelWindow->textBaseLine());
}
void ClockApplet::updateContent()
{
QDateTime dateTimeNow = QDateTime::currentDateTime();
m_text = dateTimeNow.toString();
m_textItem->setText(m_text);
update();
scheduleUpdate();
}
QSize ClockApplet::desiredSize()
{
return QSize(m_textItem->boundingRect().width() + 16, m_textItem->boundingRect().height() + 16);
}
void ClockApplet::scheduleUpdate()
{
m_timer->setInterval(1000 - QDateTime::currentDateTime().time().msec());
m_timer->start();
}