summaryrefslogtreecommitdiff
path: root/noncore/games/minesweep/minesweep.cpp
Unidiff
Diffstat (limited to 'noncore/games/minesweep/minesweep.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/minesweep/minesweep.cpp34
1 files changed, 20 insertions, 14 deletions
diff --git a/noncore/games/minesweep/minesweep.cpp b/noncore/games/minesweep/minesweep.cpp
index 7214a73..d707dab 100644
--- a/noncore/games/minesweep/minesweep.cpp
+++ b/noncore/games/minesweep/minesweep.cpp
@@ -1,52 +1,52 @@
1/********************************************************************** 1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved. 2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3** 3**
4** This file is part of Qtopia Environment. 4** This file is part of the Qtopia Environment.
5** 5**
6** This file may be distributed and/or modified under the terms of the 6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software 7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the 8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file. 9** packaging of this file.
10** 10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13** 13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information. 14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15** 15**
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20 20
21#include "minesweep.h" 21#include "minesweep.h"
22#include "minefield.h" 22#include "minefield.h"
23 23
24#include <qpe/qpeapplication.h> 24#include <qtopia/qpeapplication.h>
25#include <qpe/resource.h> 25#include <qtopia/resource.h>
26#include <qpe/config.h> 26#include <qtopia/config.h>
27 27
28#include <qpe/qpetoolbar.h> 28#include <qtoolbar.h>
29#include <qmenubar.h> 29#include <qmenubar.h>
30#include <qpopupmenu.h> 30#include <qpopupmenu.h>
31#include <qpushbutton.h> 31#include <qpushbutton.h>
32#include <qlcdnumber.h> 32#include <qlcdnumber.h>
33#include <qmessagebox.h> 33#include <qmessagebox.h>
34#include <qtimer.h> 34#include <qtimer.h>
35#include <qpalette.h> 35#include <qpalette.h>
36#include <qapplication.h> 36#include <qapplication.h>
37#include <qlayout.h> 37#include <qlayout.h>
38#include <qlabel.h> 38#include <qlabel.h>
39 39
40#include <stdlib.h> 40#include <stdlib.h>
41#include <time.h> 41#include <time.h>
42 42
43 43
44 44
45 45
46static const char *pix_new[]={ 46static const char *pix_new[]={
47"20 20 3 1", 47"20 20 3 1",
48" c None", 48" c None",
49"# c #00FF00", 49"# c #00FF00",
50". c #000000", 50". c #000000",
51" ", 51" ",
52" ...... ", 52" ...... ",
@@ -177,155 +177,161 @@ void ResultIndicator::showResult( QWidget *ref, bool won )
177 if ( won ) { 177 if ( won ) {
178 r->setText( MineSweep::tr("You won!") ); 178 r->setText( MineSweep::tr("You won!") );
179 r->center(); 179 r->center();
180 r->show(); 180 r->show();
181 r->twoStage = FALSE; 181 r->twoStage = FALSE;
182 r->timerId = r->startTimer(1500); 182 r->timerId = r->startTimer(1500);
183 } else { 183 } else {
184 QPalette p( red ); 184 QPalette p( red );
185 r->setPalette( p ); 185 r->setPalette( p );
186 r->setText( MineSweep::tr("You exploded!") ); 186 r->setText( MineSweep::tr("You exploded!") );
187 r->resize( ref->size() ); 187 r->resize( ref->size() );
188 r->move( ref->mapToGlobal(QPoint(0,0)) ); 188 r->move( ref->mapToGlobal(QPoint(0,0)) );
189 r->show(); 189 r->show();
190 r->twoStage = TRUE; 190 r->twoStage = TRUE;
191 r->timerId =r->startTimer(200); 191 r->timerId =r->startTimer(200);
192 } 192 }
193} 193}
194 194
195void ResultIndicator::center() 195void ResultIndicator::center()
196{ 196{
197 QWidget *w = parentWidget(); 197 QWidget *w = parentWidget();
198 198
199 QPoint pp = w->mapToGlobal( QPoint(0,0) ); 199 QPoint pp = w->mapToGlobal( QPoint(0,0) );
200 QSize s = sizeHint()*3; 200 QSize s = sizeHint()*3;
201 s.setWidth( QMIN(s.width(), w->width()) );
201 pp = QPoint( pp.x() + w->width()/2 - s.width()/2, 202 pp = QPoint( pp.x() + w->width()/2 - s.width()/2,
202 pp.y() + w->height()/ 2 - s.height()/2 ); 203 pp.y() + w->height()/ 2 - s.height()/2 );
203 204
204 setGeometry( QRect(pp, s) ); 205 setGeometry( QRect(pp, s) );
205 206
206} 207}
207 208
208void ResultIndicator::timerEvent( QTimerEvent *te ) 209void ResultIndicator::timerEvent( QTimerEvent *te )
209{ 210{
210 if ( te->timerId() != timerId ) 211 if ( te->timerId() != timerId )
211 return; 212 return;
212 killTimer( timerId ); 213 killTimer( timerId );
213 if ( twoStage ) { 214 if ( twoStage ) {
214 center(); 215 center();
215 twoStage = FALSE; 216 twoStage = FALSE;
216 timerId = startTimer( 1000 ); 217 timerId = startTimer( 1000 );
217 } else { 218 } else {
218 delete this; 219 delete this;
219 } 220 }
220} 221}
221 222
222 223
223class MineFrame : public QFrame 224class MineFrame : public QFrame
224{ 225{
225public: 226public:
226 MineFrame( QWidget *parent, const char *name = 0 ) 227 MineFrame( QWidget *parent, const char *name = 0 )
227 :QFrame( parent, name ) {} 228 :QFrame( parent, name ), field(0) {}
228 void setField( MineField *f ) { field = f; } 229 void setField( MineField *f ) {
230 field = f;
231 setMinimumSize( field->sizeHint() );
232 }
229protected: 233protected:
230 void resizeEvent( QResizeEvent *e ) { 234 void resizeEvent( QResizeEvent *e ) {
231 field->setAvailableRect( contentsRect()); 235 field->setAvailableRect( contentsRect());
232 QFrame::resizeEvent(e); 236 QFrame::resizeEvent(e);
233 } 237 }
234private: 238private:
235 MineField *field; 239 MineField *field;
236}; 240};
237 241
238 242
239 243
240MineSweep::MineSweep( QWidget* parent, const char* name, WFlags f ) 244MineSweep::MineSweep( QWidget* parent, const char* name, WFlags f )
241: QMainWindow( parent, name, f ) 245: QMainWindow( parent, name, f )
242{ 246{
243 QPEApplication::setInputMethodHint(this, QPEApplication::AlwaysOff );
244 srand(::time(0)); 247 srand(::time(0));
245 setCaption( tr("Mine Hunt") ); 248 setCaption( tr("Mine Hunt") );
246 setIcon( Resource::loadPixmap( "minesweep_icon" ) ); 249 QPEApplication::setInputMethodHint(this, QPEApplication::AlwaysOff );
250 setIcon( Resource::loadPixmap( "minesweep/MineHunt" ) );
247 251
248 QToolBar *toolBar = new QToolBar( this ); 252 QToolBar *toolBar = new QToolBar( this );
249 toolBar->setHorizontalStretchable( TRUE ); 253 toolBar->setHorizontalStretchable( TRUE );
250 254
251 QMenuBar *menuBar = new QMenuBar( toolBar ); 255 QMenuBar *menuBar = new QMenuBar( toolBar );
252 256
253 QPopupMenu *gameMenu = new QPopupMenu( this ); 257 QPopupMenu *gameMenu = new QPopupMenu( this );
254 gameMenu->insertItem( tr("Beginner"), this, SLOT( beginner() ) ); 258 gameMenu->insertItem( tr("Beginner"), this, SLOT( beginner() ) );
255 gameMenu->insertItem( tr("Advanced"), this, SLOT( advanced() ) ); 259 gameMenu->insertItem( tr("Advanced"), this, SLOT( advanced() ) );
256 gameMenu->insertItem( tr("Expert"), this, SLOT( expert() ) ); 260
261 if (qApp->desktop()->width() >= 240) {
262 gameMenu->insertItem( tr("Expert"), this, SLOT( expert() ) );
263 }
257 264
258 menuBar->insertItem( tr("Game"), gameMenu ); 265 menuBar->insertItem( tr("Game"), gameMenu );
259 266
260 guessLCD = new QLCDNumber( toolBar ); 267 guessLCD = new QLCDNumber( toolBar );
261 toolBar->setStretchableWidget( guessLCD ); 268 toolBar->setStretchableWidget( guessLCD );
262 269
263 QPalette lcdPal( red ); 270 QPalette lcdPal( red );
264 lcdPal.setColor( QColorGroup::Background, QApplication::palette().active().background() ); 271 lcdPal.setColor( QColorGroup::Background, QApplication::palette().active().background() );
265 lcdPal.setColor( QColorGroup::Button, QApplication::palette().active().button() ); 272 lcdPal.setColor( QColorGroup::Button, QApplication::palette().active().button() );
266 273
267// guessLCD->setPalette( lcdPal ); 274// guessLCD->setPalette( lcdPal );
268 guessLCD->setSegmentStyle( QLCDNumber::Flat ); 275 guessLCD->setSegmentStyle( QLCDNumber::Flat );
269 guessLCD->setFrameStyle( QFrame::NoFrame ); 276 guessLCD->setFrameStyle( QFrame::NoFrame );
270 guessLCD->setNumDigits( 2 ); 277 guessLCD->setNumDigits( 2 );
271 guessLCD->setBackgroundMode( PaletteButton ); 278 guessLCD->setBackgroundMode( PaletteButton );
272 newGameButton = new QPushButton( toolBar ); 279 newGameButton = new QPushButton( toolBar );
273 newGameButton->setPixmap( QPixmap( pix_new ) ); 280 newGameButton->setPixmap( QPixmap( pix_new ) );
274 newGameButton->setFocusPolicy(QWidget::NoFocus); 281 newGameButton->setFocusPolicy(QWidget::NoFocus);
275 connect( newGameButton, SIGNAL(clicked()), this, SLOT(newGame()) ); 282 connect( newGameButton, SIGNAL(clicked()), this, SLOT(newGame()) );
276 283
277 timeLCD = new QLCDNumber( toolBar ); 284 timeLCD = new QLCDNumber( toolBar );
278// timeLCD->setPalette( lcdPal ); 285// timeLCD->setPalette( lcdPal );
279 timeLCD->setSegmentStyle( QLCDNumber::Flat ); 286 timeLCD->setSegmentStyle( QLCDNumber::Flat );
280 timeLCD->setFrameStyle( QFrame::NoFrame ); 287 timeLCD->setFrameStyle( QFrame::NoFrame );
281 timeLCD->setNumDigits( 5 ); // "mm:ss" 288 timeLCD->setNumDigits( 5 ); // "mm:ss"
282 timeLCD->setBackgroundMode( PaletteButton ); 289 timeLCD->setBackgroundMode( PaletteButton );
283 290
284 setToolBarsMovable ( FALSE ); 291 setToolBarsMovable ( FALSE );
285 292
286 addToolBar( toolBar ); 293 addToolBar( toolBar );
287 294
288 MineFrame *mainframe = new MineFrame( this ); 295 MineFrame *mainframe = new MineFrame( this );
289 mainframe->setFrameShape( QFrame::Box ); 296 mainframe->setFrameShape( QFrame::Box );
290 mainframe->setFrameShadow( QFrame::Raised ); 297 mainframe->setFrameShadow( QFrame::Raised );
291 298
292 mainframe->setLineWidth(2); 299 mainframe->setLineWidth(2);
293 300
294 field = new MineField( mainframe ); 301 field = new MineField( mainframe );
295 mainframe->setField( field ); 302 mainframe->setField( field );
296 QFont fnt = field->font(); 303 QFont fnt = field->font();
297 fnt.setBold( TRUE ); 304 fnt.setBold( TRUE );
298 field->setFont( QFont( fnt ) ); 305 field->setFont( QFont( fnt ) );
299 field->setFocus(); 306 field->setFocus();
300 setCentralWidget( mainframe ); 307 setCentralWidget( mainframe );
301 308
302 connect( field, SIGNAL( gameOver( bool ) ), this, SLOT( gameOver( bool ) ) ); 309 connect( field, SIGNAL( gameOver(bool) ), this, SLOT( gameOver(bool) ) );
303 connect( field, SIGNAL( mineCount( int ) ), this, SLOT( setCounter( int ) ) ); 310 connect( field, SIGNAL( mineCount(int) ), this, SLOT( setCounter(int) ) );
304 connect( field, SIGNAL( gameStarted()), this, SLOT( startPlaying() ) ); 311 connect( field, SIGNAL( gameStarted()), this, SLOT( startPlaying() ) );
305 312
306 timer = new QTimer( this ); 313 timer = new QTimer( this );
307
308 connect( timer, SIGNAL( timeout() ), this, SLOT( updateTime() ) ); 314 connect( timer, SIGNAL( timeout() ), this, SLOT( updateTime() ) );
309 315
310 readConfig(); 316 readConfig();
311} 317}
312 318
313MineSweep::~MineSweep() 319MineSweep::~MineSweep()
314{ 320{
315 writeConfig(); 321 writeConfig();
316} 322}
317 323
318void MineSweep::gameOver( bool won ) 324void MineSweep::gameOver( bool won )
319{ 325{
320 field->showMines(); 326 field->showMines();
321 if ( won ) { 327 if ( won ) {
322 newGameButton->setPixmap( QPixmap( happy_xpm ) ); 328 newGameButton->setPixmap( QPixmap( happy_xpm ) );
323 } else { 329 } else {
324 newGameButton->setPixmap( QPixmap( dead_xpm ) ); 330 newGameButton->setPixmap( QPixmap( dead_xpm ) );
325 } 331 }
326 ResultIndicator::showResult( this, won ); 332 ResultIndicator::showResult( this, won );
327 timer->stop(); 333 timer->stop();
328} 334}
329 335
330void MineSweep::newGame() 336void MineSweep::newGame()
331{ 337{