-rw-r--r-- | noncore/games/minesweep/minefield.h | 30 | ||||
-rw-r--r-- | noncore/games/minesweep/minesweep.cpp | 35 |
2 files changed, 47 insertions, 18 deletions
diff --git a/noncore/games/minesweep/minefield.h b/noncore/games/minesweep/minefield.h index 4ede435..1349c35 100644 --- a/noncore/games/minesweep/minefield.h +++ b/noncore/games/minesweep/minefield.h | |||
@@ -15,73 +15,87 @@ | |||
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 | #ifndef MINEFIELD_H | 20 | #ifndef MINEFIELD_H |
21 | #define MINEFIELD_H | 21 | #define MINEFIELD_H |
22 | 22 | ||
23 | #include <qtable.h> | 23 | #include <qscrollview.h> |
24 | 24 | ||
25 | class Mine; | 25 | class Mine; |
26 | class Config; | 26 | class Config; |
27 | 27 | ||
28 | class MineField : public QTable | 28 | class MineField : public QScrollView |
29 | { | 29 | { |
30 | Q_OBJECT | 30 | Q_OBJECT |
31 | public: | 31 | public: |
32 | MineField( QWidget* parent = 0, const char* name = 0 ); | 32 | MineField( QWidget* parent = 0, const char* name = 0 ); |
33 | ~MineField(); | 33 | ~MineField(); |
34 | 34 | ||
35 | enum State { Waiting, Playing, GameOver }; | 35 | enum State { Waiting, Playing, GameOver }; |
36 | 36 | ||
37 | State state() const { return stat; } | 37 | State state() const { return stat; } |
38 | 38 | ||
39 | void readConfig(Config&); | 39 | void readConfig(Config&); |
40 | void writeConfig(Config&) const; | 40 | void writeConfig(Config&) const; |
41 | 41 | ||
42 | int level() const { return lev; } | 42 | int level() const { return lev; } |
43 | 43 | ||
44 | void setAvailableRect( const QRect & ); | ||
44 | public slots: | 45 | public slots: |
45 | void setup( int level ); | 46 | void setup( int level ); |
46 | 47 | ||
47 | void showMines(); | 48 | void showMines(); |
48 | 49 | ||
49 | signals: | 50 | signals: |
50 | void gameOver( bool won ); | 51 | void gameOver( bool won ); |
51 | void gameStarted(); | 52 | void gameStarted(); |
52 | void mineCount( int ); | 53 | void mineCount( int ); |
53 | 54 | ||
54 | protected: | 55 | protected: |
55 | void paintFocus( QPainter*, const QRect& ); | 56 | |
56 | void viewportMousePressEvent( QMouseEvent* ); | 57 | void contentsMousePressEvent( QMouseEvent* ); |
57 | void viewportMouseReleaseEvent( QMouseEvent* ); | 58 | void contentsMouseReleaseEvent( QMouseEvent* ); |
58 | void keyPressEvent( QKeyEvent* ); | 59 | void keyPressEvent( QKeyEvent* ); |
59 | void keyReleaseEvent( QKeyEvent* ); | 60 | void keyReleaseEvent( QKeyEvent* ); |
60 | 61 | void drawContents( QPainter * p, int clipx, int clipy, int clipw, int cliph ); | |
62 | |||
61 | int getHint( int row, int col ); | 63 | int getHint( int row, int col ); |
62 | void setHint( Mine* ); | 64 | void setHint( int r, int c ); |
63 | void updateMine( int row, int col ); | 65 | void updateMine( int row, int col ); |
64 | void paletteChange( const QPalette & ); | 66 | void paletteChange( const QPalette & ); |
65 | 67 | void updateCell( int r, int c ); | |
68 | bool onBoard( int r, int c ) const { return r >= 0 && r < numRows && c >= 0 && c < numCols; } | ||
69 | Mine *mine( int row, int col ) { return onBoard(row, col ) ? mines[row+numCols*col] : 0; } | ||
70 | const Mine *mine( int row, int col ) const { return onBoard(row, col ) ? mines[row+numCols*col] : 0; } | ||
71 | |||
66 | protected slots: | 72 | protected slots: |
67 | void cellPressed( int row, int col ); | 73 | void cellPressed( int row, int col ); |
68 | void cellClicked( int row, int col ); | 74 | void cellClicked( int row, int col ); |
69 | void held(); | 75 | void held(); |
70 | 76 | ||
71 | private: | 77 | private: |
78 | int findCellSize(); | ||
79 | void setCellSize( int ); | ||
80 | |||
72 | State stat; | 81 | State stat; |
73 | void MineField::setState( State st ); | 82 | void MineField::setState( State st ); |
74 | void MineField::placeMines(); | 83 | void MineField::placeMines(); |
75 | enum FlagAction { NoAction, FlagOn, FlagNext }; | 84 | enum FlagAction { NoAction, FlagOn, FlagNext }; |
76 | FlagAction flagAction; | 85 | FlagAction flagAction; |
77 | bool ignoreClick; | 86 | bool ignoreClick; |
78 | int currRow; | 87 | int currRow; |
79 | int currCol; | 88 | int currCol; |
89 | int numRows, numCols; | ||
90 | |||
80 | int minecount; | 91 | int minecount; |
81 | int mineguess; | 92 | int mineguess; |
82 | int nonminecount; | 93 | int nonminecount; |
83 | int lev; | 94 | int lev; |
95 | QRect availableRect; | ||
96 | int cellSize; | ||
84 | QTimer *holdTimer; | 97 | QTimer *holdTimer; |
98 | Mine **mines; | ||
85 | }; | 99 | }; |
86 | 100 | ||
87 | #endif // MINEFIELD_H | 101 | #endif // MINEFIELD_H |
diff --git a/noncore/games/minesweep/minesweep.cpp b/noncore/games/minesweep/minesweep.cpp index 6492462..c84fe53 100644 --- a/noncore/games/minesweep/minesweep.cpp +++ b/noncore/games/minesweep/minesweep.cpp | |||
@@ -214,35 +214,51 @@ void ResultIndicator::timerEvent( QTimerEvent *te ) | |||
214 | twoStage = FALSE; | 214 | twoStage = FALSE; |
215 | timerId = startTimer( 1000 ); | 215 | timerId = startTimer( 1000 ); |
216 | } else { | 216 | } else { |
217 | delete this; | 217 | delete this; |
218 | } | 218 | } |
219 | } | 219 | } |
220 | 220 | ||
221 | 221 | ||
222 | class MineFrame : public QFrame | ||
223 | { | ||
224 | public: | ||
225 | MineFrame( QWidget *parent, const char *name = 0 ) | ||
226 | :QFrame( parent, name ) {} | ||
227 | void setField( MineField *f ) { field = f; } | ||
228 | protected: | ||
229 | void resizeEvent( QResizeEvent *e ) { | ||
230 | field->setAvailableRect( contentsRect()); | ||
231 | QFrame::resizeEvent(e); | ||
232 | } | ||
233 | private: | ||
234 | MineField *field; | ||
235 | }; | ||
236 | |||
237 | |||
238 | |||
222 | MineSweep::MineSweep( QWidget* parent, const char* name, WFlags f ) | 239 | MineSweep::MineSweep( QWidget* parent, const char* name, WFlags f ) |
223 | : QMainWindow( parent, name, f ) | 240 | : QMainWindow( parent, name, f ) |
224 | { | 241 | { |
225 | srand(::time(0)); | 242 | srand(::time(0)); |
226 | setCaption( tr("Mine Hunt") ); | 243 | setCaption( tr("Mine Hunt") ); |
227 | setIcon( Resource::loadPixmap( "minesweep_icon" ) ); | 244 | setIcon( Resource::loadPixmap( "minesweep_icon" ) ); |
228 | 245 | ||
229 | QPEToolBar *menuToolBar = new QPEToolBar( this ); | 246 | QPEToolBar *toolBar = new QPEToolBar( this ); |
230 | QPEMenuBar *menuBar = new QPEMenuBar( menuToolBar ); | 247 | toolBar->setHorizontalStretchable( TRUE ); |
248 | |||
249 | QPEMenuBar *menuBar = new QPEMenuBar( toolBar ); | ||
231 | 250 | ||
232 | QPopupMenu *gameMenu = new QPopupMenu( this ); | 251 | QPopupMenu *gameMenu = new QPopupMenu( this ); |
233 | gameMenu->insertItem( tr("Beginner"), this, SLOT( beginner() ) ); | 252 | gameMenu->insertItem( tr("Beginner"), this, SLOT( beginner() ) ); |
234 | gameMenu->insertItem( tr("Advanced"), this, SLOT( advanced() ) ); | 253 | gameMenu->insertItem( tr("Advanced"), this, SLOT( advanced() ) ); |
235 | gameMenu->insertItem( tr("Expert"), this, SLOT( expert() ) ); | 254 | gameMenu->insertItem( tr("Expert"), this, SLOT( expert() ) ); |
236 | 255 | ||
237 | menuBar->insertItem( tr("Game"), gameMenu ); | 256 | menuBar->insertItem( tr("Game"), gameMenu ); |
238 | |||
239 | QPEToolBar *toolBar = new QPEToolBar( this ); | ||
240 | toolBar->setHorizontalStretchable( TRUE ); | ||
241 | 257 | ||
242 | guessLCD = new QLCDNumber( toolBar ); | 258 | guessLCD = new QLCDNumber( toolBar ); |
243 | toolBar->setStretchableWidget( guessLCD ); | 259 | toolBar->setStretchableWidget( guessLCD ); |
244 | 260 | ||
245 | QPalette lcdPal( red ); | 261 | QPalette lcdPal( red ); |
246 | lcdPal.setColor( QColorGroup::Background, QApplication::palette().active().background() ); | 262 | lcdPal.setColor( QColorGroup::Background, QApplication::palette().active().background() ); |
247 | lcdPal.setColor( QColorGroup::Button, QApplication::palette().active().button() ); | 263 | lcdPal.setColor( QColorGroup::Button, QApplication::palette().active().button() ); |
248 | 264 | ||
@@ -260,27 +276,26 @@ MineSweep::MineSweep( QWidget* parent, const char* name, WFlags f ) | |||
260 | // timeLCD->setPalette( lcdPal ); | 276 | // timeLCD->setPalette( lcdPal ); |
261 | timeLCD->setSegmentStyle( QLCDNumber::Flat ); | 277 | timeLCD->setSegmentStyle( QLCDNumber::Flat ); |
262 | timeLCD->setFrameStyle( QFrame::NoFrame ); | 278 | timeLCD->setFrameStyle( QFrame::NoFrame ); |
263 | timeLCD->setNumDigits( 5 ); // "mm:ss" | 279 | timeLCD->setNumDigits( 5 ); // "mm:ss" |
264 | timeLCD->setBackgroundMode( PaletteButton ); | 280 | timeLCD->setBackgroundMode( PaletteButton ); |
265 | 281 | ||
266 | setToolBarsMovable ( FALSE ); | 282 | setToolBarsMovable ( FALSE ); |
267 | 283 | ||
268 | addToolBar( menuToolBar ); | ||
269 | addToolBar( toolBar ); | 284 | addToolBar( toolBar ); |
270 | 285 | ||
271 | QFrame *mainframe = new QFrame( this ); | 286 | MineFrame *mainframe = new MineFrame( this ); |
272 | mainframe->setFrameShape( QFrame::Box ); | 287 | mainframe->setFrameShape( QFrame::Box ); |
273 | mainframe->setFrameShadow( QFrame::Raised ); | 288 | mainframe->setFrameShadow( QFrame::Raised ); |
274 | mainframe->setMargin(5); | 289 | |
275 | mainframe->setLineWidth(2); | 290 | mainframe->setLineWidth(2); |
276 | QBoxLayout *box = new QVBoxLayout( mainframe ); | 291 | |
277 | field = new MineField( mainframe ); | 292 | field = new MineField( mainframe ); |
278 | box->addWidget( field, 0, AlignCenter ); | 293 | mainframe->setField( field ); |
279 | QFont fnt = field->font(); | 294 | QFont fnt = field->font(); |
280 | fnt.setBold( TRUE ); | 295 | fnt.setBold( TRUE ); |
281 | field->setFont( QFont( fnt ) ); | 296 | field->setFont( QFont( fnt ) ); |
282 | field->setFocus(); | 297 | field->setFocus(); |
283 | setCentralWidget( mainframe ); | 298 | setCentralWidget( mainframe ); |
284 | 299 | ||
285 | connect( field, SIGNAL( gameOver( bool ) ), this, SLOT( gameOver( bool ) ) ); | 300 | connect( field, SIGNAL( gameOver( bool ) ), this, SLOT( gameOver( bool ) ) ); |
286 | connect( field, SIGNAL( mineCount( int ) ), this, SLOT( setCounter( int ) ) ); | 301 | connect( field, SIGNAL( mineCount( int ) ), this, SLOT( setCounter( int ) ) ); |