File indexing completed on 2024-04-21 04:05:23

0001 /*
0002     This file is part of the KDE games lskat program
0003     SPDX-FileCopyrightText: 2006 Martin Heni <kde@heni-online.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "config_two.h"
0009 
0010 // Qt includes
0011 
0012 // KF includes
0013 #include <KConfigGroup>
0014 #include <KLocalizedString>
0015 
0016 // Constructor for the configuration
0017 ConfigTwo::ConfigTwo(QObject *parent)
0018          : QObject(parent)
0019 {
0020     // Create Players
0021     mPlayers.clear();
0022     mPlayers[0] = new Player(0, this);
0023     mPlayers[1] = new Player(1, this);
0024 }
0025 
0026 // Resets the data
0027 void ConfigTwo::reset()
0028 {
0029     mPlayers[0]->setName(i18nc("Default player name", "Alice"));
0030     mPlayers[1]->setName(i18nc("Default player name", "Bob"));
0031 
0032     // Default input types (must be after GUI and players)
0033     setInputType(0, TypeMouseInput);
0034     setInputType(1, TypeAiInput);
0035 }
0036 
0037 // Destructor
0038 ConfigTwo::~ConfigTwo()
0039 {
0040     QHashIterator<int, Player *> it(mPlayers);
0041     while (it.hasNext())
0042     {
0043         it.next();
0044         Player *player = it.value();
0045         delete player;
0046     }
0047     mPlayers.clear();
0048 }
0049 
0050 // Save properties
0051 void ConfigTwo::save(KConfig *cfg)
0052 {
0053     KConfigGroup group = cfg->group(QStringLiteral("LSkatData"));
0054     group.writeEntry("input0", (int)mInputTypes[0]);
0055     group.writeEntry("input1", (int)mInputTypes[1]);
0056 
0057     // Save player
0058     QHashIterator<int, Player *> it = playerIterator();
0059     while (it.hasNext())
0060     {
0061         it.next();
0062         Player *player = it.value();
0063         int no = it.key();
0064         KConfigGroup playercfg = cfg->group(QStringLiteral("LSkat_Player%1").arg(no));
0065         player->save(playercfg);
0066     }
0067 }
0068 
0069 // Load properties
0070 void ConfigTwo::load(KConfig *cfg)
0071 {
0072     reset();
0073     KConfigGroup group = cfg->group(QStringLiteral("LSkatData"));
0074     int num;
0075     num = group.readEntry("input0", (int)mInputTypes[0]);
0076     setInputType(0, (InputDeviceType)num);
0077     num = group.readEntry("input1", (int)mInputTypes[1]);
0078     setInputType(1, (InputDeviceType)num);
0079 
0080     // Load player
0081     QHashIterator<int, Player *> it = playerIterator();
0082     while (it.hasNext())
0083     {
0084         it.next();
0085         Player *player = it.value();
0086         int no = it.key();
0087         KConfigGroup playercfg = cfg->group(QStringLiteral("LSkat_Player%1").arg(no));
0088         player->load(playercfg);
0089     }
0090 }
0091 
0092 // Retrieve a player.
0093 Player *ConfigTwo::player(int no)
0094 {
0095     if (!mPlayers.contains(no)) return nullptr;
0096     return mPlayers[no];
0097 }
0098 
0099 // Retrieve player as iterator
0100 QHashIterator<int, Player *> ConfigTwo::playerIterator()
0101 {
0102     QHashIterator<int, Player *> it(mPlayers);
0103     return it;
0104 }
0105 
0106 // Retrieve input type of given player
0107 InputDeviceType ConfigTwo::inputType(int no)
0108 {
0109     return mInputTypes[no];
0110 }
0111 
0112 // Set the input type for a given players
0113 void ConfigTwo::setInputType(int no, InputDeviceType type)
0114 {
0115     mInputTypes[no] = type;
0116     Q_EMIT signalInputType(no, type);
0117 }
0118 
0119 #include "moc_config_two.cpp"