1/*******************************************************************
2 *
3 * Copyright 2013 Denis Kuplyakov <dener.kup@gmail.com>
4 *
5 * This file is part of the KDE project "KReversi"
6 *
7 * KReversi is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * KReversi is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with KReversi; see the file COPYING. If not, write to
19 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 ********************************************************************/
23
24#include <startgamedialog.h>
25#include <ui_startgamedialog.h>
26
27#include <QMessageBox>
28#include <QCloseEvent>
29#include <KgDifficulty>
30
31#include <KDebug>
32#include <QSvgRenderer>
33#include <QPainter>
34
35StartGameDialog::StartGameDialog(QWidget *parent, KgThemeProvider *provider) :
36 KDialog(parent),
37 ui(new Ui::StartGameDialog), m_provider(provider), m_chipsPrefix(BlackWhite)
38{
39 setModal(true);
40
41 setFixedSize(width(), height());
42
43 setCaption(i18n("New game"));
44
45 setButtons(Ok | Close);
46 setButtonText(Ok, i18n("Start game"));
47 setButtonToolTip(Ok, i18n("Let's start playing!"));
48 setButtonText(Close, i18n("Quit"));
49 setButtonToolTip(Close, i18n("Quit KReversi"));
50
51 m_contents = new QWidget(this);
52 setMainWidget(m_contents);
53 ui->setupUi(m_contents);
54
55 loadChipImages();
56
57 ui->whiteTypeGroup->setId(ui->whiteHuman, GameStartInformation::Human);
58 ui->whiteTypeGroup->setId(ui->whiteAI, GameStartInformation::AI);
59 ui->whiteAI->setIcon(KIcon("computer"));
60 ui->whiteHuman->setIcon(KIcon("user-identity"));
61
62 ui->blackTypeGroup->setId(ui->blackHuman, GameStartInformation::Human);
63 ui->blackTypeGroup->setId(ui->blackAI, GameStartInformation::AI);
64 ui->blackAI->setIcon(KIcon("computer"));
65 ui->blackHuman->setIcon(KIcon("user-identity"));
66
67 QList< const KgDifficultyLevel * > diffList = Kg::difficulty()->levels();
68 const KIcon icon("games-difficult");
69
70 for (int i = 0; i < diffList.size(); i++) {
71 ui->blackSkill->addItem(icon, diffList.at(i)->title());
72 ui->whiteSkill->addItem(icon, diffList.at(i)->title());
73 if (diffList.at(i)->isDefault()) {
74 ui->whiteSkill->setCurrentIndex(i);
75 ui->blackSkill->setCurrentIndex(i);
76 }
77 }
78
79 connect(ui->blackTypeGroup, SIGNAL(buttonClicked(int)), this, SLOT(slotUpdateBlack(int)));
80 connect(ui->whiteTypeGroup, SIGNAL(buttonClicked(int)), this, SLOT(slotUpdateWhite(int)));
81
82 slotUpdateBlack(GameStartInformation::Human);
83 slotUpdateWhite(GameStartInformation::AI);
84}
85
86StartGameDialog::~StartGameDialog()
87{
88 delete ui;
89}
90
91
92void StartGameDialog::loadChipImages()
93{
94 QSvgRenderer svgRenderer;
95 svgRenderer.load(m_provider->currentTheme()->graphicsPath());
96
97 QPixmap blackChip(QSize(46, 46));
98 blackChip.fill(Qt::transparent);
99 QPixmap whiteChip(QSize(46, 46));
100 whiteChip.fill(Qt::transparent);
101
102 QPainter *painter = new QPainter(&blackChip);
103 QString prefix = Utils::chipPrefixToString(m_chipsPrefix);
104 svgRenderer.render(painter, prefix + "_1");
105 delete painter;
106
107 painter = new QPainter(&whiteChip);
108 // TODO: get 12 from some global constant that is shared with QML
109 svgRenderer.render(painter, prefix + "_12");
110 delete painter;
111
112 ui->blackLabel->setPixmap(blackChip);
113 ui->whiteLabel->setPixmap(whiteChip);
114
115 QGraphicsDropShadowEffect *blackShadow = new QGraphicsDropShadowEffect(this);
116 blackShadow->setBlurRadius(10.0);
117 blackShadow->setColor(Qt::black);
118 blackShadow->setOffset(0.0);
119
120 QGraphicsDropShadowEffect *whiteShadow = new QGraphicsDropShadowEffect(this);
121 whiteShadow->setBlurRadius(10.0);
122 whiteShadow->setColor(Qt::black);
123 whiteShadow->setOffset(0.0);
124
125 ui->blackLabel->setGraphicsEffect(blackShadow);
126 ui->whiteLabel->setGraphicsEffect(whiteShadow);
127}
128
129void StartGameDialog::slotButtonClicked(int button)
130{
131 if (button == KDialog::Ok)
132 emit startGame();
133 KDialog::slotButtonClicked(button);
134}
135
136GameStartInformation StartGameDialog::createGameStartInformation() const
137{
138 GameStartInformation info;
139 info.name[Black] = ui->blackName->text();
140 info.name[White] = ui->whiteName->text();
141 info.type[Black] = (GameStartInformation::PlayerType)ui->blackTypeGroup->checkedId();
142 info.type[White] = (GameStartInformation::PlayerType)ui->whiteTypeGroup->checkedId();
143 info.skill[Black] = ui->blackSkill->currentIndex();
144 info.skill[White] = ui->whiteSkill->currentIndex();
145
146 return info;
147}
148
149void StartGameDialog::setChipsPrefix(ChipsPrefix prefix)
150{
151 m_chipsPrefix = prefix;
152 loadChipImages();
153}
154
155
156void StartGameDialog::slotUpdateBlack(int clickedId)
157{
158 ui->blackSkill->setEnabled(clickedId == GameStartInformation::AI);
159 ui->blackName->setEnabled(clickedId == GameStartInformation::Human);
160 if (clickedId == GameStartInformation::Human)
161 ui->blackName->setText(m_user.loginName());
162 else
163 ui->blackName->setText(i18n("Computer"));
164}
165
166void StartGameDialog::slotUpdateWhite(int clickedId)
167{
168 ui->whiteSkill->setEnabled(clickedId == GameStartInformation::AI);
169 ui->whiteName->setEnabled(clickedId == GameStartInformation::Human);
170 if (clickedId == GameStartInformation::Human)
171 ui->whiteName->setText(m_user.loginName());
172 else
173 ui->whiteName->setText(i18n("Computer"));
174}
175