dtkWidgetsParameterIntSlider.cpp 2.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Version: $Id$
//
//

// Commentary:
//
//

// Change Log:
//
//

// Code:

#include "dtkWidgetsParameterIntSlider.h"

17
#include <QtWidgets>
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

// ///////////////////////////////////////////////////////////////////
// dtkWidgetsParameterIntSliderPrivate declaration
// ///////////////////////////////////////////////////////////////////

class dtkWidgetsParameterIntSliderPrivate
{
public:
    QSlider *slider = nullptr;
};

// ///////////////////////////////////////////////////////////////////
// dtkWidgetsParameterIntSlider implementation
// ///////////////////////////////////////////////////////////////////

dtkWidgetsParameterIntSlider::dtkWidgetsParameterIntSlider(QWidget* parent) : dtkWidgetsParameterBase<dtk::d_int>(parent), d(new dtkWidgetsParameterIntSliderPrivate)
{
    d->slider = new QSlider;
    d->slider->setOrientation(Qt::Horizontal);

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(d->slider);

    this->setLayout(layout);
}

dtkWidgetsParameterIntSlider::~dtkWidgetsParameterIntSlider(void)
{
    delete d;
}

Pierre Fernique's avatar
Push  
Pierre Fernique committed
49
bool dtkWidgetsParameterIntSlider::connect(dtkCoreParameter *p)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
{
    if (!p) {
        qWarning() << Q_FUNC_INFO << "The input parameter is null. Nothing is done.";
        return false;
    }

    m_parameter = dynamic_cast<dtk::d_int *>(p);

    if(!m_parameter) {
        qWarning() << Q_FUNC_INFO << "The type of the parameter is not compatible with the widget dtkWidgetsParameterIntSlider.";
        return false;
    }

    d->slider->setMinimum(m_parameter->min());
    d->slider->setMaximum(m_parameter->max());
    d->slider->setToolTip(m_parameter->documentation());

    d->slider->setValue(m_parameter->value());

    m_parameter->connect([=] (QVariant v)
    {
        int value = v.value<dtk::d_int>().value();
        d->slider->blockSignals(true);
        d->slider->setValue(value);
        d->slider->blockSignals(false);
    });

    QObject::connect(d->slider, QOverload<int>::of(&QSlider::valueChanged), [=] (int v)
    {
        m_parameter->setValue(v);
    });

    return true;
}

//
// dtkWidgetsParameterIntSliderWidget.cpp ends here