282 lines
7.3 KiB
C++
Executable File
282 lines
7.3 KiB
C++
Executable File
/**
|
|
* LtsAngleEdit
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
*
|
|
* Created by Steve Kemayou, ESZS, 2024-10-16
|
|
*/
|
|
|
|
#include "lts_angle_edit.h"
|
|
#include "ui_lts_angle_edit.h"
|
|
|
|
#include <cmath>
|
|
|
|
|
|
namespace LtsWidgets
|
|
{
|
|
// some helper functions
|
|
inline constexpr double pi() { return M_PI; }
|
|
inline constexpr double deg2rad() { return pi() / 180.; }
|
|
inline constexpr double rad2deg() { return 1. / deg2rad(); }
|
|
|
|
constexpr int maxAngleDeg = 360;
|
|
constexpr int maxAngleMil = 6400;
|
|
constexpr std::tuple<int,int,int,int> c_limDeg (0, maxAngleDeg,0, 10); // angleMinVal, angleMaxVal, decimal place, spinboxStep
|
|
constexpr std::tuple<int,int,int, int> c_limMil(0, maxAngleMil,0, 50);
|
|
constexpr std::tuple<int,int,int, int> c_limRad(-99999, 99999, 3, 1);
|
|
|
|
inline constexpr double mil2Deg(const double &mil)
|
|
{
|
|
return maxAngleDeg * mil / maxAngleMil;
|
|
}
|
|
inline constexpr double deg2Mil(const double &angleDeg)
|
|
{
|
|
return maxAngleMil * angleDeg / maxAngleDeg;
|
|
}
|
|
inline constexpr double deg2Rad(const double &angleDeg)
|
|
{
|
|
return angleDeg * deg2rad();
|
|
}
|
|
inline constexpr double rad2Degree(const double &angleRad)
|
|
{
|
|
return angleRad * 180/pi();
|
|
}
|
|
inline constexpr double mil2Rad(const double &mil)
|
|
{
|
|
return deg2Rad(mil2Deg(mil));
|
|
}
|
|
inline constexpr double rad2Mil(const double &angleRad)
|
|
{
|
|
return deg2Mil(rad2Degree(angleRad));
|
|
}
|
|
|
|
/*!
|
|
* \brief LtsAngleEdit::LtsAngleEdit
|
|
* \param parent
|
|
*/
|
|
LtsAngleEdit::LtsAngleEdit(QWidget *parent) :
|
|
QWidget(parent),
|
|
m_ui(new Ui_LtsAngleEdit)
|
|
{
|
|
m_ui->setupUi(this);
|
|
|
|
}
|
|
|
|
LtsAngleEdit::~LtsAngleEdit()
|
|
{
|
|
delete m_ui;
|
|
}
|
|
|
|
/*!
|
|
* \brief LtsAngleEdit::initAngleEditWidget
|
|
*/
|
|
void LtsAngleEdit::initAngleEditWidget(const unsigned int sboxCount)
|
|
{
|
|
// set Limits
|
|
m_dboxLimitMap.insert(AngleUnitType::Deg, c_limDeg);
|
|
m_dboxLimitMap.insert(AngleUnitType::Mil, c_limMil);
|
|
m_dboxLimitMap.insert(AngleUnitType::Rad, c_limRad);
|
|
m_hiddenUnits.clear();
|
|
m_hiddenUnits.insert((int)AngleUnitType::Rad); // default hid rad unit
|
|
|
|
// add new dboxes
|
|
for(unsigned int i=0; i<sboxCount; ++i)
|
|
{
|
|
QSharedPointer<QDoubleSpinBox> spinbox = QSharedPointer<QDoubleSpinBox>(new QDoubleSpinBox(this) );
|
|
spinbox->setValue(0);
|
|
spinbox->setAlignment(Qt::AlignRight);
|
|
spinbox->setMinimumWidth(91);
|
|
// set object name
|
|
spinbox.data()->setObjectName(spinBoxName+QString::number(m_sBoxMap.count()));
|
|
m_ui->hLayoutSpinbox->addWidget(spinbox.data(), 0);
|
|
m_sBoxMap.insert(m_sBoxMap.count(), spinbox);
|
|
}
|
|
m_ui->hLayoutSpinbox->setContentsMargins(1,1,1,1);
|
|
|
|
|
|
// set combobox units
|
|
connect(m_ui->cBoxAngleUnit, qOverload<int>(&QComboBox::currentIndexChanged),
|
|
this, &LtsAngleEdit::onCBoxAngleUnitChanged);
|
|
|
|
setCboxUnit();
|
|
// set default unit
|
|
}
|
|
|
|
/*!
|
|
* \brief LtsAngleEdit::setValue
|
|
* \param value
|
|
* \param dboxIndex
|
|
*/
|
|
void LtsAngleEdit::setValue(const double value, unsigned int dboxIndex)
|
|
{
|
|
if ((int)dboxIndex > m_sBoxMap.count())
|
|
return;
|
|
auto spinbox = m_sBoxMap.value(dboxIndex);
|
|
spinbox->setValue(value);
|
|
}
|
|
|
|
/*!
|
|
* \brief LtsAngleEdit::setValue
|
|
* \param value
|
|
* \param valueUnit
|
|
* \param dboxIndex
|
|
*/
|
|
void LtsAngleEdit::setValue(const double value, AngleUnitType valueUnit, unsigned int dboxIndex)
|
|
{
|
|
setValue(getAngleConverted(value, getAngleCurrentUnit(), valueUnit),dboxIndex);
|
|
}
|
|
|
|
void LtsAngleEdit::hideUnits(const QVector<AngleUnitType> &list)
|
|
{
|
|
for(auto u : list)
|
|
m_hiddenUnits.insert((int)u);
|
|
setCboxUnit();
|
|
}
|
|
|
|
void LtsAngleEdit::showUnits(const QVector<AngleUnitType> &list)
|
|
{
|
|
for (const auto &u : list)
|
|
m_hiddenUnits.remove((int)u);
|
|
setCboxUnit();
|
|
}
|
|
|
|
/*!
|
|
* \brief LtsAngleEdit::setCboxUnit
|
|
*/
|
|
void LtsAngleEdit::setCboxUnit()
|
|
{
|
|
m_ui->cBoxAngleUnit->clear();
|
|
const std::vector<AngleUnitType> allUnits = {AngleUnitType::Deg, AngleUnitType::Mil, AngleUnitType::Rad};
|
|
for(const auto &u : allUnits)
|
|
{
|
|
if (m_hiddenUnits.contains((int)u))
|
|
continue;
|
|
m_ui->cBoxAngleUnit->insertItem(static_cast<int>(u), toString(u),static_cast<int>(u));
|
|
}
|
|
|
|
setAngleUnit(AngleUnitType::Deg);
|
|
}
|
|
|
|
/*!
|
|
* \brief LtsAngleEdit::setValue
|
|
* \param value
|
|
* \param valueUnit
|
|
*/
|
|
void LtsAngleEdit::setValue(const double value, AngleUnitType valueUnit)
|
|
{
|
|
// get current unit of given angle and convert it to current defined in combobox unit
|
|
setValue(getAngleConverted(value, getAngleCurrentUnit(), valueUnit),0);
|
|
}
|
|
|
|
/*!
|
|
* \brief LtsAngleEdit::setAngleUnit
|
|
* \param type
|
|
*/
|
|
void LtsAngleEdit::setAngleUnit(const AngleUnitType &type)
|
|
{
|
|
m_ui->cBoxAngleUnit->setCurrentIndex(static_cast<int>(type));
|
|
}
|
|
|
|
/*!
|
|
* \brief LtsAngleEdit::getCurrentAngleUnit
|
|
* \return
|
|
*/
|
|
LtsAngleEdit::AngleUnitType LtsAngleEdit::getAngleCurrentUnit() const
|
|
{
|
|
return static_cast<AngleUnitType>(m_ui->cBoxAngleUnit->currentIndex());
|
|
}
|
|
|
|
/*!
|
|
* \brief LtsAngleEdit::getAngleValue
|
|
* \param dboxIndex default index value is 0; also when more than one dbox is created
|
|
* \return
|
|
*/
|
|
double LtsAngleEdit::getAngle(unsigned int sboxIndex) const
|
|
{
|
|
if ((int)sboxIndex > m_sBoxMap.count())
|
|
return 0;
|
|
auto spinbox = m_sBoxMap.value(sboxIndex);
|
|
return spinbox->value();
|
|
}
|
|
|
|
/*!
|
|
* \brief LtsAngleEdit::toString
|
|
* \param t
|
|
* \return
|
|
*/
|
|
QString LtsAngleEdit::toString(LtsAngleEdit::AngleUnitType t)
|
|
{
|
|
switch(t)
|
|
{
|
|
case AngleUnitType::Deg:
|
|
return tr("Degree");
|
|
case AngleUnitType::Mil:
|
|
return tr("Mil");
|
|
case AngleUnitType::Rad:
|
|
return tr("Radian");
|
|
default:
|
|
return tr("");
|
|
}
|
|
return tr("");
|
|
}
|
|
|
|
void LtsAngleEdit::onCBoxAngleUnitChanged()
|
|
{
|
|
AngleUnitType newUnit = static_cast<AngleUnitType>(m_ui->cBoxAngleUnit->currentData().toInt());
|
|
for(auto dbox : qAsConst(m_sBoxMap))
|
|
{
|
|
const double value = getAngleConverted(dbox->value(), newUnit, m_currAngleUnit);
|
|
auto mapVal = m_dboxLimitMap.value(newUnit);
|
|
dbox->setMinimum(std::get<LimTuple::AngleMin>(mapVal));
|
|
dbox->setMaximum(std::get<LimTuple::AngleMax>(mapVal));
|
|
dbox->setDecimals(std::get<LimTuple::DecimalPlace>(mapVal));
|
|
dbox->setSingleStep(std::get<LimTuple::SingleStep>(mapVal));
|
|
dbox->setValue(value);
|
|
}
|
|
m_currAngleUnit = newUnit;
|
|
}
|
|
|
|
/*!
|
|
* \brief LtsAngleEdit::convertAngleValue
|
|
* \param angle
|
|
* \param newAngleUnit
|
|
* \param oldAngleUnit
|
|
* \return
|
|
*/
|
|
double LtsAngleEdit::getAngleConverted(const double angle, LtsAngleEdit::AngleUnitType newAngleUnit, LtsAngleEdit::AngleUnitType oldAngleUnit)
|
|
{
|
|
double convertedAngle = angle;
|
|
if (oldAngleUnit == AngleUnitType::Deg && newAngleUnit == AngleUnitType::Mil )
|
|
convertedAngle = deg2Mil(angle);
|
|
else if (oldAngleUnit == AngleUnitType::Deg && newAngleUnit == AngleUnitType::Rad )
|
|
convertedAngle = deg2Rad(angle);
|
|
else if (oldAngleUnit == AngleUnitType::Mil && newAngleUnit == AngleUnitType::Deg )
|
|
convertedAngle = mil2Deg(angle);
|
|
else if (oldAngleUnit == AngleUnitType::Mil && newAngleUnit == AngleUnitType::Rad )
|
|
convertedAngle = mil2Rad(angle);
|
|
else if (oldAngleUnit == AngleUnitType::Rad && newAngleUnit == AngleUnitType::Deg )
|
|
convertedAngle = rad2Degree(angle);
|
|
else if (oldAngleUnit == AngleUnitType::Rad && newAngleUnit == AngleUnitType::Mil )
|
|
convertedAngle = rad2Mil(angle);
|
|
|
|
return convertedAngle;
|
|
}
|
|
|
|
void LtsAngleEdit::setEnable(int sbIndex, bool enabled)
|
|
{
|
|
if ((int)sbIndex > m_sBoxMap.count())
|
|
return;
|
|
m_sBoxMap.value(sbIndex)->setEnabled(enabled);
|
|
}
|
|
|
|
void LtsAngleEdit::showSpinbox(int sbIndex, bool visible)
|
|
{
|
|
if ((int)sbIndex > m_sBoxMap.count())
|
|
return;
|
|
m_sBoxMap.value(sbIndex)->setVisible(visible);
|
|
}
|
|
|
|
}// namespace LtsAngleEdit
|
|
|