startgamedialog.cpp 4.46 KB
Newer Older
Denis Kuplyakov's avatar
Denis Kuplyakov committed
1 2
#include <startgamedialog.h>
#include <ui_startgamedialog.h>
3 4 5

#include <QMessageBox>
#include <QCloseEvent>
6
#include <KgDifficulty>
Denis Kuplyakov's avatar
Denis Kuplyakov committed
7
#include <KDebug>
8 9
#include <QSvgRenderer>
#include <QPainter>
10

11
StartGameDialog::StartGameDialog(QWidget *parent, KgThemeProvider *provider) :
12
    KDialog(parent),
13
    ui(new Ui::StartGameDialog), m_provider(provider), m_chipsPrefix(BlackWhite)
14 15 16 17 18
{
    setModal(true);

    setFixedSize(width(), height());

19 20
    setCaption(i18n("New game"));

21
    setButtons(Ok | Close);
Denis Kuplyakov's avatar
Denis Kuplyakov committed
22 23 24 25
    setButtonText(Ok, i18n("Start game"));
    setButtonToolTip(Ok, i18n("Let's start playing!"));
    setButtonText(Close, i18n("Quit"));
    setButtonToolTip(Close, i18n("Quit KReversi"));
26 27 28 29 30

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

31
    loadChipImages();
32

33 34 35 36 37
    ui->whiteTypeGroup->setId(ui->whiteHuman, GameStartInformation::Human);
    ui->whiteTypeGroup->setId(ui->whiteAI, GameStartInformation::AI);

    ui->blackTypeGroup->setId(ui->blackHuman, GameStartInformation::Human);
    ui->blackTypeGroup->setId(ui->blackAI, GameStartInformation::AI);
38

39 40 41 42 43 44
    QList< const KgDifficultyLevel * > diffList = Kg::difficulty()->levels();
    const KIcon icon("games-difficult");

    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
45
        if (diffList.at(i)->isDefault()) {
46 47 48 49 50
            ui->whiteSkill->setCurrentIndex(i);
            ui->blackSkill->setCurrentIndex(i);
        }
    }

51 52 53 54 55
    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);
56 57 58 59 60 61 62
}

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

63 64 65 66 67 68 69 70 71 72 73 74

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);
75
    QString prefix = Utils::chipPrefixToString(m_chipsPrefix);
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    svgRenderer.render(painter, prefix + "_1");
    delete painter;

    painter = new QPainter(&whiteChip);
    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);
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
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;
}
119

120
void StartGameDialog::setChipsPrefix(ChipsPrefix prefix)
121
{
122
    m_chipsPrefix = prefix;
123 124 125
    loadChipImages();
}

126 127 128 129 130 131 132 133

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
134
        ui->blackName->setText(i18n("Computer"));
135 136 137 138 139 140 141 142 143
}

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