Skip to content

Commit

Permalink
Refactor|UI|Client: Added CVarChoiceWidget, used it in Audio Settings
Browse files Browse the repository at this point in the history
A more convenient way to modify cvars with a limited number of valid
values.
  • Loading branch information
skyjake committed Sep 2, 2013
1 parent e5f146b commit 15a9069
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 26 deletions.
2 changes: 2 additions & 0 deletions doomsday/client/client.pro
Expand Up @@ -397,6 +397,7 @@ DENG_HEADERS += \
include/ui/widgets/choicewidget.h \
include/ui/widgets/consolecommandwidget.h \
include/ui/widgets/consolewidget.h \
include/ui/widgets/cvarchoicewidget.h \
include/ui/widgets/cvarsliderwidget.h \
include/ui/widgets/cvartogglewidget.h \
include/ui/widgets/dialogwidget.h \
Expand Down Expand Up @@ -720,6 +721,7 @@ SOURCES += \
src/ui/widgets/choicewidget.cpp \
src/ui/widgets/consolecommandwidget.cpp \
src/ui/widgets/consolewidget.cpp \
src/ui/widgets/cvarchoicewidget.cpp \
src/ui/widgets/cvarsliderwidget.cpp \
src/ui/widgets/cvartogglewidget.cpp \
src/ui/widgets/dialogwidget.cpp \
Expand Down
2 changes: 0 additions & 2 deletions doomsday/client/include/ui/dialogs/audiosettingsdialog.h
Expand Up @@ -32,8 +32,6 @@ class AudioSettingsDialog : public DialogWidget
AudioSettingsDialog(de::String const &name = "audiosettings");

public slots:
void sampleRateChanged(uint);
void musicSourceChanged(uint);
void resetToDefaults();

private:
Expand Down
45 changes: 45 additions & 0 deletions doomsday/client/include/ui/widgets/cvarchoicewidget.h
@@ -0,0 +1,45 @@
/** @file cvarchoicewidget.h Console variable choice.
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#ifndef DENG_CLIENT_CVARCHOICEWIDGET_H
#define DENG_CLIENT_CVARCHOICEWIDGET_H

#include "choicewidget.h"

/**
* Console variable choice for integer-type cvars with a limited number of
* valid settings. The choice items' user data is used as the cvar value.
*/
class CVarChoiceWidget : public ChoiceWidget
{
Q_OBJECT

public:
CVarChoiceWidget(char const *cvarPath);

public slots:
void updateFromCVar();

protected slots:
void setCVarValueFromWidget();

private:
DENG2_PRIVATE(d)
};

#endif // DENG_CLIENT_CVARCHOICEWIDGET_H
31 changes: 7 additions & 24 deletions doomsday/client/src/ui/dialogs/audiosettingsdialog.cpp
Expand Up @@ -19,7 +19,7 @@
#include "ui/dialogs/audiosettingsdialog.h"
#include "ui/widgets/cvarsliderwidget.h"
#include "ui/widgets/cvartogglewidget.h"
#include "ui/widgets/choicewidget.h"
#include "ui/widgets/cvarchoicewidget.h"

#include "de_audio.h"
#include "con_main.h"
Expand All @@ -35,8 +35,8 @@ DENG_GUI_PIMPL(AudioSettingsDialog)
CVarSliderWidget *reverbVolume;
CVarToggleWidget *sound3D;
CVarToggleWidget *sound16bit;
ChoiceWidget *sampleRate;
ChoiceWidget *musicSource;
CVarChoiceWidget *sampleRate;
CVarChoiceWidget *musicSource;
CVarToggleWidget *soundInfo;

Instance(Public *i) : Base(i)
Expand All @@ -48,8 +48,8 @@ DENG_GUI_PIMPL(AudioSettingsDialog)
area.add(reverbVolume = new CVarSliderWidget("sound-reverb-volume"));
area.add(sound3D = new CVarToggleWidget("sound-3d"));
area.add(sound16bit = new CVarToggleWidget("sound-16bit"));
area.add(sampleRate = new ChoiceWidget);
area.add(musicSource = new ChoiceWidget);
area.add(sampleRate = new CVarChoiceWidget("sound-rate"));
area.add(musicSource = new CVarChoiceWidget("music-source"));
area.add(soundInfo = new CVarToggleWidget("sound-info"));
}

Expand All @@ -61,9 +61,8 @@ DENG_GUI_PIMPL(AudioSettingsDialog)
sound3D->updateFromCVar();
sound16bit->updateFromCVar();
soundInfo->updateFromCVar();

sampleRate->setSelected(sampleRate->items().findData(Con_GetInteger("sound-rate")));
musicSource->setSelected(musicSource->items().findData(Con_GetInteger("music-source")));
sampleRate->updateFromCVar();
musicSource->updateFromCVar();
}
};

Expand Down Expand Up @@ -96,9 +95,6 @@ AudioSettingsDialog::AudioSettingsDialog(String const &name)
<< new ChoiceItem(tr("2x @ 22050 Hz"), 22050)
<< new ChoiceItem(tr("4x @ 44100 Hz"), 44100);

connect(d->sampleRate, SIGNAL(selectionChangedByUser(uint)),
this, SLOT(sampleRateChanged(uint)));

LabelWidget *musSrcLabel = new LabelWidget;
musSrcLabel->setText(tr("Preferred Music:"));
area().add(musSrcLabel);
Expand All @@ -108,9 +104,6 @@ AudioSettingsDialog::AudioSettingsDialog(String const &name)
<< new ChoiceItem(tr("External files"), MUSP_EXT)
<< new ChoiceItem(tr("CD"), MUSP_CD);

connect(d->musicSource, SIGNAL(selectionChangedByUser(uint)),
this, SLOT(musicSourceChanged(uint)));

d->soundInfo->setText(tr("Developer Info"));

// Layout.
Expand All @@ -136,16 +129,6 @@ AudioSettingsDialog::AudioSettingsDialog(String const &name)
d->fetch();
}

void AudioSettingsDialog::sampleRateChanged(uint)
{
Con_SetInteger("sound-rate", d->sampleRate->selectedItem().data().toInt());
}

void AudioSettingsDialog::musicSourceChanged(uint)
{
Con_SetInteger("music-source", d->musicSource->selectedItem().data().toInt());
}

void AudioSettingsDialog::resetToDefaults()
{
Con_SetInteger("sound-volume", 255 );
Expand Down
54 changes: 54 additions & 0 deletions doomsday/client/src/ui/widgets/cvarchoicewidget.cpp
@@ -0,0 +1,54 @@
/** @file cvarchoicewidget.cpp Console variable choice.
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#include "ui/widgets/cvarchoicewidget.h"
#include "con_main.h"

using namespace de;
using namespace ui;

DENG2_PIMPL_NOREF(CVarChoiceWidget)
{
char const *cvar;

cvar_t *var() const
{
cvar_t *cv = Con_FindVariable(cvar);
DENG2_ASSERT(cv != 0);
return cv;
}
};

CVarChoiceWidget::CVarChoiceWidget(char const *cvarPath) : d(new Instance)
{
d->cvar = cvarPath;
updateFromCVar();

connect(this, SIGNAL(selectionChangedByUser(uint)),
this, SLOT(setCVarValueFromWidget()));
}

void CVarChoiceWidget::updateFromCVar()
{
setSelected(items().findData(CVar_Integer(d->var())));
}

void CVarChoiceWidget::setCVarValueFromWidget()
{
CVar_SetInteger(d->var(), selectedItem().data().toInt());
}

0 comments on commit 15a9069

Please sign in to comment.