/* Handles mouse press events for the KBBGraphic widget. */ void KBBGraphic::mousePressEvent( QMouseEvent* e ) { if (inputAccepted) { /* * Middle click finishes the game. */ if (e->button() == MidButton) { emit endMouseClicked(); return; } int oldRow = curRow; int oldCol = curCol; QPoint pos = e->pos(); // extract pointer position curRow = pos.y() / cellH; curCol = pos.x() / cellW; //kdDebug(12009) << e->state() << " " << LeftButton << " " << e->state()&LeftButton << endl; updateElement( oldCol, oldRow ); emit inputAt( curCol, curRow, e->button() ); } }
KBBGame::KBBGame() : KTopLevelWidget() { int i; setCaption(QString("KBlackBox ")+KBVERSION); menu = new KMenuBar(this, "menu"); QPopupMenu *game = new QPopupMenu; QPopupMenu *file = new QPopupMenu; sizesm = new QPopupMenu; ballsm = new QPopupMenu; QPopupMenu *help = new QPopupMenu; options = new QPopupMenu; CHECK_PTR( file ); CHECK_PTR( game ); CHECK_PTR( help ); CHECK_PTR( sizesm ); CHECK_PTR( ballsm ); CHECK_PTR( options ); CHECK_PTR( menu ); help->insertItem( trans->translate("&Help"), ID_HELP ); help->setAccel( CTRL+Key_H, ID_HELP ); help->insertSeparator(); help->insertItem( trans->translate("&About KBlackBox"), ID_ABOUT ); help->setAccel( CTRL+Key_A, ID_ABOUT ); help->insertItem( trans->translate("About &Qt"), ID_ABOUT_QT ); file->insertItem( trans->translate("&Quit"), ID_QUIT ); file->setAccel( CTRL+Key_Q, ID_QUIT ); game->insertItem( trans->translate("&New"), ID_NEW ); game->insertItem( trans->translate("&Give up"), ID_GIVEUP ); game->insertItem( trans->translate("&Done"), ID_DONE ); game->insertSeparator(); game->insertItem( trans->translate("&Resize"), ID_RESIZE ); sizes1id = sizesm->insertItem( " 8 x 8 ", this, SLOT(size1()) ); sizes2id = sizesm->insertItem( " 10 x 10 ", this, SLOT(size2()) ); sizes3id = sizesm->insertItem( " 12 x 12 ", this, SLOT(size3()) ); sizesm->setCheckable( TRUE ); balls1id = ballsm->insertItem( " 4 ", this, SLOT(balls1()) ); balls2id = ballsm->insertItem( " 6 ", this, SLOT(balls2()) ); balls3id = ballsm->insertItem( " 8 ", this, SLOT(balls3()) ); ballsm->setCheckable( TRUE ); options->insertItem( trans->translate("&Size"), sizesm ); options->insertItem( trans->translate("&Balls"), ballsm ); tut1id = options->insertItem( trans->translate("&Tutorial"), this, SLOT(tutorialSwitch()) ); options->setCheckable( TRUE ); connect( file, SIGNAL(activated(int)), SLOT(callBack(int)) ); connect( help, SIGNAL(activated(int)), SLOT(callBack(int)) ); connect( game, SIGNAL(activated(int)), SLOT(callBack(int)) ); menu->insertItem( trans->translate("&File"), file ); menu->insertItem( trans->translate("&Game"), game ); menu->insertItem( trans->translate("&Options"), options ); menu->insertSeparator(); menu->insertItem( trans->translate("&Help"), help ); menu->show(); setMenu( menu ); KIconLoader *loader = kapp->getIconLoader(); QPixmap **pix = new QPixmap*[NROFTYPES]; pix[0] = new QPixmap(); *pix[0] = loader->loadIcon( pFNames[0] ); if (!pix[0]->isNull()) { debug( "Pixmap \"%s\" loaded.", pFNames[0] ); for (i = 1; i < NROFTYPES; i++) { pix[i] = new QPixmap; *pix[i] = loader->loadIcon( pFNames[i] ); if (!pix[i]->isNull()) { debug( "Pixmap \"%s\" loaded.", pFNames[i] ); } else { pix[i] = pix[i-1]; pix[i]->detach(); debug( "Cannot find pixmap \"%s\". Using previous one.", pFNames[i] ); } } } else { debug( "Cannot find pixmap \"%s\". Pixmaps will not be loaded.", pFNames[0] ); delete pix[0]; delete pix; pix = NULL; } gr = new KBBGraphic( pix, this ); connect( gr, SIGNAL(inputAt(int,int,int)), this, SLOT(gotInputAt(int,int,int)) ); connect( this, SIGNAL(gameRuns(bool)), gr, SLOT(setInputAccepted(bool)) ); connect( gr, SIGNAL(endMouseClicked()), this, SLOT(gameFinished()) ); /* QToolTip::add( doneButton, trans->translate( "Click here when you think you placed all the balls.") ); */ QString tmps; stat = new KStatusBar( this ); tmps.sprintf( "aaaaaaaa" ); stat->insertItem( (const char *) tmps, SSCORE ); tmps.sprintf( "aaaaaaaaaaa" ); stat->insertItem( (const char *) tmps, SBALLS ); tmps.sprintf( "aaaaaaa" ); stat->insertItem( (const char *) tmps, SRUN ); tmps.sprintf( "aaaaaaaaaa" ); stat->insertItem( (const char *) tmps, SSIZE ); setStatusBar( stat ); tool = new KToolBar( this ); tool->insertButton( loader->loadIcon("exit.xpm"), ID_QUIT, TRUE, trans->translate("Quit") ); tool->insertButton( loader->loadIcon("reload.xpm"), ID_NEW, TRUE, trans->translate("New") ); tool->insertButton( loader->loadIcon("giveup.xpm"), ID_GIVEUP, TRUE, trans->translate("Give up") ); tool->insertButton( loader->loadIcon("done.xpm"), ID_DONE, TRUE, trans->translate("Done") ); tool->insertSeparator(); tool->insertButton( loader->loadIcon("help.xpm"), ID_HELP, TRUE, trans->translate("Help") ); connect( tool, SIGNAL(clicked(int)), SLOT(callBack(int)) ); addToolBar( tool ); tool->setBarPos( KToolBar::Top ); tool->show(); /* Game initializations */ running = FALSE; gameBoard = NULL; KConfig *kConf; int j; kConf = KApplication::getKApplication()->getConfig(); kConf->setGroup( "KBlackBox Setup" ); if (kConf->hasKey( "Balls" )) { i = kConf->readNumEntry( "Balls" ); balls = i; switch (i) { case 4: ballsm->setItemChecked( balls1id, TRUE ); break; case 6: ballsm->setItemChecked( balls2id, TRUE ); break; case 8: ballsm->setItemChecked( balls3id, TRUE ); break; } } else { balls = 4; ballsm->setItemChecked( balls1id, TRUE ); } if ((kConf->hasKey( "Width" )) && (kConf->hasKey( "Balls" ))) { i = kConf->readNumEntry( "Width" ); j = kConf->readNumEntry( "Height" ); gr->setSize( i+4, j+4 ); // +4 is the space for "lasers" and an edge... gameBoard = new RectOnArray( gr->numC(), gr->numR() ); switch (i) { case 8: sizesm->setItemChecked( sizes1id, TRUE ); break; case 10: sizesm->setItemChecked( sizes2id, TRUE ); break; case 12: sizesm->setItemChecked( sizes3id, TRUE ); break; } } else { gr->setSize( 8+4, 8+4 ); // +4 is the space for "lasers" and an edge... gameBoard = new RectOnArray( gr->numC(), gr->numR() ); sizesm->setItemChecked( sizes1id, TRUE ); } if (kConf->hasKey( "tutorial" )) { tutorial = (bool) kConf->readNumEntry( "tutorial" ); } else tutorial = FALSE; options->setItemChecked( tut1id, tutorial ); QString s, s1; int pos; setMinSize(); gameResize(); if (kConf->hasKey( "appsize" )) { s = kConf->readEntry( "appsize" ); debug("%s", (const char *) s); pos = s.find( 'x' ); s1 = s.right( s.length() - pos - 1 ); s.truncate( pos - 1 ); // debug("%s", (const char *) s); // debug("%s", (const char *) s1); resize( s.toInt(), s1.toInt() ); } setScore( 0 ); ballsPlaced = 0; updateStats(); setView( gr ); newGame(); }