File indexing completed on 2024-11-10 09:46:46

0001 // Copyright (c) 2002-2003 Rob Kaper <cap@capsi.com>
0002 //
0003 // This program is free software; you can redistribute it and/or
0004 // modify it under the terms of the GNU General Public License
0005 // version 2 as published by the Free Software Foundation.
0006 //
0007 // This program is distributed in the hope that it will be useful,
0008 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0009 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0010 // General Public License for more details.
0011 //
0012 // You should have received a copy of the GNU General Public License
0013 // along with this program; see the file COPYING.  If not, write to
0014 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0015 // Boston, MA 02110-1301, USA.
0016 
0017 #include <QGroupBox>
0018 #include <QVBoxLayout>
0019 #include <QHBoxLayout>
0020 #include <QTreeWidget>
0021 #include <QHeaderView>
0022 #include <QPushButton>
0023 
0024 #include <klocalizedstring.h>
0025 #include <kiconloader.h>
0026 #include <knotification.h>
0027 #include <kguiitem.h>
0028 #include <kstandardguiitem.h>
0029 
0030 #include <atlantic_core.h>
0031 #include <game.h>
0032 #include <player.h>
0033 
0034 #include "selectgame_widget.h"
0035 
0036 enum { GameTypeRole = Qt::UserRole + 1 };
0037 
0038 SelectGame::SelectGame(AtlanticCore *atlanticCore, QWidget *parent) 
0039     : QWidget(parent)
0040     , m_atlanticCore(atlanticCore)
0041 {
0042     connect(m_atlanticCore, SIGNAL(createGUI(Game *)), this, SLOT(addGame(Game *)));
0043     connect(m_atlanticCore, SIGNAL(removeGUI(Game *)), this, SLOT(delGame(Game *)));
0044 
0045     setupUi(this);
0046     layout()->setContentsMargins(0, 0, 0, 0);
0047 
0048     const QPair<KGuiItem, KGuiItem> icons = KStandardGuiItem::backAndForward();
0049 
0050     // List of games
0051     m_gameList->header()->setSectionsClickable(false);
0052 
0053     connect(m_gameList, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(validateConnectButton()));
0054     connect(m_gameList, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(connectClicked()));
0055     connect(m_gameList, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), this, SLOT(validateConnectButton()));
0056 
0057     backButton->setIcon(icons.first.icon());
0058 
0059     connect(backButton, SIGNAL(clicked()), this, SIGNAL(leaveServer()));
0060 
0061     m_connectButton->setIcon(icons.second.icon());
0062     m_connectButton->setEnabled(false);
0063 
0064     connect(m_connectButton, SIGNAL(clicked()), this, SLOT(connectClicked()));
0065 
0066 }
0067 
0068 void SelectGame::addGame(Game *game)
0069 {
0070     connect(game, SIGNAL(changed(Game *)), this, SLOT(updateGame(Game *)));
0071 
0072     if (game->id() == -1)
0073     {
0074         QTreeWidgetItem *item = new QTreeWidgetItem();
0075         item->setText(0, i18n("Create a new %1 Game", game->name()));
0076         item->setText(1, game->description());
0077         item->setData(0, GameTypeRole, game->type());
0078         item->setIcon(0, KDE::icon(QStringLiteral("document-new")));
0079         m_gameList->addTopLevelItem(item);
0080     }
0081     else
0082     {
0083         Player *master = game->master();
0084         QTreeWidgetItem *item = new QTreeWidgetItem();
0085         if (!game->canBeJoined() && game->canBeWatched())
0086             item->setText(0, i18n("Watch %1's %2 Game", (master ? master->name() : QString()), game->name()));
0087         else
0088             item->setText(0, i18n("Join %1's %2 Game", (master ? master->name() : QString()), game->name()));
0089         item->setText(1, game->description());
0090         item->setText(2, QString::number(game->id()));
0091         item->setText(3, QString::number(game->players()));
0092         item->setData(0, GameTypeRole, game->type());
0093         item->setIcon(0, KDE::icon(QStringLiteral("atlantik")));
0094         item->setDisabled(!game->canBeJoined() && !game->canBeWatched());
0095         m_gameList->addTopLevelItem(item);
0096 
0097         KNotification::event(QStringLiteral("newgame"), i18n("New game available."));
0098 
0099         if (master)
0100             connect(master, SIGNAL(changed(Player *)), this, SLOT(playerChanged(Player *)));
0101     }
0102     m_gameList->resizeColumnToContents(0);
0103     m_gameList->resizeColumnToContents(1);
0104     m_gameList->resizeColumnToContents(2);
0105     m_gameList->resizeColumnToContents(3);
0106 
0107 //  validateConnectButton();
0108 }
0109 
0110 void SelectGame::delGame(Game *game)
0111 {
0112     QTreeWidgetItem *item = findItem(game);
0113     if (!item)
0114         return;
0115 
0116     delete item;
0117 
0118     validateConnectButton();
0119 }
0120 
0121 void SelectGame::updateGame(Game *game)
0122 {
0123     QTreeWidgetItem *item = findItem(game);
0124     if (!item)
0125         return;
0126 
0127     item->setText( 1, game->description() );
0128 
0129     if (game->id() == -1)
0130         item->setText(0, i18n("Create a new %1 Game", game->name()));
0131     else
0132     {
0133         Player *master = game->master();
0134         if (!game->canBeJoined() && game->canBeWatched())
0135             item->setText(0, i18n("Watch %1's %2 Game", (master ? master->name() : QString()), game->name()));
0136         else
0137             item->setText(0, i18n("Join %1's %2 Game", (master ? master->name() : QString()), game->name()));
0138         item->setText( 3, QString::number( game->players() ) );
0139         item->setDisabled(!game->canBeJoined() && !game->canBeWatched());
0140 
0141         connect(master, SIGNAL(changed(Player *)), this, SLOT(playerChanged(Player *)));
0142     }
0143 
0144     validateConnectButton();
0145 }
0146 
0147 void SelectGame::playerChanged(Player *player)
0148 {
0149     const int count = m_gameList->topLevelItemCount();
0150     Game *game = nullptr;
0151 
0152     for (int i = 0; i < count; ++i)
0153     {
0154         QTreeWidgetItem *item = m_gameList->topLevelItem(i);
0155         game = m_atlanticCore->findGame( item->text(2).toInt() );
0156         if ( game && game->master() == player )
0157         {
0158             if (!game->canBeJoined() && game->canBeWatched())
0159                 item->setText( 0, i18n("Watch %1's %2 Game", player->name(), game->name() ) );
0160             else
0161                 item->setText( 0, i18n("Join %1's %2 Game", player->name(), game->name() ) );
0162             return;
0163         }
0164     }
0165 }
0166 
0167 QTreeWidgetItem *SelectGame::findItem(Game *game)
0168 {
0169     const int count = m_gameList->topLevelItemCount();
0170     for (int i = 0; i < count; ++i)
0171     {
0172         QTreeWidgetItem *item = m_gameList->topLevelItem(i);
0173         if ( (game->id() == -1 || item->text(2) == QString::number(game->id())) && item->data(0, GameTypeRole).toString() == game->type() )
0174             return item;
0175     }
0176     return nullptr;
0177 }
0178 
0179 void SelectGame::validateConnectButton()
0180 {
0181     const QList<QTreeWidgetItem *> items = m_gameList->selectedItems();
0182     if (!items.isEmpty())
0183     {
0184         const QTreeWidgetItem *item = items.first();
0185         if (item->text(2).toInt() > 0)
0186         {
0187             Game *game = m_atlanticCore->findGame(item->text(2).toInt());
0188             if (!game->canBeJoined() && game->canBeWatched())
0189                 m_connectButton->setText(i18n("Watch Game"));
0190             else
0191                 m_connectButton->setText(i18n("Join Game"));
0192         }
0193         else
0194             m_connectButton->setText(i18n("Create Game"));
0195 
0196         m_connectButton->setEnabled(true);
0197     }
0198     else
0199         m_connectButton->setEnabled(false);
0200 }
0201 
0202 void SelectGame::connectClicked()
0203 {
0204     const QList<QTreeWidgetItem *> items = m_gameList->selectedItems();
0205     if (!items.isEmpty())
0206     {
0207         const QTreeWidgetItem *item = items.first();
0208         if (int gameId = item->text(2).toInt())
0209         {
0210             Game *game = m_atlanticCore->findGame(item->text(2).toInt());
0211             if (!game->canBeJoined() && game->canBeWatched())
0212                 Q_EMIT watchGame(gameId);
0213             else
0214                 Q_EMIT joinGame(gameId);
0215         }
0216         else
0217             Q_EMIT newGame(item->data(0, GameTypeRole).toString());
0218     }
0219 }
0220 
0221 #include "moc_selectgame_widget.cpp"