| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2009 by The qGo Project * |
| 3 | * * |
| 4 | * This file is part of qGo. * |
| 5 | * * |
| 6 | * qGo is free software: you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/> * |
| 18 | * or write to the Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | |
| 23 | #include <QtWidgets> |
| 24 | #include "serverlistdialog.h" |
| 25 | |
| 26 | ServerListDialog::ServerListDialog(std::vector <ServerItem *> serverlist, int current) |
| 27 | { |
| 28 | current_server = current; |
| 29 | serverListView = constructTreeWidget(serverlist); |
| 30 | |
| 31 | connectButton = new QPushButton(tr("Connect")); |
| 32 | connectButton->setDefault(true); |
| 33 | |
| 34 | cancelButton = new QPushButton(tr("&Cancel")); |
| 35 | cancelButton->setAutoDefault(false); |
| 36 | |
| 37 | buttonBox = new QDialogButtonBox(Qt::Horizontal); |
| 38 | buttonBox->addButton(cancelButton, QDialogButtonBox::ActionRole); |
| 39 | buttonBox->addButton(connectButton, QDialogButtonBox::ActionRole); |
| 40 | |
| 41 | connect(serverListView, &QTreeWidget::itemDoubleClicked, this, &ServerListDialog::slot_listDoubleClicked); |
| 42 | |
| 43 | connect(connectButton, &QPushButton::clicked, this, &ServerListDialog::slot_connect); |
| 44 | connect(cancelButton, &QPushButton::clicked, this, &ServerListDialog::slot_cancel); |
| 45 | |
| 46 | QGridLayout * mainLayout = new QGridLayout; |
| 47 | mainLayout->setSizeConstraint(QLayout::SetFixedSize); |
| 48 | mainLayout->addWidget(serverListView, 0, 0); |
| 49 | mainLayout->addWidget(buttonBox, 1, 0); |
| 50 | setLayout(mainLayout); |
| 51 | |
| 52 | setWindowTitle(tr("Choose server...")); |
| 53 | } |
| 54 | |
| 55 | ServerListDialog::~ServerListDialog() |
| 56 | { |
| 57 | delete serverListView; |
| 58 | delete connectButton; |
| 59 | delete cancelButton; |
| 60 | delete buttonBox; |
| 61 | } |
| 62 | |
| 63 | QTreeWidget * ServerListDialog::constructTreeWidget(std::vector<ServerItem *> serverlist) |
| 64 | { |
| 65 | QTreeWidget * treeWidget = new QTreeWidget(this); |
| 66 | QTreeWidgetItem * i; |
| 67 | QStringList stringList; |
| 68 | int index = 0; |
| 69 | |
| 70 | treeWidget->setRootIsDecorated(false); |
| 71 | treeWidget->setColumnCount(2); |
| 72 | stringList << "Name" << "IP Address"; |
| 73 | treeWidget->setHeaderLabels(stringList); |
| 74 | stringList.clear(); |
| 75 | |
| 76 | std::vector<ServerItem *>::iterator it; |
| 77 | for(it = serverlist.begin(); it != serverlist.end(); it++) |
| 78 | { |
| 79 | stringList << (*it)->name << (*it)->ipaddress; |
| 80 | i = new QTreeWidgetItem(treeWidget, stringList); |
| 81 | if(index == current_server) |
| 82 | i->setDisabled(true); |
| 83 | if(it == serverlist.begin()) //ugly but its a short list |
| 84 | treeWidget->setCurrentItem(i); |
| 85 | stringList.clear(); |
| 86 | index++; |
| 87 | } |
| 88 | return treeWidget; |
| 89 | } |
| 90 | |
| 91 | void ServerListDialog::slot_listDoubleClicked(QTreeWidgetItem * i, int) |
| 92 | { |
| 93 | int index = serverListView->indexOfTopLevelItem(i); |
| 94 | if(index < 0 || index == current_server) |
| 95 | return; |
| 96 | done(index + 1); |
| 97 | } |
| 98 | |
| 99 | void ServerListDialog::slot_connect(void) |
| 100 | { |
| 101 | QTreeWidgetItem * i = serverListView->currentItem(); |
| 102 | if(!i) |
| 103 | { |
| 104 | //FIXME error message necessary? |
| 105 | return; |
| 106 | } |
| 107 | int index = serverListView->indexOfTopLevelItem(i); |
| 108 | done(index + 1); |
| 109 | } |
| 110 | |
| 111 | void ServerListDialog::slot_cancel(void) |
| 112 | { |
| 113 | qDebug("Cancel"); |
| 114 | deleteLater(); |
| 115 | done(QDialog::Rejected); |
| 116 | } |