1 | /****************************************************************************
|
---|
2 | ** $Id: lifedlg.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 "lifedlg.h"
|
---|
12 | #include <qapplication.h>
|
---|
13 | #include <qpushbutton.h>
|
---|
14 | #include <qlabel.h>
|
---|
15 | #include <qslider.h>
|
---|
16 | #include <qcombobox.h>
|
---|
17 | #include <qdatetime.h>
|
---|
18 | #include <stdlib.h>
|
---|
19 |
|
---|
20 | #include "patterns.cpp"
|
---|
21 |
|
---|
22 |
|
---|
23 | // A simple timer which has a pause and a setSpeed slot
|
---|
24 |
|
---|
25 | LifeTimer::LifeTimer( QWidget *parent ) : QTimer( parent ), interval( 500 )
|
---|
26 | {
|
---|
27 | start( interval );
|
---|
28 | }
|
---|
29 |
|
---|
30 |
|
---|
31 | void LifeTimer::pause( bool stopIt )
|
---|
32 | {
|
---|
33 | if ( stopIt )
|
---|
34 | stop();
|
---|
35 | else
|
---|
36 | start( interval );
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 | void LifeTimer::setSpeed( int speed )
|
---|
41 | {
|
---|
42 | interval = MAXSPEED - speed;
|
---|
43 | if ( isActive() )
|
---|
44 | changeInterval( interval );
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | // A top-level container widget to organize the others
|
---|
49 |
|
---|
50 | LifeDialog::LifeDialog( int scale, QWidget * parent, const char * name )
|
---|
51 | : QWidget( parent, name )
|
---|
52 | {
|
---|
53 | qb = new QPushButton( "Quit!", this );
|
---|
54 | cb = new QComboBox( this, "comboBox" );
|
---|
55 | life = new LifeWidget(scale, this);
|
---|
56 | life->move( SIDEBORDER, TOPBORDER );
|
---|
57 |
|
---|
58 |
|
---|
59 | connect( qb, SIGNAL(clicked()), qApp, SLOT(quit()) );
|
---|
60 | qb->setGeometry( SIDEBORDER, SIDEBORDER, qb->sizeHint().width(), 25 );
|
---|
61 | timer = new LifeTimer( this );
|
---|
62 |
|
---|
63 | connect( timer, SIGNAL(timeout()), life, SLOT(nextGeneration()) );
|
---|
64 | pb = new QPushButton( "Pause", this );
|
---|
65 | pb->setToggleButton( TRUE );
|
---|
66 | connect( pb, SIGNAL(toggled(bool)), timer, SLOT(pause(bool)) );
|
---|
67 | pb->resize( pb->sizeHint().width(), 25 );
|
---|
68 | pb->move( width() - SIDEBORDER - pb->width(), SIDEBORDER );
|
---|
69 |
|
---|
70 | sp = new QLabel( "Speed:", this );
|
---|
71 | sp->adjustSize();
|
---|
72 | sp->move( SIDEBORDER, 45 );
|
---|
73 | scroll = new QSlider( 0, LifeTimer::MAXSPEED, 50,
|
---|
74 | LifeTimer::MAXSPEED / 2,
|
---|
75 | QSlider::Horizontal, this );
|
---|
76 | connect( scroll, SIGNAL(valueChanged(int)),
|
---|
77 | timer, SLOT(setSpeed(int)) );
|
---|
78 |
|
---|
79 | scroll->move( sp->width() + 2 * SIDEBORDER, 45 );
|
---|
80 | scroll->resize( 200, 15 );
|
---|
81 |
|
---|
82 | life->setFrameStyle( QFrame::Panel | QFrame::Sunken );
|
---|
83 | life->show();
|
---|
84 |
|
---|
85 | srand( QTime(0,0,0).msecsTo(QTime::currentTime()) );
|
---|
86 | int sel = rand() % NPATS;
|
---|
87 | getPattern( sel );
|
---|
88 |
|
---|
89 | cb->move( 2*SIDEBORDER + qb->width(), SIDEBORDER);
|
---|
90 | cb->insertItem( "Glider Gun " );
|
---|
91 | cb->insertItem( "Figure Eight " );
|
---|
92 | cb->insertItem( "Pulsar " );
|
---|
93 | cb->insertItem( "Barber Pole P2 " );
|
---|
94 | cb->insertItem( "Achim P5 " );
|
---|
95 | cb->insertItem( "Hertz P4 " );
|
---|
96 | cb->insertItem( "Tumbler " );
|
---|
97 | cb->insertItem( "Pulse1 P4" );
|
---|
98 | cb->insertItem( "Shining Flower P5 " );
|
---|
99 | cb->insertItem( "Pulse2 P6 " );
|
---|
100 | cb->insertItem( "Pinwheel, Clock P4 " );
|
---|
101 | cb->insertItem( "Pentadecatholon " );
|
---|
102 | cb->insertItem( "Piston " );
|
---|
103 | cb->insertItem( "Piston2 " );
|
---|
104 | cb->insertItem( "Switch Engine " );
|
---|
105 | cb->insertItem( "Gears (Gear, Flywheel, Blinker) " );
|
---|
106 | cb->insertItem( "Turbine8 " );
|
---|
107 | cb->insertItem( "P16 " );
|
---|
108 | cb->insertItem( "Puffer " );
|
---|
109 | cb->insertItem( "Escort " );
|
---|
110 | cb->insertItem( "Dart Speed 1/3 " );
|
---|
111 | cb->insertItem( "Period 4 Speed 1/2 " );
|
---|
112 | cb->insertItem( "Another Period 4 Speed 1/2 " );
|
---|
113 | cb->insertItem( "Smallest Known Period 3 Spaceship Speed 1/3 " );
|
---|
114 | cb->insertItem( "Turtle Speed 1/3 " );
|
---|
115 | cb->insertItem( "Smallest Known Period 5 Speed 2/5 " );
|
---|
116 | cb->insertItem( "Sym Puffer " );
|
---|
117 | cb->insertItem( "], Near Ship, Pi Heptomino " );
|
---|
118 | cb->insertItem( "R Pentomino " );
|
---|
119 | cb->setAutoResize( FALSE );
|
---|
120 | cb->setCurrentItem( sel );
|
---|
121 | cb->show();
|
---|
122 | connect( cb, SIGNAL(activated(int)), SLOT(getPattern(int)) );
|
---|
123 |
|
---|
124 | QSize s;
|
---|
125 | s = life->minimumSize();
|
---|
126 | setMinimumSize( s.width() + 2 * SIDEBORDER,
|
---|
127 | s.height() + TOPBORDER + SIDEBORDER );
|
---|
128 | s = life->maximumSize();
|
---|
129 | setMaximumSize( s.width() + 2 * SIDEBORDER,
|
---|
130 | s.height() + TOPBORDER + SIDEBORDER );
|
---|
131 | s = life->sizeIncrement();
|
---|
132 | setSizeIncrement( s.width(), s.height() );
|
---|
133 |
|
---|
134 | resize( QMIN(512, qApp->desktop()->width()),
|
---|
135 | QMIN(480, qApp->desktop()->height()) );
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | void LifeDialog::resizeEvent( QResizeEvent * e )
|
---|
140 | {
|
---|
141 | life->resize( e->size() - QSize( 2 * SIDEBORDER, TOPBORDER + SIDEBORDER ));
|
---|
142 | pb->move( e->size().width() - SIDEBORDER - pb->width(), SIDEBORDER );
|
---|
143 | scroll->resize( e->size().width() - sp->width() - 3 * SIDEBORDER,
|
---|
144 | scroll->height() );
|
---|
145 | cb->resize( width() - 4*SIDEBORDER - qb->width() - pb->width() , 25 );
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | // Adapted from xlock, see pattern.cpp for copyright info.
|
---|
150 |
|
---|
151 | void LifeDialog::getPattern( int pat )
|
---|
152 | {
|
---|
153 | life->clear();
|
---|
154 | int i = pat % NPATS;
|
---|
155 | int col;
|
---|
156 | int * patptr = &patterns[i][0];
|
---|
157 | while ( (col = *patptr++) != 127 ) {
|
---|
158 | int row = *patptr++;
|
---|
159 | col += life->maxCol() / 2;
|
---|
160 | row += life->maxRow() / 2;
|
---|
161 | life->setPoint( col, row );
|
---|
162 | }
|
---|
163 | }
|
---|