summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/examples/aclock
diff options
context:
space:
mode:
authortoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
committertoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
commit90825e2392b2d70e43c7a25b8a3752299a933894 (patch)
treee33aa27f02b74604afbfd0ea4f1cfca8833d882a /qtjava/javalib/examples/aclock
downloadtdebindings-90825e2392b2d70e43c7a25b8a3752299a933894.tar.gz
tdebindings-90825e2392b2d70e43c7a25b8a3752299a933894.zip
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdebindings@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'qtjava/javalib/examples/aclock')
-rw-r--r--qtjava/javalib/examples/aclock/AnalogClock.java158
1 files changed, 158 insertions, 0 deletions
diff --git a/qtjava/javalib/examples/aclock/AnalogClock.java b/qtjava/javalib/examples/aclock/AnalogClock.java
new file mode 100644
index 00000000..2d97cb67
--- /dev/null
+++ b/qtjava/javalib/examples/aclock/AnalogClock.java
@@ -0,0 +1,158 @@
+/****************************************************************************
+** $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;
+
+class AnalogClock extends QWidget // analog clock widget
+{
+
+private Date time;
+
+//
+// Constructs an analog clock widget that uses an internal QTimer.
+//
+
+public AnalogClock( QWidget parent )
+{
+ this(parent, null);
+}
+
+public AnalogClock( QWidget parent, String name )
+{
+ super( parent, name );
+ time = new Date(); // get current time
+ QTimer internalTimer = new QTimer( this ); // create internal timer
+ connect( internalTimer, SIGNAL("timeout()"), SLOT("timeout()") );
+ internalTimer.start( 5000 ); // emit signal every 5 seconds
+}
+
+
+public void setTime( Date t )
+{
+ time = t;
+ timeout();
+}
+
+//
+// The QTimer.timeout() signal is received by this slot.
+//
+
+public void timeout()
+{
+ Date old_time = new Date(); // get the current time
+ old_time.setTime( time.getTime() - 5000 );
+ time.setTime( time.getTime() + 5000 );
+ if ( old_time.getMinutes() != time.getMinutes() ) { // minute has changed
+ if (autoMask())
+ updateMask();
+ else
+ update();
+ }
+}
+
+
+public void paintEvent( QPaintEvent event )
+{
+ if ( autoMask() )
+ return;
+ QPainter paint = new QPainter( this );
+ paint.setBrush( colorGroup().foreground() );
+ drawClock( paint );
+}
+
+// If the clock is transparent, we use updateMask()
+// instead of paintEvent()
+
+public void updateMask() // paint clock mask
+{
+ QBitmap bm = new QBitmap( size() );
+ bm.fill( color0() ); //transparent
+
+ QPainter paint = new QPainter();
+ paint.begin( bm, this );
+ paint.setBrush( color1() ); // use non-transparent color
+ paint.setPen( color1() );
+
+ drawClock( paint );
+
+ paint.end();
+ setMask( bm );
+}
+
+//
+// The clock is painted using a 1000x1000 square coordinate system, in
+// the a centered square, as big as possible. The painter's pen and
+// brush colors are used.
+//
+public void drawClock( QPainter paint )
+{
+ paint.save();
+
+ paint.setWindow( -500,-500, 1000,1000 );
+
+ QRect v = paint.viewport();
+ int d = v.width() < v.height() ? v.width() : v.height();
+ paint.setViewport( v.left() + (v.width()-d)/2,
+ v.top() + (v.height()-d)/2, d, d );
+
+ // time = QTime.currentTime();
+ QPointArray pts = new QPointArray();
+
+ paint.save();
+ paint.rotate( 30*(time.getHours()%12-3) + time.getMinutes()/2 );
+ pts.setPoints( 4, new short[] { -20,0, 0,-20, 300,0, 0,20 } );
+ paint.drawConvexPolygon( pts );
+ paint.restore();
+
+ paint.save();
+ paint.rotate( (time.getMinutes()-15)*6 );
+ pts.setPoints( 4, new short[] { -10,0, 0,-10, 400,0, 0,10 } );
+ paint.drawConvexPolygon( pts );
+ paint.restore();
+
+ for ( int i=0; i<12; i++ ) {
+ paint.drawLine( 440,0, 460,0 );
+ paint.rotate( 30 );
+ }
+
+ paint.restore();
+}
+
+
+public void setAutoMask(boolean b)
+{
+ if (b)
+ setBackgroundMode( PaletteForeground );
+ else
+ setBackgroundMode( PaletteBackground );
+ super.setAutoMask(b);
+}
+
+public static void main( String[] args )
+{
+ QApplication a = new QApplication( args );
+ AnalogClock clock = new AnalogClock(null, null);
+ if ( args.length == 1 && args[0].equals("-transparent") )
+ clock.setAutoMask( true );
+ clock.resize( 100, 100 );
+ a.setMainWidget( clock );
+ clock.setCaption("Qt Example - Analog Clock");
+ clock.show();
+ int result = a.exec();
+ return;
+}
+
+static {
+ qtjava.initialize();
+}
+
+}
+