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