summaryrefslogtreecommitdiffstats
path: root/PerlQt/examples/dclock/DigitalClock.pm
blob: 461127284399da9388c6028e04414d75887aad38 (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
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
package DigitalClock;
use strict;
use Qt;
use Qt::isa qw(Qt::LCDNumber);
use Qt::slots
	stopDate => [],
	showTime => [];
use Qt::attributes qw(
	showingColon
	normalTimer
	showDateTimer
);

#
# Constructs a DigitalClock widget
#

sub NEW {
    shift->SUPER::NEW(@_);
    showingColon = 0;
    setFrameStyle(&Panel | &Raised);
    setLineWidth(2);
    showTime();
    normalTimer = startTimer(500);
    showDateTimer = -1;
}

#
# Handles timer events and the digital clock widget.
# There are two different timers; one timer for updating the clock
# and another one for switching back from date mode to time mode
#

sub timerEvent {
    my $e = shift;
    if($e->timerId == showDateTimer) {		# stop showing date
	stopDate();
    } elsif(showDateTimer == -1) {		# normal timer
	showTime();
    }
}

#
# Enters date mode when the left mouse button is pressed
#

sub mousePressEvent {
    my $e = shift;
    showDate() if $e->button == &LeftButton;
}

#
# Shows the durrent date in the internal lcd widget.
# Fires a timer to stop showing the date.
#

sub showDate {
    return if showDateTimer != -1;		# already showing date
    my $date = Qt::Date::currentDate();
    my $s = sprintf("%2d %2d", $date->month, $date->day);
    display($s);				# sets the LCD number/text
    showDateTimer = startTimer(2000);		# keep this state for 2 secs
}

#
# Stops showing the date.
#

sub stopDate {
    killTimer(showDateTimer);
    showDateTimer = -1;
    showTime();
}

#
# Shows the current time in the internal lcd widget.
#

sub showTime {
    showingColon = !showingColon;
    my $s = substr(Qt::Time::currentTime()->toString, 0, 5);
    $s =~ s/^0/ /;
    $s =~ s/:/ / unless showingColon;
    display($s);
}

1;