File indexing completed on 2024-04-14 14:31:57

0001 // Copyright (c) 2002-2004 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 <QCheckBox>
0018 #include <QVBoxLayout>
0019 #include <QHBoxLayout>
0020 #include <QGroupBox>
0021 #include <QPushButton>
0022 
0023 #include <klocalizedstring.h>
0024 #include <kguiitem.h>
0025 #include <kstandardguiitem.h>
0026 
0027 #include <atlantic_core.h>
0028 #include <configoption.h>
0029 #include <game.h>
0030 #include <player.h>
0031 
0032 #include "selectconfiguration_widget.h"
0033 
0034 #include <atlantik_debug.h>
0035 
0036 SelectConfiguration::SelectConfiguration(AtlanticCore *atlanticCore, QWidget *parent)
0037     : QWidget(parent)
0038     , m_game(nullptr)
0039     , m_atlanticCore(atlanticCore)
0040 {
0041     m_mainLayout = new QVBoxLayout(this );
0042         m_mainLayout->setContentsMargins(0, 0, 0, 0);
0043     Q_CHECK_PTR(m_mainLayout);
0044 
0045     const QPair<KGuiItem, KGuiItem> icons = KStandardGuiItem::backAndForward();
0046 
0047     // Game configuration.
0048     m_configBox = new QGroupBox(i18n("Game Configuration"), this);
0049     m_configBox->setObjectName(QStringLiteral("configBox"));
0050     m_mainLayout->addWidget(m_configBox);
0051     QVBoxLayout *configBoxLayout = new QVBoxLayout(m_configBox);
0052     Q_UNUSED(configBoxLayout)
0053 
0054     // Player buttons.
0055     QHBoxLayout *playerButtons = new QHBoxLayout();
0056         m_mainLayout->addItem( playerButtons );
0057 
0058     playerButtons->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
0059 
0060     // Vertical spacer.
0061     m_mainLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding));
0062 
0063     // Server buttons.
0064     QHBoxLayout *serverButtons = new QHBoxLayout();
0065         m_mainLayout->addItem( serverButtons );
0066 
0067     m_backButton = new QPushButton(icons.first.icon(), i18n("Leave Game"), this);
0068     serverButtons->addWidget(m_backButton);
0069 
0070     connect(m_backButton, SIGNAL(clicked()), this, SIGNAL(leaveGame()));
0071 
0072     serverButtons->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
0073 
0074     m_startButton = new QPushButton(icons.second.icon(), i18n("Start Game"), this);
0075     serverButtons->addWidget(m_startButton);
0076     m_startButton->setEnabled(false);
0077 
0078     connect(m_startButton, SIGNAL(clicked()), this, SIGNAL(startGame()));
0079 
0080     Player *playerSelf = m_atlanticCore->playerSelf();
0081     if (playerSelf) {
0082         playerChanged(playerSelf);
0083         connect(playerSelf, SIGNAL(changed(Player *)), this, SLOT(playerChanged(Player *)));
0084     }
0085 
0086     Q_EMIT statusMessage(i18n("Retrieving configuration list..."));
0087 
0088     if (m_game) {
0089         foreach (ConfigOption *opt, m_game->configOptions())
0090             addConfigOption(opt);
0091 
0092         connect(m_game, SIGNAL(createGUI(ConfigOption *)), this, SLOT(addConfigOption(ConfigOption *)));
0093     }
0094 
0095     slotEndUpdate();
0096 }
0097 
0098 void SelectConfiguration::initGame()
0099 {
0100     Q_EMIT statusMessage(i18n("Game started. Retrieving full game data..."));
0101 }
0102 
0103 void SelectConfiguration::addConfigOption(ConfigOption *configOption)
0104 {
0105     const QString type = configOption->type();
0106     if (type == QLatin1String("bool"))
0107         addConfigOptionBool(configOption);
0108     else
0109         qCDebug(ATLANTIK_LOG) << "unknown type" << type << "for option" << configOption->name();
0110 }
0111 
0112 void SelectConfiguration::addConfigOptionBool(ConfigOption *configOption)
0113 {
0114     QCheckBox *checkBox = m_configBoxMap.value(configOption);
0115 
0116     if (!checkBox)
0117     {
0118         checkBox = new QCheckBox(configOption->description(), m_configBox);
0119         m_configBox->layout()->addWidget(checkBox);
0120         checkBox->setObjectName(QStringLiteral("checkbox"));
0121         m_configMap.insert((QObject *)checkBox, configOption);
0122         m_configBoxMap.insert(configOption, checkBox);
0123 
0124         connect(checkBox, SIGNAL(clicked()), this, SLOT(changeOption()));
0125         connect(configOption, SIGNAL(changed(ConfigOption *)), this, SLOT(optionChanged(ConfigOption *)));
0126     }
0127 
0128     checkBox->setChecked(configOption->value().toInt());
0129     checkBox->setEnabled(configOption->edit() && m_atlanticCore->selfIsMaster());
0130 }
0131 
0132 void SelectConfiguration::gameOption(const QString &title, const QString &type, const QString &value, const QString &edit, const QString &command)
0133 {
0134     // Find if option exists in GUI yet
0135     if (QCheckBox *checkBox = dynamic_cast<QCheckBox *>(m_checkBoxMap[command]))
0136     {
0137         checkBox->setChecked(value.toInt());
0138         checkBox->setEnabled(edit.toInt());
0139         return;
0140     }
0141 
0142     // Create option
0143     if (type == QLatin1String("bool"))
0144     {
0145         QCheckBox *checkBox = new QCheckBox(title, m_configBox);
0146         m_configBox->layout()->addWidget(checkBox);
0147         checkBox->setObjectName(QStringLiteral("checkbox"));
0148         m_optionCommandMap[(QObject *)checkBox] = command;
0149         m_checkBoxMap[command] = checkBox;
0150         checkBox->setChecked(value.toInt());
0151         checkBox->setEnabled(edit.toInt());
0152         checkBox->show();
0153 
0154         connect(checkBox, SIGNAL(clicked()), this, SLOT(optionChanged()));
0155     }
0156     // TODO: create options other than type=bool
0157 
0158     // TODO: Enable edit for master only
0159 }
0160 
0161 void SelectConfiguration::changeOption()
0162 {
0163     ConfigOption *configOption = m_configMap[(QObject *)QObject::sender()];
0164     if (configOption)
0165     {
0166         qCDebug(ATLANTIK_LOG) << "checked" << ((QCheckBox *)QObject::sender())->isChecked();
0167         Q_EMIT changeOption( configOption->id(), QString::number( ((QCheckBox *)QObject::sender())->isChecked() ) );
0168     }
0169 }
0170 
0171 void SelectConfiguration::optionChanged(ConfigOption *configOption)
0172 {
0173     QCheckBox *checkBox = m_configBoxMap[configOption];
0174     if (checkBox)
0175     {
0176         checkBox->setText( configOption->description() );
0177         checkBox->setChecked( configOption->value().toInt() );
0178         checkBox->setEnabled( configOption->edit() && m_atlanticCore->selfIsMaster() );
0179     }
0180 }
0181 
0182 void SelectConfiguration::optionChanged()
0183 {
0184     QString command = m_optionCommandMap[(QObject *)QObject::sender()];
0185 
0186     if (QCheckBox *checkBox = m_checkBoxMap[command])
0187     {
0188         command.append(QString::number(checkBox->isChecked()));
0189         Q_EMIT buttonCommand(command);
0190     }
0191 }
0192 
0193 void SelectConfiguration::slotEndUpdate()
0194 {
0195     Q_EMIT statusMessage(i18n("Retrieved configuration list."));
0196 }
0197 
0198 void SelectConfiguration::playerChanged(Player *player)
0199 {
0200     qCDebug(ATLANTIK_LOG);
0201 
0202     if (player->game() != m_game)
0203     {
0204         qCDebug(ATLANTIK_LOG) << "change";
0205 
0206         if (m_game)
0207         {
0208             disconnect(m_game, SIGNAL(createGUI(ConfigOption *)), this, SLOT(addConfigOption(ConfigOption *)));
0209             disconnect(m_game, SIGNAL(changed(Game *)), this, SLOT(gameChanged(Game *)));
0210         }
0211 
0212         m_game = player->game();
0213 
0214         if (m_game)
0215         {
0216             connect(m_game, SIGNAL(createGUI(ConfigOption *)), this, SLOT(addConfigOption(ConfigOption *)));
0217             connect(m_game, SIGNAL(changed(Game *)), this, SLOT(gameChanged(Game *)));
0218         }
0219     }
0220 }
0221 
0222 void SelectConfiguration::gameChanged(Game *game)
0223 {
0224     m_startButton->setEnabled( game->master() == m_atlanticCore->playerSelf() );
0225 
0226 QMapIterator<ConfigOption *, QCheckBox *> it(m_configBoxMap);
0227 while (it.hasNext()) {
0228             it.next();
0229             it.value()->setEnabled(it.key()->edit() && m_atlanticCore->selfIsMaster() );
0230 
0231     }
0232 
0233 }
0234 
0235 #include "moc_selectconfiguration_widget.cpp"