Labrador/Desktop_Interface/ui_elements/espospinbox.cpp

110 lines
2.8 KiB
C++
Raw Normal View History

#include "espospinbox.h"
espoSpinBox::espoSpinBox(QWidget *parent) : QDoubleSpinBox(parent)
{
setKeyboardTracking(false);
//connect(this, SIGNAL(valueChanged(double)), this, SLOT(changeStepping(double)));
}
QString espoSpinBox::textFromValue(double value) const{
QString windowText;
if (value == 0){
QTextStream(&windowText) << value;
2018-09-17 03:01:17 +01:00
lastValidValue = value;
return windowText;
}
if (abs(value) >= 1000000){
QTextStream(&windowText) << value/1000000 << "M";
2018-09-17 03:01:17 +01:00
lastValidValue = value;
return windowText;
}
if (abs(value) >= 1000){
QTextStream(&windowText) << value/1000 << "k";
2018-09-17 03:01:17 +01:00
lastValidValue = value;
return windowText;
}
if (abs(value) >= 1){
QTextStream(&windowText) << value;
2018-09-17 03:01:17 +01:00
lastValidValue = value;
return windowText;
}
if (abs(value) >= 1/1000){
QTextStream(&windowText) << value * 1000 << "m";
2018-09-17 03:01:17 +01:00
lastValidValue = value;
return windowText;
}
if (abs(value) >= 1/1000000){
QTextStream(&windowText) << value * 1000000 << "u";
2018-09-17 03:01:17 +01:00
lastValidValue = value;
return windowText;
}
return "invalid";
}
void espoSpinBox::maximumChanged(double linked){
this->setMaximum(9.6-linked);
}
void espoSpinBox::setMax(double newMax){
this->setMaximum(newMax);
}
void espoSpinBox::setMin(double newMin){
this->setMinimum(newMin);
}
void espoSpinBox::changeStepping(double value){
double roundval = pow(10.0, floor(log10(abs(value)))); //http://stackoverflow.com/questions/22491505/how-to-round-down-to-the-nearest-power-of-10
2018-09-21 21:04:52 +01:00
double minimumStepSize = pow(10, -1 * decimals());
qDebug() << "roundval" << roundval;
qDebug() << "minimumStepSize" << minimumStepSize;
setSingleStep(std::max(minimumStepSize, roundval/10));
}
2018-09-17 03:01:17 +01:00
QValidator::State espoSpinBox::validate(QString& text, int& pos) const
{
return QValidator::State::Acceptable;
}
double espoSpinBox::valueFromText(const QString &text) const
{
double ret;
bool isValid;
2018-09-21 21:04:52 +01:00
uint32_t prefixLength = text.length() - suffix().length();
2018-09-17 03:01:17 +01:00
qDebug() << text.mid(0, prefixLength - 1) << text.at(prefixLength - 1).toLatin1();
switch (text.at(prefixLength - 1).toLatin1())
{
case 'M':
ret = text.mid(0, prefixLength - 1).toDouble(&isValid) * 1000000;
break;
case 'k':
ret = text.mid(0, prefixLength - 1).toDouble(&isValid) * 1000;
break;
case 'm':
ret = text.mid(0, prefixLength - 1).toDouble(&isValid) / 1000;
break;
case 'u':
ret = text.mid(0, prefixLength - 1).toDouble(&isValid) / 1000000;
break;
default:
ret = text.mid(0, prefixLength).toDouble(&isValid);
}
if (isValid)
{
return ret;
}
else
{
qDebug() << "espoSpinBox: warning: invalid text input." << "Defaulting to last known good value of" << lastValidValue;
return lastValidValue;
}
}