2018-04-17 13:13:32 +00:00
|
|
|
#include <QtWidgets>
|
2018-09-18 03:17:35 +00:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QVBoxLayout>
|
2018-12-02 13:46:04 +00:00
|
|
|
#include "theme.h"
|
2018-04-17 13:13:32 +00:00
|
|
|
#include "floatnumberwidget.h"
|
|
|
|
|
2018-09-18 03:17:35 +00:00
|
|
|
FloatNumberWidget::FloatNumberWidget(QWidget *parent, bool singleLine) :
|
2018-04-17 13:13:32 +00:00
|
|
|
QWidget(parent)
|
|
|
|
{
|
|
|
|
m_slider = new QSlider(Qt::Horizontal, this);
|
|
|
|
m_slider->setRange(0, 100);
|
2019-10-19 03:08:59 +00:00
|
|
|
m_slider->setFixedWidth(240);
|
2018-04-17 13:13:32 +00:00
|
|
|
|
|
|
|
m_label = new QLabel(this);
|
2018-09-06 15:04:59 +00:00
|
|
|
m_label->setAlignment(Qt::AlignLeft);
|
2018-04-17 13:13:32 +00:00
|
|
|
|
|
|
|
connect(m_slider, &QAbstractSlider::valueChanged, [=](int value) {
|
|
|
|
float fvalue = value / 100.0;
|
2018-09-06 15:04:59 +00:00
|
|
|
updateValueLabel(fvalue);
|
2018-04-17 13:13:32 +00:00
|
|
|
emit valueChanged(fvalue);
|
|
|
|
});
|
2018-09-18 03:17:35 +00:00
|
|
|
|
|
|
|
QBoxLayout *layout = nullptr;
|
|
|
|
if (singleLine) {
|
|
|
|
layout = new QHBoxLayout(this);
|
|
|
|
layout->setMargin(2);
|
|
|
|
layout->addWidget(m_slider);
|
|
|
|
layout->addWidget(m_label);
|
|
|
|
} else {
|
|
|
|
layout = new QVBoxLayout(this);
|
|
|
|
layout->setMargin(2);
|
|
|
|
layout->addWidget(m_label);
|
|
|
|
layout->addWidget(m_slider);
|
|
|
|
}
|
2018-04-17 13:13:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 15:04:59 +00:00
|
|
|
void FloatNumberWidget::updateValueLabel(float value)
|
|
|
|
{
|
|
|
|
QString valueString = QString().sprintf("%.2f", value);
|
|
|
|
if (m_itemName.isEmpty())
|
|
|
|
m_label->setText(valueString);
|
|
|
|
else
|
|
|
|
m_label->setText(m_itemName + ": " + valueString);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatNumberWidget::setItemName(const QString &name)
|
|
|
|
{
|
|
|
|
m_itemName = name;
|
|
|
|
updateValueLabel(value());
|
|
|
|
}
|
|
|
|
|
2018-04-17 13:13:32 +00:00
|
|
|
void FloatNumberWidget::setRange(float min, float max)
|
|
|
|
{
|
|
|
|
m_slider->setRange(min * 100, max * 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatNumberWidget::increaseValue()
|
|
|
|
{
|
|
|
|
m_slider->triggerAction(QSlider::SliderPageStepAdd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatNumberWidget::descreaseValue()
|
|
|
|
{
|
|
|
|
m_slider->triggerAction(QSlider::SliderPageStepSub);
|
|
|
|
}
|
|
|
|
|
|
|
|
float FloatNumberWidget::value() const
|
|
|
|
{
|
|
|
|
return m_slider->value() / 100.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatNumberWidget::setValue(float value)
|
|
|
|
{
|
|
|
|
m_slider->setValue(value * 100);
|
|
|
|
}
|