QTfrontend/pageroomslist.cpp
branchhedgeroid
changeset 6224 42b256eca362
parent 6055 88cfcd9161d3
parent 6223 cc3eb9b7230f
child 6226 3106add9a5bf
equal deleted inserted replaced
6055:88cfcd9161d3 6224:42b256eca362
     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 PageRoomsList::PageRoomsList(QWidget* parent, QSettings * gameSettings, SDLInteraction * sdli) :
       
    34   AbstractPage(parent)
       
    35 {
       
    36     QGridLayout * pageLayout = new QGridLayout(this);
       
    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     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);
       
    68 
       
    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);
       
    80 
       
    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());
       
    88     }
       
    89     filterLayout->addWidget(weaponLabel);
       
    90     filterLayout->addWidget(CBWeapons);
       
    91     filterLayout->addSpacing(30);
       
    92 
       
    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);
       
    99 
       
   100     pageLayout->addLayout(filterLayout, 4, 0, 1, 2);
       
   101 
       
   102     chatWidget = new HWChatWidget(this, gameSettings, sdli, false);
       
   103     pageLayout->addWidget(chatWidget, 5, 0, 1, 3);
       
   104     pageLayout->setRowStretch(5, 350);
       
   105 
       
   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);
       
   110 
       
   111 
       
   112     BtnBack = addButton(":/res/Exit.png", pageLayout, 6, 0, true);
       
   113     connect(BtnBack, SIGNAL(clicked()), this, SIGNAL(goBack()));
       
   114 
       
   115 
       
   116     lblCount = new QLabel(this);
       
   117     pageLayout->addWidget(lblCount, 6, 1, Qt::AlignHCenter);
       
   118     lblCount->setText("?");
       
   119     lblCount->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
       
   120 
       
   121     connect(chatWidget, SIGNAL(nickCountUpdate(const int)), this, SLOT(updateNickCounter(const int)));
       
   122 
       
   123     BtnAdmin = addButton(tr("Admin features"), pageLayout, 6, 2);
       
   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     gameInLobby = false;
       
   137 }
       
   138 
       
   139 void PageRoomsList::setAdmin(bool flag)
       
   140 {
       
   141     BtnAdmin->setVisible(flag);
       
   142 }
       
   143 
       
   144 void PageRoomsList::setRoomsList(const QStringList & list)
       
   145 {
       
   146     QBrush red(QColor(255, 0, 0));
       
   147     QBrush orange(QColor(127, 127, 0));
       
   148     QBrush yellow(QColor(255, 255, 0));
       
   149     QBrush green(QColor(0, 255, 0));
       
   150 
       
   151     listFromServer = list;
       
   152     
       
   153     QString selection = "";
       
   154     
       
   155     if(QTableWidgetItem *item = roomsList->item(roomsList->currentRow(), 0))
       
   156         selection = item->text();
       
   157     
       
   158     roomsList->clear();
       
   159     roomsList->setColumnCount(7);
       
   160     roomsList->setHorizontalHeaderLabels(
       
   161             QStringList() <<
       
   162             QTableWidget::tr("Room Name") <<
       
   163             QTableWidget::tr("C") <<
       
   164             QTableWidget::tr("T") <<
       
   165             QTableWidget::tr("Owner") <<
       
   166             QTableWidget::tr("Map") <<
       
   167             QTableWidget::tr("Rules") <<
       
   168             QTableWidget::tr("Weapons")
       
   169             );
       
   170 
       
   171     // set minimum sizes
       
   172 //  roomsList->horizontalHeader()->resizeSection(0, 200);
       
   173 //  roomsList->horizontalHeader()->resizeSection(1, 50);
       
   174 //  roomsList->horizontalHeader()->resizeSection(2, 50);
       
   175 //  roomsList->horizontalHeader()->resizeSection(3, 100);
       
   176 //  roomsList->horizontalHeader()->resizeSection(4, 100);
       
   177 //  roomsList->horizontalHeader()->resizeSection(5, 100);
       
   178 //  roomsList->horizontalHeader()->resizeSection(6, 100);
       
   179 
       
   180     // set resize modes
       
   181 //  roomsList->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
       
   182 
       
   183     bool gameCanBeJoined = true;
       
   184 
       
   185     if (list.size() % 8)
       
   186         return;
       
   187 
       
   188     roomsList->setRowCount(list.size() / 8);
       
   189     for(int i = 0, r = 0; i < list.size(); i += 8, r++)
       
   190     {
       
   191         // if we are joining a game
       
   192         // TODO: Should NOT be done here
       
   193         if (gameInLobby) {
       
   194             if (gameInLobbyName == list[i + 1]) {
       
   195                 gameCanBeJoined = list[i].compare("True");
       
   196             }
       
   197         }
       
   198         
       
   199         // check filter settings
       
   200         #define NO_FILTER_MATCH roomsList->setRowCount(roomsList->rowCount() - 1); --r; continue
       
   201         
       
   202         if (list[i].compare("True") && CBState->currentIndex() == 2) { NO_FILTER_MATCH; }
       
   203         if (list[i].compare("False") && CBState->currentIndex() == 1) { NO_FILTER_MATCH; }
       
   204         if (CBRules->currentIndex() != 0 && list[i + 6].compare(CBRules->currentText())) { NO_FILTER_MATCH; }
       
   205         if (CBWeapons->currentIndex() != 0 && list[i + 7].compare(CBWeapons->currentText())) { NO_FILTER_MATCH; }
       
   206         bool found = list[i + 1].contains(searchText->text(), Qt::CaseInsensitive);
       
   207         if (!found) {
       
   208             for (int a = 4; a <= 7; ++a) {
       
   209                 QString compString = list[i + a];
       
   210                 if (a == 5 && compString == "+rnd+") {
       
   211                     compString = "Random Map";
       
   212                 } else if (a == 5 && compString == "+maze+") {
       
   213                     compString = "Random Maze";
       
   214                 } else if (a == 5 && compString == "+drawn+") {
       
   215                     compString = "Drawn Map";
       
   216                 }
       
   217                 if (compString.contains(searchText->text(), Qt::CaseInsensitive)) {
       
   218                     found = true;
       
   219                     break;
       
   220                 }
       
   221             }
       
   222         }
       
   223         if (!searchText->text().isEmpty() && !found) { NO_FILTER_MATCH; }
       
   224         
       
   225         QTableWidgetItem * item;
       
   226         item = new QTableWidgetItem(list[i + 1]); // room name
       
   227         item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
       
   228         
       
   229         // pick appropriate room icon and tooltip (game in progress yes/no; later maybe locked rooms etc.)
       
   230         if(list[i].compare("True"))
       
   231         {
       
   232             item->setIcon(QIcon(":/res/iconTime.png"));// game is in lobby
       
   233             item->setToolTip(tr("This game is in lobby.\nYou may join and start playing once the game starts."));
       
   234         }
       
   235         else
       
   236         {
       
   237             item->setIcon(QIcon(":/res/iconDamage.png"));// game has started
       
   238             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."));
       
   239         }
       
   240 
       
   241         roomsList->setItem(r, 0, item);
       
   242 
       
   243         item = new QTableWidgetItem(list[i + 2]); // number of clients
       
   244         item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
       
   245         item->setTextAlignment(Qt::AlignCenter);
       
   246         item->setToolTip(tr("There are %1 clients connected to this room.", "", list[i + 2].toInt()).arg(list[i + 2]));
       
   247         roomsList->setItem(r, 1, item);
       
   248 
       
   249         item = new QTableWidgetItem(list[i + 3]); // number of teams
       
   250         item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
       
   251         item->setTextAlignment(Qt::AlignCenter);
       
   252         item->setToolTip(tr("There are %1 teams participating in this room.", "", list[i + 3].toInt()).arg(list[i + 3]));
       
   253         //Should we highlight "full" games? Might get misinterpreted
       
   254         //if(list[i + 3].toInt() >= cMaxTeams)
       
   255         //    item->setForeground(red);
       
   256         roomsList->setItem(r, 2, item);
       
   257 
       
   258         item = new QTableWidgetItem(list[i + 4].left(15)); // name of host
       
   259         item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
       
   260         item->setToolTip(tr("%1 is the host. He may adjust settings and start the game.").arg(list[i + 4]));
       
   261         roomsList->setItem(r, 3, item);
       
   262 
       
   263         if(list[i + 5] == "+rnd+")
       
   264         {
       
   265             item = new QTableWidgetItem(tr("Random Map")); // selected map (is randomized)
       
   266 // FIXME - need real icons. Disabling until then
       
   267 //            item->setIcon(QIcon(":/res/mapRandom.png"));
       
   268         }
       
   269         else if (list[i+5] == "+maze+")
       
   270         {
       
   271             item = new QTableWidgetItem(tr("Random Maze"));
       
   272 // FIXME - need real icons. Disabling until then
       
   273 //            item->setIcon(QIcon(":/res/mapMaze.png"));
       
   274         }
       
   275         else
       
   276         {
       
   277             item = new QTableWidgetItem(list[i + 5]); // selected map
       
   278             
       
   279             // check to see if we've got this map
       
   280             // not perfect but a start
       
   281             if(!mapList->contains(list[i + 5]))
       
   282             {
       
   283                 item->setForeground(red);
       
   284                 item->setIcon(QIcon(":/res/mapMissing.png"));
       
   285             }
       
   286             else
       
   287             {
       
   288                // todo: mission icon?
       
   289 // FIXME - need real icons. Disabling until then
       
   290 //               item->setIcon(QIcon(":/res/mapCustom.png"));
       
   291             }
       
   292         }
       
   293         
       
   294         item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
       
   295         item->setToolTip(tr("Games may be played on precreated or randomized maps."));
       
   296         roomsList->setItem(r, 4, item);
       
   297 
       
   298         item = new QTableWidgetItem(list[i + 6].left(24)); // selected game scheme
       
   299         item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
       
   300         item->setToolTip(tr("The Game Scheme defines general options and preferences like Round Time, Sudden Death or Vampirism."));
       
   301         roomsList->setItem(r, 5, item);
       
   302 
       
   303         item = new QTableWidgetItem(list[i + 7].left(24)); // selected weapon scheme
       
   304         item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
       
   305         item->setToolTip(tr("The Weapon Scheme defines available weapons and their ammunition count."));
       
   306         roomsList->setItem(r, 6, item);
       
   307 
       
   308         if(!list[i + 1].compare(selection) && !selection.isEmpty())
       
   309             roomsList->selectionModel()->setCurrentIndex(roomsList->model()->index(r, 0), QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
       
   310     }
       
   311 
       
   312     roomsList->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
       
   313     roomsList->horizontalHeader()->setResizeMode(1, QHeaderView::ResizeToContents);
       
   314     roomsList->horizontalHeader()->setResizeMode(2, QHeaderView::ResizeToContents);
       
   315     roomsList->horizontalHeader()->setResizeMode(3, QHeaderView::ResizeToContents);
       
   316     roomsList->horizontalHeader()->setResizeMode(4, QHeaderView::ResizeToContents);
       
   317     roomsList->horizontalHeader()->setResizeMode(5, QHeaderView::ResizeToContents);
       
   318     roomsList->horizontalHeader()->setResizeMode(6, QHeaderView::ResizeToContents);
       
   319 
       
   320     // TODO: Should NOT be done here
       
   321     if (gameInLobby) {
       
   322         gameInLobby = false;
       
   323         if (gameCanBeJoined) {
       
   324             emit askForJoinRoom(gameInLobbyName);
       
   325         } else {
       
   326             emit askJoinConfirmation(gameInLobbyName);
       
   327         }
       
   328     }
       
   329 
       
   330 //  roomsList->resizeColumnsToContents();
       
   331 }
       
   332 
       
   333 void PageRoomsList::onCreateClick()
       
   334 {
       
   335     if (roomName->text().size())
       
   336         emit askForCreateRoom(roomName->text());
       
   337     else
       
   338         QMessageBox::critical(this,
       
   339                 tr("Error"),
       
   340                 tr("Please enter room name"),
       
   341                 tr("OK"));
       
   342 }
       
   343 
       
   344 void PageRoomsList::onJoinClick()
       
   345 {
       
   346     QTableWidgetItem * curritem = roomsList->item(roomsList->currentRow(), 0);
       
   347     if (!curritem)
       
   348     {
       
   349         QMessageBox::critical(this,
       
   350                 tr("Error"),
       
   351                 tr("Please select room from the list"),
       
   352                 tr("OK"));
       
   353         return;
       
   354     }
       
   355 
       
   356     for (int i = 0; i < listFromServer.size(); i += 8) {
       
   357         if (listFromServer[i + 1] == curritem->data(Qt::DisplayRole).toString()) {
       
   358             gameInLobby = listFromServer[i].compare("True");
       
   359             break;
       
   360         }
       
   361     }
       
   362     
       
   363     if (gameInLobby) {
       
   364         gameInLobbyName = curritem->data(Qt::DisplayRole).toString();
       
   365         emit askForRoomList();
       
   366     } else {
       
   367         emit askForJoinRoom(curritem->data(Qt::DisplayRole).toString());
       
   368     }
       
   369 }
       
   370 
       
   371 void PageRoomsList::onRefreshClick()
       
   372 {
       
   373     emit askForRoomList();
       
   374 }
       
   375 
       
   376 void PageRoomsList::onClearClick()
       
   377 {
       
   378     CBState->setCurrentIndex(0);
       
   379     CBRules->setCurrentIndex(0);
       
   380     CBWeapons->setCurrentIndex(0);
       
   381     searchText->clear();
       
   382 }
       
   383 
       
   384 void PageRoomsList::onJoinConfirmation(const QString & room)
       
   385 {
       
   386     if (QMessageBox::warning(this,
       
   387         tr("Warning"),
       
   388         tr("The game you are trying to join has started.\nDo you still want to join the room?"),
       
   389         QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
       
   390     {
       
   391         emit askForJoinRoom(room);
       
   392     }
       
   393 }
       
   394 
       
   395 void PageRoomsList::updateNickCounter(int cnt)
       
   396 {
       
   397     lblCount->setText(tr("%1 players online", 0, cnt).arg(cnt));
       
   398 }
       
   399