Skip to content

Commit

Permalink
UI|Client: Added VariableChoiceWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Jan 6, 2014
1 parent 11c746e commit 51ce7c8
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doomsday/client/client.pro
Expand Up @@ -471,6 +471,7 @@ DENG_HEADERS += \
include/ui/widgets/sliderwidget.h \
include/ui/widgets/taskbarwidget.h \
include/ui/widgets/togglewidget.h \
include/ui/widgets/variablechoicewidget.h \
include/ui/widgets/variabletogglewidget.h \
include/ui/windowsystem.h \
include/ui/windowtransform.h \
Expand Down Expand Up @@ -828,6 +829,7 @@ SOURCES += \
src/ui/widgets/sliderwidget.cpp \
src/ui/widgets/taskbarwidget.cpp \
src/ui/widgets/togglewidget.cpp \
src/ui/widgets/variablechoicewidget.cpp \
src/ui/widgets/variabletogglewidget.cpp \
src/ui/windowsystem.cpp \
src/ui/windowtransform.cpp \
Expand Down
52 changes: 52 additions & 0 deletions doomsday/client/include/ui/widgets/variablechoicewidget.h
@@ -0,0 +1,52 @@
/** @file variablechoicewidget.h Choice widget for Variable values.
*
* @authors Copyright (c) 2014 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_VARIABLECHOICEWIDGET_H
#define DENG_CLIENT_VARIABLECHOICEWIDGET_H

#include <de/Variable>

#include "choicewidget.h"

/**
* Widget for choosing the value of a variable from a set of possible values.
*/
class VariableChoiceWidget : public ChoiceWidget
{
Q_OBJECT

public:
/// Thrown when the variable is gone and someone tries to access it. @ingroup errors
DENG2_ERROR(VariableMissingError);

public:
VariableChoiceWidget(de::Variable &variable, de::String const &name = "");

de::Variable &variable() const;

public slots:
void updateFromVariable();

protected slots:
void setVariableFromWidget();

private:
DENG2_PRIVATE(d)
};

#endif // DENG_CLIENT_VARIABLECHOICEWIDGET_H
102 changes: 102 additions & 0 deletions doomsday/client/src/ui/widgets/variablechoicewidget.cpp
@@ -0,0 +1,102 @@
/** @file variablechoicewidget.cpp Choice widget for Variable values.
*
* @authors Copyright (c) 2014 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/variablechoicewidget.h"

#include <de/NumberValue>

using namespace de;

/// @todo Add support for TextValue in addition to NumberValue.
DENG2_PIMPL(VariableChoiceWidget),
DENG2_OBSERVES(Variable, Deletion),
DENG2_OBSERVES(Variable, Change )
{
Variable *var;

Instance(Public *i, Variable &variable) : Base(i), var(&variable)
{
updateFromVariable();

var->audienceForDeletion += this;
var->audienceForChange += this;
}

~Instance()
{
if(var)
{
var->audienceForDeletion -= this;
var->audienceForChange -= this;
}
}

void updateFromVariable()
{
if(!var) return;

self.setSelected(self.items().findData(var->value().asNumber()));
}

void setVariableFromWidget()
{
if(!var) return;

var->audienceForChange -= this;
var->set(NumberValue(self.selectedItem().data().toInt()));
var->audienceForChange += this;
}

void variableValueChanged(Variable &, Value const &)
{
updateFromVariable();
}

void variableBeingDeleted(Variable &)
{
var = 0;
self.disable();
}
};

VariableChoiceWidget::VariableChoiceWidget(Variable &variable, String const &name)
: ChoiceWidget(name), d(new Instance(this, variable))
{
connect(this, SIGNAL(selectionChangedByUser(uint)),
this, SLOT(setVariableFromWidget()));
}

Variable &VariableChoiceWidget::variable() const
{
if(!d->var)
{
throw VariableMissingError("VariableChoiceWidget::variable",
"Widget is not associated with a variable");
}
return *d->var;
}

void VariableChoiceWidget::updateFromVariable()
{
d->updateFromVariable();
}

void VariableChoiceWidget::setVariableFromWidget()
{
d->setVariableFromWidget();
}

0 comments on commit 51ce7c8

Please sign in to comment.