summaryrefslogtreecommitdiffstats
path: root/examples/dclock
diff options
context:
space:
mode:
authorTimothy Pearson <[email protected]>2011-07-10 15:24:15 -0500
committerTimothy Pearson <[email protected]>2011-07-10 15:24:15 -0500
commitbd0f3345a938b35ce6a12f6150373b0955b8dd12 (patch)
tree7a520322212d48ebcb9fbe1087e7fca28b76185c /examples/dclock
downloadqt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.tar.gz
qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.zip
Add Qt3 development HEAD version
Diffstat (limited to 'examples/dclock')
-rw-r--r--examples/dclock/README5
-rw-r--r--examples/dclock/dclock.cpp97
-rw-r--r--examples/dclock/dclock.doc29
-rw-r--r--examples/dclock/dclock.h39
-rw-r--r--examples/dclock/dclock.pro11
-rw-r--r--examples/dclock/main.cpp23
6 files changed, 204 insertions, 0 deletions
diff --git a/examples/dclock/README b/examples/dclock/README
new file mode 100644
index 0000000..dfa5840
--- /dev/null
+++ b/examples/dclock/README
@@ -0,0 +1,5 @@
+The dclock program displays a digital LCD clock and can switch
+between time and date.
+
+See also the aclock sample that displays an analog clock.
+
diff --git a/examples/dclock/dclock.cpp b/examples/dclock/dclock.cpp
new file mode 100644
index 0000000..18d5be0
--- /dev/null
+++ b/examples/dclock/dclock.cpp
@@ -0,0 +1,97 @@
+/****************************************************************************
+**
+** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
+**
+** This file is part of an example program for Qt. 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( QWidget *parent, const char *name )
+ : QLCDNumber( 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.
+//
+
+void DigitalClock::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.
+//
+
+void DigitalClock::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 DigitalClock::showDate()
+{
+ if ( showDateTimer != -1 ) // already showing date
+ return;
+ QDate date = QDate::currentDate();
+ QString 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
+ QString s = QTime::currentTime().toString().left(5);
+ if ( !showingColon )
+ s[2] = ' ';
+ if ( s[0] == '0' )
+ s[0] = ' ';
+ display( s ); // set LCD number/text
+}
diff --git a/examples/dclock/dclock.doc b/examples/dclock/dclock.doc
new file mode 100644
index 0000000..b341b66
--- /dev/null
+++ b/examples/dclock/dclock.doc
@@ -0,0 +1,29 @@
+/*
+*/
+/*! \page dclock-example.html
+
+ \ingroup examples
+ \title Digital Clock
+
+ This example displays a digital LCD clock that can switch between time
+ and date.
+
+ <hr>
+
+ Header file:
+
+ \include dclock/dclock.h
+
+ <hr>
+
+ Implementation:
+
+ \include dclock/dclock.cpp
+
+ <hr>
+
+ Main:
+
+ \include dclock/main.cpp
+*/
+
diff --git a/examples/dclock/dclock.h b/examples/dclock/dclock.h
new file mode 100644
index 0000000..562bdd2
--- /dev/null
+++ b/examples/dclock/dclock.h
@@ -0,0 +1,39 @@
+/****************************************************************************
+**
+** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
+**
+** This file is part of an example program for Qt. This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#ifndef DCLOCK_H
+#define DCLOCK_H
+
+#include <qlcdnumber.h>
+
+
+class DigitalClock : public QLCDNumber // digital clock widget
+{
+ Q_OBJECT
+public:
+ DigitalClock( QWidget *parent=0, const char *name=0 );
+
+protected: // event handlers
+ void timerEvent( QTimerEvent * );
+ void mousePressEvent( QMouseEvent * );
+
+private slots: // internal slots
+ void stopDate();
+ void showTime();
+
+private: // internal data
+ void showDate();
+
+ bool showingColon;
+ int normalTimer;
+ int showDateTimer;
+};
+
+
+#endif // DCLOCK_H
diff --git a/examples/dclock/dclock.pro b/examples/dclock/dclock.pro
new file mode 100644
index 0000000..f244d7a
--- /dev/null
+++ b/examples/dclock/dclock.pro
@@ -0,0 +1,11 @@
+TEMPLATE = app
+TARGET = dclock
+
+CONFIG += qt warn_on release
+DEPENDPATH = ../../include
+
+REQUIRES = large-config
+
+HEADERS = dclock.h
+SOURCES = dclock.cpp \
+ main.cpp
diff --git a/examples/dclock/main.cpp b/examples/dclock/main.cpp
new file mode 100644
index 0000000..fd7b916
--- /dev/null
+++ b/examples/dclock/main.cpp
@@ -0,0 +1,23 @@
+/****************************************************************************
+**
+** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
+**
+** This file is part of an example program for Qt. This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#include "dclock.h"
+#include <qapplication.h>
+
+
+int main( int argc, char **argv )
+{
+ QApplication a( argc, argv );
+ DigitalClock *clock = new DigitalClock;
+ clock->resize( 170, 80 );
+ a.setMainWidget( clock );
+ clock->setCaption("Qt Example - Digital Clock");
+ clock->show();
+ return a.exec();
+}