• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kgoldrunner

kgrdialog.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002     Copyright 2003 Marco Krüger <grisuji@gmx.de>
00003     Copyright 2003 Ian Wadham <ianw2@optusnet.com.au>
00004 *                                                                         *
00005 *   This program is free software; you can redistribute it and/or modify  *
00006 *   it under the terms of the GNU General Public License as published by  *
00007 *   the Free Software Foundation; either version 2 of the License, or     *
00008 *   (at your option) any later version.                                   *
00009 ***************************************************************************/
00010 
00011 #include "kgrdialog.h"
00012 
00013 #include "kgrconsts.h"
00014 #include "kgrcanvas.h"
00015 #include "kgrgame.h"
00016 
00017 #include <KGlobalSettings>
00018 #include <QTextStream>
00019 #include <QGridLayout>
00020 #include <QLabel>
00021 #include <QVBoxLayout>
00022 #include <QSpacerItem>
00023 #include <QHeaderView>
00024 
00025 /******************************************************************************/
00026 /*****************    DIALOG BOX TO SELECT A GAME AND LEVEL   *****************/
00027 /******************************************************************************/
00028 
00029 KGrSLDialog::KGrSLDialog (int action, int requestedLevel, int collnIndex,
00030                         QList<KGrCollection *> & gamesList, KGrGame * theGame,
00031                         QWidget * parent)
00032                 : KDialog (parent)
00033 {
00034     slAction     = action;
00035     defaultLevel = requestedLevel;
00036     defaultGame  = collnIndex;
00037     collections  = gamesList;
00038     game         = theGame;
00039     collection   = collections.at (defaultGame);
00040     slParent     = parent;
00041 
00042     int margin      = marginHint(); 
00043     int spacing     = spacingHint(); 
00044     QWidget * dad   = new QWidget (this);
00045     setMainWidget (dad);
00046     setCaption (i18n ("Select Game"));
00047     setButtons (KDialog::Ok | KDialog::Cancel | KDialog::Help);
00048     setDefaultButton (KDialog::Ok);
00049 
00050     QVBoxLayout * mainLayout = new QVBoxLayout (dad);
00051     mainLayout->setSpacing (spacing);
00052     mainLayout->setMargin (margin);
00053 
00054     collnL    = new QLabel
00055                 (i18n ("<html><b>Please select a game:</b></html>"), dad);
00056     mainLayout->addWidget (collnL, 5);
00057 
00058     colln     = new QTreeWidget (dad);
00059     mainLayout->addWidget (colln, 50);
00060     colln->setColumnCount (4);
00061     colln->setHeaderLabels (QStringList() <<
00062                             i18n ("Name of Game") <<
00063                             i18n ("Rules") <<
00064                             i18n ("Levels") <<
00065                             i18n ("Skill"));
00066     colln->setRootIsDecorated (false);
00067 
00068     QHBoxLayout * hboxLayout1 = new QHBoxLayout();
00069     hboxLayout1->setSpacing (6);
00070     hboxLayout1->setMargin (0);
00071 
00072     collnN    = new QLabel ("", dad);   // Name of selected collection.
00073     QFont f = collnN->font();
00074     f.setBold (true);
00075     collnN->setFont (f);
00076     hboxLayout1->addWidget (collnN);
00077 
00078     QSpacerItem * spacerItem1 = new QSpacerItem
00079                         (21, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
00080     hboxLayout1->addItem (spacerItem1);
00081 
00082     collnD    = new QLabel ("", dad);       // Description of collection.
00083     hboxLayout1->addWidget (collnD);
00084     mainLayout->addLayout (hboxLayout1, 5);
00085 
00086     collnAbout = new QTextEdit (dad);
00087     collnAbout->setReadOnly (true);
00088     mainLayout->addWidget (collnAbout, 25);
00089 
00090     QFrame * separator = new QFrame (dad);
00091     separator->setFrameShape (QFrame::HLine);
00092     mainLayout->addWidget (separator);
00093 
00094     if ((action == SL_START) || (action == SL_UPD_GAME)) {
00095         dad->   setWindowTitle (i18n ("Select Game"));
00096         QLabel * startMsg = new QLabel
00097             ("<b>" + i18n ("Level 1 of the selected game is:") + "</b>", dad);
00098         mainLayout->addWidget (startMsg, 5);
00099     }
00100     else {
00101         dad->   setWindowTitle (i18n ("Select Game/Level"));
00102         QLabel * selectLev = new QLabel
00103             ("<b>" + i18n ("Please select a level:") + "</b>", dad);
00104         mainLayout->addWidget (selectLev, 5);
00105     }
00106 
00107     QGridLayout * grid = new QGridLayout;
00108     mainLayout->addLayout (grid);
00109 
00110     number    = new QScrollBar (Qt::Vertical, dad); // IDW
00111     number->setRange (1, 150);
00112     number->setSingleStep (1);
00113     number->setPageStep (10);
00114     number->setValue (1);
00115     grid->addWidget (number, 1, 5, 4, 1);
00116 
00117     QWidget * numberPair = new QWidget (dad);
00118     QHBoxLayout *hboxLayout2 = new QHBoxLayout (numberPair);
00119     hboxLayout2->setMargin (0);
00120     numberPair->setLayout (hboxLayout2);
00121     grid->addWidget (numberPair, 1, 1, 1, 3);
00122     numberL   = new QLabel (i18n ("Level number:"), numberPair);
00123     display   = new QSpinBox (numberPair);
00124     display->setRange (1, 150);
00125     hboxLayout2->addWidget (numberL);
00126     hboxLayout2->addWidget (display);
00127 
00128     levelNH   = new QPushButton (i18n ("Edit Level Name && Hint"), dad);
00129     mainLayout->addWidget (levelNH);
00130 
00131     slName    = new QLabel ("", dad);
00132     grid->addWidget (slName, 2, 1, 1, 4);
00133     thumbNail = new KGrThumbNail (dad);
00134     grid->addWidget (thumbNail, 1, 6, 4, 5);
00135 
00136     // Set thumbnail cell size to about 1/5 of game cell size.
00137     int cellSize = parent->width() / (5 * (FIELDWIDTH + 4));
00138     cellSize = (cellSize < 4) ? 4 : cellSize;
00139     thumbNail-> setFixedWidth  ((FIELDWIDTH  * cellSize) + 2);
00140     thumbNail-> setFixedHeight ((FIELDHEIGHT * cellSize) + 2);
00141 
00142     // Base the geometry of the dialog box on the playing area.
00143     int cell = parent->width() / (FIELDWIDTH + 4);
00144     dad->   setMinimumSize ((FIELDWIDTH*cell/2), (FIELDHEIGHT-3)*cell);
00145 
00146     // Set the default for the level-number in the scrollbar.
00147     number->    setTracking (true);
00148     number->setValue (requestedLevel);
00149 
00150     slSetCollections (defaultGame);
00151 
00152     // Vary the dialog according to the action.
00153     QString s1 = i18nc ("Default action at startup of game", "PLAY");
00154     QString s2 = i18nc ("Alternate action at startup of game", "Select Level");
00155     QString s3 = i18nc ("Alternate action at startup of game", "Use Menu");
00156 
00157     QString OKText = "";
00158     switch (slAction) {
00159     case SL_START:  // Must start at level 1, but can choose a collection.
00160                         OKText = i18n ("Start Game");
00161                         number->setValue (1);
00162                         number->setEnabled (false);
00163                         display->setEnabled (false);
00164                         number->hide();
00165                         numberL->hide();
00166                         display->hide();
00167                         break;
00168     case SL_ANY:    // Can start playing at any level in any collection.
00169                         OKText = i18n ("Play Level");
00170                         break;
00171     case SL_UPDATE: // Can use any level in any collection as edit input.
00172                         OKText = i18n ("Edit Level");
00173                         break;
00174     case SL_CREATE: // Can save a new level only in a USER collection.
00175                         OKText = i18n ("Save New");
00176                         break;
00177     case SL_SAVE:   // Can save an edited level only in a USER collection.
00178                         OKText = i18n ("Save Change");
00179                         break;
00180     case SL_DELETE: // Can delete a level only in a USER collection.
00181                         OKText = i18n ("Delete Level");
00182                         break;
00183     case SL_MOVE:   // Can move a level only into a USER collection.
00184                         OKText = i18n ("Move To...");
00185                         break;
00186     case SL_UPD_GAME:   // Can only edit USER collection details.
00187                         OKText = i18n ("Edit Game Info");
00188                         number->setValue (1);
00189                         number->setEnabled (false);
00190                         display->setEnabled (false);
00191                         number->hide();
00192                         numberL->hide();
00193                         display->hide();
00194                         break;
00195 
00196     default:        break;          // Keep the default settings.
00197     }
00198     if (!OKText.isEmpty()) {
00199         setButtonGuiItem (KDialog::Ok, KGuiItem (OKText));
00200     }
00201 
00202     // Set value in the line-edit box.
00203     slShowLevel (number->value());
00204 
00205     if (display->isEnabled()) {
00206         display->setFocus();            // Set the keyboard input on.
00207         display->selectAll();
00208     }
00209 
00210     // Paint a thumbnail sketch of the level.
00211     thumbNail->setFrameStyle (QFrame::Box | QFrame::Plain);
00212     thumbNail->setLineWidth (1);
00213     slPaintLevel();
00214     thumbNail->show();
00215 
00216     connect (colln,   SIGNAL (itemSelectionChanged()), this, SLOT (slColln()));
00217 
00218     connect (display, SIGNAL (valueChanged (const QString &)),
00219                 this, SLOT (slUpdate (const QString &)));
00220 
00221     connect (number, SIGNAL(valueChanged (int)), this, SLOT(slShowLevel (int)));
00222 
00223     // Only enable name and hint dialog here if saving a new or edited level.
00224     // At other times the name and hint have not been loaded or initialised yet.
00225     if ((slAction == SL_CREATE) || (slAction == SL_SAVE)) {
00226         connect (levelNH,  SIGNAL (clicked()), game, SLOT (editNameAndHint()));
00227     }
00228     else {
00229         levelNH->setEnabled (false);
00230         levelNH->hide();
00231     }
00232 
00233     connect (colln, SIGNAL(itemSelectionChanged()), this, SLOT(slPaintLevel()));
00234     connect (number,  SIGNAL (sliderReleased()), this, SLOT (slPaintLevel()));
00235 
00236     connect (this,    SIGNAL (helpClicked()), this, SLOT (slotHelp()));
00237 }
00238 
00239 KGrSLDialog::~KGrSLDialog()
00240 {
00241 }
00242 
00243 /******************************************************************************/
00244 /*****************    LOAD THE LIST OF GAMES (COLLECTIONS)    *****************/
00245 /******************************************************************************/
00246 
00247 void KGrSLDialog::slSetCollections (int cIndex)
00248 {
00249     int i;
00250     int imax = collections.count();
00251 
00252     // Set values in the list box that holds details of available games.
00253     // The list is displayed in order of skill then the kind of rules.
00254     colln->clear();
00255     slCollnIndex = -1;
00256 
00257     QList<char> sortOrder1, sortOrder2;     // Crude, but effective.
00258     sortOrder1 << 'N' << 'C' << 'T';
00259     sortOrder2 << 'T' << 'K';
00260 
00261     foreach (char sortItem1, sortOrder1) {
00262         foreach (char sortItem2, sortOrder2) {
00263             for (i = 0; i < imax; i++) {
00264                 if ((collections.at (i)->skill == sortItem1) &&
00265                     (collections.at (i)->settings == sortItem2)) {
00266                     QStringList data;
00267                     data
00268                         << collections.at (i)->name
00269                         << ((collections.at (i)->settings == 'K') ? 
00270                             i18nc ("Rules", "KGoldrunner") :
00271                             i18nc ("Rules", "Traditional"))
00272                         << QString().setNum (collections.at (i)->nLevels)
00273                         << ((collections.at (i)->skill == 'T') ? 
00274                             i18nc ("Skill Level", "Tutorial") :
00275                             ((collections.at (i)->skill == 'N') ? 
00276                             i18nc ("Skill Level", "Normal") :
00277                             i18nc ("Skill Level", "Championship")));
00278                     KGrGameListItem * thisGame = new KGrGameListItem (data, i);
00279                     colln->addTopLevelItem (thisGame);
00280 
00281                     if (slCollnIndex < 0) {
00282                         slCollnIndex = i; // There is at least one collection.
00283                     }
00284                     if (i == cIndex) {
00285                         // Mark the currently selected collection (default 0).
00286                         colln->setCurrentItem (thisGame);
00287                     }
00288                 }
00289             } // End "for" loop.
00290         }
00291     }
00292 
00293     if (slCollnIndex < 0) {
00294         return;             // There are no collections (unlikely).
00295     }
00296 
00297     // Fetch and display information on the selected collection.
00298     slColln();
00299 
00300     // Make the column for the game's name a bit wider.
00301     colln->header()->setResizeMode (0, QHeaderView::ResizeToContents);
00302     colln->header()->setResizeMode (1, QHeaderView::ResizeToContents);
00303     colln->header()->setResizeMode (2, QHeaderView::ResizeToContents);
00304 }
00305 
00306 /******************************************************************************/
00307 /*****************    SLOTS USED BY LEVEL SELECTION DIALOG    *****************/
00308 /******************************************************************************/
00309 
00310 void KGrSLDialog::slColln()
00311 {
00312 
00313     if (slCollnIndex < 0) {
00314         // Ignore the "highlighted" signal caused by inserting in an empty box.
00315         return;
00316     }
00317 
00318     if (colln->selectedItems().size() <= 0) {
00319         return;
00320     }
00321 
00322     slCollnIndex = (dynamic_cast<KGrGameListItem *>
00323                         (colln->selectedItems().first()))->id();
00324     int n = slCollnIndex;               // Collection selected.
00325     int N = defaultGame;                // Current collection.
00326     if (collections.at (n)->nLevels > 0) {
00327         number->setMaximum (collections.at (n)->nLevels);
00328         display->setMaximum (collections.at (n)->nLevels);
00329     }
00330     else {
00331         number->setMaximum (1);         // Avoid range errors.
00332         display->setMaximum (1);
00333     }
00334 
00335     // Set a default level number for the selected collection.
00336     switch (slAction) {
00337     case SL_ANY:
00338     case SL_UPDATE:
00339     case SL_DELETE:
00340     case SL_UPD_GAME:
00341         // If selecting the current collection, use the current level number.
00342         if (n == N)
00343             number->setValue (defaultLevel);
00344         else
00345             number->setValue (1);           // Else use level 1.
00346         break;
00347     case SL_CREATE:
00348     case SL_SAVE:
00349     case SL_MOVE:
00350         if ((n == N) && (slAction != SL_CREATE)) {
00351             // Saving/moving level in current collection: use current number.
00352             number->setValue (defaultLevel);
00353         }
00354         else {
00355             // Saving new/edited level or relocating a level: use "nLevels + 1".
00356             number->setMaximum (collections.at (n)->nLevels + 1);
00357             display->setMaximum (collections.at (n)->nLevels + 1);
00358             number->setValue (number->maximum());
00359         }
00360         break;
00361     default:
00362         number->setValue (1);               // Default is level 1.
00363         break;
00364     }
00365 
00366     slShowLevel (number->value());
00367 
00368     int levCnt = collections.at (n)->nLevels;
00369     if (collections.at (n)->settings == 'K')
00370         collnD->setText (i18np ("1 level, uses KGoldrunner rules.",
00371                                 "%1 levels, uses KGoldrunner rules.", levCnt));
00372     else
00373         collnD->setText (i18np ("1 level, uses Traditional rules.",
00374                                 "%1 levels, uses Traditional rules.", levCnt));
00375     collnN->setText (collections.at (n)->name);
00376     QString s;
00377     if (collections.at (n)->about.isEmpty()) {
00378         s = i18n ("Sorry, there is no further information about this game.");
00379     }
00380     else {
00381         s = (i18n (collections.at (n)->about.toUtf8().constData()));
00382     } 
00383     collnAbout->setText (s);
00384 }
00385 
00386 void KGrSLDialog::slShowLevel (int i)
00387 {
00388     // Display the level number as the slider is moved.
00389     display->setValue (i);
00390 }
00391 
00392 void KGrSLDialog::slUpdate (const QString & text)
00393 {
00394     // Move the slider when a valid level number is entered.
00395     QString s = text;
00396     bool ok = false;
00397     int n = s.toInt (&ok);
00398     if (ok) {
00399         number->setValue (n);
00400         slPaintLevel();
00401     }
00402     else
00403         KGrMessage::information (this, i18n ("Select Level"),
00404                 i18n ("This level number is not valid. It can not be used."));
00405 }
00406 
00407 void KGrSLDialog::slPaintLevel()
00408 {
00409     // Repaint the thumbnail sketch of the level whenever the level changes.
00410     int n = slCollnIndex;
00411     if (n < 0) {
00412         return;                 // Owner has no collections.
00413     }
00414     // Fetch level-data and save layout, name and label in the thumbnail.
00415     QString dir = game->getDirectory (collections.at (n)->owner);
00416     thumbNail->setLevelData (dir, collections.at (n)->prefix,
00417                                 number->value(), slName);
00418     thumbNail->repaint();           // Will call "paintEvent (e)".
00419 }
00420 
00421 void KGrSLDialog::slotHelp()
00422 {
00423     // Help for "Select Game and Level" dialog box.
00424     QString s =
00425         i18n ("The main button at the bottom echoes the "
00426         "menu action you selected. Click it after choosing "
00427         "a game and level - or use \"Cancel\".");
00428 
00429     if (slAction == SL_START) {
00430         s += i18n ("\n\nIf this is your first time in KGoldrunner, select the "
00431             "tutorial game or click \"Cancel\" and click that item in "
00432             "the Game or Help menu. The tutorial game gives you hints "
00433             "as you go.\n\n"
00434             "Otherwise, just click on the name of a game (in the list box), "
00435             "then, to start at level 001, click on the main button at the "
00436             "bottom. Play begins when you move the mouse or press a key.");
00437    }
00438    else {
00439         switch (slAction) {
00440         case SL_UPDATE:
00441             s += i18n ("\n\nYou can select System levels for editing (or "
00442                 "copying), but you must save the result in a game you have "
00443                 "created. Use the mouse as a paintbrush and the editor "
00444                 "toolbar buttons as a palette. Use the 'Empty Space' button "
00445                 "to erase.");
00446             break;
00447         case SL_CREATE:
00448             s += i18n("\n\nYou can add a name and hint to your new level here, "
00449                 "but you must save the level you have created into one of "
00450                 "your own games. By default your new level will go at the "
00451                 "end of your game, but you can also select a level number and "
00452                 "save into the middle of your game.");
00453             break;
00454         case SL_SAVE:
00455             s += i18n("\n\nYou can create or edit a name and hint here, before "
00456                 "saving. If you change the game or level, you can do a copy "
00457                 "or \"Save As\", but you must always save into one of your "
00458                 "own games. If you save a level into the middle of a series, "
00459                 "the other levels are automatically re-numbered.");
00460             break;
00461         case SL_DELETE:
00462             s += i18n ("\n\nYou can only delete levels from one of your own "
00463                 "games. If you delete a level from the middle of a series, "
00464                 "the other levels are automatically re-numbered.");
00465             break;
00466         case SL_MOVE:
00467             s += i18n ("\n\nTo move (re-number) a level, you must first select "
00468                 "it by using \"Edit Any Level...\", then you can use "
00469                 "\"Move Level...\" to assign it a new number or even a different "
00470                 "game. Other levels are automatically re-numbered as "
00471                 "required. You can only move levels within your own games.");
00472             break;
00473         case SL_UPD_GAME:
00474             s += i18n ("\n\nWhen editing game info you only need to choose a "
00475                 "game, then you can go to a dialog where you edit the "
00476                 "details of the game.");
00477             break;
00478         default:
00479             break;
00480         }
00481         s += i18n ("\n\nClick on the list box to choose a game.  "
00482             "Below the list box you can see more information about the "
00483             "selected game, including how many levels there are and what "
00484             "rules the enemies follow (see the Settings menu).\n\n"
00485             "You select "
00486             "a level number by typing it or using the scroll bar.  As "
00487             "you vary the game or level, the thumbnail area shows a "
00488             "preview of your choice.");
00489    }
00490 
00491    KGrMessage::information (slParent, i18n ("Help: Select Game & Level"), s);
00492 }
00493 
00494 /*******************************************************************************
00495 *************** DIALOG BOX TO CREATE/EDIT A LEVEL NAME AND HINT ****************
00496 *******************************************************************************/
00497 
00498 KGrNHDialog::KGrNHDialog (const QString & levelName, const QString & levelHint,
00499                         QWidget * parent)
00500                 : KDialog (parent)
00501 {
00502     setCaption (i18n ("Edit Name & Hint"));
00503     setButtons (KDialog::Ok | KDialog::Cancel);
00504     setDefaultButton (KDialog::Ok);
00505     int margin      = marginHint();
00506     int spacing     = spacingHint();
00507     QWidget * dad   = new QWidget (this);
00508     setMainWidget (dad);
00509 
00510     QVBoxLayout * mainLayout = new QVBoxLayout (dad);
00511     mainLayout->setSpacing (spacing);
00512     mainLayout->setMargin (margin);
00513 
00514     QLabel *        nameL  = new QLabel (i18n ("Name of level:"), dad);
00515     mainLayout->addWidget (nameL);
00516                         nhName  = new QLineEdit (dad);
00517     mainLayout->addWidget (nhName);
00518 
00519     QLabel *        mleL = new QLabel (i18n ("Hint for level:"), dad);
00520     mainLayout->addWidget (mleL);
00521 
00522     // Set up a widget to hold the wrapped text, using \n for paragraph breaks.
00523                          mle = new QTextEdit (dad);
00524      mle->      setAcceptRichText (false);
00525      mainLayout->addWidget (mle);
00526 
00527     // Base the geometry of the text box on the playing area.
00528     QPoint      p = parent->mapToGlobal (QPoint (0,0));
00529     int         c = parent->width() / (FIELDWIDTH + 4);
00530     dad->       move (p.x()+4*c, p.y()+4*c);
00531     mle->       setMinimumSize ((FIELDWIDTH*c/2), (FIELDHEIGHT/2)*c);
00532 
00533     // Configure the text box.
00534     mle->       setAlignment (Qt::AlignLeft);
00535 
00536     nhName->        setText (levelName);
00537     mle->       setText (levelHint);
00538 }
00539 
00540 KGrNHDialog::~KGrNHDialog()
00541 {
00542 }
00543 
00544 /*******************************************************************************
00545 *************** DIALOG BOX TO CREATE OR EDIT A GAME (COLLECTION) ***************
00546 *******************************************************************************/
00547 
00548 KGrECDialog::KGrECDialog (int action, int collnIndex,
00549                         QList<KGrCollection *> & gamesList,
00550                         QWidget * parent)
00551                 : KDialog (parent)
00552 {
00553     collections  = gamesList;
00554     defaultGame  = collnIndex;
00555 
00556     setCaption (i18n ("Edit Game Info"));
00557     setButtons (KDialog::Ok | KDialog::Cancel);
00558     setDefaultButton (KDialog::Ok);
00559     int margin      = marginHint();
00560     int spacing     = spacingHint();
00561     QWidget * dad   = new QWidget (this);
00562     setMainWidget (dad);
00563 
00564     QVBoxLayout * mainLayout = new QVBoxLayout (dad);
00565     mainLayout->setSpacing (spacing);
00566     mainLayout->setMargin (margin);
00567 
00568     QHBoxLayout *hboxLayout5 = new QHBoxLayout();
00569     hboxLayout5->setSpacing (spacing);
00570     nameL    = new QLabel (i18n ("Name of game:"), dad);
00571     hboxLayout5->addWidget (nameL);
00572     ecName   = new QLineEdit (dad);
00573     hboxLayout5->addWidget (ecName);
00574     mainLayout->addLayout (hboxLayout5);
00575 
00576     QHBoxLayout *hboxLayout6 = new QHBoxLayout();
00577     hboxLayout6->setSpacing (spacing);
00578     prefixL  = new QLabel (i18n ("File name prefix:"), dad);
00579     hboxLayout6->addWidget (prefixL);
00580     ecPrefix = new QLineEdit (dad);
00581     hboxLayout6->addWidget (ecPrefix);
00582     mainLayout->addLayout (hboxLayout6);
00583 
00584     //In Qt4, QButtonGroup is no longer a widget...
00585     ecGrp    = new QButtonGroup (dad);
00586     ecTradB  = new QRadioButton (i18n ("Traditional rules"), dad);
00587     ecKGrB   = new QRadioButton (i18n ("KGoldrunner rules"), dad);
00588     ecGrp->addButton (ecTradB);
00589     ecGrp->addButton (ecKGrB);
00590 
00591     //..so we need to add the radio buttons directly to the layout
00592     mainLayout->addWidget (ecTradB);
00593     mainLayout->addWidget (ecKGrB);
00594 
00595 
00596     nLevL    = new QLabel (i18n ("0 levels"), dad);
00597     mainLayout->addWidget (nLevL);
00598 
00599     mleL     = new QLabel (i18n ("About this game:"), dad);
00600     mainLayout->addWidget (mleL);
00601 
00602     // Set up a widget to hold the wrapped text, using \n for paragraph breaks.
00603     mle      = new QTextEdit (dad);
00604     mle->    setAcceptRichText (false);
00605     mainLayout->addWidget (mle);
00606 
00607     QPoint p = parent->mapToGlobal (QPoint (0,0));
00608 
00609     // Base the geometry of the dialog box on the playing area.
00610     int cell = qMin(parent->height() / (FIELDHEIGHT + 4), parent->width() / FIELDWIDTH + 4);
00611     dad->   move (p.x()+2*cell, p.y()+2*cell);
00612     dad->   setMinimumSize ((FIELDWIDTH*cell/2), (FIELDHEIGHT-1)*cell);
00613 
00614     if (action == SL_CR_GAME) {
00615          setCaption (i18n ("Create Game"));
00616     }
00617     else {
00618          setCaption (i18n ("Edit Game Info"));
00619     }
00620 
00621     QString OKText = "";
00622     if (action == SL_UPD_GAME) {        // Edit existing collection.
00623         ecName->    setText (collections.at (defaultGame)->name);
00624         ecPrefix->  setText (collections.at (defaultGame)->prefix);
00625         if (collections.at (defaultGame)->nLevels > 0) {
00626             // Collection already has some levels, so cannot change the prefix.
00627             ecPrefix->  setEnabled (false);
00628         }
00629         QString     s;
00630         nLevL->     setText (i18np ("1 level", "%1 levels",
00631                                         collections.at (defaultGame)->nLevels));
00632         OKText = i18n ("Save Changes");
00633     }
00634     else {                  // Create a collection.
00635         ecName->        setText ("");
00636         ecPrefix->      setText ("");
00637         nLevL->         setText (i18n ("0 levels"));
00638         OKText = i18n ("Save New");
00639     }
00640     setButtonGuiItem (KDialog::Ok, KGuiItem (OKText));
00641 
00642     if ((action == SL_CR_GAME) ||
00643         (collections.at (defaultGame)->settings == 'T')) {
00644         ecSetRules ('T');           // Traditional settings.
00645     }
00646     else {
00647         ecSetRules ('K');           // KGoldrunner settings.
00648     }
00649 
00650     // Configure the edit box.
00651     mle->       setAlignment (Qt::AlignLeft);
00652 
00653     if ((action == SL_UPD_GAME) &&
00654         (collections.at (defaultGame)->about.length() > 0)) {
00655         // Display and edit the game description in its original language.
00656         mle->       setText (collections.at (defaultGame)->about);
00657     }
00658     else {
00659         mle->       setText ("");
00660     }
00661 
00662     connect (ecKGrB,  SIGNAL (clicked()), this, SLOT (ecSetKGr()));
00663     connect (ecTradB, SIGNAL (clicked()), this, SLOT (ecSetTrad()));
00664 }
00665 
00666 KGrECDialog::~KGrECDialog()
00667 {
00668 }
00669 
00670 void KGrECDialog::ecSetRules (const char settings)
00671 {
00672     ecKGrB->    setChecked (false);
00673     ecTradB->   setChecked (false);
00674     if (settings == 'K')
00675         ecKGrB->    setChecked (true);
00676     else
00677         ecTradB->   setChecked (true);
00678 }
00679 
00680 void KGrECDialog::ecSetKGr()  {ecSetRules ('K');}   // Radio button slots.
00681 void KGrECDialog::ecSetTrad() {ecSetRules ('T');}
00682 
00683 /*******************************************************************************
00684 ***************  DIALOG TO SELECT A SAVED GAME TO BE RE-LOADED  ****************
00685 *******************************************************************************/
00686 
00687 KGrLGDialog::KGrLGDialog (QFile * savedGames,
00688                         QList<KGrCollection *> & collections,
00689                         QWidget * parent)
00690                 : KDialog (parent)
00691 {
00692     setCaption (i18n ("Select Saved Game"));
00693     setButtons (KDialog::Ok | KDialog::Cancel);
00694     setDefaultButton (KDialog::Ok);
00695     int margin      = marginHint();
00696     int spacing     = spacingHint();
00697     QWidget * dad   = new QWidget (this);
00698     setMainWidget (dad);
00699 
00700     QVBoxLayout *   mainLayout = new QVBoxLayout (dad);
00701     mainLayout->setSpacing (spacing);
00702     mainLayout->setMargin (margin);
00703 
00704     QLabel *        lgHeader = new QLabel (
00705                         i18n ("Game                       Level/Lives/Score   "
00706                         "Day    Date     Time  "), dad);
00707 
00708     lgList   = new QListWidget (dad);
00709     QFont       f = KGlobalSettings::fixedFont();   // KDE version.
00710                         f.setFixedPitch (true);
00711     lgList->        setFont (f);
00712                         f.setBold (true);
00713     lgHeader->      setFont (f);
00714 
00715     mainLayout->    addWidget (lgHeader);
00716     mainLayout->    addWidget (lgList);
00717 
00718     // Base the geometry of the list box on the playing area.
00719     QPoint      p = parent->mapToGlobal (QPoint (0,0));
00720     int         c = parent->width() / (FIELDWIDTH + 4);
00721     dad->       move (p.x()+2*c, p.y()+2*c);
00722     lgList->        setMinimumHeight ((FIELDHEIGHT/2)*c);
00723 
00724                         lgHighlight  = -1;
00725 
00726     QTextStream     gameText (savedGames);
00727     QString     s = "";
00728     QString     pr = "";
00729     int         i;
00730     int         imax = collections.count();
00731 
00732     // Read the saved games into the list box.
00733     while (! gameText.endData()) {
00734         s = gameText.readLine();        // Read in one saved game.
00735         pr = s.left (s.indexOf (" ", 0,
00736                         Qt::CaseInsensitive));  // Get the collection prefix.
00737         for (i = 0; i < imax; i++) {        // Get the collection name.
00738             if (collections.at (i)->prefix == pr) {
00739                 s = s.insert (0,
00740                 collections.at (i)->name.leftJustified (20, ' ', true) + ' ');
00741                 break;
00742             }
00743         }
00744         lgList-> addItem (s);
00745     }
00746     savedGames->close();
00747 
00748     // Mark row 0 (the most recently saved game) as the default selection.
00749     lgList->    setSelectionMode (QAbstractItemView::SingleSelection);
00750     lgList->    setCurrentRow (0);
00751     lgList->    setItemSelected  (lgList->currentItem(), true);
00752                 lgHighlight = 0;
00753 
00754     connect (lgList, SIGNAL (itemClicked (QListWidgetItem *)),
00755                 this, SLOT (lgSelect (QListWidgetItem *)));
00756 }
00757 
00758 void KGrLGDialog::lgSelect (QListWidgetItem * item)
00759 {
00760     lgHighlight = lgList->row (item);
00761 }
00762 
00763 /*******************************************************************************
00764 ***********************  CENTRALIZED MESSAGE FUNCTIONS  ************************
00765 *******************************************************************************/
00766 
00767 void KGrMessage::information (QWidget * parent,
00768                         const QString &caption, const QString &text)
00769 {
00770     // KDE does word-wrapping and will observe "\n" line-breaks.
00771     KMessageBox::information (parent, text, caption);
00772 }
00773 
00774 int KGrMessage::warning (QWidget * parent, const QString &caption,
00775                         const QString &text, const QString &label0,
00776                         const QString &label1, const QString &label2)
00777 {
00778     // KDE does word-wrapping and will observe "\n" line-breaks.
00779     int ans = 0;
00780     if (label2.isEmpty()) {
00781         // Display a box with 2 buttons.
00782         ans = KMessageBox::questionYesNo (parent, text, caption,
00783                             KGuiItem (label0), KGuiItem (label1));
00784         ans = (ans == KMessageBox::Yes) ? 0 : 1;
00785     }
00786     else {
00787         // Display a box with 3 buttons.
00788         ans = KMessageBox::questionYesNoCancel (parent, text, caption,
00789                             KGuiItem (label0), KGuiItem (label1));
00790         if (ans == KMessageBox::Cancel)
00791             ans = 2;
00792         else
00793             ans = (ans == KMessageBox::Yes) ? 0 : 1;
00794     }
00795     return (ans);
00796 }
00797 
00798 /*******************************************************************************
00799 *************************  ITEM FOR THE LIST OF GAMES  *************************
00800 *******************************************************************************/
00801 
00802 KGrGameListItem::KGrGameListItem (const QStringList & data,
00803                                   const int internalId)
00804         : QTreeWidgetItem (data)
00805 {
00806     mInternalId = internalId;
00807 }
00808 
00809 int KGrGameListItem::id() const
00810 {
00811     return mInternalId;
00812 }
00813 
00814 void KGrGameListItem::setId (const int internalId)
00815 {
00816     mInternalId = internalId;
00817 }
00818 
00819 #include "kgrdialog.moc"

kgoldrunner

Skip menu "kgoldrunner"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal