startgamedialog.cpp 5.78 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 36
#include <QGraphicsDropShadowEffect>

37
StartGameDialog::StartGameDialog(QWidget *parent, KgThemeProvider *provider) :
38
    KDialog(parent),
39
    ui(new Ui::StartGameDialog), m_provider(provider), m_chipsPrefix(BlackWhite)
40 41 42 43 44
{
    setModal(true);

    setFixedSize(width(), height());

45 46
    setCaption(i18n("New game"));

47
    setButtons(Ok | Close);
Denis Kuplyakov's avatar
Denis Kuplyakov committed
48 49 50 51
    setButtonText(Ok, i18n("Start game"));
    setButtonToolTip(Ok, i18n("Let's start playing!"));
    setButtonText(Close, i18n("Quit"));
    setButtonToolTip(Close, i18n("Quit KReversi"));
52 53 54 55 56

    m_contents = new QWidget(this);
    setMainWidget(m_contents);
    ui->setupUi(m_contents);

57
    loadChipImages();
58

59 60
    ui->whiteTypeGroup->setId(ui->whiteHuman, GameStartInformation::Human);
    ui->whiteTypeGroup->setId(ui->whiteAI, GameStartInformation::AI);
61 62
    ui->whiteAI->setIcon(QIcon::fromTheme("computer"));
    ui->whiteHuman->setIcon(QIcon::fromTheme("user-identity"));
63 64 65

    ui->blackTypeGroup->setId(ui->blackHuman, GameStartInformation::Human);
    ui->blackTypeGroup->setId(ui->blackAI, GameStartInformation::AI);
66 67
    ui->blackAI->setIcon(QIcon::fromTheme("computer"));
    ui->blackHuman->setIcon(QIcon::fromTheme("user-identity"));
68

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

Laurent Montel's avatar
Laurent Montel committed
72
    for (int i = 0; i < diffList.size(); ++i) {
73 74
        ui->blackSkill->addItem(icon, diffList.at(i)->title());
        ui->whiteSkill->addItem(icon, diffList.at(i)->title());
Denis Kuplyakov's avatar
Denis Kuplyakov committed
75
        if (diffList.at(i)->isDefault()) {
76 77 78 79 80
            ui->whiteSkill->setCurrentIndex(i);
            ui->blackSkill->setCurrentIndex(i);
        }
    }

81 82 83 84 85
    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);
86 87 88 89 90 91 92
}

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

93 94 95 96 97 98 99 100 101 102 103 104

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);
105
    QString prefix = Utils::chipPrefixToString(m_chipsPrefix);
106 107 108 109
    svgRenderer.render(painter, prefix + "_1");
    delete painter;

    painter = new QPainter(&whiteChip);
110
    // TODO: get 12 from some global constant that is shared with QML
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    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);
}

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
void StartGameDialog::slotButtonClicked(int button)
{
    if (button == KDialog::Ok)
        emit startGame();
    KDialog::slotButtonClicked(button);
}

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

151
void StartGameDialog::setChipsPrefix(ChipsPrefix prefix)
152
{
153
    m_chipsPrefix = prefix;
154 155 156
    loadChipImages();
}

157 158 159 160 161 162 163 164

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
165
        ui->blackName->setText(i18n("Computer"));
166 167 168 169 170 171 172 173 174
}

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
175
        ui->whiteName->setText(i18n("Computer"));
176
}