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>

Denis Kuplyakov's avatar
Denis Kuplyakov committed
31
#include <KDebug>
32
#include <QSvgRenderer>
33
#include <KLocalizedString>
34
#include <QPainter>
35

36 37
#include <QGraphicsDropShadowEffect>

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

    setFixedSize(width(), height());

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

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

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

58
    loadChipImages();
59

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

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

70
    QList< const KgDifficultyLevel * > diffList = Kg::difficulty()->levels();
71
    const QIcon icon("games-difficult");
72 73 74 75

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

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

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

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

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

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

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
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;
}
151

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

158 159 160 161 162 163 164 165

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

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