source: trunk/doc/html/life-example.html@ 190

Last change on this file since 190 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 10.2 KB
Line 
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/life/life.doc:4 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Conway's Game of Life</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { 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&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;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&nbsp;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>Conway's Game of Life</h1>
33
34
35<p>
36<hr>
37<p> Header file:
38<p> <pre>/****************************************************************************
39** $Id: life-example.html 2051 2007-02-21 10:04:20Z chehrlic $
40**
41** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
42**
43** This file is part of an example program for Qt. This example
44** program may be used, distributed and modified without limitation.
45**
46*****************************************************************************/
47
48#ifndef LIFE_H
49#define LIFE_H
50
51#include &lt;<a href="qframe-h.html">qframe.h</a>&gt;
52
53
54class LifeWidget : public <a href="qframe.html">QFrame</a>
55{
56 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
57public:
58 LifeWidget( int s = 10, QWidget *parent = 0, const char *name = 0 );
59
60 void setPoint( int i, int j );
61
62 int maxCol() { return maxi; }
63 int maxRow() { return maxj; }
64
65public slots:
66 void nextGeneration();
67 void clear();
68
69protected:
70 virtual void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * );
71 virtual void mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> * );
72 virtual void mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> * );
73 virtual void resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * );
74 void mouseHandle( const <a href="qpoint.html">QPoint</a> &amp;pos );
75
76private:
77 enum { MAXSIZE = 50, MINSIZE = 10, BORDER = 5 };
78
79 bool cells[2][MAXSIZE + 2][MAXSIZE + 2];
80 int current;
81 int maxi, maxj;
82
83 int pos2index( int x )
84 {
85 return ( x - BORDER ) / SCALE + 1;
86 }
87 int index2pos( int i )
88 {
89 return ( i - 1 ) * SCALE + BORDER;
90 }
91
92 int SCALE;
93};
94
95
96#endif // LIFE_H
97</pre>
98
99<p> <hr>
100<p> Implementation:
101<p> <pre>/****************************************************************************
102** $Id: life-example.html 2051 2007-02-21 10:04:20Z chehrlic $
103**
104** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
105**
106** This file is part of an example program for Qt. This example
107** program may be used, distributed and modified without limitation.
108**
109*****************************************************************************/
110
111#include "life.h"
112
113#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
114#include &lt;<a href="qdrawutil-h.html">qdrawutil.h</a>&gt;
115#include &lt;<a href="qcheckbox-h.html">qcheckbox.h</a>&gt;
116#include &lt;<a href="qevent-h.html">qevent.h</a>&gt;
117#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
118
119// The main game of life widget
120
121<a name="f517"></a>LifeWidget::LifeWidget( int s, QWidget *parent, const char *name )
122 : <a href="qframe.html">QFrame</a>( parent, name )
123{
124 SCALE = s;
125
126 maxi = maxj = 50;
127 <a href="qwidget.html#setMinimumSize">setMinimumSize</a>( MINSIZE * SCALE + 2 * BORDER,
128 MINSIZE * SCALE + 2 * BORDER );
129 <a href="qwidget.html#setMaximumSize">setMaximumSize</a>( MAXSIZE * SCALE + 2 * BORDER,
130 MAXSIZE * SCALE + 2 * BORDER );
131 <a href="qwidget.html#setSizeIncrement">setSizeIncrement</a>( SCALE, SCALE);
132
133 clear();
134 <a href="qwidget.html#resize">resize</a>( maxi * SCALE + 2 * BORDER , maxj * SCALE + 2 * BORDER );
135
136}
137
138
139void <a name="f518"></a>LifeWidget::clear()
140{
141 current = 0;
142 for ( int t = 0; t &lt; 2; t++ )
143 for ( int i = 0; i &lt; MAXSIZE + 2; i++ )
144 for ( int j = 0; j &lt; MAXSIZE + 2; j++ )
145 cells[t][i][j] = FALSE;
146
147 <a href="qwidget.html#repaint">repaint</a>();
148}
149
150
151// We assume that the size will never be beyond the maximum size set
152// this is not in general TRUE, but in practice it's good enough for
153// this program
154
155<a name="x1889"></a>void LifeWidget::<a href="qframe.html#resizeEvent">resizeEvent</a>( <a href="qresizeevent.html">QResizeEvent</a> * e )
156{
157<a name="x1895"></a> maxi = (e-&gt;<a href="qresizeevent.html#size">size</a>().width() - 2 * BORDER) / SCALE;
158 maxj = (e-&gt;<a href="qresizeevent.html#size">size</a>().height() - 2 * BORDER) / SCALE;
159}
160
161
162void <a name="f519"></a>LifeWidget::setPoint( int i, int j )
163{
164 if ( i &lt; 1 || i &gt; maxi || j &lt; 1 || j &gt; maxi )
165 return;
166 cells[current][i][j] = TRUE;
167 <a href="qwidget.html#repaint">repaint</a>( index2pos(i), index2pos(j), SCALE, SCALE, FALSE );
168}
169
170
171void <a name="f520"></a>LifeWidget::mouseHandle( const <a href="qpoint.html">QPoint</a> &amp;pos )
172{
173<a name="x1893"></a> int i = pos2index( pos.<a href="qpoint.html#x">x</a>() );
174<a name="x1894"></a> int j = pos2index( pos.<a href="qpoint.html#y">y</a>() );
175 setPoint( i, j );
176}
177
178
179<a name="x1896"></a>void LifeWidget::<a href="qwidget.html#mouseMoveEvent">mouseMoveEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e )
180{
181<a name="x1891"></a> mouseHandle( e-&gt;<a href="qmouseevent.html#pos">pos</a>() );
182}
183
184
185<a name="x1897"></a>void LifeWidget::<a href="qwidget.html#mousePressEvent">mousePressEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e )
186{
187<a name="x1890"></a> if ( e-&gt;<a href="qmouseevent.html#button">button</a>() == QMouseEvent::LeftButton )
188 mouseHandle( e-&gt;<a href="qmouseevent.html#pos">pos</a>() );
189}
190
191
192void <a name="f521"></a>LifeWidget::nextGeneration()
193{
194 for ( int i = 1; i &lt;= MAXSIZE; i++ ) {
195 for ( int j = 1; j &lt;= MAXSIZE; j++ ) {
196 int t = cells[current][i - 1][j - 1]
197 + cells[current][i - 1][j]
198 + cells[current][i - 1][j + 1]
199 + cells[current][i][j - 1]
200 + cells[current][i][j + 1]
201 + cells[current][i + 1][j - 1]
202 + cells[current][i + 1][j]
203 + cells[current][i + 1][j + 1];
204
205 cells[!current][i][j] = ( t == 3 ||
206 t == 2 &amp;&amp; cells[current][i][j] );
207 }
208 }
209 current = !current;
210 <a href="qwidget.html#repaint">repaint</a>( FALSE ); // repaint without erase
211}
212
213
214<a name="x1888"></a>void LifeWidget::<a href="qframe.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> * e )
215{
216<a name="x1892"></a> int starti = pos2index( e-&gt;<a href="qpaintevent.html#rect">rect</a>().left() );
217 int stopi = pos2index( e-&gt;<a href="qpaintevent.html#rect">rect</a>().right() );
218 int startj = pos2index( e-&gt;<a href="qpaintevent.html#rect">rect</a>().top() );
219 int stopj = pos2index( e-&gt;<a href="qpaintevent.html#rect">rect</a>().bottom() );
220
221 if (stopi &gt; maxi)
222 stopi = maxi;
223 if (stopj &gt; maxj)
224 stopj = maxj;
225
226 <a href="qpainter.html">QPainter</a> paint( this );
227 for ( int i = starti; i &lt;= stopi; i++ ) {
228 for ( int j = startj; j &lt;= stopj; j++ ) {
229 if ( cells[current][i][j] )
230 <a href="qpainter.html#qDrawShadePanel">qDrawShadePanel</a>( &amp;paint, index2pos( i ), index2pos( j ),
231 SCALE - 1, SCALE - 1, colorGroup() );
232 else if ( cells[!current][i][j] )
233 <a href="qwidget.html#erase">erase</a>(index2pos( i ), index2pos( j ), SCALE - 1, SCALE - 1);
234 }
235 }
236 <a href="qframe.html#drawFrame">drawFrame</a>( &amp;paint );
237}
238</pre>
239
240<p> <hr>
241<p> Main:
242<p> <pre>/****************************************************************************
243** $Id: life-example.html 2051 2007-02-21 10:04:20Z chehrlic $
244**
245** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
246**
247** This file is part of an example program for Qt. This example
248** program may be used, distributed and modified without limitation.
249**
250*****************************************************************************/
251
252#include "lifedlg.h"
253#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
254#include &lt;stdlib.h&gt;
255
256void usage()
257{
258 <a href="qapplication.html#qWarning">qWarning</a>( "Usage: life [-scale scale]" );
259}
260
261int main( int argc, char **argv )
262{
263 <a href="qapplication.html">QApplication</a> a( argc, argv );
264
265 int scale = 10;
266
267 for ( int i = 1; i &lt; argc; i++ ){
268 <a href="qstring.html">QString</a> arg = argv[i];
269 if ( arg == "-scale" )
270 scale = atoi( argv[++i] );
271 else {
272 usage();
273 exit(1);
274 }
275 }
276
277 if ( scale &lt; 2 )
278 scale = 2;
279
280 LifeDialog *life = new LifeDialog( scale );
281 a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( life );
282 life-&gt;<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Life");
283 life-&gt;<a href="qwidget.html#show">show</a>();
284
285 return a.<a href="qapplication.html#exec">exec</a>();
286}
287</pre>
288
289<p>See also <a href="examples.html">Examples</a>.
290
291<!-- eof -->
292<p><address><hr><div align=center>
293<table width=100% cellspacing=0 border=0><tr>
294<td>Copyright &copy; 2007
295<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
296<td align=right><div align=right>Qt 3.3.8</div>
297</table></div></address></body>
298</html>
Note: See TracBrowser for help on using the repository browser.