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>
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
)
35 , Ui_OnlineDialog(*this)
39 _alreadyJoined
= false;
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();
56 if (myNameLineEdit
->text() == "") {
57 QMessageBox::warning(this, tr("Warning"), tr("You cannot have an empty name"), QMessageBox::Ok
);
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
));
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
);
82 QMessageBox::warning(this, tr("Warning"), tr("Players' colors are too similar."), QMessageBox::Ok
);
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
)
113 name_0
->setText(myNameLineEdit
->text());
114 color_0
->setColor(myColor
->color());
115 type_0
->setText(tr("Human"));
119 name_1
->setText(myNameLineEdit
->text());
120 color_1
->setColor(myColor
->color());
121 type_1
->setText(tr("Human"));
125 name_2
->setText(myNameLineEdit
->text());
126 color_2
->setColor(myColor
->color());
127 type_2
->setText(tr("Human"));
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
;
157 name_0
->setText(name
);
159 type_0
->setText(tr("AI"));
162 type_0
->setText(tr("Remote"));
166 name_1
->setText(name
);
168 type_1
->setText(tr("AI"));
171 type_1
->setText(tr("Remote"));
175 name_2
->setText(name
);
177 type_2
->setText(tr("AI"));
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
)
195 playerJoinButton
->setEnabled(!_alreadyJoined
);
210 void OnlineDialog::displaySettings(int d1
, int d2
, int numPlayers
, int timer
, bool playing
)
213 myNameLineEdit
->setText(setS
.myName());
214 myColor
->setColor(setS
.myColor());
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);
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()
256 void OnlineDialog::connectToServer()
259 quint16 serverPort
= portLineEdit
->text().toUShort(&ok
);
261 QMessageBox::critical(this, tr("Error"), tr("Cannot connect,\n'%1' is not a valid port number.").arg(portLineEdit
->text()), QMessageBox::Ok
);
263 } else if (serverPort
<= 1024) {
264 QMessageBox::critical(this, tr("Error"), tr("Cannot connect,\nport number must be greater than 1024."), QMessageBox::Ok
);
268 connectButton
->setEnabled(false);
269 ipLineEdit
->setEnabled(false);
270 portLineEdit
->setEnabled(false);
272 MainWindow
*mainwin
= qobject_cast
<MainWindow
*>(parent());
274 mainwin
->_net
->deleteLater();
275 QApplication::processEvents();
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
);
288 mainwin
->_net
->deleteLater();
289 QApplication::processEvents();
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);
316 PlSettings
->setEnabled(false);
317 myNameLineEdit
->setText("");
320 QList
<QColor
> OnlineDialog::colorList() const
323 list
.append(color_0
->color());
324 list
.append(color_1
->color());
325 list
.append(color_2
->color());
329 void OnlineDialog::enableHelp()
331 QWhatsThis::enterWhatsThisMode();