Advertisement
Guest User

creategamedialog.cpp

a guest
Aug 23rd, 2017
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /***************************************************************************
  2.  *   Copyright (C) 2008 by MacJariel                                       *
  3.  *   echo "badmailet@gbalt.dob" | tr "edibmlt" "ecrmjil"                   *
  4.  *                                                                         *
  5.  *   This program is free software; you can redistribute it and/or modify  *
  6.  *   it under the terms of the GNU General Public License as published by  *
  7.  *   the Free Software Foundation; either version 2 of the License, or     *
  8.  *   (at your option) any later version.                                   *
  9.  *                                                                         *
  10.  *   This program is distributed in the hope that it will be useful,       *
  11.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  12.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  13.  *   GNU General Public License for more details.                          *
  14.  *                                                                         *
  15.  *   You should have received a copy of the GNU General Public License     *
  16.  *   along with this program; if not, write to the                         *
  17.  *   Free Software Foundation, Inc.,                                       *
  18.  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  19.  ***************************************************************************/
  20.  
  21. #include <QRegExpValidator>
  22. #include <QRegExp>
  23. #include "creategamedialog.h"
  24. #include "config.h"
  25.  
  26. using namespace client;
  27.  
  28. const int minPlayers = 4, maxPlayers = 7;
  29. const int maxCharacterNumber = 3;
  30.  
  31. CreateGameDialog::CreateGameDialog(QWidget *parent)
  32.  : QDialog(parent), Ui::CreateGameDialog()
  33. {
  34.     setupUi(this);
  35.  
  36.     pushButtonCreate->setEnabled(0);
  37.  
  38.     spinBoxMinPlayers->setRange(minPlayers, maxPlayers);
  39.     spinBoxMaxPlayers->setRange(minPlayers, maxPlayers);
  40.     spinBoxAIPlayers->setRange(0, maxPlayers - 1);
  41.  
  42.     //This class does not know this spinbox
  43.     spinBoxCharacterNumber->setRange(1, maxCharacterNumber);
  44.    
  45.     if (Config::instance().hasGroup("create-game-dialog")) {
  46.         loadConfigValues();
  47.     } else {
  48.         spinBoxMinPlayers->setValue(minPlayers);
  49.         spinBoxMaxPlayers->setValue(maxPlayers);
  50.         spinBoxAIPlayers->setValue(0);
  51.         spinBoxMaxSpectators->setValue(-1);
  52.     }
  53.  
  54.     connect(spinBoxMinPlayers, SIGNAL(valueChanged(int)),
  55.             this, SLOT(playerCountsChanged()));
  56.     connect(spinBoxMaxPlayers, SIGNAL(valueChanged(int)),
  57.             this, SLOT(playerCountsChanged()));
  58.  
  59.     connect(lineEditGameName,   SIGNAL(textChanged(const QString&)),
  60.             this, SLOT(validateInput()));
  61.     connect(lineEditPlayerName, SIGNAL(textChanged(const QString&)),
  62.             this, SLOT(validateInput()));
  63. }
  64.  
  65.  
  66. CreateGameDialog::~CreateGameDialog()
  67. {
  68. }
  69.  
  70. void CreateGameDialog::playerCountsChanged()
  71. {
  72.     spinBoxMinPlayers->setMaximum(spinBoxMaxPlayers->value());
  73.     spinBoxMaxPlayers->setMinimum(spinBoxMinPlayers->value());
  74.     spinBoxAIPlayers->setMaximum(spinBoxMaxPlayers->value() - 1);
  75. }
  76.  
  77. void CreateGameDialog::validateInput()
  78. {
  79.     pushButtonCreate->setEnabled(!lineEditGameName->text().isEmpty() &&
  80.                                  !lineEditPlayerName->text().isEmpty());
  81. }
  82.  
  83. void CreateGameDialog::on_pushButtonCreate_clicked()
  84. {
  85.     CreateGameData createGameData;
  86.     createGameData.name                 = lineEditGameName->text();
  87.     createGameData.description          = lineEditGameDescription->text();
  88.     createGameData.minPlayers           = spinBoxMinPlayers->value();
  89.     createGameData.maxPlayers           = spinBoxMaxPlayers->value();
  90.     createGameData.maxSpectators        = spinBoxMaxSpectators->value();
  91.     createGameData.AIPlayers            = spinBoxAIPlayers->value();
  92.     createGameData.playerPassword       = lineEditGamePasswordPlayers->text();
  93.     createGameData.spectatorPassword    = lineEditGamePasswordSpectators->text();
  94.     createGameData.flagShufflePlayers   = radioButtonOrderRandom->isChecked();
  95.  
  96.     CreatePlayerData createPlayerData;
  97.     createPlayerData.name               = lineEditPlayerName->text();
  98.     createPlayerData.password           = lineEditPlayerPassword->text();
  99.     createPlayerData.avatar             = selectPlayerIconWidget->image();
  100.  
  101.     saveConfigValues(createGameData);
  102.  
  103.     emit createGame(createGameData, createPlayerData);
  104.     close();
  105. }
  106.  
  107.  
  108. void CreateGameDialog::loadConfigValues()
  109. {
  110.     Config& cfg = Config::instance();
  111.     cfg.refresh();
  112.     QString grp("create-game-dialog");
  113.     lineEditGameName->setText(cfg.readString(grp, "name"));
  114.     lineEditGameDescription->setText(cfg.readString(grp, "description"));
  115.     spinBoxMinPlayers->setValue(cfg.readInt(grp, "min-players"));
  116.     spinBoxMaxPlayers->setValue(cfg.readInt(grp, "max-players"));
  117.     spinBoxMaxSpectators->setValue(cfg.readInt(grp, "max-spectators"));
  118.     spinBoxAIPlayers->setValue(cfg.readInt(grp, "ai-players"));
  119.     int shufflePlayers = cfg.readInt(grp, "flag-shuffle-players");
  120.     if (shufflePlayers)
  121.         radioButtonOrderRandom->setChecked(1);
  122.     else
  123.         radioButtonOrderChronological->setChecked(1);
  124.     lineEditPlayerName->setText(cfg.readString("player", "name"));
  125.     lineEditPlayerPassword->setText(cfg.readString("player", "password"));
  126.     selectPlayerIconWidget->setImageFileName(cfg.readString("player", "image"));
  127.     validateInput();
  128. }
  129.  
  130. void CreateGameDialog::saveConfigValues(const CreateGameData& game)
  131. {
  132.     Config& cfg = Config::instance();
  133.     QString grp("create-game-dialog");
  134.     cfg.writeString(grp, "name", game.name);
  135.     cfg.writeString(grp, "description", game.description);
  136.     cfg.writeInt(grp, "min-players", game.minPlayers);
  137.     cfg.writeInt(grp, "max-players", game.maxPlayers);
  138.     cfg.writeInt(grp, "max-spectators", game.maxSpectators);
  139.     cfg.writeInt(grp, "ai-players", game.AIPlayers);
  140.     cfg.writeInt(grp, "flag-shuffle-players", game.flagShufflePlayers);
  141.  
  142.     cfg.writeString("player", "name", lineEditPlayerName->text());
  143.     cfg.writeString("player", "password", lineEditPlayerPassword->text());
  144.     cfg.writeString("player", "image", selectPlayerIconWidget->imageFileName());
  145.  
  146.     cfg.store();
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement