2
* Hedgewars, a free turn based strategy game
3
* Copyright (c) 2006-2011 Andrey Korotaev <unC0Rr@gmail.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; version 2 of the License
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19
#include <QGridLayout>
20
#include <QPushButton>
24
#include <QMessageBox>
25
#include <QHeaderView>
26
#include <QTableWidget>
28
#include "ammoSchemeModel.h"
29
#include "pageroomslist.h"
31
#include "chatwidget.h"
33
PageRoomsList::PageRoomsList(QWidget* parent, QSettings * gameSettings, SDLInteraction * sdli) :
36
QGridLayout * pageLayout = new QGridLayout(this);
38
QHBoxLayout * newRoomLayout = new QHBoxLayout();
39
QLabel * roomNameLabel = new QLabel(this);
40
roomNameLabel->setText(tr("Room Name:"));
41
roomName = new QLineEdit(this);
42
roomName->setMaxLength(60);
43
newRoomLayout->addWidget(roomNameLabel);
44
newRoomLayout->addWidget(roomName);
45
pageLayout->addLayout(newRoomLayout, 0, 0, 1, 2);
47
roomsList = new QTableWidget(this);
48
roomsList->setSelectionBehavior(QAbstractItemView::SelectRows);
49
roomsList->verticalHeader()->setVisible(false);
50
roomsList->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
51
roomsList->setAlternatingRowColors(true);
52
roomsList->setShowGrid(false);
53
roomsList->setSelectionMode(QAbstractItemView::SingleSelection);
54
pageLayout->addWidget(roomsList, 1, 0, 3, 2);
55
pageLayout->setRowStretch(2, 100);
57
QHBoxLayout * filterLayout = new QHBoxLayout();
59
QLabel * stateLabel = new QLabel(this);
60
stateLabel->setText(tr("State:"));
61
CBState = new QComboBox(this);
62
CBState->addItem(QComboBox::tr("Any"));
63
CBState->addItem(QComboBox::tr("In lobby"));
64
CBState->addItem(QComboBox::tr("In progress"));
65
filterLayout->addWidget(stateLabel);
66
filterLayout->addWidget(CBState);
67
filterLayout->addSpacing(30);
69
QLabel * ruleLabel = new QLabel(this);
70
ruleLabel->setText(tr("Rules:"));
71
CBRules = new QComboBox(this);
72
CBRules->addItem(QComboBox::tr("Any"));
73
// not the most elegant solution but it works
74
ammoSchemeModel = new AmmoSchemeModel(this, NULL);
75
for (int i = 0; i < ammoSchemeModel->predefSchemesNames.count(); i++)
76
CBRules->addItem(ammoSchemeModel->predefSchemesNames.at(i).toAscii().constData());
77
filterLayout->addWidget(ruleLabel);
78
filterLayout->addWidget(CBRules);
79
filterLayout->addSpacing(30);
81
QLabel * weaponLabel = new QLabel(this);
82
weaponLabel->setText(tr("Weapons:"));
83
CBWeapons = new QComboBox(this);
84
CBWeapons->addItem(QComboBox::tr("Any"));
85
for (int i = 0; i < cDefaultAmmos.count(); i++) {
86
QPair<QString,QString> ammo = cDefaultAmmos.at(i);
87
CBWeapons->addItem(ammo.first.toAscii().constData());
89
filterLayout->addWidget(weaponLabel);
90
filterLayout->addWidget(CBWeapons);
91
filterLayout->addSpacing(30);
93
QLabel * searchLabel = new QLabel(this);
94
searchLabel->setText(tr("Search:"));
95
searchText = new QLineEdit(this);
96
searchText->setMaxLength(60);
97
filterLayout->addWidget(searchLabel);
98
filterLayout->addWidget(searchText);
100
pageLayout->addLayout(filterLayout, 4, 0, 1, 2);
102
chatWidget = new HWChatWidget(this, gameSettings, sdli, false);
103
pageLayout->addWidget(chatWidget, 5, 0, 1, 3);
104
pageLayout->setRowStretch(5, 350);
106
BtnCreate = addButton(tr("Create"), pageLayout, 0, 2);
107
BtnJoin = addButton(tr("Join"), pageLayout, 1, 2);
108
BtnRefresh = addButton(tr("Refresh"), pageLayout, 3, 2);
109
BtnClear = addButton(tr("Clear"), pageLayout, 4, 2);
111
BtnBack = addButton(":/res/Exit.png", pageLayout, 6, 0, true);
113
lblCount = new QLabel(this);
114
pageLayout->addWidget(lblCount, 6, 1, Qt::AlignHCenter);
115
lblCount->setText("?");
116
lblCount->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
118
connect(chatWidget, SIGNAL(nickCountUpdate(const int)), this, SLOT(updateNickCounter(const int)));
120
BtnAdmin = addButton(tr("Admin features"), pageLayout, 6, 2);
122
connect(BtnCreate, SIGNAL(clicked()), this, SLOT(onCreateClick()));
123
connect(BtnJoin, SIGNAL(clicked()), this, SLOT(onJoinClick()));
124
connect(BtnRefresh, SIGNAL(clicked()), this, SLOT(onRefreshClick()));
125
connect(BtnClear, SIGNAL(clicked()), this, SLOT(onClearClick()));
126
connect(roomsList, SIGNAL(doubleClicked (const QModelIndex &)), this, SLOT(onJoinClick()));
127
connect(CBState, SIGNAL(currentIndexChanged (int)), this, SLOT(onRefreshClick()));
128
connect(CBRules, SIGNAL(currentIndexChanged (int)), this, SLOT(onRefreshClick()));
129
connect(CBWeapons, SIGNAL(currentIndexChanged (int)), this, SLOT(onRefreshClick()));
130
connect(searchText, SIGNAL(textChanged (const QString &)), this, SLOT(onRefreshClick()));
131
connect(this, SIGNAL(askJoinConfirmation (const QString &)), this, SLOT(onJoinConfirmation(const QString &)), Qt::QueuedConnection);
136
void PageRoomsList::setAdmin(bool flag)
138
BtnAdmin->setVisible(flag);
141
void PageRoomsList::setRoomsList(const QStringList & list)
143
QBrush red(QColor(255, 0, 0));
144
QBrush orange(QColor(127, 127, 0));
145
QBrush yellow(QColor(255, 255, 0));
146
QBrush green(QColor(0, 255, 0));
148
listFromServer = list;
150
QString selection = "";
152
if(QTableWidgetItem *item = roomsList->item(roomsList->currentRow(), 0))
153
selection = item->text();
156
roomsList->setColumnCount(7);
157
roomsList->setHorizontalHeaderLabels(
159
QTableWidget::tr("Room Name") <<
160
QTableWidget::tr("C") <<
161
QTableWidget::tr("T") <<
162
QTableWidget::tr("Owner") <<
163
QTableWidget::tr("Map") <<
164
QTableWidget::tr("Rules") <<
165
QTableWidget::tr("Weapons")
169
// roomsList->horizontalHeader()->resizeSection(0, 200);
170
// roomsList->horizontalHeader()->resizeSection(1, 50);
171
// roomsList->horizontalHeader()->resizeSection(2, 50);
172
// roomsList->horizontalHeader()->resizeSection(3, 100);
173
// roomsList->horizontalHeader()->resizeSection(4, 100);
174
// roomsList->horizontalHeader()->resizeSection(5, 100);
175
// roomsList->horizontalHeader()->resizeSection(6, 100);
178
// roomsList->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
180
bool gameCanBeJoined = true;
185
roomsList->setRowCount(list.size() / 8);
186
for(int i = 0, r = 0; i < list.size(); i += 8, r++)
188
// if we are joining a game
189
// TODO: Should NOT be done here
191
if (gameInLobbyName == list[i + 1]) {
192
gameCanBeJoined = list[i].compare("True");
196
// check filter settings
197
#define NO_FILTER_MATCH roomsList->setRowCount(roomsList->rowCount() - 1); --r; continue
199
if (list[i].compare("True") && CBState->currentIndex() == 2) { NO_FILTER_MATCH; }
200
if (list[i].compare("False") && CBState->currentIndex() == 1) { NO_FILTER_MATCH; }
201
if (CBRules->currentIndex() != 0 && list[i + 6].compare(CBRules->currentText())) { NO_FILTER_MATCH; }
202
if (CBWeapons->currentIndex() != 0 && list[i + 7].compare(CBWeapons->currentText())) { NO_FILTER_MATCH; }
203
bool found = list[i + 1].contains(searchText->text(), Qt::CaseInsensitive);
205
for (int a = 4; a <= 7; ++a) {
206
QString compString = list[i + a];
207
if (a == 5 && compString == "+rnd+") {
208
compString = "Random Map";
209
} else if (a == 5 && compString == "+maze+") {
210
compString = "Random Maze";
211
} else if (a == 5 && compString == "+drawn+") {
212
compString = "Drawn Map";
214
if (compString.contains(searchText->text(), Qt::CaseInsensitive)) {
220
if (!searchText->text().isEmpty() && !found) { NO_FILTER_MATCH; }
222
QTableWidgetItem * item;
223
item = new QTableWidgetItem(list[i + 1]); // room name
224
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
226
// pick appropriate room icon and tooltip (game in progress yes/no; later maybe locked rooms etc.)
227
if(list[i].compare("True"))
229
item->setIcon(QIcon(":/res/iconTime.png"));// game is in lobby
230
item->setToolTip(tr("This game is in lobby.\nYou may join and start playing once the game starts."));
234
item->setIcon(QIcon(":/res/iconDamage.png"));// game has started
235
item->setToolTip(tr("This game is in progress.\nYou may join and spectate now but you'll have to wait for the game to end to start playing."));
238
roomsList->setItem(r, 0, item);
240
item = new QTableWidgetItem(list[i + 2]); // number of clients
241
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
242
item->setTextAlignment(Qt::AlignCenter);
243
item->setToolTip(tr("There are %1 clients connected to this room.", "", list[i + 2].toInt()).arg(list[i + 2]));
244
roomsList->setItem(r, 1, item);
246
item = new QTableWidgetItem(list[i + 3]); // number of teams
247
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
248
item->setTextAlignment(Qt::AlignCenter);
249
item->setToolTip(tr("There are %1 teams participating in this room.", "", list[i + 3].toInt()).arg(list[i + 3]));
250
//Should we highlight "full" games? Might get misinterpreted
251
//if(list[i + 3].toInt() >= cMaxTeams)
252
// item->setForeground(red);
253
roomsList->setItem(r, 2, item);
255
item = new QTableWidgetItem(list[i + 4].left(15)); // name of host
256
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
257
item->setToolTip(tr("%1 is the host. He may adjust settings and start the game.").arg(list[i + 4]));
258
roomsList->setItem(r, 3, item);
260
if(list[i + 5] == "+rnd+")
262
item = new QTableWidgetItem(tr("Random Map")); // selected map (is randomized)
263
// FIXME - need real icons. Disabling until then
264
// item->setIcon(QIcon(":/res/mapRandom.png"));
266
else if (list[i+5] == "+maze+")
268
item = new QTableWidgetItem(tr("Random Maze"));
269
// FIXME - need real icons. Disabling until then
270
// item->setIcon(QIcon(":/res/mapMaze.png"));
274
item = new QTableWidgetItem(list[i + 5]); // selected map
276
// check to see if we've got this map
277
// not perfect but a start
278
if(!mapList->contains(list[i + 5]))
280
item->setForeground(red);
281
item->setIcon(QIcon(":/res/mapMissing.png"));
285
// todo: mission icon?
286
// FIXME - need real icons. Disabling until then
287
// item->setIcon(QIcon(":/res/mapCustom.png"));
291
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
292
item->setToolTip(tr("Games may be played on precreated or randomized maps."));
293
roomsList->setItem(r, 4, item);
295
item = new QTableWidgetItem(list[i + 6].left(24)); // selected game scheme
296
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
297
item->setToolTip(tr("The Game Scheme defines general options and preferences like Round Time, Sudden Death or Vampirism."));
298
roomsList->setItem(r, 5, item);
300
item = new QTableWidgetItem(list[i + 7].left(24)); // selected weapon scheme
301
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
302
item->setToolTip(tr("The Weapon Scheme defines available weapons and their ammunition count."));
303
roomsList->setItem(r, 6, item);
305
if(!list[i + 1].compare(selection) && !selection.isEmpty())
306
roomsList->selectionModel()->setCurrentIndex(roomsList->model()->index(r, 0), QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
309
roomsList->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
310
roomsList->horizontalHeader()->setResizeMode(1, QHeaderView::ResizeToContents);
311
roomsList->horizontalHeader()->setResizeMode(2, QHeaderView::ResizeToContents);
312
roomsList->horizontalHeader()->setResizeMode(3, QHeaderView::ResizeToContents);
313
roomsList->horizontalHeader()->setResizeMode(4, QHeaderView::ResizeToContents);
314
roomsList->horizontalHeader()->setResizeMode(5, QHeaderView::ResizeToContents);
315
roomsList->horizontalHeader()->setResizeMode(6, QHeaderView::ResizeToContents);
317
// TODO: Should NOT be done here
320
if (gameCanBeJoined) {
321
emit askForJoinRoom(gameInLobbyName);
323
emit askJoinConfirmation(gameInLobbyName);
327
// roomsList->resizeColumnsToContents();
330
void PageRoomsList::onCreateClick()
332
if (roomName->text().size())
333
emit askForCreateRoom(roomName->text());
335
QMessageBox::critical(this,
337
tr("Please enter room name"),
341
void PageRoomsList::onJoinClick()
343
QTableWidgetItem * curritem = roomsList->item(roomsList->currentRow(), 0);
346
QMessageBox::critical(this,
348
tr("Please select room from the list"),
353
for (int i = 0; i < listFromServer.size(); i += 8) {
354
if (listFromServer[i + 1] == curritem->data(Qt::DisplayRole).toString()) {
355
gameInLobby = listFromServer[i].compare("True");
361
gameInLobbyName = curritem->data(Qt::DisplayRole).toString();
362
emit askForRoomList();
364
emit askForJoinRoom(curritem->data(Qt::DisplayRole).toString());
368
void PageRoomsList::onRefreshClick()
370
emit askForRoomList();
373
void PageRoomsList::onClearClick()
375
CBState->setCurrentIndex(0);
376
CBRules->setCurrentIndex(0);
377
CBWeapons->setCurrentIndex(0);
381
void PageRoomsList::onJoinConfirmation(const QString & room)
383
if (QMessageBox::warning(this,
385
tr("The game you are trying to join has started.\nDo you still want to join the room?"),
386
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
388
emit askForJoinRoom(room);
392
void PageRoomsList::updateNickCounter(int cnt)
394
lblCount->setText(tr("%1 players online", 0, cnt).arg(cnt));