1 /***********************************************************************
2 *
3 * Copyright (C) 2009, 2014 Graeme Gott <graeme@gottcode.org>
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 "clock.h"
21
22 #include <QBrush>
23 #include <QCursor>
24 #include <QFont>
25 #include <QPen>
26 #include <QSettings>
27 #include <QTimeLine>
28
Clock(QWidget * parent)29 Clock::Clock(QWidget* parent)
30 : QLabel(parent), m_clock(0, 0, 0), m_paused(true), m_stopped(true) {
31 QFont f = font();
32 f.setPixelSize(20);
33 setFont(f);
34 setCursor(Qt::PointingHandCursor);
35
36 QPalette p = palette();
37 p.setColor(foregroundRole(), Qt::white);
38 setPalette(p);
39
40 m_clock_timer = new QTimeLine(1000, this);
41 connect(m_clock_timer, &QTimeLine::finished, this, &Clock::tick);
42 }
43
44 //-----------------------------------------------------------------------------
45
start()46 void Clock::start() {
47 m_paused = false;
48 m_stopped = false;
49 m_clock_timer->stop();
50 m_clock = QTime(0, 0, 0);
51 m_clock = m_clock.addMSecs(QSettings().value("Current/Time").toInt());
52 updateText();
53 m_clock_timer->start();
54 }
55
56 //-----------------------------------------------------------------------------
57
stop()58 void Clock::stop() {
59 m_stopped = true;
60 m_clock_timer->stop();
61 QSettings().remove("Current/Time");
62 updateText();
63 }
64
65 //-----------------------------------------------------------------------------
66
setLoading()67 void Clock::setLoading() {
68 m_stopped = true;
69 m_clock_timer->stop();
70 setText(tr("Loading"));
71 }
72
73 //-----------------------------------------------------------------------------
74
setPaused(bool paused)75 void Clock::setPaused(bool paused) {
76 m_paused = paused;
77 if (m_clock_timer->state() != QTimeLine::NotRunning) {
78 m_clock_timer->setPaused(paused);
79 }
80 if (!m_stopped) {
81 updateText();
82 }
83 }
84
85 //-----------------------------------------------------------------------------
86
mousePressEvent(QMouseEvent * event)87 void Clock::mousePressEvent(QMouseEvent* event) {
88 QLabel::mousePressEvent(event);
89 emit togglePaused();
90 }
91
92 //-----------------------------------------------------------------------------
93
tick()94 void Clock::tick() {
95 updateTime();
96 if (!m_stopped) {
97 m_clock_timer->start();
98 }
99 }
100
101 //-----------------------------------------------------------------------------
102
updateTime()103 void Clock::updateTime() {
104 m_clock = m_clock.addMSecs(m_clock_timer->currentTime());
105 QSettings().setValue("Current/Time", QTime(0, 0, 0).msecsTo(m_clock));
106 updateText();
107 }
108
109 //-----------------------------------------------------------------------------
110
updateText()111 void Clock::updateText() {
112 setText(!m_paused ? m_clock.toString("hh:mm:ss") : tr("Paused"));
113 }
114