File indexing completed on 2024-04-21 04:04:04
0001 /*************************************************************************** 0002 * newGameDialogImpl.cpp 0003 * ------------------- 0004 * begin : Wed Feb 23 2005 0005 * copyright : (C) 2005 by Gael de Chalendar <kleag@free.fr> 0006 ***************************************************************************/ 0007 0008 /*************************************************************************** 0009 * * 0010 * This program is free software; you can redistribute it and/or modify * 0011 * it under the terms of the GNU General Public License as published by * 0012 * the Free Software Foundation; either either version 2 0013 of the License, or (at your option) any later version.of the License, or * 0014 * (at your option) any later version. * 0015 * * 0016 * You should have received a copy of the GNU General Public License 0017 * along with this program; if not, write to the Free Software 0018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 0019 * 02110-1301, USA 0020 ***************************************************************************/ 0021 #include "newGameDialogImpl.h" 0022 #include "ksirksettings.h" 0023 0024 #include "GameLogic/onu.h" 0025 0026 #include <KLocalizedString> 0027 #include "ksirk_debug.h" 0028 #include <QStandardPaths> 0029 #include <KMessageBox> 0030 #include <KConfigDialog> 0031 0032 #include <QDir> 0033 #include <QStringList> 0034 #include <QPushButton> 0035 0036 using namespace Ksirk::GameLogic; 0037 0038 namespace Ksirk 0039 { 0040 0041 NewGameWidget::NewGameWidget(NewGameSetup* newGameSetup, QWidget *parent) : 0042 QWidget(parent), 0043 Ui::NewGameDialog(), 0044 m_newGameSetup(newGameSetup) 0045 { 0046 qCDebug(KSIRK_LOG); 0047 setupUi(this); 0048 0049 QObject::connect(nextButton, &QAbstractButton::clicked, this, &NewGameWidget::slotOK ); 0050 QObject::connect(cancelButton, &QAbstractButton::clicked, this, &NewGameWidget::slotCancel ); 0051 QObject::connect(skinCombo, &QComboBox::activated, this, &NewGameWidget::slotSkinChanged); 0052 QObject::connect(ghnsbutton, &KNSWidgets::Button::dialogFinished, this, &NewGameWidget::slotGHNS ); 0053 QObject::connect(tcpPortEntry, &QSpinBox::valueChanged, this, &NewGameWidget::slotTcpPortEdited); 0054 } 0055 0056 void NewGameWidget::init(const QString& skin, GameAutomaton::NetworkGameType netGameType) 0057 { 0058 qCDebug(KSIRK_LOG) << "Skin got: " << skin << " ; network=" << netGameType; 0059 m_newGameSetup->setSkin(skin); 0060 if (netGameType != GameAutomaton::None) 0061 { 0062 m_newGameSetup->setNetworkGameType(netGameType); 0063 localPlayersNumberLabel->show(); 0064 localPlayersNumberEntry->show(); 0065 if (netGameType == GameAutomaton::Socket) 0066 { 0067 tcpPortLabel->show(); 0068 tcpPortEntry->show(); 0069 } 0070 #if HAVE_JABBER_SUPPORT 0071 if (netGameType == GameAutomaton::Jabber) 0072 { 0073 tcpPortLabel->hide(); 0074 tcpPortEntry->hide(); 0075 } 0076 #endif 0077 m_newGameSetup->setNbNetworkPlayers(1); 0078 localPlayersNumberEntry->setValue(m_newGameSetup->nbNetworkPlayers()); 0079 } 0080 else 0081 { 0082 m_newGameSetup->setNetworkGameType(GameAutomaton::None); 0083 localPlayersNumberLabel->hide(); 0084 localPlayersNumberEntry->hide(); 0085 tcpPortLabel->hide(); 0086 tcpPortEntry->hide(); 0087 m_newGameSetup->setNbNetworkPlayers(0); 0088 } 0089 0090 m_newGameSetup->setSkin(skin); 0091 0092 fillSkinsCombo(); 0093 } 0094 0095 NewGameWidget::~NewGameWidget() 0096 { 0097 } 0098 0099 void NewGameWidget::slotOK() 0100 { 0101 qCDebug(KSIRK_LOG) << " skin is " << m_newGameSetup->worlds()[skinCombo->currentText()]->skin(); 0102 // m_networkGame = networkGameCheckBox->isChecked(); 0103 m_newGameSetup->setSkin(m_newGameSetup->worlds()[skinCombo->currentText()]->skin()); 0104 m_newGameSetup->setNbPlayers(playersNumberEntry->value()); 0105 m_newGameSetup->setNbNetworkPlayers(m_newGameSetup->automaton()->networkGameType() == GameAutomaton::None?0:playersNumberEntry->value()-localPlayersNumberEntry->value()); 0106 m_newGameSetup->setUseGoals(radioGoal->isChecked()); 0107 emit newGameOK(); 0108 } 0109 0110 void NewGameWidget::slotCancel() 0111 { 0112 qCDebug(KSIRK_LOG); 0113 emit newGameKO(); 0114 } 0115 0116 void NewGameWidget::fillSkinsCombo() 0117 { 0118 qCDebug(KSIRK_LOG) << "Filling skins combo"; 0119 0120 skinCombo->clear(); 0121 qDeleteAll(m_newGameSetup->worlds()); 0122 0123 QStringList skinsDirs = QStandardPaths::locateAll(QStandardPaths::AppDataLocation, "skins", QStandardPaths::LocateDirectory); 0124 qCDebug(KSIRK_LOG) << skinsDirs; 0125 uint skinNum = 0; 0126 uint currentSkinNum = 0; 0127 foreach (const QString &skinsDirName, skinsDirs) 0128 { 0129 if (skinsDirName.isEmpty()) 0130 { 0131 KMessageBox::error(nullptr, 0132 i18n("Skins directory not found - Verify your installation\nProgram cannot continue"), 0133 i18n("Fatal Error!")); 0134 exit(2); 0135 } 0136 qCDebug(KSIRK_LOG) << "Got skins dir name: " << skinsDirName; 0137 QDir skinsDir(skinsDirName); 0138 QStringList skinsDirsNames = skinsDir.entryList(QStringList("[a-zA-Z]*"), QDir::Dirs); 0139 0140 foreach (const QString& name, skinsDirsNames) 0141 { 0142 qCDebug(KSIRK_LOG) << "Got skin dir name: " << name; 0143 QDir skinDir(skinsDirName + '/' + name); 0144 if (skinDir.exists()) 0145 { 0146 qCDebug(KSIRK_LOG) << "Got skin dir: " << skinDir.dirName(); 0147 GameLogic::ONU* world = new GameLogic::ONU(m_newGameSetup->automaton(),skinsDirName + '/' + skinDir.dirName() + "/Data/world.desktop"); 0148 if (!world->skin().isEmpty()) 0149 { 0150 skinCombo->addItem(i18n(world->name().toUtf8().data())); 0151 m_newGameSetup->worlds()[i18n(world->name().toUtf8().data())] = world; 0152 if (QString(QLatin1String("skins/")+skinDir.dirName()) == m_newGameSetup->skin()) 0153 { 0154 qCDebug(KSIRK_LOG) << "Setting currentSkinNum to " << skinNum; 0155 currentSkinNum = skinNum; 0156 } 0157 } 0158 else 0159 { 0160 delete world; 0161 } 0162 } 0163 } 0164 } 0165 skinCombo->setCurrentIndex(currentSkinNum); 0166 slotSkinChanged(currentSkinNum); 0167 } 0168 0169 void NewGameWidget::slotSkinChanged(int skinNum) 0170 { 0171 qCDebug(KSIRK_LOG) << "NewGameDialogImpl::slotSkinChanged " 0172 << skinNum << " ; " << skinCombo->currentText() 0173 << " ; " << m_newGameSetup->worlds()[skinCombo->currentText()]->name() << " ; " 0174 << m_newGameSetup->worlds()[skinCombo->currentText()]->description(); 0175 skinDescLabel->setText(i18n(m_newGameSetup->worlds()[skinCombo->currentText()]->description().toUtf8().data())); 0176 skinSnapshotPixmap->setPixmap(m_newGameSetup->worlds()[skinCombo->currentText()]->snapshot()); 0177 0178 playersNumberEntry->setMinimum(2); 0179 playersNumberEntry->setMaximum(m_newGameSetup->worlds()[skinCombo->currentText()]->getNationalities().size()); 0180 localPlayersNumberEntry->setMinimum(1); 0181 localPlayersNumberEntry->setMaximum(m_newGameSetup->worlds()[skinCombo->currentText()]->getNationalities().size()-1); 0182 } 0183 0184 void NewGameWidget::slotGHNS() 0185 { 0186 // Bug 281294. No need to touch ConfigDialog. 0187 // Skins are handled by KNS3 dialog. 0188 // if ( KConfigDialog::showDialog("settings") ) 0189 // { 0190 // return; 0191 // } 0192 0193 fillSkinsCombo(); 0194 /* KConfigDialog *dialog = new KConfigDialog(this, "settings", KsirkSettings::self()); 0195 KGameThemeSelector* kgts = new KGameThemeSelector(dialog, KsirkSettings::self()); 0196 dialog->addPage(kgts, i18n("Theme"), "games-config-theme");*/ 0197 /* connect(dialog, SIGNAL(settingsChanged(QString)), view, SLOT(settingsChanged())); 0198 connect(dialog, SIGNAL(hidden()), view, SLOT(resumeFromConfigure()));*/ 0199 // dialog->show(); 0200 0201 } 0202 0203 void NewGameWidget::slotTcpPortEdited(int port) 0204 { 0205 m_newGameSetup->setTcpPort(port); 0206 } 0207 0208 void NewGameWidget::slotNbNetworkPlayersEdited(int) 0209 { 0210 } 0211 0212 } 0213 0214 #include "moc_newGameDialogImpl.cpp"