File indexing completed on 2022-09-27 13:19:29

0001 /*
0002     SPDX-FileCopyrightText: 2006 Matthew Williams <matt@milliams.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "newgamedialog.h"
0008 #include <QDebug>
0009 #include <QDialogButtonBox>
0010 #include <QPushButton>
0011 #include <QVBoxLayout>
0012 
0013 NewGameDialog::NewGameDialog(QWidget *parent) : QDialog(parent)
0014 {
0015   auto mainWidget = new QWidget(this);
0016   auto mainLayout = new QVBoxLayout;
0017   setLayout(mainLayout);
0018   mainLayout->addWidget(mainWidget);
0019   setupUi(mainWidget);
0020   auto buttonBox =
0021       new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0022   QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0023   okButton->setDefault(true);
0024   okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0025   connect(buttonBox, &QDialogButtonBox::accepted, this, &NewGameDialog::accept);
0026   connect(buttonBox, &QDialogButtonBox::rejected, this, &NewGameDialog::reject);
0027   mainLayout->addWidget(buttonBox);
0028   setWindowTitle(i18nc("@title:window", "New Game"));
0029   connect(spinNumOfPlayers,
0030           static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this,
0031           &NewGameDialog::adjustEnabledUsers);
0032 
0033   adjustEnabledUsers(spinNumOfPlayers->value());
0034 }
0035 
0036 void NewGameDialog::adjustEnabledUsers(int numOfPlayers)
0037 {
0038     switch (numOfPlayers) {
0039     case 2:
0040         labelPlayer3Name->setEnabled(false);
0041         playerThreeName->setEnabled(false);
0042         playerThreeHuman->setEnabled(false);
0043     // no break!
0044         Q_FALLTHROUGH();
0045     case 3:
0046         labelPlayer4Name->setEnabled(false);
0047         playerFourName->setEnabled(false);
0048         playerFourHuman->setEnabled(false);
0049     case 4:
0050         break;
0051     default:
0052         qCritical() << "NewGameDialog::adjustEnabledUsers(): numOfPlayers out of range!!";
0053     }
0054 
0055     switch (numOfPlayers) {
0056     case 4:
0057         labelPlayer4Name->setEnabled(true);
0058         playerFourName->setEnabled(true);
0059         playerFourHuman->setEnabled(true);
0060         Q_FALLTHROUGH();
0061     case 3:
0062         labelPlayer3Name->setEnabled(true);
0063         playerThreeName->setEnabled(true);
0064         playerThreeHuman->setEnabled(true);
0065     case 2:
0066     default:
0067         break;
0068     }
0069 }
0070