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