| 1 | /**************************************************************************** | 
|---|
| 2 | ** $Id: dclock.cpp 2 2005-11-16 15:49:26Z dmik $ | 
|---|
| 3 | ** | 
|---|
| 4 | ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved. | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of an example program for Qt.  This example | 
|---|
| 7 | ** program may be used, distributed and modified without limitation. | 
|---|
| 8 | ** | 
|---|
| 9 | *****************************************************************************/ | 
|---|
| 10 |  | 
|---|
| 11 | #include "dclock.h" | 
|---|
| 12 | #include <qdatetime.h> | 
|---|
| 13 |  | 
|---|
| 14 |  | 
|---|
| 15 | // | 
|---|
| 16 | // Constructs a DigitalClock widget with a parent and a name. | 
|---|
| 17 | // | 
|---|
| 18 |  | 
|---|
| 19 | DigitalClock::DigitalClock( QWidget *parent, const char *name ) | 
|---|
| 20 | : QLCDNumber( parent, name ) | 
|---|
| 21 | { | 
|---|
| 22 | showingColon = FALSE; | 
|---|
| 23 | setFrameStyle( QFrame::Panel | QFrame::Raised ); | 
|---|
| 24 | setLineWidth( 2 );                          // set frame line width | 
|---|
| 25 | showTime();                                 // display the current time | 
|---|
| 26 | normalTimer = startTimer( 500 );            // 1/2 second timer events | 
|---|
| 27 | showDateTimer = -1;                         // not showing date | 
|---|
| 28 | } | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 | // | 
|---|
| 32 | // Handles timer events for the digital clock widget. | 
|---|
| 33 | // There are two different timers; one timer for updating the clock | 
|---|
| 34 | // and another one for switching back from date mode to time mode. | 
|---|
| 35 | // | 
|---|
| 36 |  | 
|---|
| 37 | void DigitalClock::timerEvent( QTimerEvent *e ) | 
|---|
| 38 | { | 
|---|
| 39 | if ( e->timerId() == showDateTimer )        // stop showing date | 
|---|
| 40 | stopDate(); | 
|---|
| 41 | else {                                      // normal timer | 
|---|
| 42 | if ( showDateTimer == -1 )              // not showing date | 
|---|
| 43 | showTime(); | 
|---|
| 44 | } | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | // | 
|---|
| 48 | // Enters date mode when the left mouse button is pressed. | 
|---|
| 49 | // | 
|---|
| 50 |  | 
|---|
| 51 | void DigitalClock::mousePressEvent( QMouseEvent *e ) | 
|---|
| 52 | { | 
|---|
| 53 | if ( e->button() == QMouseEvent::LeftButton )               // left button pressed | 
|---|
| 54 | showDate(); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 |  | 
|---|
| 58 | // | 
|---|
| 59 | // Shows the current date in the internal lcd widget. | 
|---|
| 60 | // Fires a timer to stop showing the date. | 
|---|
| 61 | // | 
|---|
| 62 |  | 
|---|
| 63 | void DigitalClock::showDate() | 
|---|
| 64 | { | 
|---|
| 65 | if ( showDateTimer != -1 )                  // already showing date | 
|---|
| 66 | return; | 
|---|
| 67 | QDate date = QDate::currentDate(); | 
|---|
| 68 | QString s; | 
|---|
| 69 | s.sprintf( "%2d %2d", date.month(), date.day() ); | 
|---|
| 70 | display( s );                               // sets the LCD number/text | 
|---|
| 71 | showDateTimer = startTimer( 2000 );         // keep this state for 2 secs | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | // | 
|---|
| 75 | // Stops showing the date. | 
|---|
| 76 | // | 
|---|
| 77 |  | 
|---|
| 78 | void DigitalClock::stopDate() | 
|---|
| 79 | { | 
|---|
| 80 | killTimer( showDateTimer ); | 
|---|
| 81 | showDateTimer = -1; | 
|---|
| 82 | showTime(); | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | // | 
|---|
| 86 | // Shows the current time in the internal lcd widget. | 
|---|
| 87 | // | 
|---|
| 88 |  | 
|---|
| 89 | void DigitalClock::showTime() | 
|---|
| 90 | { | 
|---|
| 91 | showingColon = !showingColon;               // toggle/blink colon | 
|---|
| 92 | QString s = QTime::currentTime().toString().left(5); | 
|---|
| 93 | if ( !showingColon ) | 
|---|
| 94 | s[2] = ' '; | 
|---|
| 95 | if ( s[0] == '0' ) | 
|---|
| 96 | s[0] = ' '; | 
|---|
| 97 | display( s );                               // set LCD number/text | 
|---|
| 98 | } | 
|---|