Files
fedora-tools/framework-laptop-tools/src/valuecontrol.cpp
T

43 lines
1.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SPDX-License-Identifier: MIT
#include "valuecontrol.h"
#include <QHBoxLayout>
#include <QSignalBlocker>
#include <algorithm>
ValueControl::ValueControl(QWidget *parent) : QWidget(parent),
m_slider(new QSlider(Qt::Horizontal, this)), m_number(new QSpinBox(this))
{
auto *layout = new QHBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(m_slider, 1); layout->addWidget(m_number);
m_slider->setMinimumWidth(100);
setFocusProxy(m_number);
connect(m_slider, &QSlider::valueChanged, m_number, &QSpinBox::setValue);
connect(m_number, &QSpinBox::valueChanged, m_slider, &QSlider::setValue);
connect(m_number, &QSpinBox::valueChanged, this, &ValueControl::valueChanged);
setRange(0, 100);
}
void ValueControl::setRange(int minimum, int maximum)
{
// Block the slider while changing its range so its old value cannot clamp
// the number field before both controls have the new bounds.
const QSignalBlocker block(m_slider);
m_slider->setRange(minimum, maximum);
m_number->setRange(minimum, maximum);
m_slider->setValue(m_number->value());
m_slider->setPageStep(std::max(1, (maximum - minimum) / 10));
updateRangeHint();
}
void ValueControl::setSuffix(const QString &suffix)
{
m_number->setSuffix(suffix); updateRangeHint();
}
void ValueControl::setSingleStep(int step)
{
m_slider->setSingleStep(step); m_number->setSingleStep(step);
}
void ValueControl::updateRangeHint()
{
m_slider->setToolTip(QString("%1%2%3").arg(m_number->minimum()).arg(m_number->maximum()).arg(m_number->suffix()));
}