QTfrontend/ui/page/pagenet.cpp
changeset 6060 fdfc01419815
parent 6042 8b5345758f62
child 6171 8627698134b6
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 <QGroupBox>
       
    22 #include <QTableView>
       
    23 #include <QMessageBox>
       
    24 #include <QHeaderView>
       
    25 
       
    26 #include "pagenet.h"
       
    27 #include "hwconsts.h"
       
    28 #include "netudpwidget.h"
       
    29 
       
    30 QLayout * PageNet::bodyLayoutDefinition()
       
    31 {
       
    32     QGridLayout * pageLayout = new QGridLayout();
       
    33 
       
    34     pageLayout->setColumnStretch(0, 1);
       
    35     pageLayout->setColumnStretch(1, 1);
       
    36     pageLayout->setColumnStretch(2, 1);
       
    37 
       
    38     BtnNetSvrStart = new QPushButton(this);
       
    39     BtnNetSvrStart->setFont(*font14);
       
    40     BtnNetSvrStart->setText(QPushButton::tr("Start server"));
       
    41     BtnNetSvrStart->setVisible(haveServer);
       
    42     pageLayout->addWidget(BtnNetSvrStart, 4, 2);
       
    43 
       
    44     ConnGroupBox = new QGroupBox(this);
       
    45     ConnGroupBox->setTitle(QGroupBox::tr("Net game"));
       
    46     pageLayout->addWidget(ConnGroupBox, 2, 0, 1, 3);
       
    47     GBClayout = new QGridLayout(ConnGroupBox);
       
    48     GBClayout->setColumnStretch(0, 1);
       
    49     GBClayout->setColumnStretch(1, 1);
       
    50     GBClayout->setColumnStretch(2, 1);
       
    51 
       
    52     BtnNetConnect = new QPushButton(ConnGroupBox);
       
    53     BtnNetConnect->setFont(*font14);
       
    54     BtnNetConnect->setText(QPushButton::tr("Connect"));
       
    55     GBClayout->addWidget(BtnNetConnect, 2, 2);
       
    56 
       
    57     tvServersList = new QTableView(ConnGroupBox);
       
    58     tvServersList->setSelectionBehavior(QAbstractItemView::SelectRows);
       
    59     GBClayout->addWidget(tvServersList, 1, 0, 1, 3);
       
    60 
       
    61     BtnUpdateSList = new QPushButton(ConnGroupBox);
       
    62     BtnUpdateSList->setFont(*font14);
       
    63     BtnUpdateSList->setText(QPushButton::tr("Update"));
       
    64     GBClayout->addWidget(BtnUpdateSList, 2, 0);
       
    65 
       
    66     BtnSpecifyServer = new QPushButton(ConnGroupBox);
       
    67     BtnSpecifyServer->setFont(*font14);
       
    68     BtnSpecifyServer->setText(QPushButton::tr("Specify"));
       
    69     GBClayout->addWidget(BtnSpecifyServer, 2, 1);
       
    70 
       
    71     return pageLayout;
       
    72 }
       
    73 
       
    74 void PageNet::connectSignals()
       
    75 {
       
    76     connect(BtnNetConnect, SIGNAL(clicked()), this, SLOT(slotConnect()));
       
    77 }
       
    78 
       
    79 PageNet::PageNet(QWidget* parent) : AbstractPage(parent)
       
    80 {
       
    81     initPage();
       
    82 }
       
    83 
       
    84 void PageNet::updateServersList()
       
    85 {
       
    86     tvServersList->setModel(new HWNetUdpModel(tvServersList));
       
    87 
       
    88     tvServersList->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
       
    89 
       
    90     static_cast<HWNetServersModel *>(tvServersList->model())->updateList();
       
    91 
       
    92     connect(BtnUpdateSList, SIGNAL(clicked()), static_cast<HWNetServersModel *>(tvServersList->model()), SLOT(updateList()));
       
    93     connect(tvServersList, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(slotConnect()));
       
    94 }
       
    95 
       
    96 void PageNet::slotConnect()
       
    97 {
       
    98     HWNetServersModel * model = static_cast<HWNetServersModel *>(tvServersList->model());
       
    99     QModelIndex mi = tvServersList->currentIndex();
       
   100     if(!mi.isValid())
       
   101     {
       
   102         QMessageBox::information(this, tr("Error"), tr("Please select server from the list above"));
       
   103         return;
       
   104     }
       
   105     QString host = model->index(mi.row(), 1).data().toString();
       
   106     quint16 port = model->index(mi.row(), 2).data().toUInt();
       
   107 
       
   108     emit connectClicked(host, port);
       
   109 }