File indexing completed on 2024-10-13 03:43:39

0001 /*
0002     SPDX-FileCopyrightText: 2003 Marco Krüger <grisuji@gmx.de>
0003     SPDX-FileCopyrightText: 2003 Ian Wadham <iandw.au@gmail.com>
0004     SPDX-FileCopyrightText: 2009 Ian Wadham <iandw.au@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "kgrdialog.h"
0010 
0011 #include "kgrglobals.h"
0012 
0013 #include <QButtonGroup>
0014 #include <QDialogButtonBox>
0015 #include <QFile>
0016 #include <QFontDatabase>
0017 #include <QLabel>
0018 #include <QPushButton>
0019 #include <QTextStream>
0020 #include <QVBoxLayout>
0021 
0022 #include <KLocalizedString>
0023 #include <KGuiItem>
0024 #include <KMessageBox>
0025 
0026 /*******************************************************************************
0027 *************** DIALOG BOX TO CREATE/EDIT A LEVEL NAME AND HINT ****************
0028 *******************************************************************************/
0029 
0030 KGrNHDialog::KGrNHDialog (const QString & levelName, const QString & levelHint,
0031                         QWidget * parent)
0032                 : QDialog (parent)
0033 {
0034     setWindowTitle (i18nc("@title:window", "Edit Name or Hint"));
0035     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
0036     QWidget *mainWidget = new QWidget(this);
0037     QVBoxLayout *mainLayout = new QVBoxLayout;
0038     setLayout(mainLayout);
0039     mainLayout->addWidget(mainWidget);
0040     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0041     okButton->setDefault(true);
0042     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0043     connect(buttonBox, &QDialogButtonBox::accepted, this, &KGrNHDialog::accept);
0044     connect(buttonBox, &QDialogButtonBox::rejected, this, &KGrNHDialog::reject);
0045 
0046     buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
0047     int margin    = 0;
0048     int spacing    = 0;
0049     QWidget * dad = new QWidget (this);
0050     mainLayout->addWidget(dad);
0051 
0052     QVBoxLayout * mainLayout2 = new QVBoxLayout (dad);
0053     mainLayout2->setSpacing (spacing);
0054     mainLayout2->setContentsMargins(margin, margin, margin, margin);
0055 
0056     QLabel *    nameL  = new QLabel (i18n ("Name of level:"), dad);
0057     mainLayout2->addWidget(nameL);
0058     mainLayout2->addWidget (nameL);
0059                         nhName  = new QLineEdit (dad);
0060                         mainLayout->addWidget(nhName);
0061     mainLayout2->addWidget (nhName);
0062 
0063     QLabel *    mleL = new QLabel (i18n ("Hint for level:"), dad);
0064     mainLayout2->addWidget(mleL);
0065     mainLayout2->addWidget (mleL);
0066 
0067     // Set up a widget to hold the wrapped text, using \n for paragraph breaks.
0068                          mle = new QTextEdit (dad);
0069                          mainLayout->addWidget(mle);
0070     mle->    setAcceptRichText (false);
0071     mainLayout2->addWidget (mle);
0072     mainLayout->addWidget(buttonBox);
0073 
0074     // Base the geometry of the text box on the playing area.
0075     QPoint      p = parent->mapToGlobal (QPoint (0,0));
0076     int         c = parent->width() / (FIELDWIDTH + 4);
0077     dad->       move (p.x()+4*c, p.y()+4*c);
0078     mle->       setMinimumSize ((FIELDWIDTH*c/2), (FIELDHEIGHT/2)*c);
0079 
0080     // Configure the text box.
0081     mle->       setAlignment (Qt::AlignLeft);
0082 
0083     nhName->        setText (levelName);
0084     mle->       setText (levelHint);
0085 }
0086 
0087 KGrNHDialog::~KGrNHDialog()
0088 {
0089 }
0090 
0091 /*******************************************************************************
0092 *************** DIALOG BOX TO CREATE OR EDIT A GAME (COLLECTION) ***************
0093 *******************************************************************************/
0094 
0095 KGrECDialog::KGrECDialog (int action, int gameIndex,
0096                         QList<KGrGameData *> & gamesList,
0097                         QWidget * parent)
0098                 : QDialog (parent)
0099 {
0100     myGameList  = gamesList;
0101     defaultGame  = gameIndex;
0102 
0103     setWindowTitle (i18nc("@title:window", "Edit Game Info"));
0104     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
0105 
0106     connect(buttonBox, &QDialogButtonBox::accepted, this, &KGrECDialog::accept);
0107     connect(buttonBox, &QDialogButtonBox::rejected, this, &KGrECDialog::reject);
0108 
0109     buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
0110     buttonBox->button(QDialogButtonBox::Ok)->setShortcut(Qt::CTRL | Qt::Key_Return);
0111 
0112     int margin    = 0;
0113     int spacing    = 0;
0114     QWidget * dad   = new QWidget (this);
0115 
0116     QVBoxLayout * mainLayout = new QVBoxLayout (dad);
0117     mainLayout->setSpacing (spacing);
0118     mainLayout->setContentsMargins(margin, margin, margin, margin);
0119     setLayout(mainLayout);
0120 
0121     QHBoxLayout *hboxLayout5 = new QHBoxLayout();
0122     hboxLayout5->setSpacing (spacing);
0123     nameL    = new QLabel (i18n ("Name of game:"), dad);
0124     hboxLayout5->addWidget (nameL);
0125     ecName   = new QLineEdit (dad);
0126     hboxLayout5->addWidget (ecName);
0127     mainLayout->addLayout (hboxLayout5);
0128 
0129     QHBoxLayout *hboxLayout6 = new QHBoxLayout();
0130     hboxLayout6->setSpacing (spacing);
0131     prefixL  = new QLabel (i18n ("File name prefix:"), dad);
0132     hboxLayout6->addWidget (prefixL);
0133     ecPrefix = new QLineEdit (dad);
0134     hboxLayout6->addWidget (ecPrefix);
0135     mainLayout->addLayout (hboxLayout6);
0136 
0137     //In Qt4, QButtonGroup is no longer a widget...
0138     ecGrp    = new QButtonGroup (dad);
0139     ecTradB  = new QRadioButton (i18n ("Traditional rules"), dad);
0140     ecKGrB   = new QRadioButton (i18n ("KGoldrunner rules"), dad);
0141     ecGrp->addButton (ecTradB);
0142     ecGrp->addButton (ecKGrB);
0143 
0144     //..so we need to add the radio buttons directly to the layout
0145     mainLayout->addWidget (ecTradB);
0146     mainLayout->addWidget (ecKGrB);
0147 
0148 
0149     nLevL    = new QLabel (i18np ("1 level", "%1 levels", 0), dad);
0150     mainLayout->addWidget (nLevL);
0151 
0152     mleL     = new QLabel (i18n ("About this game:"), dad);
0153     mainLayout->addWidget (mleL);
0154 
0155     // Set up a widget to hold the wrapped text, using \n for paragraph breaks.
0156     mle      = new QTextEdit (dad);
0157     mle->    setAcceptRichText (false);
0158     mainLayout->addWidget (mle);
0159     mainLayout->addWidget(buttonBox);
0160 
0161     QPoint p = parent->mapToGlobal (QPoint (0,0));
0162 
0163     // Base the geometry of the dialog box on the playing area.
0164     int cell = qMin(parent->height() / (FIELDHEIGHT + 4), parent->width() / FIELDWIDTH + 4);
0165     dad->   move (p.x()+2*cell, p.y()+2*cell);
0166     dad->   setMinimumSize ((FIELDWIDTH*cell/2), (FIELDHEIGHT-1)*cell);
0167 
0168     if (action == SL_CR_GAME) {
0169          setWindowTitle (i18nc("@title:window", "Create Game"));
0170     }
0171     else {
0172          setWindowTitle (i18nc("@title:window", "Edit Game Info"));
0173     }
0174 
0175     QString OKText;
0176     if (action == SL_UPD_GAME) {        // Edit existing game.
0177         ecName->    setText (myGameList.at (defaultGame)->name);
0178         ecPrefix->  setText (myGameList.at (defaultGame)->prefix);
0179         if (myGameList.at (defaultGame)->nLevels > 0) {
0180             // Game already has some levels, so cannot change the prefix.
0181             ecPrefix->  setEnabled (false);
0182         }
0183         QString     s;
0184         nLevL->     setText (i18np ("1 level", "%1 levels",
0185                                         myGameList.at (defaultGame)->nLevels));
0186         OKText = i18n ("Save Changes");
0187     }
0188     else {                  // Create a game.
0189         ecName->        setText (QString());
0190         ecPrefix->      setText (QString());
0191         nLevL->         setText (i18n ("0 levels"));
0192         OKText = i18n ("Save New");
0193     }
0194     KGuiItem::assign(buttonBox->button(QDialogButtonBox::Ok), KGuiItem (OKText));
0195 
0196     if ((action == SL_CR_GAME) ||
0197         (myGameList.at (defaultGame)->rules == 'T')) {
0198         ecSetRules ('T');           // Traditional rules.
0199     }
0200     else {
0201         ecSetRules ('K');           // KGoldrunner rules.
0202     }
0203 
0204     // Configure the edit box.
0205     mle->       setAlignment (Qt::AlignLeft);
0206 
0207     if ((action == SL_UPD_GAME) &&
0208         (myGameList.at (defaultGame)->about.length() > 0)) {
0209         // Display and edit the game description in its original language.
0210         mle->setPlainText (QString::fromUtf8
0211                            (myGameList.at (defaultGame)->about.constData()));
0212     }
0213     else {
0214         mle->setPlainText (QString());
0215     }
0216 
0217     connect(ecKGrB, &QRadioButton::clicked, this, &KGrECDialog::ecSetKGr);
0218     connect(ecTradB, &QRadioButton::clicked, this, &KGrECDialog::ecSetTrad);
0219 }
0220 
0221 KGrECDialog::~KGrECDialog()
0222 {
0223 }
0224 
0225 void KGrECDialog::ecSetRules (const char rules)
0226 {
0227     ecKGrB->    setChecked (false);
0228     ecTradB->   setChecked (false);
0229     if (rules == 'K')
0230         ecKGrB->    setChecked (true);
0231     else
0232         ecTradB->   setChecked (true);
0233 }
0234 
0235 void KGrECDialog::ecSetKGr()  {ecSetRules ('K');}   // Radio button slots.
0236 void KGrECDialog::ecSetTrad() {ecSetRules ('T');}
0237 
0238 /*******************************************************************************
0239 ***************  DIALOG TO SELECT A SAVED GAME TO BE RE-LOADED  ****************
0240 *******************************************************************************/
0241 
0242 KGrLGDialog::KGrLGDialog (QFile * savedGames,
0243                           QList<KGrGameData *> & gameList,
0244                           QWidget * parent)
0245                 : QDialog (parent)
0246 {
0247     setWindowTitle (i18nc("@title:window", "Select Saved Game"));
0248     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
0249     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0250     okButton->setDefault(true);
0251     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0252     connect(buttonBox, &QDialogButtonBox::accepted, this, &KGrLGDialog::accept);
0253     connect(buttonBox, &QDialogButtonBox::rejected, this, &KGrLGDialog::reject);
0254 
0255     buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
0256     int margin    = 0;
0257     int spacing    = 0;
0258 
0259     QWidget * dad  = new QWidget (this);
0260     QVBoxLayout *   mainLayout = new QVBoxLayout (dad);
0261     mainLayout->setSpacing (spacing);
0262     mainLayout->setContentsMargins(margin, margin, margin, margin);
0263     setLayout(mainLayout);
0264 
0265     QLabel *        lgHeader = new QLabel (
0266                         i18n ("Game                       Level/Lives/Score   "
0267                         "Day    Date     Time  "), dad);
0268 
0269     lgList   = new QListWidget (dad);
0270     mainLayout->addWidget(lgList);
0271     QFont    f = QFontDatabase::systemFont(QFontDatabase::FixedFont);  // KDE version.
0272                         f.setFixedPitch (true);
0273     lgList->        setFont (f);
0274                         f.setBold (true);
0275     lgHeader->      setFont (f);
0276 
0277     mainLayout->    addWidget (lgHeader);
0278     mainLayout->    addWidget (lgList);
0279     mainLayout->    addWidget(buttonBox);
0280 
0281     // Base the geometry of the list box on the playing area.
0282     QPoint      p = parent->mapToGlobal (QPoint (0,0));
0283     int         c = parent->width() / (FIELDWIDTH + 4);
0284     dad->       move (p.x()+2*c, p.y()+2*c);
0285     lgList->        setMinimumHeight ((FIELDHEIGHT/2)*c);
0286 
0287                         lgHighlight  = -1;
0288 
0289     QTextStream     gameText (savedGames);
0290     QString     s;
0291     QString     pr;
0292     int         i;
0293     int         imax = gameList.count();
0294 
0295     // Read the saved games into the list box.
0296     while (! gameText.atEnd()) {
0297         s = gameText.readLine();        // Read in one saved game.
0298         pr = s.left (s.indexOf(QLatin1Char(' '), 0,
0299                         Qt::CaseInsensitive));  // Get the game prefix.
0300         for (i = 0; i < imax; ++i) {        // Get the game name.
0301             if (gameList.at (i)->prefix == pr) {
0302                 s.insert (0,
0303                 gameList.at (i)->name.leftJustified (20, QLatin1Char(' '), true) + QLatin1Char(' '));
0304                 break;
0305             }
0306         }
0307         lgList-> addItem (s);
0308     }
0309     savedGames->close();
0310 
0311     // Mark row 0 (the most recently saved game) as the default selection.
0312     lgList->    setSelectionMode (QAbstractItemView::SingleSelection);
0313     lgList->    setCurrentRow (0);
0314     lgList->currentItem()->setSelected(true);
0315                 lgHighlight = 0;
0316 
0317     connect(lgList, &QListWidget::itemClicked, this, &KGrLGDialog::lgSelect);
0318 }
0319 
0320 void KGrLGDialog::lgSelect (QListWidgetItem * item)
0321 {
0322     lgHighlight = lgList->row (item);
0323 }
0324 
0325 /*******************************************************************************
0326 ***********************  CENTRALIZED MESSAGE FUNCTIONS  ************************
0327 *******************************************************************************/
0328 
0329 void KGrMessage::information (QWidget * parent,
0330                         const QString & caption, const QString & text,
0331                         const QString & dontShowAgain)
0332 {
0333     // KDE does word-wrapping and will observe "\n" line-breaks.
0334     KMessageBox::information (parent, text, caption, dontShowAgain);
0335 }
0336 
0337 int KGrMessage::warning (QWidget * parent, const QString &caption,
0338                         const QString &text, const QString &label0,
0339                         const QString &label1, const QString &label2)
0340 {
0341     // KDE does word-wrapping and will observe "\n" line-breaks.
0342     int result = 0;
0343     if (label2.isEmpty()) {
0344         // Display a box with 2 buttons.
0345         KMessageBox::ButtonCode ans = KMessageBox::questionTwoActions (parent, text, caption,
0346                             KGuiItem (label0), KGuiItem (label1));
0347         result = (ans == KMessageBox::PrimaryAction) ? 0 : 1;
0348     }
0349     else {
0350         // Display a box with 3 buttons.
0351         KMessageBox::ButtonCode ans = KMessageBox::questionTwoActionsCancel (parent, text, caption,
0352                             KGuiItem (label0), KGuiItem (label1),
0353                             KGuiItem (label2));
0354         if (ans == KMessageBox::Cancel)
0355             result = 2;
0356         else
0357             result = (ans == KMessageBox::PrimaryAction) ? 0 : 1;
0358     }
0359     return (result);
0360 }
0361 
0362 #include "moc_kgrdialog.cpp"