1 /***********************************************************************
2  *
3  * Copyright (C) 2016 wereturtle
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  ***********************************************************************/
19 
20 #include <QTime>
21 #include <QTimer>
22 #include <QString>
23 
24 #include "TimeLabel.h"
25 
TimeLabel(QWidget * parent)26 TimeLabel::TimeLabel(QWidget* parent) :
27     QLabel(parent)
28 {
29     timer = new QTimer(this);
30     connect(timer, SIGNAL(timeout()), this, SLOT(updateTimeOfDay()));
31     this->updateTimeOfDay();
32 }
33 
~TimeLabel()34 TimeLabel::~TimeLabel()
35 {
36 
37 }
38 
updateTimeOfDay()39 void TimeLabel::updateTimeOfDay()
40 {
41     QTime currentTime = QTime::currentTime();
42     this->setText(currentTime.toString(Qt::DefaultLocaleShortDate));
43 
44     QTime nextTime = currentTime.addSecs(60);
45     nextTime.setHMS(nextTime.hour(), nextTime.minute(), 0);
46 
47     // Set the timer as a single shot rather than a recurring 1000 ms
48     // interval, since we don't want the time to slowly drift away
49     // from being accurate due to small timer inaccuracies.
50     //
51     timer->setSingleShot(true);
52 
53     int interval = currentTime.msecsTo(nextTime);
54 
55     // Ensure interval is never negative.
56     if (interval <= 0)
57     {
58         interval = 1000;
59     }
60 
61     timer->start(interval);
62 }
63 
64 
65