Updated Glossario.tex
[GoMoku3D.git] / src / gui / OnlineDialog.cpp
blobc19e1f01965f84b9624b5e518db6fb13e376e892
1 /********************************************************************
3 * Copyright (C) 2008 Daniele Battaglia
5 * This file is part of GoMoku3D.
7 * GoMoku3D is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * GoMoku3D 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.
17 * You should have received a copy of the GNU General Public License
18 * along with GoMoku3D. If not, see <http://www.gnu.org/licenses/>.
20 *******************************************************************/
22 #include <QColorDialog>
23 #include <QMessageBox>
24 #include <QHostAddress>
25 #include <QWhatsThis>
27 #include "OnlineDialog.h"
28 #include "ServerSettings.h"
29 #include "LocalSettings.h"
30 #include "MainWindow.h"
31 #include "GameClient.h"
33 OnlineDialog::OnlineDialog(QWidget *parent)
34 : QDialog(parent)
35 , Ui_OnlineDialog(*this)
36 , _typeList(3, "")
38 setupUi(this);
39 _alreadyJoined = false;
40 ServerSettings set;
41 portLineEdit->setText(QString::number(set.serverPort()));
43 connect(connectButton, SIGNAL(clicked()), this, SLOT(connectToServer()));
44 connect(playerJoinButton, SIGNAL(clicked()), this, SLOT(join()));
45 connect(spectatorJoinButton, SIGNAL(clicked()), this, SLOT(join()));
46 connect(helpButton, SIGNAL(clicked()), this, SLOT(enableHelp()));
49 void OnlineDialog::join()
51 QObject *s = sender();
52 if (!s) {
53 return;
56 if (myNameLineEdit->text() == "") {
57 QMessageBox::warning(this, tr("Warning"), tr("You cannot have an empty name"), QMessageBox::Ok);
58 return;
61 ServerSettings set;
63 QList<QColor> colors;
64 colors.append(set.backgroundColor());
65 colors.append(set.defaultCubeColor());
66 if (s == playerJoinButton) {
67 colors.append(myColor->color());
69 colors.append(color_0->color());
70 colors.append(color_1->color());
71 if (numPlayersLabel->text().toInt() == 3) {
72 colors.append(color_2->color());
74 for (int i = 0; i < colors.size(); i++) {
75 for (int j = 0; j < i; j++) {
76 bool ok = MainWindow::areDifferentColors(colors.at(j), colors.at(i));
77 if (!ok) {
78 if (j == 0 || j == 1) {
79 QMessageBox::warning(this, tr("Warning"), tr("Players' colors are too similar\nto background color or empty cube color."), QMessageBox::Ok);
81 else {
82 QMessageBox::warning(this, tr("Warning"), tr("Players' colors are too similar."), QMessageBox::Ok);
84 return;
89 _alreadyJoined = true;
90 myNameLineEdit->setEnabled(false);
91 myColor->setClickable(false);
92 color_0->setClickable(false);
93 color_1->setClickable(false);
94 color_2->setClickable(false);
95 playerJoinButton->setEnabled(false);
96 spectatorJoinButton->setEnabled(false);
98 set.setMyName(myNameLineEdit->text());
99 set.setMyColor(myColor->color());
101 if (s == playerJoinButton) {
102 emit sendJoin("player", myNameLineEdit->text());
105 if (s == spectatorJoinButton) {
106 emit sendJoin("spectator", myNameLineEdit->text());
110 void OnlineDialog::acceptedJoinRequest(int id)
112 if (id == 0) {
113 name_0->setText(myNameLineEdit->text());
114 color_0->setColor(myColor->color());
115 type_0->setText(tr("Human"));
116 _typeList[0] = "H";
118 if (id == 1) {
119 name_1->setText(myNameLineEdit->text());
120 color_1->setColor(myColor->color());
121 type_1->setText(tr("Human"));
122 _typeList[1] = "H";
124 if (id == 2) {
125 name_2->setText(myNameLineEdit->text());
126 color_2->setColor(myColor->color());
127 type_2->setText(tr("Human"));
128 _typeList[2] = "H";
132 void OnlineDialog::refusedJoinRequest(QString error)
134 QMessageBox::warning(this, tr("Warning"), error, QMessageBox::Ok);
135 _alreadyJoined = false;
136 myNameLineEdit->setEnabled(true);
137 myColor->setClickable(true);
138 color_0->setClickable(true);
139 color_1->setClickable(true);
140 color_2->setClickable(true);
141 playerJoinButton->setEnabled(true);
142 spectatorJoinButton->setEnabled(true);
144 bool b0 = frame_0->isEnabled() && name_0->text() == "";
145 bool b1 = frame_1->isEnabled() && name_1->text() == "";
146 bool b2 = frame_2->isEnabled() && name_2->text() == "";
147 if (!b0 && !b1 && !b2) {
148 playerJoinButton->setEnabled(false);
149 myColor->setEnabled(false);
153 void OnlineDialog::addPlayer(int id, QString name, QString type)
155 _typeList[id] = type;
156 if (id == 0) {
157 name_0->setText(name);
158 if (type == "A") {
159 type_0->setText(tr("AI"));
161 else {
162 type_0->setText(tr("Remote"));
165 if (id == 1) {
166 name_1->setText(name);
167 if (type == "A") {
168 type_1->setText(tr("AI"));
170 else {
171 type_1->setText(tr("Remote"));
174 if (id == 2) {
175 name_2->setText(name);
176 if (type == "A") {
177 type_2->setText(tr("AI"));
179 else {
180 type_2->setText(tr("Remote"));
184 bool b0 = frame_0->isEnabled() && name_0->text() == "";
185 bool b1 = frame_1->isEnabled() && name_1->text() == "";
186 bool b2 = frame_2->isEnabled() && name_2->text() == "";
187 if (!b0 && !b1 && !b2) {
188 playerJoinButton->setEnabled(false);
192 void OnlineDialog::removePlayer(int id)
194 _typeList[id] = "";
195 playerJoinButton->setEnabled(!_alreadyJoined);
196 if (id == 0) {
197 name_0->setText("");
198 type_0->setText("");
200 if (id == 1) {
201 name_1->setText("");
202 type_1->setText("");
204 if (id == 2) {
205 name_2->setText("");
206 type_2->setText("");
210 void OnlineDialog::displaySettings(int d1, int d2, int numPlayers, int timer, bool playing)
212 ServerSettings setS;
213 myNameLineEdit->setText(setS.myName());
214 myColor->setColor(setS.myColor());
216 LocalSettings set;
217 QList<PlayerInfo> info = set.playersInfo();
218 numPlayersLabel->setText(QString::number(numPlayers));
219 frame_0->setEnabled(true);
220 color_0->setColor(info[0].color());
221 color_0->setClickable(true);
222 frame_1->setEnabled(true);
223 color_1->setColor(info[1].color());
224 color_1->setClickable(true);
225 if (numPlayers == 3) {
226 frame_2->setEnabled(true);
227 color_2->setColor(info[2].color());
228 color_2->setClickable(true);
230 else {
231 frame_2->setEnabled(false);
233 d1Label->setText(QString::number(d1));
234 d2Label->setText(QString::number(d2));
235 timerLabel->setText(timer > 0 ? QString::number(timer) : "--");
237 mySettingsFrame->setEnabled(true);
238 label_10->setEnabled(true);
239 myNameLineEdit->setEnabled(true);
240 myColor->setEnabled(!playing);
241 myColor->setClickable(!playing);
242 spectatorJoinButton->setEnabled(true);
243 playerJoinButton->setEnabled(!playing);
246 void OnlineDialog::setStatus(QString status)
248 statusLabel->setText(status);
251 void OnlineDialog::gameStarted()
253 QDialog::accept();
256 void OnlineDialog::connectToServer()
258 bool ok;
259 quint16 serverPort = portLineEdit->text().toUShort(&ok);
260 if (!ok) {
261 QMessageBox::critical(this, tr("Error"), tr("Cannot connect,\n'%1' is not a valid port number.").arg(portLineEdit->text()), QMessageBox::Ok);
262 return;
263 } else if (serverPort <= 1024) {
264 QMessageBox::critical(this, tr("Error"), tr("Cannot connect,\nport number must be greater than 1024."), QMessageBox::Ok);
265 return;
268 connectButton->setEnabled(false);
269 ipLineEdit->setEnabled(false);
270 portLineEdit->setEnabled(false);
272 MainWindow *mainwin = qobject_cast<MainWindow*>(parent());
273 if (mainwin->_net) {
274 mainwin->_net->deleteLater();
275 QApplication::processEvents();
276 mainwin->_net = 0;
278 mainwin->_net = new GameClient(this, ipLineEdit->text(), serverPort);
280 PlSettings->setEnabled(true);
283 void OnlineDialog::networkError(QString error)
285 MainWindow *mainwin = qobject_cast<MainWindow*>(parent());
286 QMessageBox::critical(this, tr("Error"), tr("Network error.\n") + error + ".", QMessageBox::Ok);
287 if (mainwin->_net) {
288 mainwin->_net->deleteLater();
289 QApplication::processEvents();
290 mainwin->_net = 0;
293 _alreadyJoined = false;
294 _typeList = QVector<QString>(3, "");
295 ipLineEdit->setEnabled(true);
296 portLineEdit->setEnabled(true);
297 connectButton->setEnabled(true);
298 statusLabel->setText(RED_TEXT(tr("not connected")));
299 numPlayersLabel->setText("?");
300 d1Label->setText("?");
301 d2Label->setText("?");
302 timerLabel->setText("?");
304 mySettingsFrame->setEnabled(false);
305 label_10->setEnabled(false);
306 myNameLineEdit->setEnabled(false);
307 myColor->setEnabled(false);
308 spectatorJoinButton->setEnabled(false);
309 playerJoinButton->setEnabled(false);
310 name_0->setText("");
311 name_1->setText("");
312 name_2->setText("");
313 type_0->setText("");
314 type_1->setText("");
315 type_2->setText("");
316 PlSettings->setEnabled(false);
317 myNameLineEdit->setText("");
320 QList<QColor> OnlineDialog::colorList() const
322 QList<QColor> list;
323 list.append(color_0->color());
324 list.append(color_1->color());
325 list.append(color_2->color());
326 return list;
329 void OnlineDialog::enableHelp()
331 QWhatsThis::enterWhatsThisMode();