startgamedialog.cpp 6.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*******************************************************************
 *
 * Copyright 2013 Denis Kuplyakov <dener.kup@gmail.com>
 *
 * This file is part of the KDE project "KReversi"
 *
 * KReversi is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * KReversi is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with KReversi; see the file COPYING.  If not, write to
 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 ********************************************************************/

24 25
#include "startgamedialog.h"
#include "ui_startgamedialog.h"
Denis Kuplyakov's avatar
Denis Kuplyakov committed
26

27
#include <QGraphicsDropShadowEffect>
Laurent Montel's avatar
Laurent Montel committed
28 29
#include <QDialogButtonBox>
#include <QPushButton>
30 31
#include <QPainter>
#include <QSvgRenderer>
Laurent Montel's avatar
Laurent Montel committed
32
#include <QVBoxLayout>
33

34 35 36
#include <KgDifficulty>
#include <KLocalizedString>

37
StartGameDialog::StartGameDialog(QWidget *parent, KgThemeProvider *provider) :
Laurent Montel's avatar
Laurent Montel committed
38
    QDialog(parent),
39
    ui(new Ui::StartGameDialog), m_provider(provider), m_chipsPrefix(BlackWhite)
40 41 42
{
    setModal(true);

Laurent Montel's avatar
Laurent Montel committed
43 44 45 46 47 48 49 50
    setWindowTitle(i18n("New game"));

    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Close);
    QVBoxLayout *mainLayout = new QVBoxLayout;
    setLayout(mainLayout);
    QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
    okButton->setDefault(true);
    okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
Laurent Montel's avatar
Laurent Montel committed
51 52
    connect(buttonBox, &QDialogButtonBox::accepted, this, &StartGameDialog::slotAccepted);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &StartGameDialog::reject);
Laurent Montel's avatar
Laurent Montel committed
53 54 55 56
    okButton->setText(i18n("Start game"));
    okButton->setToolTip(i18n("Let's start playing!"));
    buttonBox->button(QDialogButtonBox::Close)->setText(i18n("Quit"));
    buttonBox->button(QDialogButtonBox::Close)->setToolTip(i18n("Quit KReversi"));
57 58

    m_contents = new QWidget(this);
Laurent Montel's avatar
Laurent Montel committed
59 60
    mainLayout->addWidget(m_contents);
    mainLayout->addWidget(buttonBox);
61 62
    ui->setupUi(m_contents);

63
    loadChipImages();
64

65 66
    ui->whiteTypeGroup->setId(ui->whiteHuman, GameStartInformation::Human);
    ui->whiteTypeGroup->setId(ui->whiteAI, GameStartInformation::AI);
67 68
    ui->whiteAI->setIcon(QIcon::fromTheme(QStringLiteral("computer")));
    ui->whiteHuman->setIcon(QIcon::fromTheme(QStringLiteral("user-identity")));
69 70 71

    ui->blackTypeGroup->setId(ui->blackHuman, GameStartInformation::Human);
    ui->blackTypeGroup->setId(ui->blackAI, GameStartInformation::AI);
72 73
    ui->blackAI->setIcon(QIcon::fromTheme(QStringLiteral("computer")));
    ui->blackHuman->setIcon(QIcon::fromTheme(QStringLiteral("user-identity")));
74

75
    QList< const KgDifficultyLevel * > diffList = Kg::difficulty()->levels();
76
    const QIcon icon = QIcon::fromTheme(QStringLiteral("games-difficult"));
77

Laurent Montel's avatar
Laurent Montel committed
78
    for (int i = 0; i < diffList.size(); ++i) {
79 80
        ui->blackSkill->addItem(icon, diffList.at(i)->title());
        ui->whiteSkill->addItem(icon, diffList.at(i)->title());
Denis Kuplyakov's avatar
Denis Kuplyakov committed
81
        if (diffList.at(i)->isDefault()) {
82 83 84 85 86
            ui->whiteSkill->setCurrentIndex(i);
            ui->blackSkill->setCurrentIndex(i);
        }
    }

Laurent Montel's avatar
Laurent Montel committed
87 88
    connect(ui->blackTypeGroup, static_cast<void (QButtonGroup::*)(QAbstractButton *)>(&QButtonGroup::buttonClicked), this, &StartGameDialog::slotUpdateBlack);
    connect(ui->whiteTypeGroup, static_cast<void (QButtonGroup::*)(QAbstractButton *)>(&QButtonGroup::buttonClicked), this, &StartGameDialog::slotUpdateWhite);
89

Laurent Montel's avatar
Laurent Montel committed
90 91
    slotUpdateBlack(ui->blackTypeGroup->button(GameStartInformation::Human));
    slotUpdateWhite(ui->whiteTypeGroup->button(GameStartInformation::AI));
92 93 94 95 96 97 98
}

StartGameDialog::~StartGameDialog()
{
    delete ui;
}

99 100 101 102 103 104 105 106 107 108 109 110

void StartGameDialog::loadChipImages()
{
    QSvgRenderer svgRenderer;
    svgRenderer.load(m_provider->currentTheme()->graphicsPath());

    QPixmap blackChip(QSize(46, 46));
    blackChip.fill(Qt::transparent);
    QPixmap whiteChip(QSize(46, 46));
    whiteChip.fill(Qt::transparent);

    QPainter *painter = new QPainter(&blackChip);
111
    QString prefix = Utils::chipPrefixToString(m_chipsPrefix);
112
    svgRenderer.render(painter, prefix + QStringLiteral("_1"));
113 114 115
    delete painter;

    painter = new QPainter(&whiteChip);
116
    // TODO: get 12 from some global constant that is shared with QML
117
    svgRenderer.render(painter, prefix + QStringLiteral("_12"));
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    delete painter;

    ui->blackLabel->setPixmap(blackChip);
    ui->whiteLabel->setPixmap(whiteChip);

    QGraphicsDropShadowEffect *blackShadow = new QGraphicsDropShadowEffect(this);
    blackShadow->setBlurRadius(10.0);
    blackShadow->setColor(Qt::black);
    blackShadow->setOffset(0.0);

    QGraphicsDropShadowEffect *whiteShadow = new QGraphicsDropShadowEffect(this);
    whiteShadow->setBlurRadius(10.0);
    whiteShadow->setColor(Qt::black);
    whiteShadow->setOffset(0.0);

    ui->blackLabel->setGraphicsEffect(blackShadow);
    ui->whiteLabel->setGraphicsEffect(whiteShadow);
}

Laurent Montel's avatar
Laurent Montel committed
137
void StartGameDialog::slotAccepted()
138
{
139
    Q_EMIT startGame();
Laurent Montel's avatar
Laurent Montel committed
140
    accept();
141 142 143 144 145 146 147 148 149 150 151 152 153 154
}

GameStartInformation StartGameDialog::createGameStartInformation() const
{
    GameStartInformation info;
    info.name[Black] = ui->blackName->text();
    info.name[White] = ui->whiteName->text();
    info.type[Black] = (GameStartInformation::PlayerType)ui->blackTypeGroup->checkedId();
    info.type[White] = (GameStartInformation::PlayerType)ui->whiteTypeGroup->checkedId();
    info.skill[Black] = ui->blackSkill->currentIndex();
    info.skill[White] = ui->whiteSkill->currentIndex();

    return info;
}
155

156
void StartGameDialog::setChipsPrefix(ChipsPrefix prefix)
157
{
158
    m_chipsPrefix = prefix;
159 160 161
    loadChipImages();
}

162

Laurent Montel's avatar
Laurent Montel committed
163
void StartGameDialog::slotUpdateBlack(QAbstractButton *button)
164
{
Laurent Montel's avatar
Laurent Montel committed
165 166 167 168 169 170 171 172 173
    if (button) {
        const int clickedId = ui->blackTypeGroup->id(button);
        ui->blackSkill->setEnabled(clickedId == GameStartInformation::AI);
        ui->blackName->setEnabled(clickedId == GameStartInformation::Human);
        if (clickedId == GameStartInformation::Human)
            ui->blackName->setText(m_user.loginName());
        else
            ui->blackName->setText(i18n("Computer"));
    }
174 175
}

Laurent Montel's avatar
Laurent Montel committed
176
void StartGameDialog::slotUpdateWhite(QAbstractButton *button)
177
{
Laurent Montel's avatar
Laurent Montel committed
178 179 180 181 182 183 184 185 186 187
    if (button) {
        const int clickedId = ui->whiteTypeGroup->id(button);

        ui->whiteSkill->setEnabled(clickedId == GameStartInformation::AI);
        ui->whiteName->setEnabled(clickedId == GameStartInformation::Human);
        if (clickedId == GameStartInformation::Human)
            ui->whiteName->setText(m_user.loginName());
        else
            ui->whiteName->setText(i18n("Computer"));
    }
188
}