summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/examples/dclock/DigitalClock.java
blob: 6bc1d87f9b6189bdaaf4513ee544f83001861ed6 (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
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
/***************************************************************************
* $Id$
**
* Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
* This file is part of an example program for Qt.  This example
* program may be used, distributed and modified without limitation.
**
****************************************************************************/

import org.kde.qt.*;
import java.util.Date;
import java.text.*;

class DigitalClock extends QLCDNumber		// digital clock widget
{

private boolean	showingColon;
private int		normalTimer;
private int		showDateTimer;




DigitalClock( )
{
	this(null, null);
}

//
// Constructs a DigitalClock widget with a parent and a name.
//
DigitalClock( QWidget parent )
{
	this(parent, null);
}

DigitalClock( QWidget parent, String name )
{
    super( parent, name );
    showingColon = false;
    setFrameStyle( QFrame.Panel | QFrame.Raised );
    setLineWidth( 2 );				// set frame line width
    showTime();					// display the current time
    normalTimer = startTimer( 500 );		// 1/2 second timer events
    showDateTimer = -1;				// not showing date
}


//
// Handles timer events for 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.
//

protected void timerEvent( QTimerEvent e )
{
    if ( e.timerId() == showDateTimer )	// stop showing date
	stopDate();
    else {					// normal timer
	if ( showDateTimer == -1 )		// not showing date
	    showTime();
    }
}

//
// Enters date mode when the left mouse button is pressed.
//

protected void mousePressEvent( QMouseEvent e )
{
	if ( e.button() == QMouseEvent.LeftButton )		// left button pressed
	showDate();
}


//
// Shows the current date in the internal lcd widget.
// Fires a timer to stop showing the date.
//

void showDate()
{
	if ( showDateTimer != -1 )			// already showing date
	return;
	// Java always prints at least two digits for the year
	DateFormat dateFormat = new SimpleDateFormat( "yy d" );
    String s = dateFormat.format(new Date());
    display( s );				// sets the LCD number/text
    showDateTimer = startTimer( 2000 );		// keep this state for 2 secs
}

//
// Stops showing the date.
//

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

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

void showTime()
{
    showingColon = !showingColon;		// toggle/blink colon
	DateFormat timeFormat = new SimpleDateFormat( (showingColon ? "HH:mm": "HH mm") );
    String s = timeFormat.format(new Date());
    display( s );				// set LCD number/text
}
}