108 lines
3.4 KiB
C++
108 lines
3.4 KiB
C++
#include "paramcontrol.h"
|
||
#include <QVBoxLayout>
|
||
#include <QHBoxLayout>
|
||
#include <QLabel>
|
||
#include <QPushButton>
|
||
#include "global.h"
|
||
|
||
ParamControl::ParamControl(const QString& name, const QString& unit,
|
||
double step, double min, double max,
|
||
bool withSave, bool withFactory, QWidget* parent)
|
||
: QWidget(parent), m_name(name), m_unit(unit),
|
||
m_step(step), m_min(min), m_max(max), m_value(min)
|
||
{
|
||
QVBoxLayout* lay = new QVBoxLayout(this);
|
||
lay->setContentsMargins(12, 8, 12, 8);
|
||
lay->setSpacing(6);
|
||
|
||
// 参数名
|
||
QLabel* nameLabel = new QLabel(name, this);
|
||
nameLabel->setProperty("sub", true);
|
||
nameLabel->setStyleSheet(QString("color:%1;font-size:22px;").arg(C::TEXT_SUB));
|
||
lay->addWidget(nameLabel);
|
||
|
||
// 数值 + 加减
|
||
QHBoxLayout* row = new QHBoxLayout();
|
||
row->setSpacing(12);
|
||
|
||
QPushButton* minus = new QPushButton("-", this);
|
||
minus->setFixedSize(96, 96);
|
||
minus->setStyleSheet(QString("font-size:44px;background-color:%1;color:%2;border:1px solid %3;border-radius:12px;")
|
||
.arg(C::PANEL_HI, C::TEXT, C::BORDER));
|
||
connect(minus, &QPushButton::clicked, this, &ParamControl::onMinus);
|
||
|
||
m_valueLabel = new QLabel(this);
|
||
m_valueLabel->setAlignment(Qt::AlignCenter);
|
||
m_valueLabel->setStyleSheet(QString("color:%1;font-size:52px;font-weight:700;").arg(C::TEXT));
|
||
m_valueLabel->setMinimumWidth(180);
|
||
|
||
QPushButton* plus = new QPushButton("+", this);
|
||
plus->setFixedSize(96, 96);
|
||
plus->setStyleSheet(QString("font-size:44px;background-color:%1;color:%2;border:1px solid %3;border-radius:12px;")
|
||
.arg(C::PANEL_HI, C::TEXT, C::BORDER));
|
||
connect(plus, &QPushButton::clicked, this, &ParamControl::onPlus);
|
||
|
||
row->addWidget(minus);
|
||
row->addWidget(m_valueLabel, 1);
|
||
row->addWidget(plus);
|
||
lay->addLayout(row);
|
||
|
||
// 保存 / 恢复出厂值(可选)
|
||
if (withSave || withFactory) {
|
||
QHBoxLayout* btnRow = new QHBoxLayout();
|
||
btnRow->setSpacing(12);
|
||
if (withSave) {
|
||
m_save = new QPushButton("保存", this);
|
||
m_save->setProperty("variant", "primary");
|
||
m_save->setMinimumHeight(56);
|
||
connect(m_save, &QPushButton::clicked, this, &ParamControl::saveClicked);
|
||
btnRow->addWidget(m_save);
|
||
}
|
||
if (withFactory) {
|
||
m_factory = new QPushButton("恢复出厂值", this);
|
||
m_factory->setMinimumHeight(56);
|
||
connect(m_factory, &QPushButton::clicked, this, &ParamControl::factoryResetClicked);
|
||
btnRow->addWidget(m_factory);
|
||
}
|
||
lay->addLayout(btnRow);
|
||
}
|
||
|
||
refresh();
|
||
}
|
||
|
||
void ParamControl::setValue(double v)
|
||
{
|
||
m_value = qBound(m_min, v, m_max);
|
||
refresh();
|
||
}
|
||
|
||
void ParamControl::setEnabled(bool enabled)
|
||
{
|
||
QWidget::setEnabled(enabled);
|
||
}
|
||
|
||
void ParamControl::onMinus()
|
||
{
|
||
m_value = qMax(m_min, m_value - m_step);
|
||
refresh();
|
||
emit valueChanged(m_value);
|
||
}
|
||
|
||
void ParamControl::onPlus()
|
||
{
|
||
m_value = qMin(m_max, m_value + m_step);
|
||
refresh();
|
||
emit valueChanged(m_value);
|
||
}
|
||
|
||
void ParamControl::refresh()
|
||
{
|
||
// 单位格式:整数不显示小数
|
||
QString s;
|
||
if (m_step < 1.0)
|
||
s = QString("%1").arg(m_value, 0, 'f', 1);
|
||
else
|
||
s = QString("%1").arg(m_value, 0, 'f', 0);
|
||
m_valueLabel->setText(s + " " + m_unit);
|
||
}
|