Created custom SpinBox to accept "." and "," as decimal separators

A custom subclass of QDoubleSpinBox was created to accept "." and "," as decimal separators.

DoubleVal validator was deleted from the code as it has not being used.
This commit is contained in:
andresmmera 2024-07-25 08:30:21 +02:00
parent dedcd59bf4
commit 495d0f8f40
2 changed files with 51 additions and 15 deletions

View File

@ -117,19 +117,17 @@ QucsAttenuator::QucsAttenuator()
QGridLayout * inGrid = new QGridLayout();
inGrid->setSpacing(1);
DoubleVal = new QDoubleValidator(this);
DoubleVal->setLocale(QLocale::C);
DoubleVal->setBottom(0);
DoubleValPower = new QDoubleValidator(this);
DoubleValPower->setLocale(QLocale::C);
DoubleValPower->setNotation(QDoubleValidator::StandardNotation);
DoubleValPower->setBottom(-1e9);//The default power unit is dBm, so Pin < 0 is expected
LabelAtten = new QLabel(tr("Attenuation:"), InputGroup);
inGrid ->addWidget(LabelAtten, 1,0);
QSpinBox_Attvalue = new QDoubleSpinBox();
QSpinBox_Attvalue = new QDoubleSpinBox_Comma_Dot_Decimal_Separator();
QSpinBox_Attvalue->setValue(1);
QSpinBox_Attvalue->setMinimum(0.1);
QSpinBox_Attvalue->setMaximum(1e6);
connect(QSpinBox_Attvalue, SIGNAL(valueChanged(double)), this,
SLOT(slotCalculate()) );
inGrid->addWidget(QSpinBox_Attvalue, 1,1);
@ -139,9 +137,8 @@ QucsAttenuator::QucsAttenuator()
LabelImp1 = new QLabel(tr("Zin:"), InputGroup);
LabelImp1->setWhatsThis("Input impedance");
inGrid->addWidget(LabelImp1, 2,0);
QSpinBox_Zin = new QDoubleSpinBox();
QSpinBox_Zin = new QDoubleSpinBox_Comma_Dot_Decimal_Separator();
QSpinBox_Zin->setValue(50);
QSpinBox_Zin->setMinimum(0);
QSpinBox_Zin->setMaximum(1e6);
connect(QSpinBox_Zin, SIGNAL(valueChanged(double)), this,
SLOT(slotSetText_Zin(double)) );
@ -153,9 +150,8 @@ QucsAttenuator::QucsAttenuator()
LabelImp2 = new QLabel(tr("Zout:"), InputGroup);
LabelImp2->setWhatsThis("Output impedance");
inGrid->addWidget(LabelImp2, 3,0);
QSpinBox_Zout = new QDoubleSpinBox();
QSpinBox_Zout = new QDoubleSpinBox_Comma_Dot_Decimal_Separator();
QSpinBox_Zout->setValue(50);
QSpinBox_Zout->setMinimum(0);
QSpinBox_Zout->setMaximum(1e6);
connect(QSpinBox_Zout, SIGNAL(valueChanged(double)), this,
SLOT(slotSetText_Zout(double)) );
@ -166,7 +162,7 @@ QucsAttenuator::QucsAttenuator()
Label_Pin = new QLabel(tr("Pin:"), InputGroup);
Label_Pin->setWhatsThis("Input power");
inGrid->addWidget(Label_Pin, 4,0);
QSpinBox_InputPower = new QDoubleSpinBox(0);
QSpinBox_InputPower = new QDoubleSpinBox_Comma_Dot_Decimal_Separator(0);
QSpinBox_InputPower->setMinimum(-1e3);
QSpinBox_InputPower->setMaximum(1e5);
connect(QSpinBox_InputPower, SIGNAL(valueChanged(double)), this, SLOT(slotCalculate()));
@ -363,7 +359,7 @@ QucsAttenuator::QucsAttenuator()
QucsAttenuator::~QucsAttenuator()
{
delete DoubleVal;
}
void QucsAttenuator::slotHelpIntro()
@ -732,7 +728,7 @@ void QucsAttenuator::slot_ComboInputPowerUnits_Changed(const QString& new_units)
//Change lineedit input policy
if ((new_units == "W") || (new_units == "mW"))
QSpinBox_InputPower->setMinimum(0);
QSpinBox_InputPower->setMinimum(0.1);
else//dB units
QSpinBox_InputPower->setMinimum(-1e3);
}

View File

@ -18,6 +18,8 @@
#include <QLabel>
#include <QCheckBox>
#include <QDoubleSpinBox>
#include <QLocale>
#include <QKeyEvent>
#include "attenuatorfunc.h"
@ -38,6 +40,44 @@ struct tQucsSettings
extern struct tQucsSettings QucsSettings;
// Special QDoubleSpinBox for accepting comma and dot as decimal separators
class QDoubleSpinBox_Comma_Dot_Decimal_Separator : public QDoubleSpinBox {
public:
QDoubleSpinBox_Comma_Dot_Decimal_Separator(QWidget *parent = nullptr) : QDoubleSpinBox(parent) {
setMinimum(0.1); // Set minimum value
setMaximum(1000.0); // Set maximum value
setDecimals(1); // Allow two decimal places
setSingleStep(0.1); // Set step size
}
protected:
QString textFromValue(double value) const override {
// Format the output text to use the current locale's decimal point
return QLocale().toString(value, 'f', decimals());
}
double valueFromText(const QString &text) const override {
// Create a QLocale with C locale to ensure consistent parsing
QLocale locale(QLocale::C);
// Replace the decimal separator with the one used by the current locale
QString modifiedText = text;
modifiedText.replace('.', locale.decimalPoint());
modifiedText.replace(',', locale.decimalPoint());
// Parse the modified text using the C locale
bool ok;
double value = locale.toDouble(modifiedText, &ok);
// If parsing fails, return the base class implementation
if (!ok) {
return QDoubleSpinBox::valueFromText(text);
}
return value;
}
};
class QucsAttenuator : public QMainWindow
{
Q_OBJECT
@ -70,9 +110,9 @@ class QucsAttenuator : public QMainWindow
QLabel *PdissLabel, *Label_Pin;
QLineEdit *lineEdit_R1, *lineEdit_R2, *lineEdit_R3, *lineEdit_R4, *lineEdit_Results;
QLineEdit *lineEdit_R1_Pdiss, *lineEdit_R2_Pdiss, *lineEdit_R3_Pdiss, *lineEdit_R4_Pdiss;
QDoubleSpinBox *QSpinBox_InputPower, *QSpinBox_Attvalue, *QSpinBox_Zin, *QSpinBox_Zout;
QDoubleSpinBox_Comma_Dot_Decimal_Separator *QSpinBox_InputPower, *QSpinBox_Attvalue, *QSpinBox_Zin, *QSpinBox_Zout;
QPushButton *Calculate;
QDoubleValidator *DoubleVal, *DoubleValPower;
QDoubleValidator *DoubleValPower;
QCheckBox *SparBoxCheckbox, *R_Check;
QStringList LastUnits;