Connect more signals on the server side.
[GoMoku3D.git] / src / gui / OnlineDialog.cpp
blobb29a1cf0f056d79a2514e609f837749cd036e06e
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>
26 #include "OnlineDialog.h"
27 #include "ServerSettings.h"
28 #include "LocalSettings.h"
29 #include "MainWindow.h"
30 #include "GameClient.h"
32 OnlineDialog::OnlineDialog(QWidget *parent) :
33 QDialog(parent), Ui_OnlineDialog(*this)
35 setupUi(this);
36 _alreadyJoined = false;
37 ServerSettings set;
38 portLineEdit->setText(QString::number(set.serverPort()));
39 connect(myColorButton, SIGNAL(clicked()), this, SLOT(colorSlot()));
40 connect(color_0, SIGNAL(clicked()), this, SLOT(colorSlot()));
41 connect(color_1, SIGNAL(clicked()), this, SLOT(colorSlot()));
42 connect(color_2, SIGNAL(clicked()), this, SLOT(colorSlot()));
43 connect(connectButton, SIGNAL(clicked()), this, SLOT(connectToServer()));
46 void OnlineDialog::join()
48 QObject *s = sender();
49 if (!s) {
50 return;
53 if (myNameLineEdit->text() == "") {
54 QMessageBox::warning(this, tr("Warning"), tr("You cannot have an empty name"), QMessageBox::Ok);
55 return;
58 _alreadyJoined = true;
59 myNameLineEdit->setEnabled(false);
60 myColorButton->setEnabled(false);
61 color_0->setEnabled(false);
62 color_1->setEnabled(false);
63 color_2->setEnabled(false);
64 playerJoinButton->setEnabled(false);
65 spectatorJoinButton->setEnabled(false);
67 ServerSettings set;
68 set.setMyName(myNameLineEdit->text());
69 set.setMyColor(myColorButton->palette().color(QPalette::Button));
71 if (s == playerJoinButton) {
72 emit sendJoin("player", myNameLineEdit->text());
75 if (s == spectatorJoinButton) {
76 emit sendJoin("spectator", myNameLineEdit->text());
80 void OnlineDialog::acceptedJoinRequest(int id)
82 if (id == 0) {
83 playername_0->setText(myNameLineEdit->text());
84 color_0->setPalette(myColorButton->palette());
85 human_0->setChecked(true);
87 if (id == 1) {
88 playername_1->setText(myNameLineEdit->text());
89 color_1->setPalette(myColorButton->palette());
90 human_1->setChecked(true);
92 if (id == 2) {
93 playername_2->setText(myNameLineEdit->text());
94 color_2->setPalette(myColorButton->palette());
95 human_2->setChecked(true);
99 void OnlineDialog::refusedJoinRequest(QString error)
101 QMessageBox::warning(this, tr("Warning"), error, QMessageBox::Ok);
102 _alreadyJoined = false;
103 myNameLineEdit->setEnabled(true);
104 spectatorJoinButton->setEnabled(true);
106 bool b0 = frame_0->isEnabled() && playername_0->text() == "";
107 bool b1 = frame_1->isEnabled() && playername_1->text() == "";
108 bool b2 = frame_2->isEnabled() && playername_2->text() == "";
109 if (!b0 && !b1 && !b2) {
110 playerJoinButton->setEnabled(false);
111 myColorButton->setEnabled(false);
115 void OnlineDialog::addPlayer(int id, QString name, QString type)
117 if (id == 0) {
118 playername_0->setText(name);
119 if (type == "A") {
120 remote_0->setChecked(false);
121 ai_0->setChecked(true);
123 else {
124 remote_0->setChecked(true);
125 ai_0->setChecked(false);
128 if (id == 1) {
129 playername_1->setText(name);
130 if (type == "A") {
131 remote_1->setChecked(false);
132 ai_1->setChecked(true);
134 else {
135 remote_1->setChecked(true);
136 ai_1->setChecked(false);
139 if (id == 2) {
140 playername_2->setText(name);
141 if (type == "A") {
142 remote_2->setChecked(false);
143 ai_2->setChecked(true);
145 else {
146 remote_2->setChecked(true);
147 ai_2->setChecked(false);
150 bool b0 = frame_0->isEnabled() && playername_0->text() == "";
151 bool b1 = frame_1->isEnabled() && playername_1->text() == "";
152 bool b2 = frame_2->isEnabled() && playername_2->text() == "";
153 if (!b0 && !b1 && !b2) {
154 playerJoinButton->setEnabled(false);
158 void OnlineDialog::removePlayer(int id)
160 playerJoinButton->setEnabled(!_alreadyJoined);
161 if (id == 0) {
162 playername_0->clear();
164 if (id == 1) {
165 playername_1->clear();
167 if (id == 2) {
168 playername_2->clear();
172 void OnlineDialog::displaySettings(int d1, int d2, int numPlayers, int timer, bool playing)
174 ServerSettings setS;
175 myNameLineEdit->setText(setS.myName());
176 myColorButton->setPalette(QPalette(setS.myColor()));
177 LocalSettings set;
178 QList<PlayerInfo> info = set.playersInfo();
179 numPlayersLabel->setText(QString::number(numPlayers));
180 frame_0->setEnabled(true);
181 color_0->setPalette(QPalette(info[0].color()));
182 frame_1->setEnabled(true);
183 color_1->setPalette(QPalette(info[1].color()));
184 if (numPlayers == 3) {
185 frame_2->setEnabled(true);
186 color_2->setPalette(QPalette(info[2].color()));
188 d1Label->setText(QString::number(d1));
189 d2Label->setText(QString::number(d2));
190 if (timer > 0) {
191 timerLabel->setText(QString::number(timer));
193 else {
194 timerLabel->setText(tr("--"));
196 myColorButton->setEnabled(!playing);
197 spectatorJoinButton->setEnabled(true);
198 playerJoinButton->setEnabled(!playing);
201 void OnlineDialog::setStatus(QString status)
203 statusLabel->setText(status);
206 void OnlineDialog::gameStarted()
208 QDialog::accept();
211 void OnlineDialog::colorSlot()
213 QPushButton *button = qobject_cast<QPushButton*>(sender());
214 if (!button) {
215 return;
217 bool ok;
218 QRgb chosenColor = QColorDialog::getRgba(button->palette().color(QPalette::Button).rgba(), &ok, this);
219 if (ok) {
220 button->setPalette(QPalette(QColor::fromRgba(chosenColor)));
224 void OnlineDialog::connectToServer()
226 quint16 serverPort = portLineEdit->text().toUShort();
227 if (serverPort <= 1024) {
228 QMessageBox::warning(this, tr("Warning"), tr("Cannot connect,\ninvalid port number."), QMessageBox::Ok);
229 return;
231 //FIXME vedere che succede con hostname e ip
232 QHostAddress addr;
233 if (!addr.setAddress(ipLineEdit->text())) {
234 QMessageBox::warning(this, tr("Warning"), tr("Cannot connect,\ninvalid IP."), QMessageBox::Ok);
235 return;
237 connectButton->setEnabled(false);
238 ipLineEdit->setEnabled(false);
239 portLineEdit->setEnabled(false);
241 MainWindow *mainwin = qobject_cast<MainWindow*>(parent());
242 Q_ASSERT(mainwin->_net == 0);
243 mainwin->_net = new GameClient(this, ipLineEdit->text(), serverPort);
245 PlSettings->setEnabled(true);