2022-10-18 09:35:04 +00:00
|
|
|
#include "float_number_widget.h"
|
|
|
|
#include "theme.h"
|
2018-09-18 03:17:35 +00:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QVBoxLayout>
|
2022-10-18 09:35:04 +00:00
|
|
|
#include <QtWidgets>
|
2018-04-17 13:13:32 +00:00
|
|
|
|
2022-10-18 09:35:04 +00:00
|
|
|
FloatNumberWidget::FloatNumberWidget(QWidget* parent, bool singleLine)
|
|
|
|
: QWidget(parent)
|
2018-04-17 13:13:32 +00:00
|
|
|
{
|
|
|
|
m_slider = new QSlider(Qt::Horizontal, this);
|
|
|
|
m_slider->setRange(0, 100);
|
2022-11-04 11:07:20 +00:00
|
|
|
m_slider->setFixedWidth(Theme::sidebarPreferredWidth * 0.6);
|
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);
|
|
|
|
});
|
2022-10-18 09:35:04 +00:00
|
|
|
|
|
|
|
QBoxLayout* layout = nullptr;
|
2018-09-18 03:17:35 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-18 05:03:15 +00:00
|
|
|
void FloatNumberWidget::setSliderFixedWidth(float width)
|
|
|
|
{
|
|
|
|
m_slider->setFixedWidth(width);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-10-18 09:35:04 +00:00
|
|
|
void FloatNumberWidget::setItemName(const QString& name)
|
2018-09-06 15:04:59 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|