Warning, file /games/kreversi/src/startgamedialog.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 SPDX-FileCopyrightText: 2013 Denis Kuplyakov <dener.kup@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "startgamedialog.h" 0008 #include "ui_startgamedialog.h" 0009 0010 #include <QGraphicsDropShadowEffect> 0011 #include <QDialogButtonBox> 0012 #include <QPushButton> 0013 #include <QPainter> 0014 #include <QSvgRenderer> 0015 #include <QVBoxLayout> 0016 0017 #include <KGameDifficulty> 0018 #include <KLocalizedString> 0019 0020 StartGameDialog::StartGameDialog(QWidget *parent, KGameThemeProvider *provider) : 0021 QDialog(parent), 0022 ui(new Ui::StartGameDialog), m_provider(provider), m_chipsPrefix(BlackWhite) 0023 { 0024 setModal(true); 0025 0026 setWindowTitle(i18nc("@title:window", "New game")); 0027 0028 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Close); 0029 QVBoxLayout *mainLayout = new QVBoxLayout; 0030 setLayout(mainLayout); 0031 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok); 0032 okButton->setDefault(true); 0033 okButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0034 connect(buttonBox, &QDialogButtonBox::accepted, this, &StartGameDialog::slotAccepted); 0035 connect(buttonBox, &QDialogButtonBox::rejected, this, &StartGameDialog::reject); 0036 okButton->setText(i18n("Start game")); 0037 okButton->setToolTip(i18n("Let's start playing!")); 0038 buttonBox->button(QDialogButtonBox::Close)->setText(i18n("Quit")); 0039 buttonBox->button(QDialogButtonBox::Close)->setToolTip(i18n("Quit KReversi")); 0040 0041 m_contents = new QWidget(this); 0042 mainLayout->addWidget(m_contents); 0043 mainLayout->addWidget(buttonBox); 0044 ui->setupUi(m_contents); 0045 0046 loadChipImages(); 0047 0048 ui->whiteTypeGroup->setId(ui->whiteHuman, GameStartInformation::Human); 0049 ui->whiteTypeGroup->setId(ui->whiteAI, GameStartInformation::AI); 0050 ui->whiteAI->setIcon(QIcon::fromTheme(QStringLiteral("computer"))); 0051 ui->whiteHuman->setIcon(QIcon::fromTheme(QStringLiteral("user-identity"))); 0052 0053 ui->blackTypeGroup->setId(ui->blackHuman, GameStartInformation::Human); 0054 ui->blackTypeGroup->setId(ui->blackAI, GameStartInformation::AI); 0055 ui->blackAI->setIcon(QIcon::fromTheme(QStringLiteral("computer"))); 0056 ui->blackHuman->setIcon(QIcon::fromTheme(QStringLiteral("user-identity"))); 0057 0058 QList< const KGameDifficultyLevel * > diffList = KGameDifficulty::global()->levels(); 0059 const QIcon icon = QIcon::fromTheme(QStringLiteral("games-difficult")); 0060 0061 for (int i = 0; i < diffList.size(); ++i) { 0062 ui->blackSkill->addItem(icon, diffList.at(i)->title()); 0063 ui->whiteSkill->addItem(icon, diffList.at(i)->title()); 0064 if (diffList.at(i)->isDefault()) { 0065 ui->whiteSkill->setCurrentIndex(i); 0066 ui->blackSkill->setCurrentIndex(i); 0067 } 0068 } 0069 0070 connect(ui->blackTypeGroup, static_cast<void (QButtonGroup::*)(QAbstractButton *)>(&QButtonGroup::buttonClicked), this, &StartGameDialog::slotUpdateBlack); 0071 connect(ui->whiteTypeGroup, static_cast<void (QButtonGroup::*)(QAbstractButton *)>(&QButtonGroup::buttonClicked), this, &StartGameDialog::slotUpdateWhite); 0072 0073 slotUpdateBlack(ui->blackTypeGroup->button(GameStartInformation::Human)); 0074 slotUpdateWhite(ui->whiteTypeGroup->button(GameStartInformation::AI)); 0075 } 0076 0077 StartGameDialog::~StartGameDialog() 0078 { 0079 delete ui; 0080 } 0081 0082 0083 void StartGameDialog::loadChipImages() 0084 { 0085 QSvgRenderer svgRenderer; 0086 svgRenderer.load(m_provider->currentTheme()->graphicsPath()); 0087 0088 QPixmap blackChip(QSize(46, 46)); 0089 blackChip.fill(Qt::transparent); 0090 QPixmap whiteChip(QSize(46, 46)); 0091 whiteChip.fill(Qt::transparent); 0092 0093 QPainter *painter = new QPainter(&blackChip); 0094 QString prefix = Utils::chipPrefixToString(m_chipsPrefix); 0095 svgRenderer.render(painter, prefix + QStringLiteral("_1")); 0096 delete painter; 0097 0098 painter = new QPainter(&whiteChip); 0099 // TODO: get 12 from some global constant that is shared with QML 0100 svgRenderer.render(painter, prefix + QStringLiteral("_12")); 0101 delete painter; 0102 0103 ui->blackLabel->setPixmap(blackChip); 0104 ui->whiteLabel->setPixmap(whiteChip); 0105 0106 QGraphicsDropShadowEffect *blackShadow = new QGraphicsDropShadowEffect(this); 0107 blackShadow->setBlurRadius(10.0); 0108 blackShadow->setColor(Qt::black); 0109 blackShadow->setOffset(0.0); 0110 0111 QGraphicsDropShadowEffect *whiteShadow = new QGraphicsDropShadowEffect(this); 0112 whiteShadow->setBlurRadius(10.0); 0113 whiteShadow->setColor(Qt::black); 0114 whiteShadow->setOffset(0.0); 0115 0116 ui->blackLabel->setGraphicsEffect(blackShadow); 0117 ui->whiteLabel->setGraphicsEffect(whiteShadow); 0118 } 0119 0120 void StartGameDialog::slotAccepted() 0121 { 0122 Q_EMIT startGame(); 0123 accept(); 0124 } 0125 0126 GameStartInformation StartGameDialog::createGameStartInformation() const 0127 { 0128 GameStartInformation info; 0129 info.name[Black] = ui->blackName->text(); 0130 info.name[White] = ui->whiteName->text(); 0131 info.type[Black] = (GameStartInformation::PlayerType)ui->blackTypeGroup->checkedId(); 0132 info.type[White] = (GameStartInformation::PlayerType)ui->whiteTypeGroup->checkedId(); 0133 info.skill[Black] = ui->blackSkill->currentIndex(); 0134 info.skill[White] = ui->whiteSkill->currentIndex(); 0135 0136 return info; 0137 } 0138 0139 void StartGameDialog::setChipsPrefix(ChipsPrefix prefix) 0140 { 0141 m_chipsPrefix = prefix; 0142 loadChipImages(); 0143 } 0144 0145 0146 void StartGameDialog::slotUpdateBlack(QAbstractButton *button) 0147 { 0148 if (button) { 0149 const int clickedId = ui->blackTypeGroup->id(button); 0150 ui->blackSkill->setEnabled(clickedId == GameStartInformation::AI); 0151 ui->blackName->setEnabled(clickedId == GameStartInformation::Human); 0152 if (clickedId == GameStartInformation::Human) 0153 ui->blackName->setText(m_user.loginName()); 0154 else 0155 ui->blackName->setText(i18n("Computer")); 0156 } 0157 } 0158 0159 void StartGameDialog::slotUpdateWhite(QAbstractButton *button) 0160 { 0161 if (button) { 0162 const int clickedId = ui->whiteTypeGroup->id(button); 0163 0164 ui->whiteSkill->setEnabled(clickedId == GameStartInformation::AI); 0165 ui->whiteName->setEnabled(clickedId == GameStartInformation::Human); 0166 if (clickedId == GameStartInformation::Human) 0167 ui->whiteName->setText(m_user.loginName()); 0168 else 0169 ui->whiteName->setText(i18n("Computer")); 0170 } 0171 } 0172 0173 #include "moc_startgamedialog.cpp"