summaryrefslogtreecommitdiffstats
path: root/examples/dclock/dclock.cpp
blob: be7cc8539e320dd35f690fa6e7cd5d2a1875ad90 (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
/****************************************************************************
**
** Copyright (C) 1992-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "dclock.h"
#include <qdatetime.h>


//
// Constructs a DigitalClock widget with a parent and a name.
//

DigitalClock::DigitalClock( TQWidget *parent, const char *name )
    : TQLCDNumber( parent, name )
{
    showingColon = FALSE;
    setFrameStyle( TQFrame::Panel | TQFrame::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.
//

void DigitalClock::timerEvent( TQTimerEvent *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.
//

void DigitalClock::mousePressEvent( TQMouseEvent *e )
{
    if ( e->button() == TQMouseEvent::LeftButton )		// left button pressed
	showDate();
}


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

void DigitalClock::showDate()
{
    if ( showDateTimer != -1 )			// already showing date
	return;
    TQDate date = TQDate::currentDate();
    TQString s;
    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.
//

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

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

void DigitalClock::showTime()
{
    showingColon = !showingColon;		// toggle/blink colon
    TQString s = TQTime::currentTime().toString().left(5);
    if ( !showingColon )
	s[2] = ' ';
    if ( s[0] == '0' )
	s[0] = ' ';
    display( s );				// set LCD number/text
}