Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

kpacman.cpp

Go to the documentation of this file.
00001 
00002 #include "portable.h"
00003 
00004 #if defined( KDE2_PORT )
00005 #include <kpacman.h>
00006 #include <kpacman.moc>
00007 #include <kcolordlg.h>
00008 #elif defined( QPE_PORT )
00009 #include <qmenubar.h>
00010 #include <qpe/config.h>
00011 #include <qapplication.h>
00012 #include "kpacman.h"
00013 #endif
00014 
00015 
00016 Kpacman::Kpacman(QWidget *parent, const char *name, WFlags fl)
00017         : KTMainWindow(parent, name,fl)
00018 {
00019     schemesPopup = new QList<QPopupMenu>;
00020     schemesPopup->setAutoDelete(TRUE);
00021 
00022     menu();
00023 
00024         m_view = new QWidget( this, "m_view" );
00025     m_view->setBackgroundColor( black );
00026         m_layout = new QGridLayout( m_view );
00027         m_layout->setMargin( 7 );
00028 
00029         view = new KpacmanWidget( this, QString(name)+"widget");
00030         m_layout->addWidget( view, 0, 0 );
00031 
00032     setCaption( tr("KPacman") );
00033 
00034     view->referee->setFocus();
00035 
00036     connect(view->referee, SIGNAL(setScore(int,int)),
00037             view->score, SLOT(setScore(int,int)));
00038     connect(view->referee, SIGNAL(setPoints(int)),
00039             view->score, SLOT(set(int)));
00040     connect(view->referee, SIGNAL(setLifes(int)),
00041             view->status, SLOT(setLifes(int)));
00042     connect(view->referee, SIGNAL(setLevel(int)),
00043             view->status, SLOT(setLevel(int)));
00044     connect(view->referee, SIGNAL(forcedHallOfFame(bool)),
00045             this, SLOT(forcedHallOfFame(bool)));
00046     connect(view->referee, SIGNAL(togglePaused()), this, SLOT(togglePaused()));
00047     connect(view->referee, SIGNAL(toggleNew()), this, SLOT(toggleNew()));
00048 
00049     connect(view->score, SIGNAL(toggleNew()), this, SLOT(toggleNew()));
00050     connect(view->score, SIGNAL(forcedHallOfFame(bool)),
00051             this, SLOT(forcedHallOfFame(bool)));
00052 
00053     APP_CONFIG_BEGIN( cfg );
00054     focusOutPause = !cfg->readBoolEntry("FocusOutPause", TRUE);
00055     focusInContinue = !cfg->readBoolEntry("FocusInContinue", TRUE);
00056     hideMouseCursor = !cfg->readBoolEntry("HideMouseCursor", TRUE);
00057     APP_CONFIG_END( cfg );
00058 
00059     toggleFocusOutPause();
00060     toggleFocusInContinue();
00061     toggleHideMouseCursor();
00062 
00063     setCentralWidget( m_view );
00064 }
00065 
00066 Kpacman::~Kpacman()
00067 {
00068   /* APP_CONFIG_BEGIN( cfg );
00069     cfg->writeEntry("FocusOutPause", focusOutPause);
00070     cfg->writeEntry("FocusInContinue", focusInContinue);
00071     cfg->writeEntry("HideMouseCursor", hideMouseCursor);
00072     APP_CONFIG_END( cfg );
00073   */
00074     delete _menuBar;
00075 }
00076 
00077 void Kpacman::menu()
00078 {
00079     gamePopup = new QPopupMenu();
00080     CHECK_PTR( gamePopup );
00081     newID = gamePopup->insertItem(tr("&New"), this, SLOT(newKpacman()),Key_F2);
00082     pauseID = gamePopup->insertItem(tr("&Pause"),
00083                                     this, SLOT(pauseKpacman()), Key_F3);
00084     hofID = gamePopup->insertItem(tr("&Hall of fame"),
00085                                     this, SLOT(toggleHallOfFame()), Key_F4);
00086     gamePopup->insertSeparator();
00087     gamePopup->insertItem(tr("&Quit"), this, SLOT(quitKpacman()), CTRL+Key_Q);
00088     gamePopup->setCheckable(TRUE);
00089 
00090     optionsPopup = new QPopupMenu();
00091     CHECK_PTR(optionsPopup);
00092 
00093     modesPopup = new QPopupMenu();
00094     CHECK_PTR(modesPopup);
00095 
00096     hideMouseCursorID = optionsPopup->insertItem(tr("&Hide Mousecursor"),
00097                                                  this, SLOT(toggleHideMouseCursor()),
00098                                                  CTRL+Key_H);
00099     optionsPopup->insertSeparator();
00100 
00101     if (lookupSchemes() > 0) {
00102         optionsPopup->insertItem(tr("&Select graphic scheme"),  modesPopup);
00103         optionsPopup->insertSeparator();
00104     }
00105 
00106     focusOutPauseID = optionsPopup->insertItem(tr("&Pause in Background"),
00107                                                this, SLOT(toggleFocusOutPause()));
00108     focusInContinueID = optionsPopup->insertItem(tr("&Continue in Foreground"),
00109                                               this, SLOT(toggleFocusInContinue()));
00110     optionsPopup->insertSeparator();
00111 
00112     optionsPopup->insertItem(tr("Change &keys..."), this, SLOT(confKeys()));
00113 
00114 #ifndef QPE_PORT
00115     QString aboutText = tr("@PACKAGE@ - @VERSION@\n\n"
00116                              "Joerg Thoennissen (joe@dsite.de)\n\n"
00117                              "A pacman game for the KDE Desktop\n\n"
00118                              "The program based on the source of ksnake\n"
00119                              "by Michel Filippi (mfilippi@sade.rhein-main.de).\n"
00120                              "The design was strongly influenced by the pacman\n"
00121                              "(c) 1980 MIDWAY MFG.CO.\n\n"
00122                              "I like to thank my girlfriend Elke Krueers for\n"
00123                              "the last 10 years of her friendship.\n");
00124     aboutText.replace(QRegExp("@PACKAGE@"), PACKAGE);
00125     aboutText.replace(QRegExp("@VERSION@"), VERSION);
00126     QPopupMenu *helpPopup = helpMenu(aboutText, FALSE);
00127 #endif
00128 
00129     //_menuBar = new KMenuBar(this);
00130     //CHECK_PTR( _menuBar );
00131     //_menuBar->insertItem(tr("&Game"), gamePopup);
00132     //_menuBar->insertItem(tr("&Options"), optionsPopup);
00133     //_menuBar->insertSeparator();
00134 #ifndef QPE_PORT
00135     _menuBar->insertItem(tr("&Help"), helpPopup);
00136 #endif
00137 }
00138 
00139 int Kpacman::lookupSchemes()
00140 {
00141     APP_CONFIG_BEGIN( cfg );
00142     int ModeCount = cfg->readNumEntry("ModeCount", 0);
00143     int Mode = cfg->readNumEntry("Mode", 0);
00144     int SchemeCount = cfg->readNumEntry("SchemeCount", 0);
00145     int Scheme = cfg->readNumEntry("Scheme", 0);
00146 
00147         /*
00148     if (SchemeCount == 0 || Scheme == -1) {
00149         QMessageBox::warning(this, tr("Configuration Error"),
00150                                    tr("There are no schemes defined,\n"
00151                                         "or no scheme is selected."));
00152         APP_CONFIG_END( cfg );
00153         return 0;
00154     }
00155         */
00156 
00157     connect(modesPopup, SIGNAL(activated(int)), this, SLOT(schemeChecked(int)));
00158     modeID.resize(ModeCount > 0 ? ModeCount : 0);
00159 
00160     if (!schemesPopup->isEmpty())
00161         schemesPopup->clear();
00162 
00163     SAVE_CONFIG_GROUP( cfg, oldgroup );
00164 
00165     QString ModeGroup;
00166     QString ModeName;
00167 
00168     for (int m = 0; m < ModeCount; m++) {
00169         ModeGroup.sprintf("Mode %d", m);
00170         cfg->setGroup(ModeGroup);
00171 
00172         ModeName = cfg->readEntry("Description", ModeGroup);
00173 
00174         QPopupMenu *p = new QPopupMenu;
00175         p->setCheckable(TRUE);
00176         connect(p, SIGNAL(activated(int)), this, SLOT(schemeChecked(int)));
00177         schemesPopup->append(p);
00178 
00179         modeID[m] = modesPopup->insertItem(ModeName, schemesPopup->at(m));
00180         modesPopup->setItemEnabled(modeID[m], FALSE);
00181         modesPopup->setItemChecked(modeID[m], m == Mode);
00182     }
00183 
00184     schemeID.resize(SchemeCount);
00185     schemeMode.resize(SchemeCount);
00186 
00187     QString SchemeGroup;
00188     QString SchemeName;
00189     int SchemeMode;
00190 
00191     for (int i = 0; i < SchemeCount; i++) {
00192         SchemeGroup.sprintf("Scheme %d", i);
00193         cfg->setGroup(SchemeGroup);
00194 
00195         SchemeName = cfg->readEntry("Description", SchemeGroup);
00196         SchemeMode = cfg->readNumEntry("Mode", -1);
00197 
00198         schemeMode[i] = SchemeMode;
00199         if (SchemeMode == -1) {
00200             schemeID[i] = modesPopup->insertItem(SchemeName);
00201             modesPopup->setItemChecked(schemeID[i], i == Scheme);
00202         } else {
00203             schemeID[i] = schemesPopup->at(SchemeMode)->insertItem(SchemeName);
00204             schemesPopup->at(SchemeMode)->
00205                 setItemChecked(schemeID[i], i == Scheme);
00206             modesPopup->setItemEnabled(modeID[SchemeMode], TRUE);
00207         }
00208     }
00209 
00210     RESTORE_CONFIG_GROUP( cfg, oldgroup );
00211 
00212     APP_CONFIG_END( cfg );
00213     return SchemeCount;
00214 }
00215 
00216 void Kpacman::quitKpacman()
00217 {
00218     APP_QUIT();
00219 }
00220 
00221 void Kpacman::newKpacman()
00222 {
00223     if (!gamePopup->isItemEnabled(hofID))
00224         gamePopup->setItemEnabled(hofID, TRUE);
00225 
00226     if (gamePopup->isItemChecked(hofID))
00227         toggleHallOfFame();
00228 
00229     if (gamePopup->isItemChecked(pauseID))
00230         pauseKpacman();
00231 
00232     view->referee->play();
00233 }
00234 
00235 void Kpacman::pauseKpacman()
00236 {
00237     view->referee->pause();
00238     view->score->setPause(gamePopup->isItemChecked(pauseID));
00239 }
00240 
00241 void Kpacman::toggleHallOfFame()
00242 {
00243     gamePopup->setItemChecked(hofID, !gamePopup->isItemChecked(hofID));
00244     view->referee->toggleHallOfFame();
00245 
00246     if (gamePopup->isItemChecked(hofID)) {
00247         view->referee->lower();
00248         view->status->lower();
00249     } else {
00250         view->status->raise();
00251         view->referee->raise();
00252         view->referee->setFocus();
00253     }
00254 }
00255 
00256 /*
00257  * Disable or enable the "Hall of fame"-menuitem if the referee says so.
00258  * This is done, to disable turning off the "hall of fame"-display, in the automated
00259  * sequence of displaying the introduction, the demonstration (or playing) and the
00260  * hall of fame.
00261  * If on == TRUE then also lower the referee and the status widgets.
00262  */
00263 void Kpacman::forcedHallOfFame(bool on)
00264 {
00265     if (!on && !gamePopup->isItemChecked(hofID))
00266         return;
00267 
00268     gamePopup->setItemEnabled(hofID, !on);
00269     gamePopup->setItemChecked(hofID, on);
00270 
00271     view->referee->toggleHallOfFame();
00272     if (on) {
00273         view->referee->lower();
00274         view->status->lower();
00275     } else {
00276         view->status->raise();
00277         view->referee->raise();
00278         view->referee->setFocus();
00279         view->referee->intro();
00280     }
00281 }
00282 
00283 void Kpacman::togglePaused()
00284 {
00285     static bool checked = FALSE;
00286     checked = !checked;
00287     gamePopup->setItemChecked( pauseID, checked );
00288     view->score->setPause(gamePopup->isItemChecked(pauseID));
00289 }
00290 
00291 /*
00292  * This disables the "New Game" menuitem to prevent interruptions of the current
00293  * play.
00294  */
00295 void Kpacman::toggleNew()
00296 {
00297     gamePopup->setItemEnabled(newID, !gamePopup->isItemEnabled(newID));
00298 }
00299 
00300 void Kpacman::toggleHideMouseCursor()
00301 {
00302     hideMouseCursor = !hideMouseCursor;
00303     optionsPopup->setItemChecked(hideMouseCursorID, hideMouseCursor);
00304     if (hideMouseCursor)
00305         view->setCursor(blankCursor);
00306     else
00307         view->setCursor(arrowCursor);
00308 }
00309 
00310 void Kpacman::toggleFocusOutPause()
00311 {
00312     focusOutPause = !focusOutPause;
00313     optionsPopup->setItemChecked(focusOutPauseID, focusOutPause);
00314     view->referee->setFocusOutPause(focusOutPause);
00315 }
00316 
00317 void Kpacman::toggleFocusInContinue()
00318 {
00319     focusInContinue = !focusInContinue;
00320     optionsPopup->setItemChecked(focusInContinueID, focusInContinue);
00321     view->referee->setFocusInContinue(focusInContinue);
00322 }
00323 
00324 void Kpacman::confKeys()
00325 {
00326     Keys *keys = new Keys();
00327     if (keys->exec() == QDialog::Accepted) {
00328         view->referee->initKeys();
00329         view->score->initKeys();
00330     }
00331     delete keys;
00332 }
00333 
00334 void Kpacman::schemeChecked(int id)
00335 {
00336     int mode = 0, scheme = -1;
00337 
00338     for (uint s = 0; s < schemeID.size(); s++) {
00339         if (schemeID[s] == id) {
00340             scheme = s;
00341             mode = schemeMode[s];
00342         }
00343         if (schemeMode[s] == -1) {
00344                                                 modesPopup->setItemChecked(schemeID[s], schemeID[s] == id);
00345                                 } else {
00346                                                 modesPopup->setItemChecked(modeID[schemeMode[s]], schemeMode[s] == mode);
00347                                                 schemesPopup->at(schemeMode[s])->setItemChecked(schemeID[s], schemeID[s] == id);
00348                                 }
00349                 }
00350     /*
00351     APP_CONFIG_BEGIN( cfg );
00352     cfg->writeEntry("Scheme", scheme);
00353     cfg->writeEntry("Mode", mode);
00354     APP_CONFIG_END( cfg );
00355     */
00356 
00357     view->setScheme(scheme, mode);
00358     view->updateGeometry();
00359     updateGeometry();
00360     update();
00361     repaint(TRUE);
00362     show();
00363 }

Generated on Sat Nov 5 16:17:21 2005 for OPIE by  doxygen 1.4.2