QTfrontend/pagenet.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 <QGroupBox>
       
    22 #include <QTableView>
       
    23 #include <QMessageBox>
       
    24 #include <QHeaderView>
       
    25 
       
    26 #include "pages.h"
       
    27 #include "hwconsts.h"
       
    28 #include "netudpwidget.h"
       
    29 
       
    30 PageNet::PageNet(QWidget* parent) : AbstractPage(parent)
       
    31 {
       
    32     QFont * font14 = new QFont("MS Shell Dlg", 14);
       
    33     QGridLayout * pageLayout = new QGridLayout(this);
       
    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     BtnBack = addButton(":/res/Exit.png", pageLayout, 4, 0, true);
       
    45 
       
    46     ConnGroupBox = new QGroupBox(this);
       
    47     ConnGroupBox->setTitle(QGroupBox::tr("Net game"));
       
    48     pageLayout->addWidget(ConnGroupBox, 2, 0, 1, 3);
       
    49     GBClayout = new QGridLayout(ConnGroupBox);
       
    50     GBClayout->setColumnStretch(0, 1);
       
    51     GBClayout->setColumnStretch(1, 1);
       
    52     GBClayout->setColumnStretch(2, 1);
       
    53 
       
    54     BtnNetConnect = new QPushButton(ConnGroupBox);
       
    55     BtnNetConnect->setFont(*font14);
       
    56     BtnNetConnect->setText(QPushButton::tr("Connect"));
       
    57     GBClayout->addWidget(BtnNetConnect, 2, 2);
       
    58 
       
    59     tvServersList = new QTableView(ConnGroupBox);
       
    60     tvServersList->setSelectionBehavior(QAbstractItemView::SelectRows);
       
    61     GBClayout->addWidget(tvServersList, 1, 0, 1, 3);
       
    62 
       
    63     BtnUpdateSList = new QPushButton(ConnGroupBox);
       
    64     BtnUpdateSList->setFont(*font14);
       
    65     BtnUpdateSList->setText(QPushButton::tr("Update"));
       
    66     GBClayout->addWidget(BtnUpdateSList, 2, 0);
       
    67 
       
    68     BtnSpecifyServer = new QPushButton(ConnGroupBox);
       
    69     BtnSpecifyServer->setFont(*font14);
       
    70     BtnSpecifyServer->setText(QPushButton::tr("Specify"));
       
    71     GBClayout->addWidget(BtnSpecifyServer, 2, 1);
       
    72 
       
    73     connect(BtnNetConnect, SIGNAL(clicked()), this, SLOT(slotConnect()));
       
    74 }
       
    75 
       
    76 void PageNet::updateServersList()
       
    77 {
       
    78     tvServersList->setModel(new HWNetUdpModel(tvServersList));
       
    79 
       
    80     tvServersList->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
       
    81 
       
    82     static_cast<HWNetServersModel *>(tvServersList->model())->updateList();
       
    83 
       
    84     connect(BtnUpdateSList, SIGNAL(clicked()), static_cast<HWNetServersModel *>(tvServersList->model()), SLOT(updateList()));
       
    85     connect(tvServersList, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(slotConnect()));
       
    86 }
       
    87 
       
    88 void PageNet::slotConnect()
       
    89 {
       
    90     HWNetServersModel * model = static_cast<HWNetServersModel *>(tvServersList->model());
       
    91     QModelIndex mi = tvServersList->currentIndex();
       
    92     if(!mi.isValid())
       
    93     {
       
    94         QMessageBox::information(this, tr("Error"), tr("Please select server from the list above"));
       
    95         return;
       
    96     }
       
    97     QString host = model->index(mi.row(), 1).data().toString();
       
    98     quint16 port = model->index(mi.row(), 2).data().toUInt();
       
    99 
       
   100     emit connectClicked(host, port);
       
   101 }