Skip to content

Commit

Permalink
Use the AllowedGames struct as value (and not as ptr)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benni committed Aug 4, 2024
1 parent d36503a commit 834168f
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 66 deletions.
18 changes: 6 additions & 12 deletions src/gameinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ bool GameInfo::istTrumpf(Card *card) const
*/
bool GameInfo::isAllowed( CardList* cards, int mode, int color )
{
AllowedGames* allowed = NULL;

// it is never allowed to decide to play Ramsch
if( mode == GameInfo::RAMSCH )
return false;
Expand All @@ -173,28 +171,24 @@ bool GameInfo::isAllowed( CardList* cards, int mode, int color )
return false;

// check for the users preferences now
allowed = Settings::instance()->allowedGames();
if( mode == GameInfo::DACHS && !allowed->dachs )
const AllowedGames allowed = Settings::instance()->allowedGames();
if( mode == GameInfo::DACHS && !allowed.dachs )
{
delete allowed;
return false;
}

if( (mode == GameInfo::WENZ && !allowed->wenz) ||
(mode == GameInfo::WENZ && color != Card::NOCOLOR && !allowed->farb_wenz) )
if( (mode == GameInfo::WENZ && !allowed.wenz) ||
(mode == GameInfo::WENZ && color != Card::NOCOLOR && !allowed.farb_wenz) )
{
delete allowed;
return false;
}

if( (mode == GameInfo::GEIER && !allowed->geier) ||
(mode == GameInfo::GEIER && color != Card::NOCOLOR && !allowed->farb_geier) )
if( (mode == GameInfo::GEIER && !allowed.geier) ||
(mode == GameInfo::GEIER && color != Card::NOCOLOR && !allowed.farb_geier) )
{
delete allowed;
return false;
}

delete allowed;
return true;
}

Expand Down
17 changes: 8 additions & 9 deletions src/preferencesdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ PreferencesDlg::~PreferencesDlg()
void PreferencesDlg::accept()
{
ResultValues r;
AllowedGames allowed;
Settings* s = Settings::instance();

s->setResultsType( m_radioMoney->isChecked() ? Settings::MONEY: Settings::POINTS );
Expand Down Expand Up @@ -97,12 +96,13 @@ void PreferencesDlg::accept()
s->setBackgroundImage( m_linePathBackground->text() );

// allowed games
AllowedGames allowed;
allowed.wenz = m_games_wenz->isChecked();
allowed.farb_wenz = m_games_farbwenz->isChecked();
allowed.geier = m_games_geier->isChecked();
allowed.farb_geier = m_games_farbgeier->isChecked();
allowed.dachs = m_games_dachs->isChecked();
s->setAllowedGames( &allowed );
s->setAllowedGames(allowed);

KPageDialog::accept();
}
Expand Down Expand Up @@ -423,13 +423,12 @@ void PreferencesDlg::addPageGames()
layout->addItem( spacer, 6, 0 );

// load settings
AllowedGames* allowed = Settings::instance()->allowedGames();
m_games_wenz->setChecked( allowed->wenz );
m_games_farbwenz->setChecked( allowed->farb_wenz );
m_games_geier->setChecked( allowed->geier );
m_games_farbgeier->setChecked( allowed->farb_geier );
m_games_dachs->setChecked( allowed->dachs );
delete allowed;
const AllowedGames allowed = Settings::instance()->allowedGames();
m_games_wenz->setChecked( allowed.wenz );
m_games_farbwenz->setChecked( allowed.farb_wenz );
m_games_geier->setChecked( allowed.geier );
m_games_farbgeier->setChecked( allowed.farb_geier );
m_games_dachs->setChecked( allowed.dachs );

// connections
connect(m_games_wenz, &QCheckBox::clicked, this, &PreferencesDlg::enableControls);
Expand Down
13 changes: 4 additions & 9 deletions src/selectgamecolorbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ SelectGameColorBox::SelectGameColorBox( QWidget *parent )
: QWizardPage( parent )
{
setTitle( tr("Step 2/2: Select Color") );
m_allowed = Settings::instance()->allowedGames();

m_gameinfo=0;

Expand Down Expand Up @@ -115,12 +114,6 @@ SelectGameColorBox::SelectGameColorBox( QWidget *parent )
this->setLayout( layout );
}

SelectGameColorBox::~SelectGameColorBox()
{

delete m_allowed;
}

int SelectGameColorBox::getColor()
{
if(checkEichel->isChecked()) return (Card::EICHEL);
Expand Down Expand Up @@ -208,11 +201,13 @@ void SelectGameColorBox::setStatus(bool Eichel, bool Gras, bool Herz, bool Schel
{
bool allow_colors = true;

const AllowedGames allowed = Settings::instance()->allowedGames();

if( m_gameinfo->mode() == GameInfo::WENZ )
allow_colors = allow_colors & m_allowed->farb_wenz;
allow_colors = allow_colors & allowed.farb_wenz;

if( m_gameinfo->mode() == GameInfo::GEIER )
allow_colors = allow_colors & m_allowed->farb_geier;
allow_colors = allow_colors & allowed.farb_geier;

checkEichel->setVisible(allow_colors&&Eichel==true);
labelEichel->setVisible(allow_colors&&Eichel==true);
Expand Down
6 changes: 1 addition & 5 deletions src/selectgamecolorbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class Card;
class SelectGameWizard;
class GameInfo;

struct AllowedGames;

/**
@author Christian Kern
Expand All @@ -44,14 +43,11 @@ class SelectGameColorBox : public QWizardPage
SelectGameColorBox( QWidget *parent = 0 );
void cleanGameInfo();

~SelectGameColorBox();
~SelectGameColorBox() = default;
void setGameInfo(GameInfo* info);
GameInfo* getGameInfo();
bool getFinish();

private:
AllowedGames* m_allowed;

protected:
GameInfo* m_gameinfo;
QRadioButton* checkFarblos;
Expand Down
13 changes: 4 additions & 9 deletions src/selectgametypebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ SelectGameTypeBox::SelectGameTypeBox( QWidget *parent )
: QWizardPage( parent )
{
setTitle( tr("Step 1/2: Select Game") );
m_allowed = Settings::instance()->allowedGames();

QWidget* row1 = new QWidget;

Expand All @@ -55,9 +54,10 @@ SelectGameTypeBox::SelectGameTypeBox( QWidget *parent )

checkRufspiel->setChecked(true);

checkGeier->setEnabled( m_allowed->geier );
checkWenz->setEnabled( m_allowed->wenz );
checkDachs->setEnabled( m_allowed->dachs );
const AllowedGames allowed = Settings::instance()->allowedGames();
checkGeier->setEnabled(allowed.geier);
checkWenz->setEnabled(allowed.wenz);
checkDachs->setEnabled(allowed.dachs);

QButtonGroup *myGroup = new QButtonGroup;
myGroup->addButton( checkRufspiel );
Expand Down Expand Up @@ -91,11 +91,6 @@ SelectGameTypeBox::SelectGameTypeBox( QWidget *parent )
infoLabel->setMaximumWidth(preview->size().width());
}

SelectGameTypeBox::~SelectGameTypeBox()
{
delete m_allowed;
}

void SelectGameTypeBox::typeChanged()
{
const auto HelpGametypeSolo = tr("When you play a \"Solo\", you have to additionally choose a color. Trumps are all \"Ober\", \"Unter\" and the color you chose. You will play alone against the other three players.");
Expand Down
6 changes: 1 addition & 5 deletions src/selectgametypebox.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class QLabel;
class QRadioButton;
class GameInfo;

struct AllowedGames;

/**
@author Christian Kern
Expand All @@ -39,12 +38,9 @@ class SelectGameTypeBox : public QWizardPage
public:
SelectGameTypeBox(QWidget *parent = 0 );

~SelectGameTypeBox();
~SelectGameTypeBox() = default;
GameInfo* gameInfo() const;

private:
AllowedGames* m_allowed;

protected:
QRadioButton* checkRufspiel;
QRadioButton* checkSolo;
Expand Down
26 changes: 13 additions & 13 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,29 +331,29 @@ QString Settings::backgroundImage() const
}


AllowedGames* Settings::allowedGames() const
AllowedGames Settings::allowedGames() const
{
QMutexLocker locker( m_mutex );
AllowedGames* a = new AllowedGames;

AllowedGames a;
KConfigGroup config = m_config->group("Games");
a->wenz = config.readEntry( "AllowWenz", true );
a->farb_wenz = config.readEntry( "AllowFarbWenz", true );
a->geier = config.readEntry( "AllowGeier", true );
a->farb_geier = config.readEntry( "AllowFarbGeier", false );
a->dachs = config.readEntry( "AllowDachs", false );
a.wenz = config.readEntry( "AllowWenz", true );
a.farb_wenz = config.readEntry( "AllowFarbWenz", true );
a.geier = config.readEntry( "AllowGeier", true );
a.farb_geier = config.readEntry( "AllowFarbGeier", false );
a.dachs = config.readEntry( "AllowDachs", false );

return a;
}

void Settings::setAllowedGames( const AllowedGames* allowed )
void Settings::setAllowedGames(const AllowedGames& allowed)
{
QMutexLocker locker( m_mutex );
KConfigGroup config = m_config->group("Games");
config.writeEntry( "AllowWenz", allowed->wenz );
config.writeEntry( "AllowFarbWenz", allowed->farb_wenz );
config.writeEntry( "AllowGeier", allowed->geier );
config.writeEntry( "AllowFarbGeier", allowed->farb_geier );
config.writeEntry( "AllowDachs", allowed->dachs );
config.writeEntry( "AllowWenz", allowed.wenz );
config.writeEntry( "AllowFarbWenz", allowed.farb_wenz );
config.writeEntry( "AllowGeier", allowed.geier );
config.writeEntry( "AllowFarbGeier", allowed.farb_geier );
config.writeEntry( "AllowDachs", allowed.dachs );
config.sync();
}
6 changes: 2 additions & 4 deletions src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,8 @@ class Settings : public QObject
ResultValues moneyResults() const;
ResultValues pointResults() const;

/** delete after usage
*/
AllowedGames* allowedGames() const;
void setAllowedGames( const AllowedGames* allowed );
AllowedGames allowedGames() const;
void setAllowedGames(const AllowedGames& allowed);

signals:
void cardChanged();
Expand Down

0 comments on commit 834168f

Please sign in to comment.