QTfrontend/pageroomslist.cpp
changeset 6101 5a4ea2c7b9df
parent 5801 531f64292489
parent 6100 e6426c6b2882
child 6102 97565ab4afe9
equal deleted inserted replaced
5801:531f64292489 6101:5a4ea2c7b9df
     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     BtnBack = addButton(":/res/Exit.png", pageLayout, 6, 0, true);
       
   112 
       
   113     lblCount = new QLabel(this);
       
   114     pageLayout->addWidget(lblCount, 6, 1, Qt::AlignHCenter);
       
   115     lblCount->setText("?");
       
   116     lblCount->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
       
   117 
       
   118     connect(chatWidget, SIGNAL(nickCountUpdate(const int)), this, SLOT(updateNickCounter(const int)));
       
   119 
       
   120     BtnAdmin = addButton(tr("Admin features"), pageLayout, 6, 2);
       
   121 
       
   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);
       
   132     
       
   133     gameInLobby = false;
       
   134 }
       
   135 
       
   136 void PageRoomsList::setAdmin(bool flag)
       
   137 {
       
   138     BtnAdmin->setVisible(flag);
       
   139 }
       
   140 
       
   141 void PageRoomsList::setRoomsList(const QStringList & list)
       
   142 {
       
   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));
       
   147 
       
   148     listFromServer = list;
       
   149     
       
   150     QString selection = "";
       
   151     
       
   152     if(QTableWidgetItem *item = roomsList->item(roomsList->currentRow(), 0))
       
   153         selection = item->text();
       
   154     
       
   155     roomsList->clear();
       
   156     roomsList->setColumnCount(7);
       
   157     roomsList->setHorizontalHeaderLabels(
       
   158             QStringList() <<
       
   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")
       
   166             );
       
   167 
       
   168     // set minimum sizes
       
   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);
       
   176 
       
   177     // set resize modes
       
   178 //  roomsList->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
       
   179 
       
   180     bool gameCanBeJoined = true;
       
   181 
       
   182     if (list.size() % 8)
       
   183         return;
       
   184 
       
   185     roomsList->setRowCount(list.size() / 8);
       
   186     for(int i = 0, r = 0; i < list.size(); i += 8, r++)
       
   187     {
       
   188         // if we are joining a game
       
   189         // TODO: Should NOT be done here
       
   190         if (gameInLobby) {
       
   191             if (gameInLobbyName == list[i + 1]) {
       
   192                 gameCanBeJoined = list[i].compare("True");
       
   193             }
       
   194         }
       
   195         
       
   196         // check filter settings
       
   197         #define NO_FILTER_MATCH roomsList->setRowCount(roomsList->rowCount() - 1); --r; continue
       
   198         
       
   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);
       
   204         if (!found) {
       
   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";
       
   213                 }
       
   214                 if (compString.contains(searchText->text(), Qt::CaseInsensitive)) {
       
   215                     found = true;
       
   216                     break;
       
   217                 }
       
   218             }
       
   219         }
       
   220         if (!searchText->text().isEmpty() && !found) { NO_FILTER_MATCH; }
       
   221         
       
   222         QTableWidgetItem * item;
       
   223         item = new QTableWidgetItem(list[i + 1]); // room name
       
   224         item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
       
   225         
       
   226         // pick appropriate room icon and tooltip (game in progress yes/no; later maybe locked rooms etc.)
       
   227         if(list[i].compare("True"))
       
   228         {
       
   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."));
       
   231         }
       
   232         else
       
   233         {
       
   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."));
       
   236         }
       
   237 
       
   238         roomsList->setItem(r, 0, item);
       
   239 
       
   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);
       
   245 
       
   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);
       
   254 
       
   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);
       
   259 
       
   260         if(list[i + 5] == "+rnd+")
       
   261         {
       
   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"));
       
   265         }
       
   266         else if (list[i+5] == "+maze+")
       
   267         {
       
   268             item = new QTableWidgetItem(tr("Random Maze"));
       
   269 // FIXME - need real icons. Disabling until then
       
   270 //            item->setIcon(QIcon(":/res/mapMaze.png"));
       
   271         }
       
   272         else
       
   273         {
       
   274             item = new QTableWidgetItem(list[i + 5]); // selected map
       
   275             
       
   276             // check to see if we've got this map
       
   277             // not perfect but a start
       
   278             if(!mapList->contains(list[i + 5]))
       
   279             {
       
   280                 item->setForeground(red);
       
   281                 item->setIcon(QIcon(":/res/mapMissing.png"));
       
   282             }
       
   283             else
       
   284             {
       
   285                // todo: mission icon?
       
   286 // FIXME - need real icons. Disabling until then
       
   287 //               item->setIcon(QIcon(":/res/mapCustom.png"));
       
   288             }
       
   289         }
       
   290         
       
   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);
       
   294 
       
   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);
       
   299 
       
   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);
       
   304 
       
   305         if(!list[i + 1].compare(selection) && !selection.isEmpty())
       
   306             roomsList->selectionModel()->setCurrentIndex(roomsList->model()->index(r, 0), QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
       
   307     }
       
   308 
       
   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);
       
   316 
       
   317     // TODO: Should NOT be done here
       
   318     if (gameInLobby) {
       
   319         gameInLobby = false;
       
   320         if (gameCanBeJoined) {
       
   321             emit askForJoinRoom(gameInLobbyName);
       
   322         } else {
       
   323             emit askJoinConfirmation(gameInLobbyName);
       
   324         }
       
   325     }
       
   326 
       
   327 //  roomsList->resizeColumnsToContents();
       
   328 }
       
   329 
       
   330 void PageRoomsList::onCreateClick()
       
   331 {
       
   332     if (roomName->text().size())
       
   333         emit askForCreateRoom(roomName->text());
       
   334     else
       
   335         QMessageBox::critical(this,
       
   336                 tr("Error"),
       
   337                 tr("Please enter room name"),
       
   338                 tr("OK"));
       
   339 }
       
   340 
       
   341 void PageRoomsList::onJoinClick()
       
   342 {
       
   343     QTableWidgetItem * curritem = roomsList->item(roomsList->currentRow(), 0);
       
   344     if (!curritem)
       
   345     {
       
   346         QMessageBox::critical(this,
       
   347                 tr("Error"),
       
   348                 tr("Please select room from the list"),
       
   349                 tr("OK"));
       
   350         return;
       
   351     }
       
   352 
       
   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");
       
   356             break;
       
   357         }
       
   358     }
       
   359     
       
   360     if (gameInLobby) {
       
   361         gameInLobbyName = curritem->data(Qt::DisplayRole).toString();
       
   362         emit askForRoomList();
       
   363     } else {
       
   364         emit askForJoinRoom(curritem->data(Qt::DisplayRole).toString());
       
   365     }
       
   366 }
       
   367 
       
   368 void PageRoomsList::onRefreshClick()
       
   369 {
       
   370     emit askForRoomList();
       
   371 }
       
   372 
       
   373 void PageRoomsList::onClearClick()
       
   374 {
       
   375     CBState->setCurrentIndex(0);
       
   376     CBRules->setCurrentIndex(0);
       
   377     CBWeapons->setCurrentIndex(0);
       
   378     searchText->clear();
       
   379 }
       
   380 
       
   381 void PageRoomsList::onJoinConfirmation(const QString & room)
       
   382 {
       
   383     if (QMessageBox::warning(this,
       
   384         tr("Warning"),
       
   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)
       
   387     {
       
   388         emit askForJoinRoom(room);
       
   389     }
       
   390 }
       
   391 
       
   392 void PageRoomsList::updateNickCounter(int cnt)
       
   393 {
       
   394     lblCount->setText(tr("%1 players online", 0, cnt).arg(cnt));
       
   395 }
       
   396