forked from ElvishArtisan/rivendell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwall_clock.cpp
165 lines (139 loc) · 3.63 KB
/
wall_clock.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// wall_clock.cpp
//
// A wall-clock widget with date.
//
// (C) Copyright 2002-2019 Fred Gleason <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#include <qpainter.h>
#include <rdconf.h>
#include "colors.h"
#include "wall_clock.h"
WallClock::WallClock(QWidget *parent)
: RDPushButton(parent)
{
time_offset=rda->station()->timeOffset();
previous_time=QTime::currentTime().addMSecs(time_offset);
time_mode=RDAirPlayConf::TwentyFourHour;
previous_time_mode = RDAirPlayConf::TwentyFourHour;
show_date=true;
check_sync=true;
flash_state=false;
//
// Generate Fonts
//
label_metrics=new QFontMetrics(subLabelFont());
connect(this,SIGNAL(clicked()),this,SLOT(clickedData()));
}
QSize WallClock::sizeHint() const
{
return QSize(200,60);
}
QSizePolicy WallClock::sizePolicy() const
{
return QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
}
void WallClock::setDateDisplay(bool state)
{
show_date=state;
}
void WallClock::setTimeMode(RDAirPlayConf::TimeMode mode)
{
if(mode==time_mode) {
return;
}
time_mode=mode;
emit timeModeChanged(time_mode);
}
void WallClock::setCheckSyncEnabled(bool state)
{
check_sync=state;
}
void WallClock::clickedData()
{
if(time_mode==RDAirPlayConf::TwelveHour) {
setTimeMode(RDAirPlayConf::TwentyFourHour);
}
else {
setTimeMode(RDAirPlayConf::TwelveHour);
}
}
void WallClock::tickClock()
{
static QString date;
QString accum;
QColor system_button_text_color = palette().active().buttonText();
static QPixmap *pix=new QPixmap(sizeHint().width()-2,sizeHint().height()-2);
static bool synced=true;
if(check_sync) {
if(RDTimeSynced()!=synced) {
synced=RDTimeSynced();
}
}
current_time=QTime::currentTime().addMSecs(time_offset);
current_date=QDate::currentDate();
if((current_time.second()==previous_time.second()) && (previous_time_mode == time_mode)) {
return;
}
previous_time_mode = time_mode;
previous_time=current_time;
//
// Clock Display
//
if(time_mode==RDAirPlayConf::TwelveHour) {
accum=current_time.toString("h:mm:ss ap");
}
else {
accum=current_time.toString("hh:mm:ss");
}
if(time_string==accum) {
return;
}
time_string=accum;
if(synced) {
flash_state=false;
}
else {
flash_state=!flash_state;
}
if(previous_date!=current_date) {
previous_date=current_date;
date=current_date.toString("dddd, MMMM d, yyyy");
}
QPainter p(pix);
if(flash_state) {
p.fillRect(0,0,width()-2,height()-2,BUTTON_TIME_SYNC_LOST_COLOR);
p.setPen(Qt::color1);
}
else {
p.fillRect(0,0,width()-2,height()-2,backgroundColor());
p.setPen(QColor(system_button_text_color));
}
p.setFont(subLabelFont());
p.drawText((size().width()-2-p.fontMetrics().width(date))/2,22,date);
p.setFont(bannerFont());
p.drawText((size().width()-2-p.fontMetrics().width(accum))/2,48,accum);
p.end();
setPixmap(*pix);
}
void WallClock::flashButton(bool state)
{
flash_state=state;
tickClock();
}
void WallClock::keyPressEvent(QKeyEvent *e)
{
e->ignore();
}