213 lines
7.6 KiB
C++

#include "dialogalgoarg.h"
#include "ui_dialogalgoarg.h"
#include <QDoubleValidator>
#include <QIntValidator>
#include <QLineEdit>
#include "ScrewPositionPresenter.h"
#include "StyledMessageBox.h"
DialogAlgoArg::DialogAlgoArg(QWidget *parent)
: QDialog(parent)
, ui(new Ui::DialogAlgoArg)
{
ui->setupUi(this);
setWindowTitle("Algorithm Parameters");
initNumericEditors();
}
DialogAlgoArg::~DialogAlgoArg()
{
delete ui;
}
void DialogAlgoArg::SetPresenter(ScrewPositionPresenter* presenter)
{
m_presenter = presenter;
loadParams();
}
void DialogAlgoArg::initNumericEditors()
{
auto setupDoubleEditor = [](QLineEdit* edit, double min, double max, int decimals) {
auto* validator = new QDoubleValidator(min, max, decimals, edit);
validator->setNotation(QDoubleValidator::StandardNotation);
edit->setValidator(validator);
};
auto setupIntEditor = [](QLineEdit* edit, int min, int max) {
edit->setValidator(new QIntValidator(min, max, edit));
};
setupDoubleEditor(ui->spinRodDiameter, 1.0, 100.0, 6);
setupDoubleEditor(ui->spinCornerTh, 0.0, 180.0, 6);
setupDoubleEditor(ui->spinScale, 0.0, 200.0, 6);
setupDoubleEditor(ui->spinMinEndingGap, 0.0, 100.0, 6);
setupDoubleEditor(ui->spinMinEndingGapZ, 0.0, 100.0, 6);
setupDoubleEditor(ui->spinJumpCornerTh1, 0.0, 100.0, 6);
setupDoubleEditor(ui->spinJumpCornerTh2, 0.0, 180.0, 6);
setupDoubleEditor(ui->spinContinuityTh, 0.0, 100.0, 6);
setupDoubleEditor(ui->spinOutlierTh, 0.0, 50.0, 6);
setupIntEditor(ui->spinMaxLineSkipNum, -1, 100);
setupDoubleEditor(ui->spinYDeviationMax, 0.0, 100.0, 6);
setupDoubleEditor(ui->spinMaxSkipDistance, 0.0, 100.0, 6);
setupDoubleEditor(ui->spinZDeviationMax, 0.0, 100.0, 6);
setupDoubleEditor(ui->spinMinLTypeTreeLen, 0.0, 1000.0, 6);
setupDoubleEditor(ui->spinMinVTypeTreeLen, 0.0, 1000.0, 6);
}
void DialogAlgoArg::setDoubleEditorText(QLineEdit* edit, double value)
{
if (edit) {
edit->setText(QString::number(value, 'g', 12));
}
}
void DialogAlgoArg::setIntEditorText(QLineEdit* edit, int value)
{
if (edit) {
edit->setText(QString::number(value));
}
}
bool DialogAlgoArg::tryGetDouble(QLineEdit* edit, const QString& label, double& value)
{
bool ok = false;
value = edit->text().trimmed().toDouble(&ok);
if (!ok) {
StyledMessageBox::warning(this, "Error",
QString("Invalid numeric input for \"%1\".").arg(label));
edit->setFocus();
edit->selectAll();
return false;
}
return true;
}
bool DialogAlgoArg::tryGetInt(QLineEdit* edit, const QString& label, int& value)
{
bool ok = false;
value = edit->text().trimmed().toInt(&ok);
if (!ok) {
StyledMessageBox::warning(this, "Error",
QString("Invalid integer input for \"%1\".").arg(label));
edit->setFocus();
edit->selectAll();
return false;
}
return true;
}
void DialogAlgoArg::loadParams()
{
if (!m_presenter) {
return;
}
const auto params = m_presenter->GetAlgoParams();
setDoubleEditorText(ui->spinRodDiameter, params.screwParam.rodDiameter);
ui->chkHorizonScan->setChecked(params.screwParam.isHorizonScan);
setDoubleEditorText(ui->spinCornerTh, params.cornerParam.cornerTh);
setDoubleEditorText(ui->spinScale, params.cornerParam.scale);
setDoubleEditorText(ui->spinMinEndingGap, params.cornerParam.minEndingGap);
setDoubleEditorText(ui->spinMinEndingGapZ, params.cornerParam.minEndingGap_z);
setDoubleEditorText(ui->spinJumpCornerTh1, params.cornerParam.jumpCornerTh_1);
setDoubleEditorText(ui->spinJumpCornerTh2, params.cornerParam.jumpCornerTh_2);
setDoubleEditorText(ui->spinContinuityTh, params.filterParam.continuityTh);
setDoubleEditorText(ui->spinOutlierTh, params.filterParam.outlierTh);
setIntEditorText(ui->spinMaxLineSkipNum, params.growParam.maxLineSkipNum);
setDoubleEditorText(ui->spinYDeviationMax, params.growParam.yDeviation_max);
setDoubleEditorText(ui->spinMaxSkipDistance, params.growParam.maxSkipDistance);
setDoubleEditorText(ui->spinZDeviationMax, params.growParam.zDeviation_max);
setDoubleEditorText(ui->spinMinLTypeTreeLen, params.growParam.minLTypeTreeLen);
setDoubleEditorText(ui->spinMinVTypeTreeLen, params.growParam.minVTypeTreeLen);
}
bool DialogAlgoArg::saveParams()
{
if (!m_presenter) {
return false;
}
auto params = m_presenter->GetAlgoParams();
if (!tryGetDouble(ui->spinRodDiameter, "Rod Diameter", params.screwParam.rodDiameter)) return false;
params.screwParam.isHorizonScan = ui->chkHorizonScan->isChecked();
if (!tryGetDouble(ui->spinCornerTh, "Corner Threshold", params.cornerParam.cornerTh)) return false;
if (!tryGetDouble(ui->spinScale, "Scale", params.cornerParam.scale)) return false;
if (!tryGetDouble(ui->spinMinEndingGap, "Min Ending Gap Y", params.cornerParam.minEndingGap)) return false;
if (!tryGetDouble(ui->spinMinEndingGapZ, "Min Ending Gap Z", params.cornerParam.minEndingGap_z)) return false;
if (!tryGetDouble(ui->spinJumpCornerTh1, "Jump Corner Threshold 1", params.cornerParam.jumpCornerTh_1)) return false;
if (!tryGetDouble(ui->spinJumpCornerTh2, "Jump Corner Threshold 2", params.cornerParam.jumpCornerTh_2)) return false;
if (!tryGetDouble(ui->spinContinuityTh, "Continuity Threshold", params.filterParam.continuityTh)) return false;
if (!tryGetDouble(ui->spinOutlierTh, "Outlier Threshold", params.filterParam.outlierTh)) return false;
if (!tryGetInt(ui->spinMaxLineSkipNum, "Max Line Skip Num", params.growParam.maxLineSkipNum)) return false;
if (!tryGetDouble(ui->spinYDeviationMax, "Y Deviation Max", params.growParam.yDeviation_max)) return false;
if (!tryGetDouble(ui->spinMaxSkipDistance, "Max Skip Distance", params.growParam.maxSkipDistance)) return false;
if (!tryGetDouble(ui->spinZDeviationMax, "Z Deviation Max", params.growParam.zDeviation_max)) return false;
if (!tryGetDouble(ui->spinMinLTypeTreeLen, "Min L Type Tree Len", params.growParam.minLTypeTreeLen)) return false;
if (!tryGetDouble(ui->spinMinVTypeTreeLen, "Min V Type Tree Len", params.growParam.minVTypeTreeLen)) return false;
m_presenter->SetAlgoParams(params);
return true;
}
void DialogAlgoArg::resetParams()
{
setDoubleEditorText(ui->spinRodDiameter, 10.0);
ui->chkHorizonScan->setChecked(true);
setDoubleEditorText(ui->spinCornerTh, 60.0);
setDoubleEditorText(ui->spinScale, 50.0);
setDoubleEditorText(ui->spinMinEndingGap, 20.0);
setDoubleEditorText(ui->spinMinEndingGapZ, 20.0);
setDoubleEditorText(ui->spinJumpCornerTh1, 10.0);
setDoubleEditorText(ui->spinJumpCornerTh2, 60.0);
setDoubleEditorText(ui->spinContinuityTh, 20.0);
setDoubleEditorText(ui->spinOutlierTh, 5.0);
setIntEditorText(ui->spinMaxLineSkipNum, 10);
setDoubleEditorText(ui->spinYDeviationMax, 10.0);
setDoubleEditorText(ui->spinMaxSkipDistance, 10.0);
setDoubleEditorText(ui->spinZDeviationMax, 10.0);
setDoubleEditorText(ui->spinMinLTypeTreeLen, 100.0);
setDoubleEditorText(ui->spinMinVTypeTreeLen, 100.0);
}
void DialogAlgoArg::on_btnOK_clicked()
{
if (saveParams()) {
accept();
}
}
void DialogAlgoArg::on_btnCancel_clicked()
{
reject();
}
void DialogAlgoArg::on_btnApply_clicked()
{
if (saveParams()) {
StyledMessageBox::information(this, "Info", "Parameters applied.");
}
}
void DialogAlgoArg::on_btnReset_clicked()
{
auto ret = StyledMessageBox::question(this, "Reset",
"Reset parameters to defaults?");
if (ret == QMessageBox::Yes) {
resetParams();
}
}