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