Files

120 lines
4.0 KiB
C++

#include "CounterEditor.h"
#include "ui_CounterEditor.h"
#include "ViewModels.h"
CounterEditor::CounterEditor(QWidget *parent) :
QWidget(parent),
ui(new Ui::CounterEditor)
{
ui->setupUi(this);
connect(ui->cbBoxCounterType, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &CounterEditor::onCounterTypeChanged);
connect(ui->cbBoxDigitalNum, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &CounterEditor::onDigitalNumChanged);
connect(ui->spinBoxCnt2StartValue, QOverload<int>::of(&QSpinBox::valueChanged), this, [this](int) {fetchCounterConfigFromView();} );
connect(ui->spinBoxCnt2EndValue, QOverload<int>::of(&QSpinBox::valueChanged), this, [this](int) {fetchCounterConfigFromView();} );
connect(ui->spinBoxCnt2RepeateCnt, QOverload<int>::of(&QSpinBox::valueChanged), this, [this](int) {fetchCounterConfigFromView();} );
connect(ui->spinBoxCnt2StepValue, QOverload<int>::of(&QSpinBox::valueChanged), this, [this](int) {fetchCounterConfigFromView();} );
connect(ui->spinBoxCnt2CurrValue, QOverload<int>::of(&QSpinBox::valueChanged), this, [this](int) {fetchCounterConfigFromView();} );
setViewModel(m_vm);
}
CounterEditor::~CounterEditor()
{
delete ui;
}
/**
* @brief 设置视图模型并将其更新至UI
*
* @param vm
*/
void CounterEditor::setViewModel(CounterEditorVM vm)
{
m_vm = std::move(vm);
// 根据 VM 中的当前计数器ID 确定显示哪个计数器的配置
CounterEditorVM::CounterSlot& cnt = m_vm.counterType == COUNTER_TYPE_1 ? m_vm.counter1 : m_vm.counter2;
auto b = ui->spinBoxCnt2StartValue->blockSignals(true);
ui->spinBoxCnt2StartValue->setValue(cnt.startValue);
ui->spinBoxCnt2StartValue->blockSignals(b);
b = ui->spinBoxCnt2EndValue->blockSignals(true);
ui->spinBoxCnt2EndValue->setValue(cnt.endValue);
ui->spinBoxCnt2EndValue->blockSignals(b);
b = ui->spinBoxCnt2RepeateCnt->blockSignals(true);
ui->spinBoxCnt2RepeateCnt->setValue(cnt.repeatValue);
ui->spinBoxCnt2RepeateCnt->blockSignals(b);
b = ui->spinBoxCnt2StepValue->blockSignals(true);
ui->spinBoxCnt2StepValue->setValue(cnt.stepValue);
ui->spinBoxCnt2StepValue->blockSignals(b);
b = ui->spinBoxCnt2CurrValue->blockSignals(true);
ui->spinBoxCnt2CurrValue->setValue(cnt.currValue);
ui->spinBoxCnt2CurrValue->blockSignals(b);
ui->labelCnt2CurrValue->setText(QString::number(cnt.currValue));
b = ui->cbBoxCounterType->blockSignals(true);
ui->cbBoxCounterType->setCurrentIndex(m_vm.counterType == COUNTER_TYPE_1 ? 0 : 1);
ui->cbBoxCounterType->blockSignals(b);
b = ui->cbBoxDigitalNum->blockSignals(true);
ui->cbBoxDigitalNum->setCurrentIndex(m_vm.digitalNum - 1);
ui->cbBoxDigitalNum->blockSignals(b);
}
/**
* @brief 从界面获取最新配置以刷新视图模型
*
* @return CounterEditorVM 最新的视图模型
*/
CounterEditorVM CounterEditor::viewModel()
{
m_vm.counterType = ui->cbBoxCounterType->currentIndex() == 0 ? COUNTER_TYPE_1 : COUNTER_TYPE_2;
m_vm.digitalNum = ui->cbBoxDigitalNum->currentIndex() + 1;
fetchCounterConfigFromView();
return m_vm;
}
void CounterEditor::onCounterTypeChanged(int i)
{
i = std::max(i, 0);
m_vm.counterType = i == 0 ? COUNTER_TYPE_1 : COUNTER_TYPE_2;
setViewModel(m_vm);
}
void CounterEditor::onDigitalNumChanged(int i)
{
i = std::max(i, 0);
m_vm.digitalNum = i + 1;
}
/**
* @brief 从界面拉取计数器配置
*
* 根据当前所选的计数器ID更新对应的计数器配置
*
*/
void CounterEditor::fetchCounterConfigFromView()
{
CounterEditorVM::CounterSlot& cnt = m_vm.counterType == COUNTER_TYPE_1 ? m_vm.counter1 : m_vm.counter2;
cnt.startValue = ui->spinBoxCnt2StartValue->value();
cnt.endValue = ui->spinBoxCnt2EndValue->value();
cnt.repeatValue = ui->spinBoxCnt2RepeateCnt->value();
cnt.stepValue = ui->spinBoxCnt2StepValue->value();
cnt.currValue = ui->spinBoxCnt2CurrValue->value();
}
/**
* @brief 简单启用对应SpinBox
*
*/
void CounterEditor::onEditCurrValueToggled()
{
}