| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | 
|---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/aclock/aclock.doc:4 --> | 
|---|
| 3 | <html> | 
|---|
| 4 | <head> | 
|---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | 
|---|
| 6 | <title>Analog Clock</title> | 
|---|
| 7 | <style type="text/css"><!-- | 
|---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; } | 
|---|
| 9 | a:link { color: #004faf; text-decoration: none } | 
|---|
| 10 | a:visited { color: #672967; text-decoration: none } | 
|---|
| 11 | body { background: #ffffff; color: black; } | 
|---|
| 12 | --></style> | 
|---|
| 13 | </head> | 
|---|
| 14 | <body> | 
|---|
| 15 |  | 
|---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%"> | 
|---|
| 17 | <tr bgcolor="#E5E5E5"> | 
|---|
| 18 | <td valign=center> | 
|---|
| 19 | <a href="index.html"> | 
|---|
| 20 | <font color="#004faf">Home</font></a> | 
|---|
| 21 | | <a href="classes.html"> | 
|---|
| 22 | <font color="#004faf">All Classes</font></a> | 
|---|
| 23 | | <a href="mainclasses.html"> | 
|---|
| 24 | <font color="#004faf">Main Classes</font></a> | 
|---|
| 25 | | <a href="annotated.html"> | 
|---|
| 26 | <font color="#004faf">Annotated</font></a> | 
|---|
| 27 | | <a href="groups.html"> | 
|---|
| 28 | <font color="#004faf">Grouped Classes</font></a> | 
|---|
| 29 | | <a href="functions.html"> | 
|---|
| 30 | <font color="#004faf">Functions</font></a> | 
|---|
| 31 | </td> | 
|---|
| 32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Analog Clock</h1> | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | <p> | 
|---|
| 36 | This example displays an analog clock widget. | 
|---|
| 37 | <p> <hr> | 
|---|
| 38 | <p> Header file: | 
|---|
| 39 | <p> <pre>/**************************************************************************** | 
|---|
| 40 | ** $Id: aclock-example.html 2051 2007-02-21 10:04:20Z chehrlic $ | 
|---|
| 41 | ** | 
|---|
| 42 | ** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved. | 
|---|
| 43 | ** | 
|---|
| 44 | ** This file is part of an example program for Qt.  This example | 
|---|
| 45 | ** program may be used, distributed and modified without limitation. | 
|---|
| 46 | ** | 
|---|
| 47 | *****************************************************************************/ | 
|---|
| 48 |  | 
|---|
| 49 | #ifndef ACLOCK_H | 
|---|
| 50 | #define ACLOCK_H | 
|---|
| 51 |  | 
|---|
| 52 | #include <<a href="qwidget-h.html">qwidget.h</a>> | 
|---|
| 53 | #include <<a href="qdatetime-h.html">qdatetime.h</a>> | 
|---|
| 54 |  | 
|---|
| 55 | class QTimer; | 
|---|
| 56 | class AnalogClock : public <a href="qwidget.html">QWidget</a>              // analog clock widget | 
|---|
| 57 | { | 
|---|
| 58 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a> | 
|---|
| 59 | public: | 
|---|
| 60 | AnalogClock( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 ); | 
|---|
| 61 | void setAutoMask(bool b); | 
|---|
| 62 |  | 
|---|
| 63 | protected: | 
|---|
| 64 | void updateMask(); | 
|---|
| 65 | void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> *); | 
|---|
| 66 | void mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> *); | 
|---|
| 67 | void mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> *); | 
|---|
| 68 | void drawClock( <a href="qpainter.html">QPainter</a>* ); | 
|---|
| 69 |  | 
|---|
| 70 | private slots: | 
|---|
| 71 | void timeout(); | 
|---|
| 72 |  | 
|---|
| 73 | public slots: | 
|---|
| 74 | void setTime( const <a href="qtime.html">QTime</a> & t ); | 
|---|
| 75 |  | 
|---|
| 76 | private: | 
|---|
| 77 | <a href="qpoint.html">QPoint</a> clickPos; | 
|---|
| 78 | <a href="qtime.html">QTime</a> time; | 
|---|
| 79 | <a href="qtimer.html">QTimer</a> *internalTimer; | 
|---|
| 80 | }; | 
|---|
| 81 |  | 
|---|
| 82 |  | 
|---|
| 83 | #endif // ACLOCK_H | 
|---|
| 84 | </pre> | 
|---|
| 85 |  | 
|---|
| 86 | <p> <hr> | 
|---|
| 87 | <p> Implementation: | 
|---|
| 88 | <p> <pre>/**************************************************************************** | 
|---|
| 89 | ** $Id: aclock-example.html 2051 2007-02-21 10:04:20Z chehrlic $ | 
|---|
| 90 | ** | 
|---|
| 91 | ** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved. | 
|---|
| 92 | ** | 
|---|
| 93 | ** This file is part of an example program for Qt.  This example | 
|---|
| 94 | ** program may be used, distributed and modified without limitation. | 
|---|
| 95 | ** | 
|---|
| 96 | *****************************************************************************/ | 
|---|
| 97 |  | 
|---|
| 98 | #include "aclock.h" | 
|---|
| 99 | #include <<a href="qtimer-h.html">qtimer.h</a>> | 
|---|
| 100 | #include <<a href="qpainter-h.html">qpainter.h</a>> | 
|---|
| 101 | #include <<a href="qbitmap-h.html">qbitmap.h</a>> | 
|---|
| 102 |  | 
|---|
| 103 | // | 
|---|
| 104 | // Constructs an analog clock widget that uses an internal QTimer. | 
|---|
| 105 | // | 
|---|
| 106 |  | 
|---|
| 107 | <a name="f384"></a>AnalogClock::AnalogClock( <a href="qwidget.html">QWidget</a> *parent, const char *name ) | 
|---|
| 108 | : <a href="qwidget.html">QWidget</a>( parent, name ) | 
|---|
| 109 | { | 
|---|
| 110 | <a name="x1201"></a>    time = QTime::<a href="qtime.html#currentTime">currentTime</a>();                // get current time | 
|---|
| 111 | internalTimer = new <a href="qtimer.html">QTimer</a>( this ); // create internal timer | 
|---|
| 112 | <a name="x1205"></a>    <a href="qobject.html#connect">connect</a>( internalTimer, SIGNAL(<a href="qtimer.html#timeout">timeout</a>()), SLOT(timeout()) ); | 
|---|
| 113 | <a name="x1204"></a>    internalTimer-><a href="qtimer.html#start">start</a>( 5000 );               // emit signal every 5 seconds | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | void AnalogClock::<a href="qwidget.html#mousePressEvent">mousePressEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e ) | 
|---|
| 117 | { | 
|---|
| 118 | if(<a href="qwidget.html#isTopLevel">isTopLevel</a>()) | 
|---|
| 119 | clickPos = e-><a href="qmouseevent.html#pos">pos</a>() + QPoint(<a href="qwidget.html#geometry">geometry</a>().topLeft() - frameGeometry().topLeft()); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | <a name="x1206"></a>void AnalogClock::<a href="qwidget.html#mouseMoveEvent">mouseMoveEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e ) | 
|---|
| 123 | { | 
|---|
| 124 | if(<a href="qwidget.html#isTopLevel">isTopLevel</a>()) | 
|---|
| 125 | <a name="x1181"></a>        <a href="qwidget.html#move">move</a>( e-><a href="qmouseevent.html#globalPos">globalPos</a>() - clickPos ); | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | // | 
|---|
| 129 | // The QTimer::timeout() signal is received by this slot. | 
|---|
| 130 | // | 
|---|
| 131 |  | 
|---|
| 132 | // | 
|---|
| 133 | // When we set an explicit time we don't want the timeout() slot to be | 
|---|
| 134 | // called anymore as this relies on currentTime() | 
|---|
| 135 | // | 
|---|
| 136 | void <a name="f385"></a>AnalogClock::setTime( const <a href="qtime.html">QTime</a> & t ) | 
|---|
| 137 | { | 
|---|
| 138 | time = t; | 
|---|
| 139 | <a href="qobject.html#disconnect">disconnect</a>( internalTimer, SIGNAL(<a href="qtimer.html#timeout">timeout</a>()), this, SLOT(timeout()) ); | 
|---|
| 140 | if (<a href="qwidget.html#autoMask">autoMask</a>()) | 
|---|
| 141 | <a href="qwidget.html#updateMask">updateMask</a>(); | 
|---|
| 142 | else | 
|---|
| 143 | <a href="qwidget.html#update">update</a>(); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 |  | 
|---|
| 147 | void <a name="f386"></a>AnalogClock::timeout() | 
|---|
| 148 | { | 
|---|
| 149 | <a href="qtime.html">QTime</a> old_time = time; | 
|---|
| 150 | time = QTime::<a href="qtime.html#currentTime">currentTime</a>(); | 
|---|
| 151 | <a name="x1203"></a>    if ( old_time.<a href="qtime.html#minute">minute</a>() != time.minute() | 
|---|
| 152 | <a name="x1202"></a>        || old_time.<a href="qtime.html#hour">hour</a>() != time.hour() ) {   // minute or hour has changed | 
|---|
| 153 | if (<a href="qwidget.html#autoMask">autoMask</a>()) | 
|---|
| 154 | <a href="qwidget.html#updateMask">updateMask</a>(); | 
|---|
| 155 | else | 
|---|
| 156 | <a href="qwidget.html#update">update</a>(); | 
|---|
| 157 | } | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 |  | 
|---|
| 161 | void AnalogClock::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> * ) | 
|---|
| 162 | { | 
|---|
| 163 | if ( <a href="qwidget.html#autoMask">autoMask</a>() ) | 
|---|
| 164 | return; | 
|---|
| 165 | <a href="qpainter.html">QPainter</a> paint( this ); | 
|---|
| 166 | <a name="x1190"></a>    paint.<a href="qpainter.html#setBrush">setBrush</a>( <a href="qwidget.html#colorGroup">colorGroup</a>().foreground() ); | 
|---|
| 167 | drawClock( &paint ); | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | // If the clock is transparent, we use updateMask() | 
|---|
| 171 | // instead of paintEvent() | 
|---|
| 172 |  | 
|---|
| 173 | void AnalogClock::updateMask()  // paint clock mask | 
|---|
| 174 | { | 
|---|
| 175 | <a href="qbitmap.html">QBitmap</a> bm( <a href="qwidget.html#size">size</a>() ); | 
|---|
| 176 | bm.<a href="qpixmap.html#fill">fill</a>( color0 );                  //transparent | 
|---|
| 177 |  | 
|---|
| 178 | <a href="qpainter.html">QPainter</a> paint; | 
|---|
| 179 | <a name="x1183"></a>    paint.<a href="qpainter.html#begin">begin</a>( &bm, this ); | 
|---|
| 180 | paint.<a href="qpainter.html#setBrush">setBrush</a>( color1 );           // use non-transparent color | 
|---|
| 181 | paint.<a href="qpainter.html#setPen">setPen</a>( color1 ); | 
|---|
| 182 |  | 
|---|
| 183 | drawClock( &paint ); | 
|---|
| 184 |  | 
|---|
| 185 | <a name="x1186"></a>    paint.<a href="qpainter.html#end">end</a>(); | 
|---|
| 186 | <a href="qwidget.html#setMask">setMask</a>( bm ); | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | // | 
|---|
| 190 | // The clock is painted using a 1000x1000 square coordinate system, in | 
|---|
| 191 | // the a centered square, as big as possible.  The painter's pen and | 
|---|
| 192 | // brush colors are used. | 
|---|
| 193 | // | 
|---|
| 194 | void <a name="f387"></a>AnalogClock::drawClock( <a href="qpainter.html">QPainter</a> *paint ) | 
|---|
| 195 | { | 
|---|
| 196 | <a name="x1189"></a>    paint-><a href="qpainter.html#save">save</a>(); | 
|---|
| 197 |  | 
|---|
| 198 | <a name="x1193"></a>    paint-><a href="qpainter.html#setWindow">setWindow</a>( -500,-500, 1000,1000 ); | 
|---|
| 199 |  | 
|---|
| 200 | <a name="x1194"></a>    <a href="qrect.html">QRect</a> v = paint-><a href="qpainter.html#viewport">viewport</a>(); | 
|---|
| 201 | int d = QMIN( v.<a href="qrect.html#width">width</a>(), v.<a href="qrect.html#height">height</a>() ); | 
|---|
| 202 | <a name="x1198"></a><a name="x1192"></a>    paint-><a href="qpainter.html#setViewport">setViewport</a>( v.<a href="qrect.html#left">left</a>() + (v.<a href="qrect.html#width">width</a>()-d)/2, | 
|---|
| 203 | <a name="x1199"></a>                        v.<a href="qrect.html#top">top</a>() + (v.<a href="qrect.html#height">height</a>()-d)/2, d, d ); | 
|---|
| 204 |  | 
|---|
| 205 | <a href="qpointarray.html">QPointArray</a> pts; | 
|---|
| 206 |  | 
|---|
| 207 | paint-><a href="qpainter.html#save">save</a>(); | 
|---|
| 208 | <a name="x1188"></a>    paint-><a href="qpainter.html#rotate">rotate</a>( 30*(time.hour()%12-3) + time.minute()/2 ); | 
|---|
| 209 | <a name="x1196"></a>    pts.setPoints( 4, -20,0,  0,-20, 300,0, 0,20 ); | 
|---|
| 210 | <a name="x1184"></a>    paint-><a href="qpainter.html#drawConvexPolygon">drawConvexPolygon</a>( pts ); | 
|---|
| 211 | <a name="x1187"></a>    paint-><a href="qpainter.html#restore">restore</a>(); | 
|---|
| 212 |  | 
|---|
| 213 | paint-><a href="qpainter.html#save">save</a>(); | 
|---|
| 214 | paint-><a href="qpainter.html#rotate">rotate</a>( (time.minute()-15)*6 ); | 
|---|
| 215 | pts.setPoints( 4, -10,0, 0,-10, 400,0, 0,10 ); | 
|---|
| 216 | paint-><a href="qpainter.html#drawConvexPolygon">drawConvexPolygon</a>( pts ); | 
|---|
| 217 | paint-><a href="qpainter.html#restore">restore</a>(); | 
|---|
| 218 |  | 
|---|
| 219 | for ( int i=0; i<12; i++ ) { | 
|---|
| 220 | <a name="x1185"></a>        paint-><a href="qpainter.html#drawLine">drawLine</a>( 440,0, 460,0 ); | 
|---|
| 221 | paint-><a href="qpainter.html#rotate">rotate</a>( 30 ); | 
|---|
| 222 | } | 
|---|
| 223 |  | 
|---|
| 224 | paint-><a href="qpainter.html#restore">restore</a>(); | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 |  | 
|---|
| 228 | <a name="x1209"></a>void AnalogClock::<a href="qwidget.html#setAutoMask">setAutoMask</a>(bool b) | 
|---|
| 229 | { | 
|---|
| 230 | if (b) | 
|---|
| 231 | <a href="qwidget.html#setBackgroundMode">setBackgroundMode</a>( PaletteForeground ); | 
|---|
| 232 | else | 
|---|
| 233 | <a href="qwidget.html#setBackgroundMode">setBackgroundMode</a>( PaletteBackground ); | 
|---|
| 234 | QWidget::<a href="qwidget.html#setAutoMask">setAutoMask</a>(b); | 
|---|
| 235 | } | 
|---|
| 236 | </pre> | 
|---|
| 237 |  | 
|---|
| 238 | <p> <hr> | 
|---|
| 239 | <p> Main: | 
|---|
| 240 | <p> <pre>/**************************************************************************** | 
|---|
| 241 | ** $Id: aclock-example.html 2051 2007-02-21 10:04:20Z chehrlic $ | 
|---|
| 242 | ** | 
|---|
| 243 | ** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved. | 
|---|
| 244 | ** | 
|---|
| 245 | ** This file is part of an example program for Qt.  This example | 
|---|
| 246 | ** program may be used, distributed and modified without limitation. | 
|---|
| 247 | ** | 
|---|
| 248 | *****************************************************************************/ | 
|---|
| 249 |  | 
|---|
| 250 | #include "aclock.h" | 
|---|
| 251 | #include <<a href="qapplication-h.html">qapplication.h</a>> | 
|---|
| 252 |  | 
|---|
| 253 |  | 
|---|
| 254 | int main( int argc, char **argv ) | 
|---|
| 255 | { | 
|---|
| 256 | <a href="qapplication.html">QApplication</a> a( argc, argv ); | 
|---|
| 257 | AnalogClock *clock = new AnalogClock; | 
|---|
| 258 | if ( argc == 2 && strcmp( argv[1], "-transparent" ) == 0 ) | 
|---|
| 259 | <a name="x1213"></a>        clock-><a href="qwidget.html#setAutoMask">setAutoMask</a>( TRUE ); | 
|---|
| 260 | <a name="x1212"></a>    clock-><a href="qwidget.html#resize">resize</a>( 100, 100 ); | 
|---|
| 261 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( clock ); | 
|---|
| 262 | <a name="x1214"></a>    clock-><a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Analog Clock"); | 
|---|
| 263 | <a name="x1215"></a>    clock-><a href="qwidget.html#show">show</a>(); | 
|---|
| 264 | int result = a.<a href="qapplication.html#exec">exec</a>(); | 
|---|
| 265 | delete clock; | 
|---|
| 266 | return result; | 
|---|
| 267 | } | 
|---|
| 268 | </pre> | 
|---|
| 269 |  | 
|---|
| 270 | <p>See also <a href="examples.html">Examples</a>. | 
|---|
| 271 |  | 
|---|
| 272 | <!-- eof --> | 
|---|
| 273 | <p><address><hr><div align=center> | 
|---|
| 274 | <table width=100% cellspacing=0 border=0><tr> | 
|---|
| 275 | <td>Copyright © 2007 | 
|---|
| 276 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a> | 
|---|
| 277 | <td align=right><div align=right>Qt 3.3.8</div> | 
|---|
| 278 | </table></div></address></body> | 
|---|
| 279 | </html> | 
|---|