|
1 /* |
|
2 * Hedgewars, a free turn based strategy game |
|
3 * Copyright (c) 2006-2011 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 * |
|
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 |
|
8 * |
|
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. |
|
13 * |
|
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 |
|
17 */ |
|
18 |
|
19 #include <QGridLayout> |
|
20 #include <QHBoxLayout> |
|
21 #include <QPushButton> |
|
22 #include <QComboBox> |
|
23 #include <QLabel> |
|
24 #include <QLineEdit> |
|
25 #include <QMessageBox> |
|
26 #include <QHeaderView> |
|
27 #include <QTableWidget> |
|
28 |
|
29 #include "ammoSchemeModel.h" |
|
30 #include "pageroomslist.h" |
|
31 #include "hwconsts.h" |
|
32 #include "chatwidget.h" |
|
33 |
|
34 QLayout * PageRoomsList::bodyLayoutDefinition() |
|
35 { |
|
36 QGridLayout * pageLayout = new QGridLayout(); |
|
37 |
|
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); |
|
46 |
|
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); |
|
56 |
|
57 QHBoxLayout * filterLayout = new QHBoxLayout(); |
|
58 |
|
59 QLabel * stateLabel = new QLabel(this); |
|
60 CBState = new QComboBox(this); |
|
61 |
|
62 filterLayout->addWidget(stateLabel); |
|
63 filterLayout->addWidget(CBState); |
|
64 filterLayout->addSpacing(30); |
|
65 |
|
66 QLabel * ruleLabel = new QLabel(this); |
|
67 ruleLabel->setText(tr("Rules:")); |
|
68 CBRules = new QComboBox(this); |
|
69 |
|
70 filterLayout->addWidget(ruleLabel); |
|
71 filterLayout->addWidget(CBRules); |
|
72 filterLayout->addSpacing(30); |
|
73 |
|
74 QLabel * weaponLabel = new QLabel(this); |
|
75 weaponLabel->setText(tr("Weapons:")); |
|
76 CBWeapons = new QComboBox(this); |
|
77 |
|
78 filterLayout->addWidget(weaponLabel); |
|
79 filterLayout->addWidget(CBWeapons); |
|
80 filterLayout->addSpacing(30); |
|
81 |
|
82 QLabel * searchLabel = new QLabel(this); |
|
83 searchLabel->setText(tr("Search:")); |
|
84 searchText = new QLineEdit(this); |
|
85 searchText->setMaxLength(60); |
|
86 filterLayout->addWidget(searchLabel); |
|
87 filterLayout->addWidget(searchText); |
|
88 |
|
89 pageLayout->addLayout(filterLayout, 4, 0, 1, 2); |
|
90 |
|
91 chatWidget = new HWChatWidget(this, m_gameSettings, false); |
|
92 pageLayout->addWidget(chatWidget, 5, 0, 1, 3); |
|
93 pageLayout->setRowStretch(5, 350); |
|
94 |
|
95 BtnCreate = addButton(tr("Create"), pageLayout, 0, 2); |
|
96 BtnJoin = addButton(tr("Join"), pageLayout, 1, 2); |
|
97 BtnRefresh = addButton(tr("Refresh"), pageLayout, 3, 2); |
|
98 BtnClear = addButton(tr("Clear"), pageLayout, 4, 2); |
|
99 |
|
100 // strech all but the buttons column |
|
101 pageLayout->setColumnStretch(0, 1); |
|
102 pageLayout->setColumnStretch(1, 1); |
|
103 pageLayout->setColumnStretch(2, 0); |
|
104 |
|
105 CBRules->addItem(QComboBox::tr("Any")); |
|
106 CBState->addItem(QComboBox::tr("Any")); |
|
107 CBState->addItem(QComboBox::tr("In lobby")); |
|
108 CBState->addItem(QComboBox::tr("In progress")); |
|
109 |
|
110 return pageLayout; |
|
111 } |
|
112 |
|
113 QLayout * PageRoomsList::footerLayoutDefinition() |
|
114 { |
|
115 QHBoxLayout * bottomLayout = new QHBoxLayout(); |
|
116 |
|
117 lblCount = new QLabel(this); |
|
118 bottomLayout->addWidget(lblCount, 0, Qt::AlignHCenter); |
|
119 bottomLayout->setStretchFactor(lblCount, 1); |
|
120 lblCount->setText("?"); |
|
121 lblCount->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); |
|
122 |
|
123 BtnAdmin = addButton(tr("Admin features"), bottomLayout, 1); |
|
124 BtnAdmin->setMinimumWidth(160); |
|
125 |
|
126 // strech left part |
|
127 bottomLayout->setStretch(0, 1); |
|
128 bottomLayout->setStretch(1, 0); |
|
129 |
|
130 return bottomLayout; |
|
131 } |
|
132 |
|
133 void PageRoomsList::connectSignals() |
|
134 { |
|
135 connect(chatWidget, SIGNAL(nickCountUpdate(const int)), this, SLOT(updateNickCounter(const int))); |
|
136 |
|
137 connect(BtnCreate, SIGNAL(clicked()), this, SLOT(onCreateClick())); |
|
138 connect(BtnJoin, SIGNAL(clicked()), this, SLOT(onJoinClick())); |
|
139 connect(BtnRefresh, SIGNAL(clicked()), this, SLOT(onRefreshClick())); |
|
140 connect(BtnClear, SIGNAL(clicked()), this, SLOT(onClearClick())); |
|
141 connect(roomsList, SIGNAL(doubleClicked (const QModelIndex &)), this, SLOT(onJoinClick())); |
|
142 connect(CBState, SIGNAL(currentIndexChanged (int)), this, SLOT(onRefreshClick())); |
|
143 connect(CBRules, SIGNAL(currentIndexChanged (int)), this, SLOT(onRefreshClick())); |
|
144 connect(CBWeapons, SIGNAL(currentIndexChanged (int)), this, SLOT(onRefreshClick())); |
|
145 connect(searchText, SIGNAL(textChanged (const QString &)), this, SLOT(onRefreshClick())); |
|
146 connect(this, SIGNAL(askJoinConfirmation (const QString &)), this, SLOT(onJoinConfirmation(const QString &)), Qt::QueuedConnection); |
|
147 } |
|
148 |
|
149 |
|
150 PageRoomsList::PageRoomsList(QWidget* parent, QSettings * gameSettings) : |
|
151 AbstractPage(parent) |
|
152 { |
|
153 m_gameSettings = gameSettings; |
|
154 |
|
155 initPage(); |
|
156 |
|
157 // not the most elegant solution but it works |
|
158 ammoSchemeModel = new AmmoSchemeModel(this, NULL); |
|
159 for (int i = 0; i < ammoSchemeModel->predefSchemesNames.count(); i++) |
|
160 CBRules->addItem(ammoSchemeModel->predefSchemesNames.at(i).toAscii().constData()); |
|
161 |
|
162 CBWeapons->addItem(QComboBox::tr("Any")); |
|
163 for (int i = 0; i < cDefaultAmmos.count(); i++) { |
|
164 QPair<QString,QString> ammo = cDefaultAmmos.at(i); |
|
165 CBWeapons->addItem(ammo.first.toAscii().constData()); |
|
166 } |
|
167 |
|
168 gameInLobby = false; |
|
169 } |
|
170 |
|
171 |
|
172 void PageRoomsList::displayError(const QString & message) |
|
173 { |
|
174 chatWidget->displayError(message); |
|
175 } |
|
176 |
|
177 |
|
178 void PageRoomsList::displayNotice(const QString & message) |
|
179 { |
|
180 chatWidget->displayNotice(message); |
|
181 } |
|
182 |
|
183 void PageRoomsList::displayWarning(const QString & message) |
|
184 { |
|
185 chatWidget->displayWarning(message); |
|
186 } |
|
187 |
|
188 |
|
189 void PageRoomsList::setAdmin(bool flag) |
|
190 { |
|
191 BtnAdmin->setVisible(flag); |
|
192 } |
|
193 |
|
194 void PageRoomsList::setRoomsList(const QStringList & list) |
|
195 { |
|
196 QBrush red(QColor(255, 0, 0)); |
|
197 QBrush orange(QColor(127, 127, 0)); |
|
198 QBrush yellow(QColor(255, 255, 0)); |
|
199 QBrush green(QColor(0, 255, 0)); |
|
200 |
|
201 listFromServer = list; |
|
202 |
|
203 QString selection = ""; |
|
204 |
|
205 if(QTableWidgetItem *item = roomsList->item(roomsList->currentRow(), 0)) |
|
206 selection = item->text(); |
|
207 |
|
208 roomsList->clear(); |
|
209 roomsList->setColumnCount(7); |
|
210 roomsList->setHorizontalHeaderLabels( |
|
211 QStringList() << |
|
212 QTableWidget::tr("Room Name") << |
|
213 QTableWidget::tr("C") << |
|
214 QTableWidget::tr("T") << |
|
215 QTableWidget::tr("Owner") << |
|
216 QTableWidget::tr("Map") << |
|
217 QTableWidget::tr("Rules") << |
|
218 QTableWidget::tr("Weapons") |
|
219 ); |
|
220 |
|
221 // set minimum sizes |
|
222 // roomsList->horizontalHeader()->resizeSection(0, 200); |
|
223 // roomsList->horizontalHeader()->resizeSection(1, 50); |
|
224 // roomsList->horizontalHeader()->resizeSection(2, 50); |
|
225 // roomsList->horizontalHeader()->resizeSection(3, 100); |
|
226 // roomsList->horizontalHeader()->resizeSection(4, 100); |
|
227 // roomsList->horizontalHeader()->resizeSection(5, 100); |
|
228 // roomsList->horizontalHeader()->resizeSection(6, 100); |
|
229 |
|
230 // set resize modes |
|
231 // roomsList->horizontalHeader()->setResizeMode(QHeaderView::Interactive); |
|
232 |
|
233 bool gameCanBeJoined = true; |
|
234 |
|
235 if (list.size() % 8) |
|
236 return; |
|
237 |
|
238 roomsList->setRowCount(list.size() / 8); |
|
239 for(int i = 0, r = 0; i < list.size(); i += 8, r++) |
|
240 { |
|
241 // if we are joining a game |
|
242 // TODO: Should NOT be done here |
|
243 if (gameInLobby) { |
|
244 if (gameInLobbyName == list[i + 1]) { |
|
245 gameCanBeJoined = list[i].compare("True"); |
|
246 } |
|
247 } |
|
248 |
|
249 // check filter settings |
|
250 #define NO_FILTER_MATCH roomsList->setRowCount(roomsList->rowCount() - 1); --r; continue |
|
251 |
|
252 if (list[i].compare("True") && CBState->currentIndex() == 2) { NO_FILTER_MATCH; } |
|
253 if (list[i].compare("False") && CBState->currentIndex() == 1) { NO_FILTER_MATCH; } |
|
254 if (CBRules->currentIndex() != 0 && list[i + 6].compare(CBRules->currentText())) { NO_FILTER_MATCH; } |
|
255 if (CBWeapons->currentIndex() != 0 && list[i + 7].compare(CBWeapons->currentText())) { NO_FILTER_MATCH; } |
|
256 bool found = list[i + 1].contains(searchText->text(), Qt::CaseInsensitive); |
|
257 if (!found) { |
|
258 for (int a = 4; a <= 7; ++a) { |
|
259 QString compString = list[i + a]; |
|
260 if (a == 5 && compString == "+rnd+") { |
|
261 compString = "Random Map"; |
|
262 } else if (a == 5 && compString == "+maze+") { |
|
263 compString = "Random Maze"; |
|
264 } else if (a == 5 && compString == "+drawn+") { |
|
265 compString = "Drawn Map"; |
|
266 } |
|
267 if (compString.contains(searchText->text(), Qt::CaseInsensitive)) { |
|
268 found = true; |
|
269 break; |
|
270 } |
|
271 } |
|
272 } |
|
273 if (!searchText->text().isEmpty() && !found) { NO_FILTER_MATCH; } |
|
274 |
|
275 QTableWidgetItem * item; |
|
276 item = new QTableWidgetItem(list[i + 1]); // room name |
|
277 item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); |
|
278 |
|
279 // pick appropriate room icon and tooltip (game in progress yes/no; later maybe locked rooms etc.) |
|
280 if(list[i].compare("True")) |
|
281 { |
|
282 item->setIcon(QIcon(":/res/iconTime.png"));// game is in lobby |
|
283 item->setToolTip(tr("This game is in lobby.\nYou may join and start playing once the game starts.")); |
|
284 } |
|
285 else |
|
286 { |
|
287 item->setIcon(QIcon(":/res/iconDamage.png"));// game has started |
|
288 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.")); |
|
289 } |
|
290 |
|
291 roomsList->setItem(r, 0, item); |
|
292 |
|
293 item = new QTableWidgetItem(list[i + 2]); // number of clients |
|
294 item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); |
|
295 item->setTextAlignment(Qt::AlignCenter); |
|
296 item->setToolTip(tr("There are %1 clients connected to this room.", "", list[i + 2].toInt()).arg(list[i + 2])); |
|
297 roomsList->setItem(r, 1, item); |
|
298 |
|
299 item = new QTableWidgetItem(list[i + 3]); // number of teams |
|
300 item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); |
|
301 item->setTextAlignment(Qt::AlignCenter); |
|
302 item->setToolTip(tr("There are %1 teams participating in this room.", "", list[i + 3].toInt()).arg(list[i + 3])); |
|
303 //Should we highlight "full" games? Might get misinterpreted |
|
304 //if(list[i + 3].toInt() >= cMaxTeams) |
|
305 // item->setForeground(red); |
|
306 roomsList->setItem(r, 2, item); |
|
307 |
|
308 item = new QTableWidgetItem(list[i + 4].left(15)); // name of host |
|
309 item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); |
|
310 item->setToolTip(tr("%1 is the host. He may adjust settings and start the game.").arg(list[i + 4])); |
|
311 roomsList->setItem(r, 3, item); |
|
312 |
|
313 if(list[i + 5] == "+rnd+") |
|
314 { |
|
315 item = new QTableWidgetItem(tr("Random Map")); // selected map (is randomized) |
|
316 // FIXME - need real icons. Disabling until then |
|
317 // item->setIcon(QIcon(":/res/mapRandom.png")); |
|
318 } |
|
319 else if (list[i+5] == "+maze+") |
|
320 { |
|
321 item = new QTableWidgetItem(tr("Random Maze")); |
|
322 // FIXME - need real icons. Disabling until then |
|
323 // item->setIcon(QIcon(":/res/mapMaze.png")); |
|
324 } |
|
325 else |
|
326 { |
|
327 item = new QTableWidgetItem(list[i + 5]); // selected map |
|
328 |
|
329 // check to see if we've got this map |
|
330 // not perfect but a start |
|
331 if(!mapList->contains(list[i + 5])) |
|
332 { |
|
333 item->setForeground(red); |
|
334 item->setIcon(QIcon(":/res/mapMissing.png")); |
|
335 } |
|
336 else |
|
337 { |
|
338 // todo: mission icon? |
|
339 // FIXME - need real icons. Disabling until then |
|
340 // item->setIcon(QIcon(":/res/mapCustom.png")); |
|
341 } |
|
342 } |
|
343 |
|
344 item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); |
|
345 item->setToolTip(tr("Games may be played on precreated or randomized maps.")); |
|
346 roomsList->setItem(r, 4, item); |
|
347 |
|
348 item = new QTableWidgetItem(list[i + 6].left(24)); // selected game scheme |
|
349 item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); |
|
350 item->setToolTip(tr("The Game Scheme defines general options and preferences like Round Time, Sudden Death or Vampirism.")); |
|
351 roomsList->setItem(r, 5, item); |
|
352 |
|
353 item = new QTableWidgetItem(list[i + 7].left(24)); // selected weapon scheme |
|
354 item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); |
|
355 item->setToolTip(tr("The Weapon Scheme defines available weapons and their ammunition count.")); |
|
356 roomsList->setItem(r, 6, item); |
|
357 |
|
358 if(!list[i + 1].compare(selection) && !selection.isEmpty()) |
|
359 roomsList->selectionModel()->setCurrentIndex(roomsList->model()->index(r, 0), QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows); |
|
360 } |
|
361 |
|
362 roomsList->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch); |
|
363 roomsList->horizontalHeader()->setResizeMode(1, QHeaderView::ResizeToContents); |
|
364 roomsList->horizontalHeader()->setResizeMode(2, QHeaderView::ResizeToContents); |
|
365 roomsList->horizontalHeader()->setResizeMode(3, QHeaderView::ResizeToContents); |
|
366 roomsList->horizontalHeader()->setResizeMode(4, QHeaderView::ResizeToContents); |
|
367 roomsList->horizontalHeader()->setResizeMode(5, QHeaderView::ResizeToContents); |
|
368 roomsList->horizontalHeader()->setResizeMode(6, QHeaderView::ResizeToContents); |
|
369 |
|
370 // TODO: Should NOT be done here |
|
371 if (gameInLobby) { |
|
372 gameInLobby = false; |
|
373 if (gameCanBeJoined) { |
|
374 emit askForJoinRoom(gameInLobbyName); |
|
375 } else { |
|
376 emit askJoinConfirmation(gameInLobbyName); |
|
377 } |
|
378 } |
|
379 |
|
380 // roomsList->resizeColumnsToContents(); |
|
381 } |
|
382 |
|
383 void PageRoomsList::onCreateClick() |
|
384 { |
|
385 if (roomName->text().size()) |
|
386 emit askForCreateRoom(roomName->text()); |
|
387 else |
|
388 QMessageBox::critical(this, |
|
389 tr("Error"), |
|
390 tr("Please enter room name"), |
|
391 tr("OK")); |
|
392 } |
|
393 |
|
394 void PageRoomsList::onJoinClick() |
|
395 { |
|
396 QTableWidgetItem * curritem = roomsList->item(roomsList->currentRow(), 0); |
|
397 if (!curritem) |
|
398 { |
|
399 QMessageBox::critical(this, |
|
400 tr("Error"), |
|
401 tr("Please select room from the list"), |
|
402 tr("OK")); |
|
403 return; |
|
404 } |
|
405 |
|
406 for (int i = 0; i < listFromServer.size(); i += 8) { |
|
407 if (listFromServer[i + 1] == curritem->data(Qt::DisplayRole).toString()) { |
|
408 gameInLobby = listFromServer[i].compare("True"); |
|
409 break; |
|
410 } |
|
411 } |
|
412 |
|
413 if (gameInLobby) { |
|
414 gameInLobbyName = curritem->data(Qt::DisplayRole).toString(); |
|
415 emit askForRoomList(); |
|
416 } else { |
|
417 emit askForJoinRoom(curritem->data(Qt::DisplayRole).toString()); |
|
418 } |
|
419 } |
|
420 |
|
421 void PageRoomsList::onRefreshClick() |
|
422 { |
|
423 emit askForRoomList(); |
|
424 } |
|
425 |
|
426 void PageRoomsList::onClearClick() |
|
427 { |
|
428 CBState->setCurrentIndex(0); |
|
429 CBRules->setCurrentIndex(0); |
|
430 CBWeapons->setCurrentIndex(0); |
|
431 searchText->clear(); |
|
432 } |
|
433 |
|
434 void PageRoomsList::onJoinConfirmation(const QString & room) |
|
435 { |
|
436 if (QMessageBox::warning(this, |
|
437 tr("Warning"), |
|
438 tr("The game you are trying to join has started.\nDo you still want to join the room?"), |
|
439 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) |
|
440 { |
|
441 emit askForJoinRoom(room); |
|
442 } |
|
443 } |
|
444 |
|
445 void PageRoomsList::updateNickCounter(int cnt) |
|
446 { |
|
447 lblCount->setText(tr("%1 players online", 0, cnt).arg(cnt)); |
|
448 } |