Skip to content

Commit

Permalink
libappfw: Added VariableSliderWidget
Browse files Browse the repository at this point in the history
Also, SliderWidget's range labels now use the slider's precision.
  • Loading branch information
skyjake committed Apr 11, 2014
1 parent 0003efa commit 7d24d75
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 2 deletions.
2 changes: 2 additions & 0 deletions doomsday/libappfw/include/de/VariableSliderWidget
@@ -0,0 +1,2 @@
#include "widgets/variablesliderwidget.h"

57 changes: 57 additions & 0 deletions doomsday/libappfw/include/de/widgets/variablesliderwidget.h
@@ -0,0 +1,57 @@
/** @file variablesliderwidget.h Slider that changes a Variable.
*
* @authors Copyright (c) 2014 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 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 Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBAPPFW_VARIABLESLIDERWIDGET_H
#define LIBAPPFW_VARIABLESLIDERWIDGET_H

#include <de/Variable>

#include "../SliderWidget"

namespace de {

/**
* Widget for changing the value of a Variable using a slider.
*/
class LIBAPPFW_PUBLIC VariableSliderWidget : public SliderWidget
{
Q_OBJECT

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

public:
VariableSliderWidget(Variable &variable, Ranged const &range, ddouble step = 0,
String const &name = "");

Variable &variable() const;

public slots:
void updateFromVariable();

protected slots:
void setVariableFromWidget();

private:
DENG2_PRIVATE(d)
};

} // namespace de

#endif // LIBAPPFW_VARIABLESLIDERWIDGET_H
3 changes: 3 additions & 0 deletions doomsday/libappfw/libappfw.pro
Expand Up @@ -82,6 +82,7 @@ publicHeaders(root, \
include/de/WindowTransform \
include/de/VariableChoiceWidget \
include/de/VariableLineEditWidget \
include/de/VariableSliderWidget \
include/de/VariableToggleWidget \
include/de/VRConfig \
\
Expand Down Expand Up @@ -172,6 +173,7 @@ publicHeaders(widgets, \
include/de/widgets/togglewidget.h \
include/de/widgets/variablechoicewidget.h \
include/de/widgets/variablelineeditwidget.h \
include/de/widgets/variablesliderwidget.h \
include/de/widgets/variabletogglewidget.h \
)

Expand Down Expand Up @@ -234,6 +236,7 @@ SOURCES += \
src/widgets/togglewidget.cpp \
src/widgets/variablechoicewidget.cpp \
src/widgets/variablelineeditwidget.cpp \
src/widgets/variablesliderwidget.cpp \
src/widgets/variabletogglewidget.cpp \
src/windowsystem.cpp \
src/windowtransform.cpp
Expand Down
5 changes: 3 additions & 2 deletions doomsday/libappfw/src/widgets/sliderwidget.cpp
Expand Up @@ -382,8 +382,8 @@ DENG_GUI_PIMPL(SliderWidget)

void updateRangeLabels()
{
labels[Start].setText(minLabel.isEmpty()? QString::number(range.start * displayFactor) : minLabel);
labels[End].setText(maxLabel.isEmpty()? QString::number(range.end * displayFactor) : maxLabel);
labels[Start].setText(minLabel.isEmpty()? QString::number(range.start * displayFactor, 'g', precision) : minLabel);
labels[End].setText(maxLabel.isEmpty()? QString::number(range.end * displayFactor, 'g', precision) : maxLabel);
}

void startGrab(MouseEvent const &ev)
Expand Down Expand Up @@ -492,6 +492,7 @@ void SliderWidget::setPrecision(int precisionDecimals)
{
d->precision = precisionDecimals;
d->updateValueLabel();
d->updateRangeLabels();
}

void SliderWidget::setValue(ddouble value)
Expand Down
105 changes: 105 additions & 0 deletions doomsday/libappfw/src/widgets/variablesliderwidget.cpp
@@ -0,0 +1,105 @@
/** @file variablesliderwidget.cpp
*
* @authors Copyright (c) 2014 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 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 Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#include "de/VariableSliderWidget"

#include <de/NumberValue>

namespace de {

DENG2_PIMPL(VariableSliderWidget)
, DENG2_OBSERVES(Variable, Deletion)
, DENG2_OBSERVES(Variable, Change )
{
Variable *var;

Instance(Public *i, Variable &variable) : Base(i), var(&variable)
{
var->audienceForDeletion() += this;
var->audienceForChange() += this;
}

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

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

self.setValue(var->value<NumberValue>().asNumber());
}

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

var->audienceForChange() -= this;
var->set(NumberValue(self.value()));
var->audienceForChange() += this;
}

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

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

VariableSliderWidget::VariableSliderWidget(Variable &variable, Ranged const &range,
ddouble step, String const &name)
: SliderWidget(name), d(new Instance(this, variable))
{
setRange(range, step);
updateFromVariable();

connect(this, SIGNAL(valueChangedByUser(double)),
this, SLOT(setVariableFromWidget()));
}

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

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

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

} // namespace de

0 comments on commit 7d24d75

Please sign in to comment.