File indexing completed on 2024-04-28 07:53:28

0001 /***************************************************************************
0002                           kdialogsetupjoueur.cpp  -  description
0003                              -------------------
0004     begin                : Thu Jul 19 2001
0005     copyright            : (C) 2001 by Gaƫl de Chalendar
0006     email                : Gael.de.Chalendar@free.fr
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either either version 2
0014    of the License, or (at your option) any later version.of the License, or     *
0015  *   (at your option) any later version.                                   *
0016  *                                                                         *
0017  *   You should have received a copy of the GNU General Public License
0018  *   along with this program; if not, write to the Free Software
0019  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0020  *   02110-1301, USA
0021  ***************************************************************************/
0022 
0023 #include "kplayersetupwidget.h"
0024 #include "GameLogic/gameautomaton.h"
0025 #include "GameLogic/player.h"
0026 #include "GameLogic/nationality.h"
0027 #include "GameLogic/onu.h"
0028 #include "Sprites/skinSpritesData.h"
0029 #include "Sprites/flagsprite.h"
0030 #include "newplayerdata.h"
0031 #include "newgamesetup.h"
0032 
0033 #include <QString>
0034 #include <QLabel>
0035 #include <QLineEdit>
0036 #include <QCheckBox>
0037 #include <QComboBox>
0038 #include <QPixmap>
0039 
0040 #include <KLocalizedString>
0041 #include "ksirk_debug.h"
0042 
0043 #define _XOPEN_SOURCE_
0044 // #include <unistd.h>
0045 
0046 namespace Ksirk
0047 {
0048 
0049 KPlayerSetupWidget::KPlayerSetupWidget(QWidget *parent) :
0050   QWidget(parent), Ui::QPlayerSetupWidget()
0051 {
0052   qCDebug(KSIRK_LOG);
0053   setupUi(this);
0054 
0055   connect(nationCombo, &QComboBox::activated, this, &KPlayerSetupWidget::slotNationChanged);
0056   connect(nameLineEdit, &QLineEdit::textEdited, this, &KPlayerSetupWidget::slotNameEdited);
0057   connect(nationCombo, &QComboBox::activated, this, &KPlayerSetupWidget::slotNationChanged);
0058   connect(nextButton, &QAbstractButton::pressed,this,&KPlayerSetupWidget::slotNext);
0059   connect(previousButton, &QAbstractButton::pressed,this,&KPlayerSetupWidget::slotPrevious);
0060   connect(cancelButton, &QAbstractButton::pressed,this,&KPlayerSetupWidget::slotCancel);
0061   
0062   messageLabel->hide();
0063 }
0064 
0065 KPlayerSetupWidget::~KPlayerSetupWidget()
0066 {
0067   hide();
0068 }
0069 
0070 void KPlayerSetupWidget::init(GameLogic::GameAutomaton* automaton,
0071                               GameLogic::ONU* onu,
0072                               unsigned int playerNumber,
0073                               QString &playerName,
0074                               QString& password,
0075                               bool computerPlayer,
0076                               QMap< QString, QString >& nations,
0077                               QString & nationName,
0078                               NewGameSetup* newGameSetup)
0079 
0080 {
0081   qCDebug(KSIRK_LOG) << playerName << nationName;
0082 
0083   m_automaton = automaton;
0084   m_name = playerName;
0085   m_computer = computerPlayer;
0086   m_nationName = nationName;
0087   m_nations = nations;
0088   m_onu = onu;
0089   m_number = playerNumber;
0090   m_password = password;
0091   m_newGameSetup = newGameSetup;
0092 
0093   qCDebug(KSIRK_LOG) << "connecting to playerJoinedGame";
0094   connect(automaton,&KGame::signalPlayerJoinedGame, this, &KPlayerSetupWidget::slotPlayerJoinedGame);
0095   init();
0096   qCDebug(KSIRK_LOG) << "constructor done";
0097 }
0098 
0099 
0100 void KPlayerSetupWidget::slotNext()
0101 {
0102   qCDebug(KSIRK_LOG) << m_newGameSetup->players().size() << m_newGameSetup->nbPlayers();
0103 
0104   m_name = nameLineEdit-> text().trimmed();
0105 //     qCDebug(KSIRK_LOG) << "Got name " << name;
0106   m_computer = (isComputerCheckBox-> checkState() == Qt::Checked);
0107 //     qCDebug(KSIRK_LOG) << "computer? : " << computer;
0108   m_nationName = m_nationsNames[nationCombo->currentText()];
0109 // @toport
0110 //     m_password = QString(crypt(passwordEdit->password(),"T6"));
0111 
0112 //     accept();
0113   if (m_newGameSetup->players().size() < m_newGameSetup->nbPlayers())
0114   {
0115     qCDebug(KSIRK_LOG) << "Add new player";
0116     NewPlayerData* newPlayer = new NewPlayerData(m_name, m_nationName, m_password, m_computer, false);
0117     m_newGameSetup->players().push_back(newPlayer);
0118     fillNationsCombo();
0119     slotNationChanged();
0120     isComputerCheckBox->setCheckState(Qt::Unchecked);
0121 //     init(m_automaton,m_onu,m_newGameSetup->players().size()+1,"",false,"",false,m_nations,"", m_newGameSetup);
0122     setLabelText();
0123     emit next();
0124   }
0125   
0126 }
0127 
0128 void KPlayerSetupWidget::slotPrevious()
0129 {
0130   qCDebug(KSIRK_LOG);
0131   if (m_newGameSetup->players().empty())
0132   {
0133     emit previous();
0134   }
0135   else
0136   {
0137     NewPlayerData* player = m_newGameSetup->players().back();
0138     m_newGameSetup->players().pop_back();
0139     fillNationsCombo();
0140     slotNationChanged();
0141     nameLineEdit->setText(player->name());
0142     passwordEdit->setText(player->password());
0143     isComputerCheckBox->setCheckState(player->computer()?Qt::Checked:Qt::Unchecked);
0144     setLabelText();
0145     delete player;
0146   }
0147 }
0148 
0149 void KPlayerSetupWidget::slotCancel()
0150 {
0151   qCDebug(KSIRK_LOG);
0152   emit cancel();
0153 }
0154 
0155 
0156 void KPlayerSetupWidget::fillNationsCombo()
0157 {
0158   qCDebug(KSIRK_LOG);
0159   nationCombo->clear();
0160   
0161   GameLogic::Nationality* nation = m_onu->nationNamed(*m_nations.keys().begin());
0162   nameLineEdit-> setText(nation->leaderName());
0163 
0164   foreach (const QString& k,m_nations.keys())
0165   {
0166     if (!isAvailable(k))
0167     {
0168       continue;
0169     }
0170     const QString& v =m_nations[k];
0171     qCDebug(KSIRK_LOG) << "Adding nation " << k << " / " << v;
0172 //     load image
0173 
0174     FlagSprite flagsprite(v,
0175                   Sprites::SkinSpritesData::single().intData("flag-width"),
0176                   Sprites::SkinSpritesData::single().intData("flag-height"),
0177                   Sprites::SkinSpritesData::single().intData("flag-frames"),
0178                   Sprites::SkinSpritesData::single().intData("flag-versions"),
0179                   1.0,m_automaton->game()->backGndWorld());
0180     QPixmap flag = flagsprite.image(0);
0181 
0182 //     get name
0183     QString name = i18n(k.toUtf8().data());
0184     m_nationsNames.insert(name,k);
0185 //     fill a combo entry
0186     nationCombo->addItem(QIcon(flag),name);
0187   }
0188   
0189   qCDebug(KSIRK_LOG) << "Nations combo filled";
0190 }
0191 
0192 void KPlayerSetupWidget::slotPlayerJoinedGame(KPlayer* player)
0193 {
0194   qCDebug(KSIRK_LOG) << "KPlayerSetupWidget::slotPlayerJoinedGame: " << player->name()
0195       << " from " << ((GameLogic::Player*)player)->getNation()->name();
0196   for (int i = 0 ; i < nationCombo->count(); i++)
0197   {
0198     if (nationCombo->itemText(i) == m_nationsNames[((GameLogic::Player*)player)->getNation()->name()])
0199     {
0200       nationCombo->removeItem(i);
0201       break;
0202     }
0203   }
0204   slotNameEdited(nameLineEdit->text());
0205 }
0206 
0207 void KPlayerSetupWidget::slotNationChanged()
0208 {
0209   qCDebug(KSIRK_LOG) << "KPlayerSetupWidget::slotNationChanged " << nationCombo->currentText();
0210   if (nationCombo->currentText().isEmpty())
0211   {
0212     return;
0213   }
0214   GameLogic::Nationality* nation = m_onu->nationNamed(m_nationsNames[nationCombo->currentText()]);
0215 //   qCDebug(KSIRK_LOG) << "nation = " << nation;
0216   nameLineEdit-> setText(nation->leaderName());
0217   slotNameEdited(nameLineEdit->text());
0218 }
0219 
0220 bool KPlayerSetupWidget::isAvailable(QString nationName)
0221 {
0222   foreach (NewPlayerData* player, m_newGameSetup->players())
0223   {
0224     if (player->nation() == nationName)
0225     {
0226       return false;
0227     }
0228   }
0229   return true;
0230 }
0231 
0232 void KPlayerSetupWidget::slotNameEdited(const QString& text)
0233 {
0234   qCDebug(KSIRK_LOG) << text.trimmed();
0235   bool found = false;
0236   foreach (NewPlayerData* player, m_newGameSetup->players())
0237   {
0238     if (player->name() == text.trimmed())
0239     {
0240       found = true;
0241       break;
0242     }
0243   }
0244   if (found || text.trimmed().isEmpty())
0245   {
0246     QString message = i18n("Name already in use. Please choose another one.");
0247     if (text.trimmed().isEmpty())
0248       message = i18n("Empty name. Please choose another one.");
0249     nextButton->setEnabled(false);
0250     nameLineEdit->setStyleSheet("background: red");
0251     messageLabel->setText(message);
0252     messageLabel->show();
0253   }
0254   else
0255   {
0256     nextButton->setEnabled(true);
0257     nameLineEdit->setStyleSheet("");
0258     messageLabel->hide();
0259   }
0260 }
0261 
0262 void KPlayerSetupWidget::init(NewPlayerData* player)
0263 {
0264   qCDebug(KSIRK_LOG) << (void*)player;
0265   setLabelText();
0266 
0267   /// @TODO set the correct nation and password and computer state
0268   
0269   fillNationsCombo();
0270   if (m_newGameSetup->networkGameType() == GameLogic::GameAutomaton::None)
0271   {
0272     passwordLabel->hide();
0273     passwordEdit->hide();
0274   }
0275   if (player != nullptr)
0276   {
0277     qCDebug(KSIRK_LOG) << player->name();
0278     nationCombo->setCurrentIndex(nationCombo->findText(player->nation()));
0279     slotNationChanged();
0280     nameLineEdit->setText(player->name());
0281   }
0282   else
0283   {
0284     slotNationChanged();
0285   }
0286   nameLineEdit->setFocus();
0287 }
0288 
0289 void KPlayerSetupWidget::setLabelText()
0290 {
0291     QString labelString = i18n("Player Number %1, please type in your name and choose your nation:", m_newGameSetup->players().size() + 1);
0292     TextLabel1->setText(labelString);
0293 }
0294 
0295 } // namespace Ksirk
0296 
0297 #include "moc_kplayersetupwidget.cpp"