startgamedialog.cpp 6.34 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.
 *
 ********************************************************************/

Denis Kuplyakov's avatar
Denis Kuplyakov committed
24 25 26 27 28 29 30
#include <startgamedialog.h>
#include <ui_startgamedialog.h>

#include <QMessageBox>
#include <QCloseEvent>
#include <KgDifficulty>

31
#include <QSvgRenderer>
32
#include <KLocalizedString>
33
#include <QPainter>
34

35
#include <QGraphicsDropShadowEffect>
Laurent Montel's avatar
Laurent Montel committed
36 37 38 39
#include <KConfigGroup>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QVBoxLayout>
40

41
StartGameDialog::StartGameDialog(QWidget *parent, KgThemeProvider *provider) :
Laurent Montel's avatar
Laurent Montel committed
42
    QDialog(parent),
43
    ui(new Ui::StartGameDialog), m_provider(provider), m_chipsPrefix(BlackWhite)
44 45 46 47 48
{
    setModal(true);

    setFixedSize(width(), height());

Laurent Montel's avatar
Laurent Montel committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62
    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);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(slotAccepted()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    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"));
63 64

    m_contents = new QWidget(this);
Laurent Montel's avatar
Laurent Montel committed
65 66
    mainLayout->addWidget(m_contents);
    mainLayout->addWidget(buttonBox);
67 68
    ui->setupUi(m_contents);

69
    loadChipImages();
70

71 72
    ui->whiteTypeGroup->setId(ui->whiteHuman, GameStartInformation::Human);
    ui->whiteTypeGroup->setId(ui->whiteAI, GameStartInformation::AI);
73 74
    ui->whiteAI->setIcon(QIcon::fromTheme("computer"));
    ui->whiteHuman->setIcon(QIcon::fromTheme("user-identity"));
75 76 77

    ui->blackTypeGroup->setId(ui->blackHuman, GameStartInformation::Human);
    ui->blackTypeGroup->setId(ui->blackAI, GameStartInformation::AI);
78 79
    ui->blackAI->setIcon(QIcon::fromTheme("computer"));
    ui->blackHuman->setIcon(QIcon::fromTheme("user-identity"));
80

81
    QList< const KgDifficultyLevel * > diffList = Kg::difficulty()->levels();
Laurent Montel's avatar
Laurent Montel committed
82
    const QIcon icon = QIcon::fromTheme("games-difficult");
83

Laurent Montel's avatar
Laurent Montel committed
84
    for (int i = 0; i < diffList.size(); ++i) {
85 86
        ui->blackSkill->addItem(icon, diffList.at(i)->title());
        ui->whiteSkill->addItem(icon, diffList.at(i)->title());
Denis Kuplyakov's avatar
Denis Kuplyakov committed
87
        if (diffList.at(i)->isDefault()) {
88 89 90 91 92
            ui->whiteSkill->setCurrentIndex(i);
            ui->blackSkill->setCurrentIndex(i);
        }
    }

93 94 95 96 97
    connect(ui->blackTypeGroup, SIGNAL(buttonClicked(int)), this, SLOT(slotUpdateBlack(int)));
    connect(ui->whiteTypeGroup, SIGNAL(buttonClicked(int)), this, SLOT(slotUpdateWhite(int)));

    slotUpdateBlack(GameStartInformation::Human);
    slotUpdateWhite(GameStartInformation::AI);
98 99 100 101 102 103 104
}

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

105 106 107 108 109 110 111 112 113 114 115 116

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);
117
    QString prefix = Utils::chipPrefixToString(m_chipsPrefix);
118 119 120 121
    svgRenderer.render(painter, prefix + "_1");
    delete painter;

    painter = new QPainter(&whiteChip);
122
    // TODO: get 12 from some global constant that is shared with QML
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    svgRenderer.render(painter, prefix + "_12");
    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
143
void StartGameDialog::slotAccepted()
144
{
Laurent Montel's avatar
Laurent Montel committed
145 146
    emit startGame();
    accept();
147 148 149 150 151 152 153 154 155 156 157 158 159 160
}

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;
}
161

162
void StartGameDialog::setChipsPrefix(ChipsPrefix prefix)
163
{
164
    m_chipsPrefix = prefix;
165 166 167
    loadChipImages();
}

168 169 170 171 172 173 174 175

void StartGameDialog::slotUpdateBlack(int clickedId)
{
    ui->blackSkill->setEnabled(clickedId == GameStartInformation::AI);
    ui->blackName->setEnabled(clickedId == GameStartInformation::Human);
    if (clickedId == GameStartInformation::Human)
        ui->blackName->setText(m_user.loginName());
    else
Denis Kuplyakov's avatar
Denis Kuplyakov committed
176
        ui->blackName->setText(i18n("Computer"));
177 178 179 180 181 182 183 184 185
}

void StartGameDialog::slotUpdateWhite(int clickedId)
{
    ui->whiteSkill->setEnabled(clickedId == GameStartInformation::AI);
    ui->whiteName->setEnabled(clickedId == GameStartInformation::Human);
    if (clickedId == GameStartInformation::Human)
        ui->whiteName->setText(m_user.loginName());
    else
Denis Kuplyakov's avatar
Denis Kuplyakov committed
186
        ui->whiteName->setText(i18n("Computer"));
187
}