DoubleSliderWidget.cpp 2.43 KB
Newer Older
1 2 3 4 5
/*****************************************************************************
 * DoubleSliderWidget.cpp Handle double settings using a slider for values.
 *****************************************************************************
 * Copyright (C) 2008-2010 VideoLAN
 *
6
 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include "DoubleSliderWidget.h"

#include "SettingValue.h"

#include <QHBoxLayout>
#include <QLabel>
#include <QSlider>

DoubleSliderWidget::DoubleSliderWidget( SettingValue *s, QWidget *parent /*= NULL*/ ) :
32
        ISettingsCategoryWidget( parent, s )
33 34
{
    //Creating the slider
35
    m_slider = new QSlider( this );
36 37
    m_slider->setOrientation( Qt::Horizontal );
    //Creating the label
38
    m_valueDisplayer = new QLabel( QString::number( s->get().toDouble() ), this  );
39 40 41 42
    //Avoid label resizing due to roundups
    const QFontMetrics  &fm = m_valueDisplayer->fontMetrics();
    m_valueDisplayer->setFixedWidth( fm.width( "0.00" ) );
    //Setting the layout:
43 44
    layout()->addWidget( m_slider );
    layout()->addWidget( m_valueDisplayer );
45 46 47 48 49 50 51
    //TODO: check if the value is clamped
    m_slider->setMaximum( s->max().toDouble() * 100.0 );
    m_slider->setMinimum( s->min().toDouble() * 100.0 );
    changed ( s->get() );
    connect( m_slider, SIGNAL( valueChanged( int ) ), this, SLOT( sliderMoved( int ) ) );
}

52
bool
53 54 55
DoubleSliderWidget::save()
{
    m_setting->set( m_slider->value() / 100.0 );
56
    return true;
57 58 59 60 61 62 63 64 65 66 67 68 69
}

void
DoubleSliderWidget::changed( const QVariant &val )
{
    m_slider->setValue( val.toDouble() * 100.0 );
}

void
DoubleSliderWidget::sliderMoved( int value )
{
    m_valueDisplayer->setText( QString::number( (double)value / 100.0 ) );
}