36 #include "ui_scalecontrol.h"
39 #include <QInputDialog>
41 using namespace DISPLIB;
43 const static float m_dDefaultMin(0.0f);
44 const static float m_dDefaultMax(1.0f);
45 const static float m_dDefaultMaxSensitivityPoint((m_dDefaultMin+m_dDefaultMax)/2.0f);
46 const static float m_dDefaultSensitivity(0.3f);
47 const static int m_iDefaultSliderMin(1);
48 const static int m_iDefaultSliderMax(1000);
49 const static int m_iDefaultSliderStep(1);
50 const static int m_iDefaultSliderPageStep(10);
55 :
ScaleControl(label, nullptr, m_dDefaultMin, m_dDefaultMax)
62 :
ScaleControl(label, parent, m_dDefaultMin, m_dDefaultMax)
72 , m_pUi(new Ui::ScaleControlWidget)
73 , m_bManagingSpinBoxChange(false)
74 , m_bManagingSliderChange(false)
75 , m_fSensitivity(m_dDefaultSensitivity)
76 , m_fMaxSensitivityPoint(m_dDefaultMaxSensitivityPoint)
77 , m_fMapYconstant(0.0f)
78 , m_fMapKconstant(0.0f)
79 , m_bSliderInverted(false)
86 updateNLMapConstants();
92 void ScaleControl::initLabel(
const char* charTextLabel)
94 m_pUi->label->setText(charTextLabel);
99 void ScaleControl::initSpinBox()
101 m_pUi->spinBox->setKeyboardTracking(
false);
102 m_pUi->spinBox->setPrefix(
"+/- ");
104 connect(m_pUi->spinBox,
static_cast<void (QDoubleSpinBox::*)(
double)
>(&QDoubleSpinBox::valueChanged),
105 this,&ScaleControl::spinBoxChanged);
110 void ScaleControl::initSlider()
112 m_pUi->slider->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred));
113 m_pUi->slider->setSingleStep(m_iDefaultSliderStep);
114 m_pUi->slider->setPageStep(m_iDefaultSliderPageStep);
115 setSliderRange(m_iDefaultSliderMin, m_iDefaultSliderMax);
117 connect(m_pUi->slider,
static_cast<void (QSlider::*)(
int)
>(&QSlider::valueChanged),
118 this,&ScaleControl::sliderChanged);
132 m_pUi->spinBox->setValue(dScale);
139 if(m_pUi->spinBox->minimum() < s && s < m_pUi->spinBox->maximum())
141 m_fMaxSensitivityPoint = s;
142 updateNLMapConstants();
150 if(0.0 < s && s < 1.0)
153 updateNLMapConstants();
162 m_pUi->spinBox->setRange(min, max);
163 if(m_fMaxSensitivityPoint < m_pUi->spinBox->minimum() || m_pUi->spinBox->maximum() < m_fMaxSensitivityPoint)
165 m_fMaxSensitivityPoint = (m_pUi->spinBox->minimum() + m_pUi->spinBox->maximum())/2;
167 updateNLMapConstants();
174 m_bSliderInverted = inverted;
179 void ScaleControl::setSliderRange(
int min,
182 if( (0 < min) && (min < max) )
184 m_pUi->slider->setRange(min, max);
185 updateNLMapConstants();
193 m_pUi->spinBox->setDecimals(d);
198 void ScaleControl::spinBoxChanged(
double dScale)
200 m_bManagingSpinBoxChange =
true;
201 if(!m_bManagingSliderChange)
203 m_pUi->slider->setValue(mapSpinBoxToSlider(dScale));
205 m_bManagingSpinBoxChange =
false;
211 void ScaleControl::sliderChanged(
int dScale)
213 m_bManagingSliderChange =
true;
214 if(!m_bManagingSpinBoxChange)
216 m_pUi->spinBox->setValue(mapSliderToSpinBox(dScale));
218 m_bManagingSliderChange =
false;
223 inline float ScaleControl::weightedSensitivity(
float s)
225 return s * s * s * 100 / m_pUi->spinBox->maximum();
230 void ScaleControl::updateNLMapConstants()
232 m_fMapYconstant = atanf(weightedSensitivity(m_fSensitivity) * (m_pUi->spinBox->minimum() - m_fMaxSensitivityPoint));
233 m_fMapKconstant = (m_pUi->slider->maximum() - m_pUi->slider->minimum()) / (atanf(weightedSensitivity(m_fSensitivity) * (m_pUi->spinBox->maximum() - m_fMaxSensitivityPoint)) - m_fMapYconstant);
238 int ScaleControl::mapSpinBoxToSlider(
double dScale)
240 float map = m_fMapKconstant * (atanf(weightedSensitivity(m_fSensitivity) * (
static_cast<float>(dScale) - m_fMaxSensitivityPoint)) - m_fMapYconstant);
242 if(m_bSliderInverted)
244 out = m_pUi->slider->maximum() -
static_cast<int>(roundf(map));
246 out =
static_cast<int> (roundf(map));
253 double ScaleControl::mapSliderToSpinBox(
int dScale)
255 int valueCorrected = m_bSliderInverted? m_pUi->slider->maximum() - dScale : dScale;
256 float map = (1/weightedSensitivity(m_fSensitivity)) * tanf((
static_cast<float>(valueCorrected) / m_fMapKconstant) + m_fMapYconstant) + m_fMaxSensitivityPoint;
257 return static_cast<double>(map);
264 return m_pUi->spinBox->value();
269 void ScaleControl::initMenu()
271 m_pSettingsMenu =
new QMenu(
this);
273 QAction* minVal =
new QAction(
"Edit minimum value");
274 connect(minVal, &QAction::triggered,
275 this, &ScaleControl::promptMinValueChange,
276 Qt::UniqueConnection);
277 m_pSettingsMenu->addAction(minVal);
279 QAction* maxVal =
new QAction(
"Edit maximum value");
280 connect(maxVal, &QAction::triggered,
281 this, &ScaleControl::promptMaxValueChange,
282 Qt::UniqueConnection);
283 m_pSettingsMenu->addAction(maxVal);
285 m_pUi->toolButton->setMenu(m_pSettingsMenu);
286 m_pUi->toolButton->setPopupMode(QToolButton::InstantPopup);
287 m_pUi->toolButton->setArrowType(Qt::ArrowType::NoArrow);
292 void ScaleControl::promptMinValueChange()
295 double min = QInputDialog::getDouble(
this, tr(
"Minimum Value"),
296 tr(
"Please input a new minumum value:"),
297 m_pUi->spinBox->minimum(), 0.01, 100000,
298 2, &ok, Qt::WindowFlags(), 1);
299 if(ok && min <= m_pUi->spinBox->maximum()){
300 setRange(min, m_pUi->spinBox->maximum());
306 void ScaleControl::promptMaxValueChange()
309 double max = QInputDialog::getDouble(
this, tr(
"Maximum Value"),
310 tr(
"Please input a new maximum value:"),
311 m_pUi->spinBox->maximum(), 0.01, 100000,
312 2, &ok, Qt::WindowFlags(), 1);
313 if(ok && max >= m_pUi->spinBox->minimum()){
314 setRange(m_pUi->spinBox->minimum(), max);